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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With