Sunday, September 15, 2013

Selector on Button Example


Android – Selector on Button Example

Source button.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />

<item android:drawable="@drawable/button_normal"
android:state_focused="true" />

<item android:drawable="@drawable/button_normal" />

</selector>


Source main.xml

<?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" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="40dp"
android:background="@drawable/button"
android:textColor="#ffffff"
android:text="Button" />

</LinearLayout>

The output will be

 


POSTED BY JACK 28- APR- 2011 3 COMMENTS

SIMPLE BUTTON CLICK

SOURCE CODE [main.xml] is

<?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:id="@+id/button1"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Button 1" />

                <Button android:id="@+id/button2"
android:layout_width="100px"
                                android:layout_height="wrap_content"
android:text="Button 2" />

</LinearLayout>

SOURCE CODE [ButtonExample.java] is

package com.ButtonExample;

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

public class ButtonExample extends Activity
{
Button b1,b2;

public void onCreate(Bundle savedInstanceState)
{
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);

b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
                                                                                "You have clicked Button 1", Toast.LENGTH_LONG);
                                                                msg.show();
}
                                });

b2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
                                                                                "You have clicked Button 2", Toast.LENGTH_LONG);
 msg.show();
}
                                });
 }
}

The OUTPUT will be


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFsrVp6qx0dI4ihLCzZnWvtuz-3Uh4nSPoBSkOXHgPXTIccNuUa6ZL2bw1Kah_4AeM7kPqPGAEC2Nekr5_uLNDnHY4sLeq1LB5kNQ6VUIK6z3-LtCHs0vIvDUsfLwoA9m6BqA75ajWXgc/


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR4pe1VoDSYgF7M8Cst-fZjqXwfJab8V0jT267S2-JCQetgAP7l9B2KEo0NrDcp943CSiX5QCpLc80vcb5agQRnCr2zvufDQx2mvZH2DKoO_pvz4EwJok3ziWOEjsPgXAPUBQlZvUmHQY/

No comments:

Post a Comment