Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Set Clicked focus on Button

Tags:

android

button

I am having 4 buttons in my layout, now whenever i click any button, after the click event, button should be highlighed showing that it was clicked last.

For making such thing, i have tried the following:

code:

btn1.setOnClickListener(new button_click_listener());
btn2.setOnClickListener(new button_click_listener());


class button_click_listener implements Button.OnClickListener
{
    @Override
    public void onClick(View v) 
    {
       if(v==btn1)
       {
            btn1.requestFocus();
       }

       if(v==btn2)
       {
            btn2.requestFocus();
       }
        .......
    }
}

XML Layout:

<Button 
   android:text="Click 1" 
   android:id="@+id/btnClick1" 
   android:layout_width="70dp" 
   android:layout_height="wrap_content"
   style="@android:style/Widget.Button.Small"
   android:padding="10dp"
   android:focusableInTouchMode="true">
</Button>

How do i show clicked highlight on the button that was clicked last? Please, show me a way and give suggestions.

Update:

If i set android:focusable="true" then button is highlighted and focused, but then at the same time, i need to click twice on the button to perform the actual click event.

like image 580
Paresh Mayani Avatar asked Jan 13 '11 12:01

Paresh Mayani


People also ask

How to make a button clickable in android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

What is OnClickListener in android?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.


1 Answers

its easy buddy...just you have to set backgroundcolor of ur button on ur click event and each time any button clicked u have to set other buttons background color null

like image 131
chikka.anddev Avatar answered Sep 17 '22 08:09

chikka.anddev