Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. class StatisticDialog extends DialogFragment {
  2. @Override
  3. public Dialog onCreateDialog(Bundle savedInstanceState) {
  4. // Use the Builder class for convenient dialog construction
  5. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  6. builder .setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
  7. public void onClick(DialogInterface dialog, int id) {
  8. // FIRE ZE MISSILES!
  9. }
  10. });
  11. // Create the AlertDialog object and return it
  12. return builder.create();
  13. }
  14. @Override
  15. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  16. Bundle savedInstanceState) {
  17. return inflater.inflate(R.layout.statistics, container, true);
  18. }
  19.  
  20. @Override
  21. public void onActivityCreated(Bundle savedInstanceState) {
  22. super.onActivityCreated(savedInstanceState);
  23. }
  24. }
  25.  
  26. StatisticDialog dlg = new StatisticDialog();
  27. dlg.show(getSupportFragmentManager(), "missiles");
  28.  
  29. 11-08 18:05:14.470: E/AndroidRuntime(1075): FATAL EXCEPTION: main
  30. 11-08 18:05:14.470: E/AndroidRuntime(1075): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
  31. 11-08 18:05:14.470: E/AndroidRuntime(1075): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:215)
  32. 11-08 18:05:14.470: E/AndroidRuntime(1075): at com.android.internal.app.AlertController.installContent(AlertController.java:234)
  33. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.app.AlertDialog.onCreate(AlertDialog.java:336)
  34. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.app.Dialog.dispatchOnCreate(Dialog.java:351)
  35. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.app.Dialog.show(Dialog.java:256)
  36. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:385)
  37. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.support.v4.app.Fragment.performStart(Fragment.java:1336)
  38. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
  39. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
  40. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
  41. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
  42. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
  43. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.os.Handler.handleCallback(Handler.java:615)
  44. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.os.Handler.dispatchMessage(Handler.java:92)
  45. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.os.Looper.loop(Looper.java:137)
  46. 11-08 18:05:14.470: E/AndroidRuntime(1075): at android.app.ActivityThread.main(ActivityThread.java:4745)
  47. 11-08 18:05:14.470: E/AndroidRuntime(1075): at java.lang.reflect.Method.invokeNative(Native Method)
  48. 11-08 18:05:14.470: E/AndroidRuntime(1075): at java.lang.reflect.Method.invoke(Method.java:511)
  49. 11-08 18:05:14.470: E/AndroidRuntime(1075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  50. 11-08 18:05:14.470: E/AndroidRuntime(1075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  51. 11-08 18:05:14.470: E/AndroidRuntime(1075): at dalvik.system.NativeStart.main(Native Method)
  52.  
  53. class StatisticDialog extends DialogFragment {
  54. @Override
  55. public Dialog onCreateDialog(Bundle savedInstanceState) {
  56. // Use the Builder class for convenient dialog construction
  57. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  58. LayoutInflater inflater = getActivity().getLayoutInflater();
  59. View view = inflater.inflate(R.layout.statistics, null);
  60. builder.setView(view);
  61. builder.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
  62. public void onClick(DialogInterface dialog, int id) {
  63. // FIRE ZE MISSILES!
  64. }
  65. });
  66. // Create the AlertDialog object and return it
  67. return builder.create();
  68. }
  69. }
  70.  
  71. public static MyCombinedFragment newInstance()
  72. {
  73. MyCombinedFragment frag = new MyCombinedFragment();
  74. frag.isModal = true; // WHEN FRAGMENT IS CALLED AS A DIALOG SET FLAG
  75. return frag;
  76. }
  77.  
  78. public MyCombinedFragment()
  79. {
  80. }
  81.  
  82. @Override
  83. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  84. {
  85. if(isModal) // AVOID REQUEST FEATURE CRASH
  86. {
  87. return super.onCreateView(inflater, container, savedInstanceState);
  88. }
  89. else
  90. {
  91. View view = inflater.inflate(R.layout.fragment_layout, container, false);
  92. setupUI(view);
  93. return view;
  94. }
  95. }
  96.  
  97. @Override
  98. public Dialog onCreateDialog(Bundle savedInstanceState)
  99. {
  100. AlertDialog.Builder alertDialogBuilder = null;
  101. alertDialogBuilder = new AlertDialog.Builder(getActivity());
  102. View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_layout, null);
  103. alertDialogBuilder.setView(view);
  104. alertDialogBuilder.setTitle(“Modal Dialog“);
  105. alertDialogBuilder.setPositiveButton("Cancel", new DialogInterface.OnClickListener()
  106. {
  107. @Override
  108. public void onClick(DialogInterface dialog, int which)
  109. {
  110. dialog.dismiss();
  111. }
  112. });
  113. setupUI(view);
  114. return alertDialogBuilder.create();
  115. }
  116.  
  117. if (!getShowsDialog()) {
  118. View rootView = inflater.inflate(R.layout.fr_layout, container, false);
  119. initViews(rootView);
  120. return rootView;
  121. } else {
  122. return super.onCreateView(inflater, container, savedInstanceState);
  123. }
  124. }
  125.  
  126. public interface OnMFDialogFragmentDoneListener {
  127.  
  128. public void onMFDialogFragmentDone(String tag,boolean cancelled,
  129. CharSequence username,
  130. CharSequence password);
  131. }
  132.  
  133. public class MFDialogFragment extends DialogFragment
  134. implements View.OnClickListener{
  135.  
  136.  
  137. private EditText editTextusername,editTextpassword;
  138.  
  139. public static MFDialogFragment newInstance(String title){
  140. MFDialogFragment mfDialogFragment=new MFDialogFragment();
  141.  
  142. Bundle bundle =new Bundle();
  143. bundle.putString("title",title);
  144. mfDialogFragment.setArguments(bundle);
  145. return mfDialogFragment;
  146. }
  147.  
  148.  
  149.  
  150. @Override
  151. public void onAttach(Activity act){
  152.  
  153. try{
  154. OnMFDialogFragmentDoneListener
  155. test= (OnMFDialogFragmentDoneListener) act;
  156. }catch (ClassCastException cce){
  157. ////...activity !!!! not listen to me
  158. }
  159. super.onAttach(act);
  160. }
  161.  
  162.  
  163. @Override
  164. public void onCreate(Bundle icicle) {
  165. super.onCreate(icicle);
  166. this.setCancelable(true);
  167. int style = DialogFragment.STYLE_NORMAL, theme = 0;
  168. setStyle(style, theme);
  169. }
  170.  
  171. public View onCreateView(LayoutInflater inflater,
  172. ViewGroup container, Bundle bundle) {
  173.  
  174. View v = inflater.inflate(R.layout.mfdialogfragmentlayout, container,
  175. false);
  176.  
  177. TextView tvtitle = (TextView) v.findViewById(R.id.tv_dialogfragmentuserpass_title);
  178. tvtitle.setText(getArguments().getString("title"));
  179.  
  180. editTextusername = (EditText) v.findViewById(R.id.et_dialogfragmentuserpass_username);
  181. editTextpassword = (EditText) v.findViewById(R.id.et_dialogfragmentuserpass_password);
  182.  
  183.  
  184. Button btnlogin = (Button) v.findViewById(R.id.btn_dialogfragmentuserpass_login);
  185. btnlogin.setOnClickListener(this);
  186. Button btncancel = (Button) v.findViewById(R.id.btn_dialogfragmentuserpass_cancel);
  187. btncancel.setOnClickListener(this);
  188.  
  189.  
  190. if(bundle!=null) {
  191. editTextpassword.setText(bundle.getCharSequence("SaveInstanceStatepassword"));
  192. editTextusername.setText(bundle.getCharSequence("SaveInstanceStateusername"));
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. return v;
  201. }
  202.  
  203. @Override
  204. public void onSaveInstanceState(Bundle icicle) {
  205. super.onSaveInstanceState(icicle);
  206. icicle.putCharSequence("SaveInstanceStatepassword", editTextpassword.getText());
  207. icicle.putCharSequence("SaveInstanceStateusername", editTextusername.getText());
  208.  
  209. }
  210.  
  211.  
  212. public void onClick(View v)
  213. {
  214. OnMFDialogFragmentDoneListener act = (OnMFDialogFragmentDoneListener)getActivity();
  215. if (v.getId() == R.id.btn_dialogfragmentuserpass_login)
  216. {
  217.  
  218. act.onMFDialogFragmentDone(this.getTag(), false,
  219. editTextusername.getText(),editTextpassword.getText());
  220. dismiss();
  221. return;
  222. }
  223. if (v.getId() == R.id.btn_dialogfragmentuserpass_cancel)
  224. {
  225. act.onMFDialogFragmentDone(this.getTag(), true, "nousername", "nopass");
  226. dismiss();
  227. return;
  228. }
  229.  
  230.  
  231. }
  232. }
  233.  
  234. public class MainActivity extends Activity
  235. implements OnMFDialogFragmentDoneListener
  236.  
  237. public static final String
  238. PROMPT_MFDIALOGFRAGMENT_TAG="PROMPT_MFDIALOGFRAGMENT_TAG";
  239.  
  240. public void onMFDialogFragmentDone(String tag, boolean cancelled,
  241. CharSequence username,
  242. CharSequence password){
  243. String total;
  244.  
  245. total=username.toString()+"...."+password.toString();
  246. Toast.makeText(this,total,Toast.LengthShort).show();
  247. }
  248.  
  249. public void MFDialogFragmentShow(){
  250. FragmentTransaction ft = getFragmentManager().beginTransaction();
  251. MFDialogFragment mfDialogFragment =
  252. MFDialogFragment.newInstance("Login to your account");
  253. mfDialogFragment.show(ft, PROMPT_MFDIALOGFRAGMENT_TAG);
  254.  
  255. }
  256.  
  257. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  258. android:orientation="vertical" android:layout_width="match_parent"
  259. android:layout_height="match_parent">
  260.  
  261. <TextView
  262. android:layout_width="wrap_content"
  263. android:layout_height="wrap_content"
  264. android:text="Title"
  265. android:id="@+id/tv_dialogfragmentuserpass_title"/>
  266. <EditText
  267. android:layout_width="wrap_content"
  268. android:layout_height="wrap_content"
  269. android:hint="User Name"
  270.  
  271. android:id="@+id/et_dialogfragmentuserpass_username"/>
  272. <EditText
  273. android:layout_width="wrap_content"
  274. android:layout_height="wrap_content"
  275. android:hint="Password"
  276. android:password="true"
  277. android:id="@+id/et_dialogfragmentuserpass_password"/>
  278. <LinearLayout
  279. android:orientation="horizontal"
  280. android:layout_width="wrap_content"
  281. android:layout_height="wrap_content">
  282.  
  283. <Button
  284. android:layout_width="wrap_content"
  285. android:layout_height="wrap_content"
  286. android:text="Login"
  287. android:id="@+id/btn_dialogfragmentuserpass_login"/>
  288. <Button
  289. android:layout_width="wrap_content"
  290. android:layout_height="wrap_content"
  291. android:text="Cancel"
  292. android:id="@+id/btn_dialogfragmentuserpass_cancel"/>
  293.  
  294.  
  295. </LinearLayout>
  296. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement