Home > Mobile >  how to use the same function inside two or more Buttons in the onCreate() method
how to use the same function inside two or more Buttons in the onCreate() method

Time:12-02

Im new in android and i do not know many things. I have a problem making a function inside two ore more buttons and place it in the onCreate method. For example:

public class gameActivity extends AppCompatActivity {

    private int num = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        //the two buttons from layout
        Button tl = findViewById(R.id.buttonTL);
        Button tm = findViewById(R.id.buttonTM);

        //the void function that the buttons may use
        void printChar(){
            //do domething
        }
            

        //click listeners to above Buttons
        tl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tl.setClickable(false);
                num  ;
                //function i want to call
                printChar();
            }
        });

        tm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tm.setClickable(false);
                num  ;\
                //function i want to call
                printChar();
            }
        });            
    }
}

All i want to do is to make the printChar(); function inside the onCreate(), to make all the buttons use this, and not write the same code inside each button again and again..

CodePudding user response:

Not sure how to define another method inside another method in java, But will this be fine to you if you define the method just somewhere outside of onCreate but still inside the activity?

public class gameActivity extends AppCompatActivity {

    private int num = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        //the two buttons from layout
        Button tl = findViewById(R.id.buttonTL);
        Button tm = findViewById(R.id.buttonTM);



        //click listeners to above Buttons
        tl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tl.setClickable(false);
                num  ;
                //function i want to call
                printChar(num);
            }
        });

        tm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tm.setClickable(false);
                num  ;
                //function i want to call
                printChar(num);
            }
        });
    }

    //the void function that the buttons may use
    void printChar(int num){
        Log.d("PrintNum", "Num: "   num);
    }
}

CodePudding user response:

After reading the OP comments :

public class gameActivity extends AppCompatActivity {

    private int num = 0;
    private Button tl, tm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        //the two buttons from layout
        tl = findViewById(R.id.buttonTL);
        tm = findViewById(R.id.buttonTM);


        //click listeners to above Buttons
        tl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tl.setClickable(false);
                num  ;
                //function i want to call
                printChar();
            }
        });

        tm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tm.setClickable(false);
                num  ;\
                //function i want to call
                printChar();
            }
        });            
    }


    void printChar(){
            //do domething
            String textTl = tl.getText().ToString();
           String textTm = tm.getText().ToString();

        }  
}
  • Related