Monday, March 12, 2012

Custom Dialog In Activity

TestActivity.java
package com.chirag;

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

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnShowDialog = (Button) findViewById(R.id.button1);
        btnShowDialog.setOnClickListener(new OnClickListener() {
                 
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                Dialog dialog = new Dialog(TestActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.dialog_layout);
                dialog.setCancelable(true);
                //there are a lot of settings, for dialog, check them all out!

               
                //now that the dialog is set up, it's time to show it   
                dialog.show();
                  }
            });
    }
}



main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/mainLayout" android:layout_width="fill_parent"
      android:layout_height="fill_parent" android:orientation="vertical">
      <TextView android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/textView1" android:layout_width="wrap_content"
            android:text="Custom Dialog Example" android:layout_height="wrap_content"></TextView>
      <Button android:text="Click Me to see Dialog" android:id="@+id/button1"
            android:layout_marginTop="20dp" android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>




dialog_layout.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">
      <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
            <ImageView android:layout_height="100dp" android:id="@+id/imageView1"
                  android:src="@drawable/man" android:scaleType="fitXY"
                  android:layout_width="100dp"></ImageView>
            <TextView android:textAppearance="?android:attr/textAppearanceSmall"
                  android:id="@+id/textView1" android:layout_width="wrap_content"
                  android:text="Brandon" android:layout_height="wrap_content"></TextView>
            <ImageView android:layout_height="wrap_content" android:id="@+id/imageView2"
                  android:layout_marginLeft="70dp" android:src="@android:drawable/btn_dialog"
                  android:layout_width="wrap_content"></ImageView>
      </LinearLayout>
      <View android:id="@+id/view1" android:layout_width="fill_parent"
            android:background="@android:color/white" android:layout_height="2dp"></View>
      <TextView android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_marginTop="10dp" android:id="@+id/textView2"
            android:layout_width="wrap_content" android:layout_gravity="center_horizontal"
            android:text="          How About dinner tonoght ?               "
            android:layout_height="wrap_content"></TextView>
      <View android:id="@+id/view1" android:layout_width="fill_parent"
            android:layout_marginTop="40dp" android:background="@android:color/white"
            android:layout_height="2dp"></View>
      <LinearLayout android:orientation="horizontal"
            android:gravity="center_horizontal" android:layout_marginTop="5dp"
            android:layout_width="fill_parent" android:layout_height="wrap_content">
            <ImageButton android:id="@+id/imageButton1"
                  android:src="@android:drawable/ic_btn_speak_now"
                  android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageButton>
            <ImageButton android:id="@+id/imageButton2"
                  android:layout_marginLeft="10dp" android:src="@android:drawable/ic_lock_silent_mode_off"
                  android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageButton>
      </LinearLayout>
      <LinearLayout android:orientation="horizontal"
            android:layout_marginTop="5dp" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <EditText android:id="@+id/editText1" android:hint="Type to Reply"
                  android:layout_width="220dp" android:layout_height="wrap_content">
            </EditText>
            <Button android:text="Reply" android:id="@+id/button1"
                  android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
      </LinearLayout>
</LinearLayout>



Screen Shots


Download Source Code

1 comment:

P@nu said...

This is really helpful...my problem solved... thanx :)