Skip to content

Instantly share code, notes, and snippets.

@granoeste
Created August 8, 2012 03:06
Show Gist options
  • Save granoeste/3291697 to your computer and use it in GitHub Desktop.
Save granoeste/3291697 to your computer and use it in GitHub Desktop.
[Android] Button background change for state. (selector)
<?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>
<!-- 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>
<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>
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);
}
}
@sargispapoyan
Copy link

Thanks for this awesome tutorial, it help me lot to clear my concept. but i'm not able to understand the logic which you done in program to find view of Button and TextView.
when i hover over T , it show me [type parameter T extends object ].
ex. when we call findViewByIdAndCast(int d ). it just passing the id of view in integer. and i could not understand how it know which view is it.
happy christmas and new year in advance ,

@raja-arumugam
Copy link

Thanks for this tutorial. it help me lot.

@JonathanMonga
Copy link

tanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment