In this post we are going to see how to create a custom alert dialog in android without any plugin.
First :
Let's design the layout. create a new layout file called custom_alert.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="@drawable/bsheetbg2" android:orientation="vertical" android:layout_marginRight="5dp" android:layout_marginEnd="5dp" android:padding="10dp"> <ImageView android:id="@+id/imageView2" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_marginStart="5dp" android:src="@drawable/personround" /> <TextView android:id="@+id/textView16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_toEndOf="@+id/imageView2" android:layout_toRightOf="@+id/imageView2" android:fontFamily="sans-serif-condensed" android:text="John Doe" android:layout_marginTop="5dp" android:textColor="#00b8d4" android:textSize="25sp" /> <TextView android:id="@+id/textView15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView16" android:layout_marginLeft="10dp" android:layout_toEndOf="@+id/imageView2" android:layout_toRightOf="@+id/imageView2" android:textSize="16dp" android:layout_marginTop="5dp" android:text="Hey.. How're you doing?" android:paddingBottom="5dp" android:textColor="#5f5f5f" /> </RelativeLayout> </RelativeLayout>
Next open activity_main.xml and drag and drop a button from palette window. give that button an ID and then initialize the button in MainActivity.java. after that attach the OnClickListener(Interface definition for a callback to be invoked when a view is clicked) to the button, so that it will show our custom alert dialog when clicked.
alert5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Dialog d = new Dialog(M1_AlertDialog.this); d.requestWindowFeature(Window.FEATURE_NO_TITLE); d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); d.setContentView(R.layout.custom_alert); d.show(); } });
After running the code, you will get this :
You can get the full source code from :
https://github.com/fuadfmb/landroid
0 Comments