Now lets start with the Button Alert Box.
To start create a button on whose click the alert box would appear.
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Click Me...!!!" />
Now to make the alert box appear on click of the button and illustrate working of Button Alert Box.
public class MainActivity extends Activity
{
Button b;
AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
final CharSequence[] items={"Red","Green","Blue"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Pick a Colour.");
builder.setItems(items, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//TODO Auto-generated method stub
Toast.makeText(MainActivity.this,""+items[which],Toast.LENGTH_SHORT).show();
}
});
alert=builder.create();
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//TODO Auto-generated method stub
alert.show();
}
});
}
}
Now the output should look like this






No comments:
Post a Comment