Kushal Shah
Kushal Shah

Reputation: 1333

Want to use the same function in every class

I am developing an android application. Now i have created one function which create custom dialog box and i want this dialog box to display on every activity. So i need to call this function at every activity. But as the syntax of custom dialog (e.g. Dialog d = new Dialog(home.this)).home is the name of the activity where i have created the function so i am not ale to use this function in any other activity. And i haven't use android that much. So give me good example to solve my problem. Here is my code

Upvotes: 2

Views: 174

Answers (4)

Niranj Patel
Niranj Patel

Reputation: 33258

here is sample code code of using AlertDialog in all activity.

crate one class file like as allmethod.java

and add this code in that class

public static void showAlert(Activity act,String msg)
        {
            AlertDialog.Builder alert = new AlertDialog.Builder(act);
            alert.setMessage(msg).setPositiveButton("OK", new OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which)
                {   
                }
            }).show();
        }

and you can use from any class like as

allmethod.showAlert(Activity,"Message");

In your case..

public void SearchDialog(Context ctx){
           final Dialog dialog = new Dialog(ctx);
       dialog.setContentView(R.layout.dialogsearch);
       dialog.setTitle("       Enter The Text to Search");
       dialog.setCancelable(true);

       final EditText Text = (EditText) dialog.findViewById(R.id.EdText);

       Button buttonOK = (Button) dialog.findViewById(R.id.btnOK);
       buttonOK.setOnClickListener(new OnClickListener() {

           public void onClick(View v) {
                   String SearchText = Text.getText().toString();

               prefsPrivate =getSharedPreferences(Login.PREFS_PRIVATE,Context.MODE_PRIVATE);
                   Editor prefsPrivateEdit=prefsPrivate.edit();                        
                   prefsPrivateEdit.putString("Text",SearchText);
                   prefsPrivateEdit.commit();
                   Intent i = new Intent(ctx,SearchTask.class);
                   startActivity(i);
                   dialog.cancel();
           }
       });

       Button buttonCancel = (Button) dialog.findViewById(R.id.btnCancel);
       buttonCancel.setOnClickListener(new OnClickListener() {

           public void onClick(View v) {
                dialog.cancel();

           }
       });
       dialog.show();
   }

Upvotes: 2

Yahor10
Yahor10

Reputation: 2132

final class Uutil {

public void static func() {

}

}

then do it in your classes:

class A {
public void f() {
Uutil.func();
}
}

Upvotes: 0

Zulaxia
Zulaxia

Reputation: 2752

You could either define your own Interface and implement for every class, or make the main Activity method static (as long as it wont need to access anything in dynamic objects that aren't method arguments).

Upvotes: 0

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72341

Just add a Context parameter to your SearchDialog() constructor.

Make it like this:

   public SearchDialog(Context context){
        //....
   }

Upvotes: 0

Related Questions