Sunday 5 February 2017

To show dialog from outside the activity Android

To show dialog from outside the activity



Suppose you have a requirement to show the dialog from outside the activity or to show dialog from more than one activity or fragment .
So the efficient solution to it is to show it from a common method in some file .For this an approach that I found is to just implement the function call to show the dialog in a common file and make a call to it from all of your required places.


This looks good . But what if on the basis of the response “YES”, or “NO”, selected by the user requires you to perform some functionality on the calling class view ,like calling an api or updating the activity views. Then we will required to implement a callback for the selected result.


Example here shows a function showing the list of chocolates in a dialog . The list contains an image an text on the right of it.
The layout I made is :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/bgcolour_sharepoints">


  <TextView
      android:id="@+id/dialog_header"
      android:textColor="@color/black"
      android:textSize="18sp"
      android:padding="@dimen/margin_10dp"
      android:layout_width="match_parent"
      android:text="@string/select_language"
      android:layout_height="wrap_content" />
  <View
     android:background="@color/black"
      android:layout_width="match_parent"
      android:layout_height="1dp"/>
  <ListView
      android:id="@+id/listView_chocolate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
</LinearLayout>


Function to show the dialog :
String selectedVal ;
   public void showChocListDialog(final Context context, Activity activity) {


       ListAdapter arrayAdapter;
       final ArrayList<Chocolate> arraylist = new ArrayList<>();
//these drawables are present in the drawable folder
       Integer[] imageId = {
               R.drawable.silk,
               R.drawable.dairymilk
       };


// chocolates_list is a string array in the strings.xml file
       final String[] chocolates= context.getResources().getStringArray(R.array.chocolates_list);
       for (int i = 0; i < chocolates.length; i++) {
           Language chocObj = new Language(languages[i].substring(3, languages[i].length()), imageId[i]);
           arraylist.add(chocObj);
       }
       arrayAdapter = new ChocolateArrayAdpater(activity, arraylist);


       dialog = new Dialog(mContext);
       dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


      View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_layout, null);


       ListView lv = (ListView) view.findViewById(R.id.listView_chocolate);
       TextView tv_header = (TextView) view.findViewById(R.id.dialog_header);
       lv.setAdapter(arrayAdapter);


       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               selectedVal = arraylist.get(position).getName();
//will dicsuss about this line later.    
mListener._func_chocolateSetup();
               Log.d(TAG, "chosen " + selectedVal);
               dialog.dismiss();      
           }
       });
       dialog.setContentView(view);
       dialog.show();
   }


The above function shows how to display a dialog with a list view of items . Items contains an imageview and a text view . On clicking the an item from the list , we save the selected item in a variable to be used later .
Note : This method is written in a common class , u can name it anything like Utility or Utils, Common as per your conveniece.


As you can see we have used a model class in this named Chocolate and an array adapter named ChocolateArrayAdapater , well this is required to populate the list .
Their definition is as below:


   static Chocolate {
       private String name;
       private int image_id;


       Chocolate(String name, int imageid) {
           this.name = name;
           this.image_id = imageid;
       }


       public String getName() {
           return name;
       }


       public void setName(String name) {
           this.name = name;
       }


       int getImage_id() {
           return image_id;
       }


       public void setImage_id(int image_id) {
           this.image_id = image_id;
       }
   }


//the array adpater definition
   private class ChocolateArrayAdpater extends ArrayAdapter<Chocolate> {


       private final List<Chocolate> list;
       private final Activity context;


       class ViewHolder {
           protected TextView name;
           ImageView iv_country;
       }


       ChocolateArrayAdpater(Activity context, List<Chocolate> list) {
           super(context, R.layout.Chocolate_row, list);
           this.context = context;
           this.list = list;
       }


       @NonNull
       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
           View view;
           if (convertView == null) {
               LayoutInflater inflator = context.getLayoutInflater();
               view = inflator.inflate(R.layout.Chocolate_row, null);
               final ChocolateArrayAdpater.ViewHolder viewHolder = new ChocolateArrayAdpater.ViewHolder();
               viewHolder.name = (TextView) view.findViewById(R.id.list_value);
               viewHolder.iv_country = (ImageView) view.findViewById(R.id.iv_country);
               view.setTag(viewHolder);
           } else {
               view = convertView;
           }


           ChocolateArrayAdpater.ViewHolder holder = (ChocolateArrayAdpater.ViewHolder) view.getTag();
           holder.name.setText(list.get(position).getName());
           holder.iv_country.setBackgroundDrawable(context.getResources().getDrawable(list.get(position).getImage_id()));
           return view;
       }
   }


No for multiple activity or fragment to interact with this piece of code in the Common class we needs a mediator , this cannot be done directly.
So we define and interface in the same Common Class in our project like this :


   public interface I_Chocolatestup
   {
       void _func_chocolateSetup();
   }


In the common class for interaction with the activities and fragments in your app , we do this :


//the mListener to initialize the class which will receive the call back
  private I_Chocolatestup mListener;
   private final Context mContext;
   private final Activity mActivity;
   public Utility(Context context, I_Chocolatestup listener,Activity mActivity) {
       mContext = context;
       mListener = listener;
       this.mActivity = mActivity;
   }


Remember we wrote a line in the function to show the dialog which we wrote to discuss later . now is the correct time.
mListener._func_chocolateSetup();


This is meant to call the class that will intialiaze its context in the Utility() so that the class gets the callback to perform the action based on the selected values.


Till this point all of the code is to be in the Common Utility class


Now is the part for the Activity/Fragment  class.


For all the classes that want to display this dialog , we implement this interface in them like this:


public class ChocolateSelectionActivity extends WifogFragmentActivity implements Utility.I_Chocolatestup


So we have to override the function in the interface :
   @Override
   public void _func_chocolateSetup() {
     //perform your action whatever you want
   }


Now the final and climax line to trigger the showing of the dialog and initializing the context to the listener to get the call back :


Utility utilObj;
utilObj = new Utility(this,this,this);
utilObj.showChocolateListDialog(this.getActivity(), this.getActivity());


Finally its done. Now use the showChocolateListDialog() as  many places as you want without redundant codes in the classes .


In next blog I will tell you on how can you avoid creation of multiple dialogs from same piece of code.

No comments:

Post a Comment

Localization implementation in android apps

The Language translation for the android apps to support the Localization. In android apps, if you want to support multiple languages ...