Search Wikipedia

Search results

Dec 10, 2013

Custom Dialog in Android

Creating a Custom Dialog in Android

I create a new android application project called "Custon_dialog"
Then I need to create a custom layout for my custom dialog and for this I create a new xml file called "dialog.xml"
In my custom dialog i want to show a picture and  some texts.
So, i drag a picture in my drawable folder.

dialog.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="110dp"
        android:layout_height="160dp"
        android:contentDescription="@string/todo"
        android:padding="20dp"
        android:src="@drawable/android" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="170dp"
        android:layout_height="211dp"
        android:text="@string/i_love_android" 
        android:textSize="25sp"/>

</LinearLayout>


Now I write these codes in activity_main.xml.

activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="78dp"
        android:layout_marginTop="28dp"
        android:text="@string/show" />
   
</RelativeLayout>


MainActivity.java


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final Dialog custom = new Dialog(MainActivity.this);
custom.setContentView(R.layout.dialog);
custom.setTitle("check it out");
custom.show();
 
}
});
}

I run my project and it looks like this:


No comments:

Post a Comment

Did this post help you? Do you have any questions? Drop your thoughts here...

}