Saturday, November 17, 2012

android simple alert dialog


package com.example.alertdialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

AlertDialog.Builder builder;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

showDialog();

AlertDialog alert = builder.create();
alert.show();

}

void showDialog() {
//final CharSequence[] items = { "Red", "Green", "Blue" };
final CharSequence[] items = getResources().getStringArray(R.array.countries_array);
builder = new AlertDialog.Builder(this);
builder.setTitle("Set text color");

builder.setItems(items, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int selectItem) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
items[selectItem],
Toast.LENGTH_SHORT).show();
}
});

}

}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countries_array">
        
        <item name="bangladesh">bangladesh</item>
        <item name="usa">usa</item>
        <item name="russia">russia</item>
        <item name="england">england</item>
        
    </string-array>
</resources>

Download:
http://www.mediafire.com/?pp0fyijhpm5ks52

No comments:

Post a Comment