Welcome to ANDROID Developer!

Friday, May 23, 2014

Custom dialog


By on 7:14 PM

Hi Guys!!
Today I am sharing the code about the custom dialog with by using the inflater.
we can make the layout inflate on the view. By help of the Alert Dialog we can create the custom the Alert Dialog .
More about the Alert Dialog you can visit the android developer site Alert Dialog.

So lets start the coding for custom dialog with help of inflater.
 

activity_main.xml

?
1
2
3
4
5
6
7
8
9
<linearlayout android:id="@+id/LinearLayout1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
 
    <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/hello_world">
 
    <analogclock android:layout_height="wrap_content" android:layout_width="wrap_content">
 
    <button android:id="@+id/idButton" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Button">
 
</button></analogclock></textview></linearlayout>

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.sunil.customdialog;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
      
    void openCustomDialog(){
     AlertDialog.Builder customDialog = new AlertDialog.Builder(MainActivity.this);
     customDialog.setTitle("Custom Dialog");
     LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View view=layoutInflater.inflate(R.layout.activity_main,null);
   
  Button btn = (Button)view.findViewById(R.id.idButton);
  btn.setOnClickListener(new Button.OnClickListener(){
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "Button Pressed", Toast.LENGTH_LONG).show();
   }});
    
  customDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){
   @Override
   public void onClick(DialogInterface arg0, int arg1) {
      
   }});
    
  customDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
   @Override
   public void onClick(DialogInterface arg0, int arg1) {
      
   }});
        customDialog.setView(view);
        customDialog.show();
    }
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openCustomDialog();
    }
}

 
 You can download the source code Custom Dialog

About Theavuth NHEL

Faizan is a 17 year old young guy who is blessed with the art of Blogging,He love to Blog day in and day out,He is a Website Designer and a Certified Graphics Designer.

0 comments:

Post a Comment