Wednesday, December 26, 2012

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

Rocky

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Tipss.