Thursday, 6 September 2012

Custom Buttons in Android

         Custom Buttons in Android




CustombuttondemoActivity.java


package a.b;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CustombuttondemoActivity extends Activity implements OnClickListener {
Button b1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(this);
    }
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
}



main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button
android:background="@drawable/custom"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_width="wrap_content"></Button>
<Button android:text="Button"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_width="145dp"></Button>
</LinearLayout>



then create a xml file in the drawable folder

custom.xml


<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item    android:state_pressed="true"   android:drawable="@drawable/b3"></item>
  <item  android:state_focused="true" android:drawable="@drawable/b2"></item>
  <item android:drawable="@drawable/b1"></item>
    
</selector>


following are the images for on selected ,on pressed and default states of button








No comments:

Post a Comment