custom alert dialog in android, android tutorial

Custom Dialog in android example using custom layout

Custom Dialog in android example using custom layout

In previous tutorial we learn about android simple dialog in android. But in your live application you have to work according to your client preferred design layout, so you need to customize the alert dialog. Here we use a custom layout with ‘Dialog’ class to make it design different looking. Create a project in android and use the shortcode below otherwise download full source code of this android application.

ShortCode:-

public void getCustomDialogue(){
dialogue_custom = new Dialog(YourActivity.this);
dialogue_custom.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogue_custom.setContentView(R.layout.dialog_layout);
btn_ok_dialogue=(Button)dialogue_custom.findViewById(R.id.btn_ok_dialogue);
btn_ok_dialogue.setOnClickListener(this);
}

Under a button click we will call this function.

Let's see the source code.


CustomDialogActivity.java


 

package com.androidfromhome.customdialog;

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

public class CustomDialogActivity extends Activity implements OnClickListener {

private Dialog dialogue_custom;
private Button btn_ok_dialogue;
private Button btn_click;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_dialog);

getCustomDialogue();
btn_click=(Button)findViewById(R.id.btn_click);
btn_click.setOnClickListener(this);

}

public void getCustomDialogue(){

dialogue_custom = new Dialog(CustomDialogActivity.this);
dialogue_custom.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogue_custom.setContentView(R.layout.dialog_layout);
btn_ok_dialogue=(Button)dialogue_custom.findViewById(R.id.btn_ok_dialogue);
btn_ok_dialogue.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_click:

dialogue_custom.show();
break;

case R.id.btn_ok_dialogue:

dialogue_custom.dismiss();
break;

default:
break;
}

}
}

 


activity_custom_dialog.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#A758AB"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="Android From Home"
android:textColor="#ffffff"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >

<Button
android:id="@+id/btn_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#7BC168"
android:padding="10dp"
android:text="Custom Dialog"
android:textColor="#ffffff"
android:textStyle="bold" />

</LinearLayout>

</LinearLayout>

 


 

dialog_layout.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:layout_gravity="center"
android:background="#343434"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Android From Home"
android:textColor="#F79436"
android:textSize="18dp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_message_dialogue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2dp"
android:gravity="center"
android:padding="2dp"
android:text="Learn android from here. it&apos;s totally free site. download free source code.Follow us &apos;www.androidfromhome.com&apos;"
android:textColor="#ffffff"
android:textSize="16dp" />

<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:gravity="center"
android:padding="4dp" >

<Button
android:id="@+id/btn_ok_dialogue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#7BC168"
android:padding="10dp"
android:text=" OK "
android:textColor="#ffffff"
android:textSize="16dp"
android:textStyle="bold" />

</LinearLayout>

</LinearLayout>


 

Download Full Source Code

 

Related Post:-

Simple Alert Dialog in android

Image Download from server to android device example

Custom spinner or drop-down in android example

Custom Toast example in android example

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget