How to add an image in Alert Dialog Builder on Android
Adding an image in Alert dialog Builder on android studio is very easy. Follow these steps.
Step 1:
Create the interface. Create an .xml file called the CUSTON_LAYOUT.xml add its lines of code:
Create the interface. Create an .xml file called the CUSTON_LAYOUT.xml add its lines of code:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorAccent" android:gravity="center_vertical" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="@string/app_name" android:textColor="#fff" android:textSize="18sp" android:textStyle="bold" /> <RelativeLayout android:id="@+id/body" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFF" android:paddingBottom="16dp" android:layout_below="@+id/title" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> <ImageButton android:id="@+id/image" android:layout_width="56dp" android:layout_height="56dp" android:background="#00FFFFFF" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:scaleType="centerCrop" android:contentDescription="image" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/image" android:layout_marginTop="10dp" android:textColor="#000" android:gravity="center" android:text="Did you like the video?" android:textSize="18sp" /> </RelativeLayout> </RelativeLayout>
Step 2:
In your MainActivity.java, note that you can add the Dialog or you want in your Android application, As far as we are concerned we will put it in MainActiviy, java.
For that we will create a method we will call it POPUP (); .
For that we will create a method we will call it POPUP (); .
Copy and paste this method into your JAVA class:
public void popup(){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); View view; view = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_layout, null); TextView title = (TextView) view.findViewById(R.id.title); ImageButton imageButton = (ImageButton) view.findViewById(R.id.image); title.setText("Hello There!"); imageButton.setImageResource(R.drawable.smile); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "Thank you", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this, "Never Mind!", Toast.LENGTH_SHORT).show(); } }); builder.setView(view); builder.show(); }
Step 3:
Copy the image below by putting it in the DRAWABLE of your Android project.
Copy the image below by putting it in the DRAWABLE of your Android project.
For example, the project in which I work is called PACIFICFM. To put it in DRAWABLE I am this way:
pacificfm\app\src\main\res\drawable
Step 4:
- Now you have your Interface:
CUSTOM_LAYOUT.xml - The method for this to work:
POPUP(); - The image to put in the ALERT DIALOG BUILDER:
4. Now all you have to do is create a CLICK to ACTION. A Listener that listens to a user's click to use the POPUP method. As far as we are concerned we will use the method in our Navigation bar. In the video button.
Here is the code::
else if (id == R.id.video) { popup();}
Here is the code:
You have questions? I am available for you only if you buy my app for $ 9 on PLAYSTORE. You will find a number and my email in the application so I can help you.
Commentaires
Enregistrer un commentaire