In this tutorial we will to show how to create customize alert dialog in android. I try to use buil-in alert dialog of android but it look not good that I want, Now I find the new solution. let's start!
Open your tool! I use Eclipse https://www.eclipse.org/
-Create new android Project I called CustomizeAlertDialog
And next I select Blank Activity
And then I create new xml color.xml for color of customize alert dialog follow this code
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="action_primary">#1b68e5</color> <color name="action_alert_warning">#e5861b</color> </resources>
Next I create new customize xml follow this code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/action_alert_warning"
android:gravity="center"
android:padding="10dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_alert" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Hello Everyone in this tutorial I will show you how to create customize alert dialog in android"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/action_primary" />
</LinearLayout>
</LinearLayout>
and next in MainActivity I create one Button and set OnClickListener follow this code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/btnShowAlert"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.customizealertdialog.MainActivity" >
<Button
android:id="@+id/btnAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="Show Alert Dialog" />
</RelativeLayout>
In MainActivity.javapackage com.example.customizealertdialog;
import android.annotation.SuppressLint;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button btnAlert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAlert = (Button) findViewById(R.id.btnAlert);
btnAlert.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
//LayoutInflater inflater = getLayoutInflater();
//View view = inflater.inflate(R.layout.customize_alertdialog, null);
Builder builder = new Builder(MainActivity.this);
builder.setView(R.layout.customize_alertdialog);
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
builder.create().show();
}
}
Let's run it
1 Comments
Good article
ReplyDelete