Pages

Saturday, September 8, 2012

Android Custom Dialog Box Example



create your 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:text="Button"
android:id="@+id/button1"
android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 </Button>
</LinearLayout>


custom.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
   android:orientation="vertical">
    <ImageView android:layout_height="wrap_content"
    android:id="@+id/image"
     android:src="@drawable/bunch"
      android:layout_width="wrap_content">
      </ImageView>
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/image"
    android:id="@+id/text"
    >
    </TextView>
    <Button android:text="Button"
    android:id="@+id/button"
    android:layout_below="@id/image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    </Button>
</RelativeLayout>


create your Custom Dialog Activity


package example.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DialogExample extends Activity {
    
Context con=this;
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(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(con);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.day);
Button b=(Button)dialog.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
});
    
    }
}

No comments:

Post a Comment