Reputation: 1360
I've found other answers to this but they where 2 and 6 years old so I didn't know if that was the best option.
I'm making an app that converts a simple integer in hex, oct and binary. The user can choose in which of the three options the integer must be converted to by three buttons located at the top of the Fragment.
The effect that I'm searching for is that if the user presses one of the three Buttons, the one that has been clicked remains of a certain color.
This is my XML code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingRight="16dp"
android:paddingLeft="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp">
<Button
android:id="@+id/hex_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="@string/hex_button"/>
<Button
android:id="@+id/binary_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="@id/hex_button"
android:text="@string/binary_button"/>
<Button
android:id="@+id/oct_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="@id/binary_button"
android:text="@string/oct_button"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/converted_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:inputType="number"
android:text="@string/convert_button"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp">
<EditText
android:id="@+id/int_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/integer_edit_text_hint"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"
android:layout_alignParentBottom="true" >
<EditText
android:id="@+id/converted_number_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
Do I have to make changes in the Java code or just in the XML to achieve this?
Upvotes: 0
Views: 352
Reputation: 460
This use in xml activity like for button :-
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="@id/binary_button"
android:text="@string/oct_button"
android:onClick="changeColor"
/>
Now in java class use like....
private boolean isChangeColor=true;
public void changeColor(View v)
{
if(isChangeColor)
v.setBackgroundColor(Color.BLACK);
else
v.setBackgroundColor(Color.WHITE);//use your original color here
isChangeColor=!isChangeColor;
}
Upvotes: 0
Reputation: 5233
You will have to set an onClickListener for the buttons. In this listener, you check which button invoked the event that triggered the listener, and you change the color of this button there.
So you will have to do it in the java code. A listener basically looks like this:
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Change Button Color here, and do other stuff
Button button = (Button) findViewById(v.getId());
button.setBackgroundColor(int color);
}
});
Upvotes: 1
Reputation: 6828
You can use two background drawables : one for active and other for inactive.
Add ClickListeners on all the buttons and toggle the backgrounds using setBackgroundResource();
method.
Upvotes: 1
Reputation: 2055
You will need to change color of button widget for onClick() method
Upvotes: 2