Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#FFFFFF"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7.  
  8.  
  9. <TextView
  10. android:id="@+id/title"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:paddingTop="10dp"
  14. android:paddingBottom="10dp"
  15. android:text="Contact"
  16. android:gravity="center_horizontal"
  17. android:textAppearance="@style/TextAppearance.AppCompat.Large"
  18. android:layout_gravity="center"
  19. android:textColor="#FFFFFF"
  20. android:background="#283593"/>
  21.  
  22. <ListView xmlns:android="http://schemas.android.com/apk/res/android"
  23. android:id="@+id/listView"
  24. android:layout_height="wrap_content"
  25. android:layout_width="match_parent"
  26. android:layout_gravity="center"
  27. >
  28. </ListView>
  29.  
  30. <Button
  31. android:id="@+id/cancel_button"
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:textAppearance="@style/TextAppearance.AppCompat.Medium"
  35. android:text="Cancel"
  36. android:textColor="#FFFFFF"
  37. android:background="#283593"/>
  38.  
  39. </LinearLayout>
  40.  
  41. <?xml version="1.0" encoding="utf-8"?>
  42. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  43. android:orientation="vertical" android:layout_width="match_parent"
  44. android:layout_height="match_parent">
  45.  
  46. <TextView
  47. android:id="@android:id/text1"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content"
  50. android:textAppearance="?android:attr/textAppearanceMedium"
  51. android:gravity="center"
  52. android:textColor="#000000"
  53. android:minHeight="?android:attr/listPreferredItemHeight"
  54. />
  55. </LinearLayout
  56.  
  57. public class OptionMenuFragment extends Fragment implements View.OnClickListener{
  58.  
  59. private String phoneNumber, email;
  60.  
  61. public OptionMenuFragment() {
  62. // Required empty public constructor
  63. }
  64.  
  65. @Override
  66. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  67. Bundle savedInstanceState) {
  68.  
  69. View view = inflater.inflate(R.layout.option_main_fragment, container, false);
  70.  
  71. Button button1=(Button) view.findViewById(R.id.button1);
  72. button1.setOnClickListener(this);
  73. Button button2=(Button) view.findViewById(R.id.button2);
  74. button2.setOnClickListener(this);
  75. ....
  76. Button button6=(Button) view.findViewById(R.id.button6);
  77. button6.setOnClickListener(this);
  78.  
  79. getActivity().setTitle("Home");
  80. // Inflate the layout for this fragment
  81. return view;
  82.  
  83. }
  84. }
  85.  
  86. @Override
  87. public void onClick(View view) {
  88. switch (view.getId()) {
  89. case R.id.button1:
  90. Intent intent = new Intent(getActivity(), MyPage.class);
  91. startActivity(intent);
  92. break;
  93. case R.id.button2:
  94. Toast.makeText(getActivity(), "button 2 clicked", Toast.LENGTH_SHORT).show();
  95. break;
  96. ....................
  97. break;
  98. case R.id.button5:
  99. // show the first alert dialog
  100. showFirstDialog();
  101. break;
  102. case R.id.button6:
  103. Toast.makeText(getActivity(), "button 6 clicked", Toast.LENGTH_SHORT).show();
  104. break;
  105. }
  106. }
  107.  
  108. private void showFirstDialog() {
  109.  
  110. //Create a new builder and get the layout.
  111. final AlertDialog.Builder builder = new
  112. AlertDialog.Builder(this.getActivity());
  113. LayoutInflater inflater = this.getActivity().getLayoutInflater();
  114. View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
  115. builder.setView(dialogView);
  116. builder.setCancelable(true);
  117. //Show the dialog
  118. final AlertDialog alert = builder.show();
  119. //Get the TextView, ListView, Button from the layout.
  120. TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
  121. Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
  122. ListView alertListView = (ListView) dialogView.findViewById(listView);
  123. //Set title
  124. alertTitle.setText("Contact");
  125. //Defined Array values to show in ListView
  126. String[] companyName = getResources().getStringArray(R.array.company_name);
  127.  
  128. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
  129. (this.getActivity(), R.layout.first_alertlist_textstyle, android.R.id.text1, companyName);
  130. alertListView.setAdapter(arrayAdapter);
  131.  
  132. alertButton.setOnClickListener(new View.OnClickListener() {
  133. @Override
  134. public void onClick(View view) {
  135. alert.dismiss();
  136. }
  137. });
  138.  
  139. alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
  140. {
  141.  
  142. @Override
  143. public void onItemClick(AdapterView<?> parent, View view,
  144. int position, long id) {
  145. // ListView Clicked item index
  146. int itemPosition = position;
  147.  
  148. if (itemPosition == 0) {
  149. alert.dismiss();
  150. showSecondDialog(0);
  151. }
  152. else if (itemPosition == 1) {
  153. alert.dismiss();
  154. showSecondDialog(1);
  155. }
  156. else if (itemPosition == 2) {
  157. alert.dismiss();
  158. showSecondDialog(2);
  159. }
  160. else if (itemPosition == 3) {
  161. alert.dismiss();
  162. showSecondDialog(3);
  163. }
  164. else if (itemPosition == 3) {
  165. alert.dismiss();
  166. showSecondDialog(3);
  167. }
  168. else if (itemPosition == 4) {
  169. alert.dismiss();
  170. showSecondDialog(4);
  171. }
  172. else if (itemPosition == 5) {
  173. alert.dismiss();
  174. showSecondDialog(5);
  175. }
  176. }
  177. });
  178. }
  179.  
  180. private void showSecondDialog(int position) {
  181. final AlertDialog.Builder builder = new
  182. AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
  183. LayoutInflater inflater = this.getActivity().getLayoutInflater();
  184. View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
  185. builder.setView(dialogView);
  186. builder.setCancelable(true);
  187. //setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
  188.  
  189. //Show the dialog
  190. final AlertDialog alert = builder.show();
  191.  
  192. //Get the TextView, ListView, Button from the layout.
  193. TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
  194. Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
  195. final ListView alertListView = (ListView) dialogView.findViewById(listView);
  196.  
  197. alertTitle.setText("What do you want to do");
  198. // Defined Array values to show in ListView
  199. String[] values = getResources().getStringArray(R.array.contact_way);
  200.  
  201. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
  202. R.layout.first_alertlist_textstyle, android.R.id.text1, values);
  203. alertListView.setAdapter(adapter);
  204.  
  205. alertButton.setOnClickListener(new View.OnClickListener() {
  206. @Override
  207. public void onClick(View view) {
  208. alert.dismiss();
  209. }
  210. });
  211.  
  212. // Can I do this logic in different way?
  213.  
  214. switch (position) {
  215. case 0:
  216. phoneNumber = "12335";
  217. email="mail_1.com";
  218. break;
  219. case 1:
  220. phoneNumber = "123566";
  221. email="mail_2.com";
  222. break;
  223. case 2:
  224. phoneNumber = "125989";
  225. email="mail_3.com";
  226. break;
  227. case 3:
  228. phoneNumber = "67790";
  229. email="mail_4.de";
  230. break;
  231. case 4:
  232. phoneNumber = "874545";
  233. email="mail_5.com";
  234. break;
  235. case 5:
  236. phoneNumber = "79874";
  237. email="mail_6.de";
  238.  
  239. }
  240. alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
  241. {
  242. @Override
  243. public void onItemClick(AdapterView<?> parent, View view,
  244. int position, long id) {
  245. // ListView Clicked item index
  246. int itemPosition = position;
  247.  
  248.  
  249. if (itemPosition == 0) {
  250.  
  251. alert.dismiss();
  252. AlertDialog.Builder builder = new
  253. AlertDialog.Builder(getActivity());
  254.  
  255. builder.setTitle("calling to " + phoneNumber);
  256. //builder.setMessage("Are you sure you want to log out?");
  257.  
  258. builder.setPositiveButton("Call", new DialogInterface.OnClickListener()
  259. {
  260.  
  261. public void onClick(DialogInterface dialog, int which) {
  262. // close the dialog, go to login page
  263. if (isPermissionGranted()) {
  264. call_action();
  265. }
  266. }
  267. });
  268. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  269. @Override
  270. public void onClick(DialogInterface dialog, int which) {
  271. // Do nothing
  272. dialog.dismiss();
  273. }
  274. });
  275.  
  276. AlertDialog alert = builder.create();
  277. alert.show();
  278.  
  279. }
  280. if (itemPosition == 1){
  281. alert.dismiss();
  282. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  283.  
  284. builder.setTitle("Email to " + email);
  285.  
  286. builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
  287.  
  288. @Override
  289. public void onClick(DialogInterface dialog, int which) {
  290. // Do nothing
  291. dialog.dismiss();
  292. }
  293. });
  294. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  295. @Override
  296. public void onClick(DialogInterface dialog, int which) {
  297. // Do nothing
  298. dialog.dismiss();
  299. }
  300. });
  301.  
  302. AlertDialog alert = builder.create();
  303. alert.show();
  304. }
  305.  
  306. //several cases..
  307. }
  308. });
  309. }
  310. public void call_action(){
  311.  
  312. Intent callIntent = new Intent(Intent.ACTION_CALL);
  313. callIntent.setData(Uri.parse("tel:" + phoneNumber));
  314. startActivity(callIntent);
  315. }
  316. public boolean isPermissionGranted() {
  317. if (Build.VERSION.SDK_INT >= 23) {
  318. if (getActivity().checkSelfPermission(android.Manifest.permission.CALL_PHONE)
  319. == PackageManager.PERMISSION_GRANTED) {
  320. Log.v("TAG","Permission is granted");
  321. return true;
  322. } else {
  323.  
  324. Log.v("TAG","Permission is revoked");
  325. ActivityCompat.requestPermissions(this.getActivity(), new String[]
  326. {Manifest.permission.CALL_PHONE}, 1);
  327. return false;
  328. }
  329. }
  330. else { //permission is automatically granted on sdk<23 upon installation
  331. Log.v("TAG","Permission is granted");
  332. return true;
  333. }
  334. }
  335.  
  336. @Override
  337. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  338. switch (requestCode) {
  339. case 1: {
  340. if (grantResults.length > 0
  341. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  342. Toast.makeText(getActivity().getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
  343. call_action();
  344. } else {
  345. Toast.makeText(getActivity().getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
  346. }
  347. return;
  348. }
  349.  
  350. // other 'case' lines to check for other
  351. // permissions this app might request
  352. }
  353. }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement