Skip to main content

Passing the click events back to the dilaog's host in android

In this post i am going to show you how to get the dialog's click events back to the host activity or fragment. By default the click events of a dialog is handled by the DialogFragment. You can transfer the click events back to the host activity or fragment by declaring an interface in the dilalog class.
Ads by Google



In this example we have a TextView in the activity_main.xml file. We have to change the font color of the TextView using an alert dialog with single choice list. So the user can select a color from the dilalog and we set that particular color as the font color for the TextView.

ColorDialog.java
1:  public class ColorDialog extends DialogFragment  
2:  {  
3:   CharSequence[] sequence = {"RED","GREEN","BLUE"};  
4:   String color_selection;  
5:       @Override  
6:       public Dialog onCreateDialog(Bundle savedInstanceState) {  
7:            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());  
8:            builder.setTitle("Choose your color...");  
9:            builder.setSingleChoiceItems(sequence, -1, new OnClickListener() {  
10:                 @Override  
11:                 public void onClick(DialogInterface dialog, int which) {  
12:                      color_selection = (String)sequence[which];  
13:                 }  
14:            });  
15:            builder.setPositiveButton("OK", new OnClickListener() {  
16:                 @Override  
17:                 public void onClick(DialogInterface dialog, int which) {  
18:                 color_interface.positiveClick(ColorDialog.this,color_selection);  
19:                 }  
20:            });  
21:            builder.setNegativeButton("CANCEL", new OnClickListener() {  
22:                 @Override  
23:                 public void onClick(DialogInterface dialog, int which) {  
24:                      color_interface.negetiveClick(ColorDialog.this);  
25:                 }  
26:            });  
27:            return builder.create();  
28:       }  
29:       public interface ColorInterface  
30:       {  
31:            public void positiveClick(DialogFragment dialog,String color_selection);  
32:            public void negetiveClick(DialogFragment dialog);  
33:       }  
34:       ColorInterface color_interface;  
35:       @Override  
36:            public void onAttach(Activity activity) {  
37:                 // TODO Auto-generated method stub  
38:                 super.onAttach(activity);  
39:                 try  
40:                 {  
41:                 color_interface = (ColorInterface) activity;  
42:                 }  
43:                 catch(ClassCastException e)  
44:                 {  
45:                      throw new ClassCastException(activity.toString() + "must override methods..");  
46:                 }  
47:            }  
48:  }  

MainActivity.java
1:  public class MainActivity extends FragmentActivity implements ColorInterface {  
2:  TextView text_view;  
3:       @Override  
4:       protected void onCreate(Bundle savedInstanceState) {  
5:            super.onCreate(savedInstanceState);  
6:            setContentView(R.layout.activity_main);  
7:            text_view = (TextView) findViewById(R.id.textView1);  
8:            text_view.setOnClickListener(new OnClickListener() {  
9:                 @Override  
10:                 public void onClick(View arg0) {  
11:                 ColorDialog dialog = new ColorDialog();  
12:                 dialog.show(getSupportFragmentManager(), "my_tag");  
13:                 }  
14:            });  
15:       }  
16:       @Override  
17:       public void positiveClick(DialogFragment dialog,String color_selection) {  
18:            if(color_selection.equals("RED"))  
19:            {  
20:                 text_view.setTextColor(Color.RED);  
21:            }  
22:            else if (color_selection.equals("BLUE"))  
23:            {  
24:                 text_view.setTextColor(Color.BLUE);  
25:            }  
26:            else if (color_selection.equals("GREEN"))  
27:            {  
28:                 text_view.setTextColor(Color.GREEN);  
29:            }       
30:       }  
31:       @Override  
32:       public void negetiveClick(DialogFragment dialog) {  
33:            Toast.makeText(getBaseContext(), "No color selected..",Toast.LENGTH_SHORT).show();  
34:       }  
35:  }  



Comments

Popular posts from this blog

Android MySQL Database Operations

In this post we are going to learn about how to connect to MySQL Database from your Android Application and perform database operations. Here we create an android app that contain some login Activity through which the user can retrieve information from database and a registration Activity through which the user can add information into the database.  First you need to have the following components installed in your development machine.  1. Database : Here we use the MySQL database. 2. Web Server : Here we use the Apache Web Server. 3. Server side Scripting Language :   Here we use PHP for server side scripting. 4. Android Development environment : You must install android sdk and android studio.   I recommend you to download and install WAMPSERVER. The wamp server installer contains the following components. Apache Server Application MySQL Database PHP/phpMyAdmin First we have to create the database and table in MySQL. You can use the phpMyAdmin for mange yo

Android Swipe Views with Tabs

In this post we are going to learn about how to integrate the android tab view with the fragments using ViewPager and ActionBar class. For displaying the tabs on the top of the screen you need to interact with the android action bar, this is because the tab views is connected with the action bar. Ads by Google In this example application we make three tabs called "java", "php" and ".Net" and there are three seperate fragement view for each of these tabs. First you need to add the ViewPager into the activity_main.xml file. <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager> Now create three layout files for the fragments. 1. java_layout.xml <?xml version="1.0" encod

"please correct the errors on this form" adsense error simple solution

Many of the bloggers now facing the problem with their adsense widgets. Whenever try to add the new  adsense link unit using the blogger widgets, it shows the error " please correct the errors on this form " as shown bellow. In blogger you can add the adsense units in two ways. First one is adding the adsense using the blogger widgets(Most of the beginners doing this) and the second method is to obtain the adsense code from the adsense login page and place it into the targeted position on the blog. To improve the revenue from adsense you have to place the appropriate ad unit into the right position. The adsense link units are very important for increasing the adsense revenue. So due to this error many of the bloggers are unable to place the link units.  Here is the solution for the problem. 1. Login into your adsense and blogger account. 2. Now make sure that only two adsense widgets (units) are present on your blog, this is because Google allows onl