Using drawings instead of text for AlertDialog buttons

I am trying to set the positive, negative and neutral buttons in AlertDialog on drawables, not text.

I have so far been successful using this:

AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?") .setPositiveButton("Save", new DialogInterface.OnClickListener() {...}) .setNeutralButton("Trash", new DialogInterface.OnClickListener() {...}) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {...}); AlertDialog alert = builder.create(); alert.show(); Button button0 = alert.getButton(AlertDialog.BUTTON_POSITIVE); button0.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_save), null, null, null); button0.setText(""); Button button1 = alert.getButton(AlertDialog.BUTTON_NEUTRAL); button1.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_delete), null, null, null); button1.setText(""); Button button2 = alert.getButton(AlertDialog.BUTTON_NEGATIVE); button2.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(R.drawable.ic_menu_close_clear_cancel), null, null, null); button2.setText(""); 

This is a workaround because I really just erase the text after the fact. My problem is that you cannot create an instance of a button without setting any text.

Setting "[blank_space]" from the very beginning gives the same result when the image is shifted to the left. Setting zero or "" in the same place completely disables AlertDialog without a button. You can see him pushing left in the picture here:

picture

Can I use only photos? It would be much better than trying to process translations for my simple situation.

+4
source share
5 answers

AlertDialog is deprecated. Instead, consider using DialogFragments. You will have much more control and reuse capabilities. Here is a good Google blog that shows how to use and configure it.

+1
source

You only need to create your own dialogue! You can specify ImageButton in the layout, and then create a dialog with this layout, you do not need to approach the positive, negative and neutral buttons. You can find a good tutorial here just using ImageButton instead of buttons in your custom layout.

+1
source

From an Android document,

 setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. 

Since you put your image on the left, the image will be displayed to the left of the button. You can try to put it on top / bottom.

Another way is to create your own layout using the 3 ImageButtons buttons and set it to an alert using

 builder.setView(customLayout); 
+1
source

Try this code:

  LayoutInflater inflater=LayoutInflater.from(YourActivityName.this); View view=inflater.inflate(R.layout.buttton, null); AlertDialog.Builder builder=new AlertDialog.Builder(YourActivityName.this); builder.setView(view); builder.setTitle("Are you sure you want to exit?"); Button posButton=(Button) view.findViewById(R.id.pos); Button neuButton=(Button) view.findViewById(R.id.neu); Button negButton=(Button) view.findViewById(R.id.neg); builder.create(); builder.show(); 

inflate the xtuttton xml file as shown below:

 <?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="horizontal" > <Button android:id="@+id/pos" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/ic_launcher"/> <Button android:id="@+id/neu" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/ic_launcher"/> <Button android:id="@+id/neg" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/ic_launcher"/> </LinearLayout> 

And in the end, you can give clicks events to individuals. Hope this helps you.

+1
source
  CustomDialog dialog = new Dialog(MyActivity.this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.login); final EditText editTextEmailAddress = (EditText) dialog .findViewById(R.id.editTextEmailAddress); final EditText editTextPassword = (EditText) dialog .findViewById(R.id.editTextPassword); TextView txtViewForgetPswd = (TextView) dialog .findViewById(R.id.txtViewForgetPswd); txtViewForgetPswd.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // RETRIVE PASSWORD dialog.dismiss(); } }); ImageButton imgBtnSubmit = (ImageButton) dialog .findViewById(R.id.imgBtnSubmit); imgBtnSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // CALL WEBSERVICE OR ASYNTASK TO LOG IN USER String userName = editTextEmailAddress.getText() .toString(); String password = editTextPassword.getText().toString(); if (userName.equals("") && password.equals("")) { Toast.makeText(BibleActivity.this, "Username or password cannot be empty.", Toast.LENGTH_SHORT).show(); } else { } } }); ImageButton imgBtnCancel = (ImageButton) dialog .findViewById(R.id.imgBtnCancel); imgBtnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // CLOSE DIALOG HERE FROM CROSS BUTTON dialog.dismiss(); } }); ImageButton btnCancelCross = (ImageButton) dialog .findViewById(R.id.btnCancelCross); btnCancelCross.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // CLOSE DIALOG HERE FROM CROSS BUTTON dialog.dismiss(); } }); dialog.show(); 

//// XML LAYOUT "login" ///

 <RelativeLayout android:layout_width="fill_parent" android:layout_height="480dp" android:background="@drawable/mbc_login" > <FrameLayout android:layout_width="fill_parent" android:layout_height="45dp" android:layout_alignParentTop="true" > <ImageButton android:id="@+id/btnCancelCross" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:layout_marginRight="20dp" android:background="@drawable/cross" > </ImageButton> </FrameLayout> <TextView android:id="@+id/txtViewEmailLbl" android:layout_width="200dp" android:layout_height="20dp" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Email address" android:textColor="#000000" > </TextView> <EditText android:id="@+id/editTextEmailAddress" android:layout_width="200dp" android:layout_height="50dp" android:layout_below="@+id/txtViewEmailLbl" android:layout_centerHorizontal="true" android:hint=" example@eg.com " android:imeOptions="actionNext" > </EditText> <TextView android:id="@+id/txtViewPswdLbl" android:layout_width="200dp" android:layout_height="20dp" android:layout_below="@+id/editTextEmailAddress" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="Password" android:textColor="#000000" > </TextView> <EditText android:id="@+id/editTextPassword" android:layout_width="200dp" android:layout_height="50dp" android:layout_below="@+id/txtViewPswdLbl" android:layout_centerHorizontal="true" android:hint="password123" android:imeOptions="actionNext" android:inputType="textPassword" > </EditText> <TableRow android:id="@+id/tblRowSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editTextPassword" android:layout_centerHorizontal="true" android:layout_margin="10dp" > <ImageButton android:id="@+id/imgBtnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:background="@drawable/btn_submit" > </ImageButton> <ImageButton android:id="@+id/imgBtnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_cancel" > </ImageButton> </TableRow> <TextView android:id="@+id/txtViewForgetPswd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tblRowSubmit" android:layout_centerHorizontal="true" android:layout_margin="5dp" android:text=" Forget Password ? " android:textColor="#306EFF" android:textStyle="italic" > </TextView> </RelativeLayout> 

0
source

All Articles