Custom dialog

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



    

    

    

MainActivity.java

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

4 comments:

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me

Name

Email *

Message *