custom alert dialog in android, android tutorial

How use Activity as dialog in android

How use Activity as dialog in android

We already learn about simple alert dialog and custom alert dialog but in this tutorial we are going to learn how make a custom dialog using an Activity class. It’s a simple change have to do in our programming in application manifest file. Just open manifest file and change the theme to dialog type of that Activity as show in image below.

Read How add activity to manifest file.

 

how use activity as dialog
activity as dialog

 

 

 

 

 

Here ‘SecondActivity.java’ use as dialog. So we change the theme of ‘SecondActivity.java’ in manifest file.

Now look at the source code.


 

DialogActivity.java


 

package com.androidfromhome.activityasdialog;

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

public class DialogActivity extends Activity implements OnClickListener {

    private Button btn_click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           
        setContentView(R.layout.activity_dialog);
        
        
        btn_click=(Button)findViewById(R.id.btn_click);
        btn_click.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch (v.getId()) {
        case R.id.btn_click:
            startActivity(new Intent(DialogActivity.this,SecondActivity.class));
            break;

        default:
            break;
        }
        
    }

    
}


SecondActivity.java


 

package com.androidfromhome.activityasdialog;

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

public class SecondActivity extends Activity implements OnClickListener {
    
    private Button btn_ok_dialogue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.activity_second);
        
    btn_ok_dialogue=(Button)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_ok_dialogue:
            finish();
            break;

        default:
            break;
        }
    }

}


 

activity_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"
    tools:context="com.androidfromhome.activityasdialog.DialogActivity" >

    <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="Show Activity as Dialog"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


activity_second.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

Custom Alert Dialog in android with custom xml layout

ListView 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