Breaking News
Home / Android / Android Snippets / How to get button text on click in Android?

How to get button text on click in Android?

The following code shows how to associated the OnClickListener with the button and later on when its clicked and that handler is invoked how to get the title to the button.

Button buttonAdvert = (Button) findViewById(R.id.button_advert);
       
        CharSequence text = “Hello”;
        buttonAdvert.setText(text);

        buttonAdvert.setOnClickListener(new View.OnClickListener()

{
            public void onClick(View view) {
                Log.d(“in : Button Advert onClick(..)”);
               
              // cast the view instance into  button
            Button b = (Button)v;
           String buttonText = b.getText().toString();

           Log.d(“Button title ” + buttonText);

}

[pb_builder]

Check Also

How to check if an Android device can connect to Internet?

The following code snippet shows how to check if an Android device can connect to …

Advertisment ad adsense adlogger