Showing posts with label alert box. Show all posts
Showing posts with label alert box. Show all posts

Friday, 16 May 2014

How to create custom dialog box or alert box in android

Here's a simple alert box with customization. We create the alert box through a xml layout, here we can drag any type of widgets, text fields etc.. like spinners, edit text, image in a alert box.


To create simple alert box without creating xml layout check this link.

By creating alert box though xml layout it provides a wide variety of options to be inserted in a alert box easily.





Step 1 : Create an android project. And in layout folder create a new layout for alert box.

Customize your layout file how your alert box should look like.

custom_layout.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
android:layout_width="match_parent"
 android:layout_height="wrap_content">
 
<TextView
android:id="@+id/title"
android:src="@drawable/batman"
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="LOGIN"
android:gravity="center"
android:textSize="35sp"
android:background="#FFFFBB33"/>

<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="username" />
 
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="password"/>
</LinearLayout>

Step 2 : In your main activity file create alert dialog and inflate the view for alert box.

MainActivity.java :

package com.exam.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logout();
}

public void logout(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setView(inflater.inflate(R.layout.custom_layout, null));

/* When positive (yes/ok) is clicked */
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel(); // Your custom code
}
});

/* When negative (No/cancel) button is clicked*/
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); // Your custom code
}
});
alertDialog.show();
}
}

Thursday, 15 May 2014

Creating Alert box or Dialog box in android while onClick button.

This example program is for beginners those who want alert box in their application. I will create a alert box for logout that closes the application. With two options y/n.


To create alert box using xml and customize alert box check this link.

This is how your alert box looks.



I have just place a button in this activity, on-clicking this button pops out a alert box and asks for confirmation for logout.

MainActivity.java : 


package com.exam.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button logout = (Button)findViewById(R.id.button1);
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
logout();
}
});
}

public void logout(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
 
alertDialog.setTitle("Logout"); // Sets title for your alertbox

alertDialog.setMessage("Are you sure you want to Logout ?"); // Message to be displayed on alertbox

alertDialog.setIcon(R.drawable.logout1); // Icon for your alertbox

/* When positive (yes/ok) is clicked */
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
finish();
Toast.makeText(MainActivity.this,"Successfully Logged Out", Toast.LENGTH_LONG).show();  
}
});

/* When negative (No/cancel) button is clicked*/
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
}