Advertisement
chayanforyou

Top Sheet Dialog JAVA

Apr 21st, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.75 KB | None | 0 0
  1. package com.xor.admissiontest.Guardian;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Typeface;
  7. import android.support.design.widget.AppBarLayout;
  8. import android.support.design.widget.CollapsingToolbarLayout;
  9. import android.support.design.widget.FloatingActionButton;
  10. import android.support.v7.app.AlertDialog;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.support.v7.widget.DefaultItemAnimator;
  14. import android.support.v7.widget.LinearLayoutManager;
  15. import android.support.v7.widget.RecyclerView;
  16. import android.support.v7.widget.Toolbar;
  17. import android.util.Log;
  18. import android.view.Gravity;
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.view.inputmethod.EditorInfo;
  24. import android.view.inputmethod.InputMethodManager;
  25. import android.widget.Button;
  26. import android.widget.EditText;
  27. import android.widget.RadioButton;
  28. import android.widget.RadioGroup;
  29.  
  30. import com.android.volley.Request;
  31. import com.android.volley.Response;
  32. import com.android.volley.VolleyError;
  33. import com.android.volley.toolbox.JsonObjectRequest;
  34. import com.orhanobut.dialogplus.DialogPlus;
  35. import com.orhanobut.dialogplus.ViewHolder;
  36. import com.xor.admissiontest.AppController;
  37. import com.xor.admissiontest.Guardian.paging.Item;
  38. import com.xor.admissiontest.Guardian.paging.RecyclerViewAdapter;
  39. import com.xor.admissiontest.R;
  40. import com.xor.admissiontest.SessionManager;
  41. import com.xor.admissiontest.Shared;
  42. import com.xor.admissiontest.URL;
  43.  
  44. import org.json.JSONArray;
  45. import org.json.JSONException;
  46. import org.json.JSONObject;
  47.  
  48. import java.util.ArrayList;
  49.  
  50. public class GuardianActivity extends AppCompatActivity {
  51.  
  52.     private String TAG = GuardianActivity.class.getSimpleName();
  53.     private Context mContext = GuardianActivity.this;
  54.     // Session Manager Class
  55.     private SessionManager session;
  56.     private RecyclerView mRecyclerView;
  57.     private RecyclerViewAdapter mAdapter;
  58.  
  59.     ArrayList<Item> itemList = new ArrayList<>();
  60.  
  61.     private DialogPlus dialogPlus = null;
  62.  
  63.     @Override
  64.     protected void onCreate(Bundle savedInstanceState) {
  65.         super.onCreate(savedInstanceState);
  66.         setContentView(R.layout.activity_guardian);
  67.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  68.         setSupportActionBar(toolbar);
  69.  
  70.         // Session class instance
  71.         session = new SessionManager(mContext);
  72.  
  73.         initCollapsingToolbar();
  74.  
  75.         mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  76.         mRecyclerView.setHasFixedSize(true);
  77.         mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  78.         mRecyclerView.setItemAnimator(new DefaultItemAnimator());
  79.  
  80.         mAdapter = new RecyclerViewAdapter(this, itemList);
  81.         mRecyclerView.setAdapter(mAdapter);
  82.  
  83.         FloatingActionButton addPost = findViewById(R.id.fab);
  84.  
  85.         addPost.setOnClickListener(view -> {
  86.             AlertDialog alertDialog = showPostDialog();
  87.             alertDialog.show();
  88.         });
  89.  
  90.         // make request to server
  91.         getDataFromWeb();
  92.     }
  93.  
  94.     /*
  95.      * Make a request to server
  96.      */
  97.     private void getDataFromWeb() {
  98.  
  99.         final ProgressDialog dialog = new ProgressDialog(mContext);
  100.         dialog.setTitle("Please wait...");
  101.         dialog.setMessage("Loading nearby tutor...");
  102.         dialog.show();
  103.  
  104.         String user_id = Shared.readData("UserID", mContext);
  105.  
  106.         JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URL.GET_NEAR_TUTORS_URL + user_id, new Response.Listener<JSONObject>() {
  107.             @Override
  108.             public void onResponse(JSONObject response) {
  109.                 Log.e(TAG, response.toString());
  110.                 dialog.dismiss();
  111.  
  112.                 try {
  113.                     boolean success = response.getBoolean("success");
  114.                     if (success) {
  115.                         JSONArray jsonArray = response.getJSONArray("data");
  116.                         for (int i = 0; i < jsonArray.length(); i++) {
  117.                             JSONObject jsonObject = jsonArray.getJSONObject(i);
  118.                             Item item = new Item();
  119.                             item.setId(jsonObject.getString("id"));
  120.                             item.setName(jsonObject.getString("fullname"));
  121.                             item.setUniversity(jsonObject.getString("college"));
  122.                             item.setImage(jsonObject.getString("pic"));
  123.                             item.setSubject("Group: " + "Science" + ", Dept: " + jsonObject.getString("dept"));
  124.  
  125.                             itemList.add(item);
  126.                         }
  127.  
  128.                         mAdapter.notifyDataSetChanged();
  129.  
  130.                     } else {
  131.                         Shared.showAlert("Info", response.getString("msg"), mContext, "Ok");
  132.                     }
  133.  
  134.                 } catch (JSONException e) {
  135.                     e.printStackTrace();
  136.                 }
  137.             }
  138.         }, new Response.ErrorListener() {
  139.             @Override
  140.             public void onErrorResponse(VolleyError error) {
  141.                 error.printStackTrace();
  142.             }
  143.         });
  144.  
  145.         // Adding request to request queue
  146.         AppController.getInstance().addToRequestQueue(req);
  147.     }
  148.  
  149.     private AlertDialog showPostDialog() {
  150.  
  151.         AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext, R.style.Theme_AppCompat_Light_Dialog_MinWidth);
  152.         View view = getLayoutInflater().inflate(R.layout.custom_post_dialog_style, (ViewGroup) null);
  153.         dialogBuilder.setTitle("Post for a Tutor");
  154.         dialogBuilder.setCancelable(false);
  155.         dialogBuilder.setView(view);
  156.  
  157.         EditText edt_class = view.findViewById(R.id.edt_class);
  158.         EditText edt_group = view.findViewById(R.id.edt_group);
  159.         EditText edt_school = view.findViewById(R.id.edt_school);
  160.         EditText edt_description = view.findViewById(R.id.edt_description);
  161.  
  162.         dialogBuilder.setPositiveButton("Post", (dialogInterface, i) -> {
  163.             dialogInterface.dismiss();
  164.             String strClass = edt_class.getText().toString();
  165.             String strGroup = edt_group.getText().toString();
  166.             String strSchool = edt_school.getText().toString();
  167.             String strDescription = edt_description.getText().toString();
  168.  
  169.             Shared.printMessage(strClass + ", " + strGroup + ", " + strSchool + ", " + strDescription, mContext);
  170.         });
  171.  
  172.         dialogBuilder.setNegativeButton("Cancel", (dialogInterface, i) -> {
  173.             dialogInterface.dismiss();
  174.         });
  175.  
  176.         return dialogBuilder.create();
  177.     }
  178.  
  179.  
  180.     /*
  181.      * Initializing collapsing toolbar
  182.      * Will show and hide the toolbar title on scroll
  183.      */
  184.     private void initCollapsingToolbar() {
  185.  
  186.         Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/RealityHyperRegular.ttf");
  187.  
  188.         final CollapsingToolbarLayout collapsingToolbar = findViewById(R.id.collapsing_toolbar);
  189.         collapsingToolbar.setCollapsedTitleTypeface(customFont);
  190.         collapsingToolbar.setExpandedTitleTypeface(customFont);
  191.         collapsingToolbar.setTitle(" ");
  192.  
  193.         AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
  194.         appBarLayout.setExpanded(true);
  195.  
  196.         // hiding & showing the title when toolbar expanded & collapsed
  197.         appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
  198.             boolean isShow = false;
  199.             int scrollRange = -1;
  200.  
  201.             @Override
  202.             public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
  203.                 if (scrollRange == -1) {
  204.                     scrollRange = appBarLayout.getTotalScrollRange();
  205.                 }
  206.                 if (scrollRange + verticalOffset == 0) {
  207.                     collapsingToolbar.setTitle(getString(R.string.backdrop_title));
  208.                     isShow = true;
  209.                 } else if (isShow) {
  210.                     collapsingToolbar.setTitle(" ");
  211.                     isShow = false;
  212.                 }
  213.             }
  214.         });
  215.     }
  216.  
  217.     @Override
  218.     public boolean onCreateOptionsMenu(Menu menu) {
  219.         // Inflate the menu; this adds items to the action bar if it is present.
  220.         getMenuInflater().inflate(R.menu.menu_guardian, menu);
  221.  
  222. //        // Associate searchable configuration with the SearchView
  223. //        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  224. //        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
  225. //        searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getString(R.string.search_hint) + "</font>"));
  226. //        searchView.setMaxWidth(Integer.MAX_VALUE);
  227. //        assert searchManager != null;
  228. //        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
  229.  
  230. //        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  231. //            @Override
  232. //            public boolean onQueryTextSubmit(String query) {
  233. //                Log.e("onQueryTextSubmit", query);
  234. //                return true;
  235. //            }
  236. //
  237. //            @Override
  238. //            public boolean onQueryTextChange(String newText) {
  239. //                Log.e("onQueryTextChange", newText);
  240. //                return true;
  241. //            }
  242. //        });
  243.  
  244.         return super.onCreateOptionsMenu(menu);
  245.     }
  246.  
  247.     @Override
  248.     public boolean onOptionsItemSelected(MenuItem item) {
  249.         // Handle action bar item clicks here. The action bar will
  250.         // automatically handle clicks on the Home/Up button, so long
  251.         // as you specify a parent activity in AndroidManifest.xml.
  252.         int id = item.getItemId();
  253.  
  254.         //noinspection SimplifiableIfStatement
  255.         if (id == R.id.action_search) {
  256.             showAlert();
  257.             return true;
  258.         } else if (id == R.id.action_my_account) {
  259.             startActivity(new Intent(mContext, GuardianProfileActivity.class));
  260.             return true;
  261.         } else if (id == R.id.action_logout) {
  262.             session.logoutUser();
  263.             return true;
  264.         }
  265.  
  266.         return super.onOptionsItemSelected(item);
  267.     }
  268.  
  269.  
  270.     public void showAlert() {
  271.  
  272.         dialogPlus = DialogPlus.newDialog(this)
  273.                 .setContentHolder(new ViewHolder(R.layout.top_sheet_dialog))
  274.                 .setGravity(Gravity.TOP)
  275.                 .setPadding(26, 16, 26, 16)
  276.                 .create();
  277.  
  278.         EditText edtSearch = dialogPlus.getHolderView().findViewById(R.id.edt_search);
  279.         RadioGroup rg = dialogPlus.getHolderView().findViewById(R.id.holderRadioGroup);
  280.         Button btnSearch = dialogPlus.getHolderView().findViewById(R.id.btn_search);
  281.  
  282.         edtSearch.setOnEditorActionListener((v, actionId, event) -> {
  283.             if (actionId == EditorInfo.IME_ACTION_SEARCH) {
  284.                 String strSearch = v.getText().toString();
  285.                 String radioValue = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();
  286.                 performSearch(strSearch, radioValue);
  287.                 return true;
  288.             }
  289.             return false;
  290.         });
  291.  
  292.         btnSearch.setOnClickListener(view -> {
  293.             String strSearch = edtSearch.getText().toString();
  294.             String radioValue = ((RadioButton) findViewById(rg.getCheckedRadioButtonId())).getText().toString();
  295.             performSearch(strSearch, radioValue);
  296.         });
  297.  
  298.         dialogPlus.show();
  299.  
  300.         edtSearch.requestFocus();
  301.         showKeyboard();
  302.     }
  303.  
  304.     private void performSearch(String strSearch, String value) {
  305.         startActivity(new Intent(mContext, SearchResultsActivity.class)
  306.                 .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
  307.                 .putExtra("query", strSearch)
  308.                 .putExtra("group", value));
  309.         closeKeyboard();
  310.         dialogPlus.dismiss();
  311.  
  312.  
  313.     }
  314.  
  315.     public void showKeyboard() {
  316.         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  317.         assert inputMethodManager != null;
  318.         inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  319.     }
  320.  
  321.     public void closeKeyboard() {
  322.         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  323.         assert inputMethodManager != null;
  324.         inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  325.     }
  326.  
  327.     @Override
  328.     public void onBackPressed() {
  329.         if (dialogPlus != null && dialogPlus.isShowing())
  330.             dialogPlus.dismiss();
  331.         else
  332.             super.onBackPressed();
  333.     }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement