Hello,In this android tutorial i want to explore a simple program about android Custom Alert Dialog box to give a professional look for your app.This custom alert dialog box displays alerts to the users for get confirmation from the users.Let's go for coding with easy steps:
In this android example of Custom alert dialog box we show:
Title: The title like "Are you sure to exit". (Optional)
Content: Displays the message to the user. It can be a string message or a list or custom layout.
Action Button(s): Define action on button like "Exit" , "Yes / No" , "Later" , "Cancel" etc.
We need for custom alert box:
1) activity_main.xml
2) custom_alert.xml
3) MainActivity.java
CREATE NEW ANDROID PROJECT:
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Custom Alert me" />
</LinearLayout>
Step 2: Write code into custom_alert.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/displayimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/txview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="All Right" />
<Button
android:id="@+id/buttonexit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Exit" />
</LinearLayout>
</LinearLayout>
Step 3: Write code into MainActivity.java
package dev.androidapplink.alertboxapp;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
final Context context = this;
// create variable
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button = (Button) findViewById(R.id.buttonAlert);
// create button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog and set view
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_alert);
dialog.setTitle("androidapplink");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.txview);
text.setText("All is right in example?");
ImageView image = (ImageView) dialog
.findViewById(R.id.displayimage);
image.setImageResource(R.drawable.ic_launcher);
// Load Control
Button dialogButtonok = (Button) dialog
.findViewById(R.id.buttonok);
Button dialogButtonexit = (Button) dialog
.findViewById(R.id.buttonexit);
// if button is clicked, close the custom dialog
dialogButtonok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
// if button is clicked, close the current activity
dialogButtonexit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
// Toast...
Toast.makeText(getApplicationContext(),
"Activity is closed", Toast.LENGTH_SHORT)
.show();
// Toast...
Toast.makeText(getApplicationContext(),
"It's Too Right :)", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
}
}
NOTE:No need any Permission in AndroidManifest.xml
Step 4: Run Your Project:
In this android example of Custom alert dialog box we show:
Title: The title like "Are you sure to exit". (Optional)
Content: Displays the message to the user. It can be a string message or a list or custom layout.
Action Button(s): Define action on button like "Exit" , "Yes / No" , "Later" , "Cancel" etc.
We need for custom alert box:
1) activity_main.xml
2) custom_alert.xml
3) MainActivity.java
CREATE NEW ANDROID PROJECT:
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Custom Alert me" />
</LinearLayout>
Step 2: Write code into custom_alert.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/displayimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/txview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="All Right" />
<Button
android:id="@+id/buttonexit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Exit" />
</LinearLayout>
</LinearLayout>
Step 3: Write code into MainActivity.java
package dev.androidapplink.alertboxapp;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
final Context context = this;
// create variable
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load control
button = (Button) findViewById(R.id.buttonAlert);
// create button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog and set view
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_alert);
dialog.setTitle("androidapplink");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.txview);
text.setText("All is right in example?");
ImageView image = (ImageView) dialog
.findViewById(R.id.displayimage);
image.setImageResource(R.drawable.ic_launcher);
// Load Control
Button dialogButtonok = (Button) dialog
.findViewById(R.id.buttonok);
Button dialogButtonexit = (Button) dialog
.findViewById(R.id.buttonexit);
// if button is clicked, close the custom dialog
dialogButtonok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
// if button is clicked, close the current activity
dialogButtonexit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.this.finish();
// Toast...
Toast.makeText(getApplicationContext(),
"Activity is closed", Toast.LENGTH_SHORT)
.show();
// Toast...
Toast.makeText(getApplicationContext(),
"It's Too Right :)", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
}
}
NOTE:No need any Permission in AndroidManifest.xml
Step 4: Run Your Project:
No comments :
Post a Comment