Android Button State
Here I post how to change the buttons background based on it's state.
At the end of this tutorial i post the full source code. In that you can get button background images.
This is the simple sample code, follow my instructions.
Step 1:
Create new folder as "drawable" in res folder.In that folder create new "select.xml" file and paste the below coding. <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressed_select" android:state_pressed="true" />
<item android:drawable="@drawable/focused_select" android:state_focused="true" />
<item android:drawable="@drawable/normal_select" />
</selector>
Step 2:
Copy the below coding and paste it to main.xml file.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/select" />
</LinearLayout>
Step 3:
This is the defalut java class.You may use or may not use.import com.control.state.R;
import android.app.Activity;
import android.os.Bundle;
public class ButtonStates extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Step 4:
Now run the program now you got output,a) First the button is in normal state(white color).
b) Scroll the mouse , now it is focused state(light green).
c) Click the button, now it is in pressed state(orange color).
normal_state |
focused_state |
pressed_state |
Full Source code for ButtonState
nice tutorial
ReplyDelete