Monday, September 26, 2011

How to Create a Dialog Window and Start a new Activity on the Selection

Java Code:



package com.sai.sample;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;


public class DialogWindow extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialogscreen);


final String items[] = {"Iphone","Android","TMobile"};


AlertDialog.Builder ab=new AlertDialog.Builder(DialogWindow.this);
ab.setTitle("Title");
ab.setSingleChoiceItems(items, 0,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// onClick Action
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

if(items[0].contains("Iphone")){
startActivity(new Intent(DialogWindow.this,Iphone.class)); 
}

}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// on cancel button action
}
});
ab.show();
}
}


XMl Layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView 
    android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dialog Screen" 
android:id="@+id/textView1"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></TextView>
    
</LinearLayout>


Click on the Dialog Button.


Select the Iphone Radio button and click on the Ok Button to start the new Activity.




No comments:

Post a Comment