Guest User

Untitled

a guest
Jun 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements
  2. MyCustomDialog.OnInputSelected{
  3.  
  4. public String dialogInput;
  5. FragmentManager fragmentManager;
  6.  
  7. @Override
  8. public void sendInput(String input) {
  9. dialogInput = input;
  10. }
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16.  
  17. fragmentManager = getSupportFragmentManager();
  18. }
  19.  
  20. @Override
  21. public boolean onCreateOptionsMenu(Menu menu) {
  22. //Inflate the menu, this adds items to the action bar if it is present
  23. getMenuInflater().inflate(R.menu.menu, menu);
  24.  
  25. return super.onCreateOptionsMenu(menu);
  26. }
  27.  
  28. @Override
  29. public boolean onOptionsItemSelected(MenuItem item) {
  30.  
  31. //Handle action bar clicks here. The action bar will automatically handle clicks on the home/up button
  32. //so long as you specify a parent activity in AndroidManifest.xml
  33.  
  34. switch(item.getItemId()){
  35.  
  36. case R.id.action_add:
  37. MyCustomDialog dialog = new MyCustomDialog();
  38. dialog.show(getSupportFragmentManager(), "MyCustomDialog");
  39.  
  40. //Trying Bundle to pass data, dialog input between activity and fragment
  41. Bundle bundle = new Bundle();
  42. bundle.putString("Dialog Input", dialogInput);
  43. //Set Fragment class arguments
  44. MainFragment fragment = new MainFragment();
  45. fragment.setArguments(bundle); //set argument bundle to fragment
  46.  
  47. fragmentManager.beginTransaction().replace(R.id.MainFragment,fragment).commit(); //now replace Mainfragment
  48.  
  49.  
  50. Toast.makeText(this, "Action_Add Clicked Successfully", Toast.LENGTH_SHORT).show();
  51. }
  52.  
  53. return super.onOptionsItemSelected(item);
  54. }
  55. }
  56.  
  57. public class MainFragment extends Fragment implements MyCustomDialog.OnInputSelected{
  58.  
  59. TextView InputDisplay;
  60. Button OpenDialog;
  61.  
  62. @Nullable
  63. @Override
  64. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  65. View view = inflater.inflate(R.layout.fragment_main, container, false);
  66.  
  67. InputDisplay = view.findViewById(R.id.InputDisplay);
  68. OpenDialog = view.findViewById(R.id.Open_Dialog);
  69.  
  70. //Getting Main Activity dialog information with Bundle, that was received from toolbar add
  71. Bundle bundle = getArguments();
  72. if(bundle != null){
  73. String dialogInput = bundle.toString();
  74. //Clearing since Fragment call and activity call overlap each other.
  75. InputDisplay.setText("");
  76. InputDisplay.clearComposingText();
  77. InputDisplay.setText(dialogInput);
  78. }
  79. //String dialogInput = this.getArguments().getString("Dialog Input");
  80.  
  81. OpenDialog.setOnClickListener(new View.OnClickListener() {
  82. @Override
  83. public void onClick(View view) {
  84. Log.d("MainFragment", "onClick: opening dialog");
  85.  
  86. MyCustomDialog customDialog = new MyCustomDialog();
  87. customDialog.setTargetFragment(MainFragment.this, 1);
  88. customDialog.show(getFragmentManager(), "MyCustomDialog");
  89. }
  90. });
  91.  
  92. return view;
  93. }
  94.  
  95. @Override
  96. public void sendInput(String input) {
  97. InputDisplay.setText("");
  98. InputDisplay.setText(input);
  99. }
  100. }
  101.  
  102. public class MyCustomDialog extends DialogFragment {
  103.  
  104. private EditText Input;
  105. private TextView ActionOK, ActionCANCEL;
  106.  
  107. private OnInputSelected onInputSelected;
  108.  
  109. public interface OnInputSelected{
  110. void sendInput(String input);
  111. }
  112.  
  113. @Override
  114. public void onAttach(Context context) {
  115. super.onAttach(context);
  116. try{
  117. Fragment onInputSelected_fragment = getTargetFragment();
  118. Activity onInputSelected_activity = getActivity();
  119. if(onInputSelected_fragment != null){
  120. onInputSelected = (OnInputSelected) onInputSelected_fragment;
  121. }else{
  122. onInputSelected = (OnInputSelected) onInputSelected_activity;
  123. }
  124. //throw new RuntimeException("Custom Dialog onAttach Listener was NULL");
  125. }catch(ClassCastException e){
  126. Log.e("Custom Dialog", "onAttach: ClassCastException: " + e.getMessage());
  127. }
  128. }
  129.  
  130. @Nullable
  131. @Override
  132. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  133. View view = inflater.inflate(R.layout.dialog_my_custom, container, false);
  134.  
  135. Input = view.findViewById(R.id.Input);
  136. ActionOK = view.findViewById(R.id.Action_OK);
  137. ActionCANCEL = view.findViewById(R.id.Action_CANCEL);
  138.  
  139. ActionCANCEL.setOnClickListener(new View.OnClickListener() {
  140. @Override
  141. public void onClick(View view) {
  142. getDialog().dismiss();
  143. }
  144. });
  145.  
  146. ActionOK.setOnClickListener(new View.OnClickListener() {
  147. @Override
  148. public void onClick(View view) {
  149. onInputSelected.sendInput(Input.getText().toString());
  150.  
  151. getDialog().dismiss();
  152. }
  153. });
  154.  
  155. return view;
  156. }
  157. }
Add Comment
Please, Sign In to add comment