1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to access one onclick method for 2 buttons which are in two different activities?

Discussion in 'Android Development' started by nagamothu, May 16, 2019.

  1. nagamothu

    nagamothu Newbie
    Thread Starter

    Hi guys can anyone please clear my doubt. i have 2 activities. activity 1 and activity 2. and each activity has a button. Now my doubt is i want to access a onclick method written for button which is in activity 1 for the button which is in activity 2. in the whole i want to access same onclick method for two buttons which are in two different activities. how can we do that?

     


  2. Best Answer:
    Post #4 by 23tony, May 16, 2019 (1 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Brian Berry

    Brian Berry Well-Known Member

    Get the view for both buttons in both main and set the same code
     
  4. 23tony

    23tony Well-Known Member

    You could create a third class (generic, not an activity) and define your onclick method there. Then have your clickhandlers invoke that class & method. You may need to pass in some additional variables to give it the context.
     
    GameTheory likes this.
  5. 23tony

    23tony Well-Known Member

    Here's an example of something similar I did for file operations:
    Code (Text):
    1.  
    2.  
    3. import android.content.Context;
    4.  
    5. import java.io.FileInputStream;
    6. import java.io.FileOutputStream;
    7.  
    8. public class Lib {
    9.     public static final String ERROR = "ERROR";
    10.  
    11.     private Lib() {}
    12.  
    13.     public static String loadFile(String filename, Context context) {
    14.         FileInputStream filestream;
    15.         String filecontents = "";
    16.         try {
    17.             filestream = context.openFileInput(filename);
    18.             int c;
    19.             while( (c=filestream.read()) != -1) {
    20.                 filecontents += Character.toString((char)c);
    21.             }
    22.             filestream.close();
    23.         }
    24.         catch (Exception e) {
    25.             filecontents = ERROR;
    26.         }
    27.         return filecontents;
    28.     }
    29.  
    30.     public static void saveFile(String filename, String str, Context context) {
    31.         FileOutputStream filestream;
    32.         try {
    33.             filestream = context.openFileOutput(filename, Context.MODE_PRIVATE);
    34.             filestream.write(str.getBytes());
    35.             filestream.close();
    36.         }
    37.         catch (Exception e) {
    38.             e.printStackTrace();
    39.         }
    40.     }
    41. }
    42.  
    Hope that helps!
     
    GameTheory likes this.
  6. GameTheory

    GameTheory Android Expert

    Good answer 23tony, what you said is the right way. ;)
    Basically, a utility class that holds some reusable methods(objects) to reuse anywhere in the app as oppose to rewriting the same code over each time it's needed.
     
  7. nagamothu

    nagamothu Newbie
    Thread Starter


    Hi thank you for you response tried that but blank activity is opening instead of scan camera.please check this.
    ScancodeActivity.java
    Code (Text):
    1.  
    2. @Override
    3. public  void onClick(View v) {
    4.       zXingScannerView = new ZXingScannerView( this);
    5.     setContentView(zXingScannerView);
    6.     zXingScannerView.setResultHandler( this );
    7.     scan( zXingScannerView );
    8. }
    9.  
    10. public  static void scan( View view){
    11.     zXingScannerView.startCamera();
    12. }
    13.  
    14.  
    15. @Override
    16. public void handleResult(Result result) {
    17.  
    18.  
    19.     if (result != null) {
    20.         //if qrcode has nothing in it
    21.         //if qr contains data
    22.         if (result.getText() == null) {
    23.             Toast.makeText( this, "Result Not Found", Toast.LENGTH_LONG ).show();
    24.         } else {
    25.             try {
    26.                 //converting the data to json
    27.                 JSONObject obj = new JSONObject( result.getText() );
    28.                 SharedPreferences sharedPreferences = getSharedPreferences( "json_data_file", MODE_PRIVATE );
    29.                 SharedPreferences.Editor editor = sharedPreferences.edit();
    30.                 editor.putString( "user_data", obj.getString( "user_data" ) );
    31.                 editor.putString( "Question_id", obj.getString( "Question_id" ) );
    32.                 editor.putString( "paper_id", obj.getString( "paper_id" ) );
    33.                 editor.apply();
    34.  
    35.             } catch (JSONException e) {
    36.                 e.printStackTrace();
    37.                 Toast.makeText( this, result.getText(), Toast.LENGTH_LONG ).show();
    38.             }
    39.         }
    40.         launchcamera();
    41.     }
    42.  
    This is scancodeActivity.java class this has a button which has a button "scan QRcode". after scanning qrcode result will be displayed in display activity.

    Now i want to add another button "scan another qr code" in display activity. when i click this button scan camera should open directly . but now when i click this either blank activity is opening or scancodeActivity is opening instead of scan camera.

    this is display.java
    Code (Text):
    1.  
    2.  
    3. @Override
    4. public void onClick(View v) {
    5.     zXingScannerView = new ZXingScannerView( this);
    6.    setContentView(zXingScannerView);
    7.    zXingScannerView.setResultHandler( (ZXingScannerView.ResultHandler) Display.this );
    8.    ScancodeActivity.scan(zXingScannerView);
    9. }
    10.  
    Please check code in both activities and tell me where am doing wrong.
     
  8. 23tony

    23tony Well-Known Member

    I think you have a couple of problems with how you've structured your code. By the look of it, you still have duplicate code, and you're not calling the generic code. Also, the generic code should not be an onClick override.

    You should create a separate Java class that's not an activity or view - just a generic class. Let's call it Lib. Then, in that class, you define a static function as your click handler:
    Code (Text):
    1.  
    2. import android.content.Context;
    3. public class Lib {
    4.     public static final String ERROR = "ERROR";
    5.  
    6.     private Lib() {}
    7.  
    8.     public static void scannerClickHandler(View v, Context context) {
    9.         zXingScannerView = new ZXingScannerView(context);
    10.         setContentView(zXingScannerView);
    11.         zXingScannerView.setResultHandler( (ZXingScannerView.ResultHandler) context );
    12.         ScancodeActivity.scan(zXingScannerView);
    13.     }
    14. }
    15.  
    Then your click handlers would both be like this:
    Code (Text):
    1.  
    2. @Override
    3. public  void onClick(View v) {
    4.     Lib.scannerClickHandler(v, this);
    5. }
    6.  
     
    nagamothu likes this.
  9. nagamothu

    nagamothu Newbie
    Thread Starter

    Done in the same way but am getting error can you please see this error.
    Process: com.codeexap.ews, PID: 22934
    java.lang.ClassCastException: com.codeexap.ews.Display cannot be cast to me.dm7.barcodescanner.zxing.ZXingScannerView$ResultHandler
    at com.codeexap.ews.ScancodeActivity$lib.scannerClickHandler(ScancodeActivity.java:263)
    at com.codeexap.ews.Display.onClick(Display.java:140)
    at android.view.View.performClick(View.java:4848)
    at android.view.View$PerformClick.run(View.java:20299)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:218)
    at android.app.ActivityThread.main(ActivityThread.java:5657)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:990)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:785)


    This is the code i have changed .
    scancodeactivity.java
    Code (Text):
    1.  
    2.  @Override
    3.     public void onClick(View v) {
    4.          
    5.        lib.scannerClickHandler( v,this );
    6.         setContentView(zXingScannerView);
    7.     }
    8. public static class lib
    9.     {
    10.         public lib() {
    11.         }
    12.  
    13.         public static void scannerClickHandler (View v, Context context){
    14.         zXingScannerView = new ZXingScannerView( context );
    15.  
    16.         zXingScannerView.setResultHandler( (ZXingScannerView.ResultHandler) context );
    17.         zXingScannerView.startCamera();
    18.     }
    19.     }
    20.  
    Display.java
    Code (Text):
    1.  
    2. @Override
    3. public void onClick(View v) {
    4.  
    5.    ScancodeActivity.lib.scannerClickHandler( v,this );
    6.     setContentView(zXingScannerView);
    7. }
    8.  
     
  10. 23tony

    23tony Well-Known Member

    It looks like your context needs to have a result handler, and that's what you need to reference. So define a result handler in Display and in the activity, and use context.resultHandler
     
Loading...
Similar Threads - access onclick method
  1. Uzinoor
    Replies:
    12
    Views:
    422
  2. Adithyansm
    Replies:
    12
    Views:
    750
  3. mrspyro
    Replies:
    0
    Views:
    739
  4. Banks305
    Replies:
    2
    Views:
    561
  5. persistentone
    Replies:
    2
    Views:
    340
  6. PeterF75
    Replies:
    0
    Views:
    690
  7. neil154
    Replies:
    2
    Views:
    772
  8. Wandering Camera Guy
    Replies:
    0
    Views:
    1,064
  9. Wax Bean Tycoon
    Replies:
    14
    Views:
    1,403
  10. Emma B
    Replies:
    1
    Views:
    1,302

Share This Page

Loading...