Monday 21 September 2015

Custom Dialog

In Android you can use the Dialog component, and you can customize it to your own User Interface so it can have any use you want. In this tutorial we are going to create a Dialog that will display a custom image and a text message. As we know there is no such thing by default. That means we have to make it ourselves. To do this, we will

we show you how to create a custom dialog in Android. See following steps :

  1. Create a custom dialog layout (XML file).
  2. Attach the layout to Dialog.
  3. Display the Dialog.
  4. Done.
activity_main.xml
--------------------------
<LinearLayout
   
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"
   
android:orientation="vertical"
   
tools:context=".MainActivity">

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Custom Dialog"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />
</
LinearLayout>

mydialog.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">



    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editText"

        android:hint="enter name"

        android:layout_marginTop="15dp" />

    <TableRow

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="15dp">

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Ok"

            android:layout_weight="1"

            android:id="@+id/button2" />

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="Cancel"

            android:id="@+id/button3" />

    </TableRow>

</LinearLayout>

MainActivity.Java
-------------------------
package com.android.anil.cdialog;



import android.app.Dialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

Button cdialogbtn,okbtn,canbtn;

    EditText input;

    Dialog d1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        cdialogbtn=(Button)findViewById(R.id.button);

        cdialogbtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                d1=new Dialog(MainActivity.this);

                d1.setTitle("Custom Dialog");

                d1.setContentView(R.layout.mydialog);

                d1.show();

                okbtn=(Button)d1.findViewById(R.id.button2);

                canbtn=(Button)d1.findViewById(R.id.button3);

                input=(EditText)d1.findViewById(R.id.editText);

                okbtn.setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View v) {

                        String s1=input.getText().toString();

                        Toast.makeText(getApplicationContext(),"Name is : "+s1,Toast.LENGTH_LONG).show();

                    }

                });

                canbtn.setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View v) {

                        d1.cancel();

                    }

                });



            }

        });

    }



}

Output is:
----------------





No comments:

Post a Comment

Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...