-
-
Save nyamwaya/1839a285a0f1c99828f63554b02fe1e4 to your computer and use it in GitHub Desktop.
[Android] Button background change for state. (selector)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | |
<!-- Active tab --> | |
<item android:drawable="@drawable/bg_selected" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/> | |
<!-- Inactive tab --> | |
<item android:drawable="@drawable/bg_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/> | |
<!-- Pressed tab --> | |
<item android:drawable="@drawable/bg_focused" android:state_pressed="true"/> | |
<!-- Selected tab (using d-pad) --> | |
<item android:drawable="@drawable/bg_focused" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/> | |
</selector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- bg_focused.xml --> | |
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shape="rectangle" > | |
<corners android:radius="10dp" /> | |
<gradient | |
android:centerColor="#5290c3" | |
android:endColor="#6f9fc7" | |
android:startColor="#78a4c9" /> | |
</shape> | |
<!-- bg_selected.xml --> | |
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shape="rectangle" > | |
<corners android:radius="10dp" /> | |
<gradient | |
android:centerColor="#72b0e3" | |
android:endColor="#8fafe7" | |
android:startColor="#98c4e9" /> | |
</shape> | |
<!-- bg_unselected.xml --> | |
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shape="rectangle" > | |
<corners android:radius="10dp" /> | |
<gradient | |
android:centerColor="#7ac19b" | |
android:endColor="#588d71" | |
android:startColor="#95ceae" /> | |
</shape> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" > | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_weight="1" | |
android:background="@color/bg_selector" | |
android:text="Now" /> | |
<Button | |
android:id="@+id/button2" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_weight="1" | |
android:background="@color/bg_selector" | |
android:text="Last" /> | |
<Button | |
android:id="@+id/button3" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_weight="1" | |
android:background="@color/bg_selector" | |
android:text="2mnt Ago" /> | |
</LinearLayout> | |
<TextView | |
android:id="@+id/text1" | |
android:layout_width="fill_parent" | |
android:layout_height="0dp" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:layout_weight="1" | |
android:padding="@dimen/padding_medium" | |
android:text="@string/hello_world" | |
tools:context=".MainActivity" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.sellectorsample; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class MainActivity extends Activity implements OnClickListener { | |
Button mButton1; | |
Button mButton2; | |
Button mButton3; | |
TextView mText1; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mButton1 = findViewByIdAndCast(R.id.button1); | |
mButton2 = findViewByIdAndCast(R.id.button2); | |
mButton3 = findViewByIdAndCast(R.id.button3); | |
mText1 = findViewByIdAndCast(R.id.text1); | |
mButton1.setOnClickListener(this); | |
mButton2.setOnClickListener(this); | |
mButton3.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
Button button = (Button) v; | |
// mButton1.setBackgroundResource(R.color.bg_selector); | |
// mButton2.setBackgroundResource(R.color.bg_selector); | |
// mButton3.setBackgroundResource(R.color.bg_selector); | |
// button.setBackgroundResource(R.drawable.bg_selected); | |
// clear state | |
mButton1.setSelected(false); | |
mButton1.setPressed(false); | |
mButton2.setSelected(false); | |
mButton2.setPressed(false); | |
mButton3.setSelected(false); | |
mButton3.setPressed(false); | |
// change state | |
button.setSelected(true); | |
button.setPressed(false); | |
mText1.setText("select: " + button.getText()); | |
} | |
@SuppressWarnings("unchecked") | |
private <T> T findViewByIdAndCast(int id) { | |
return (T) findViewById(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment