Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to get button text on click in Android?

$
0
0

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);

}


Viewing all articles
Browse latest Browse all 178

Latest Images

Trending Articles



Latest Images