7

How to put close button at top corner in alert dialog box for android?

put close button at right side top corner in alert dialog.

i have used below code for scroll and dialog box

<ScrollView 
        android:id="@+id/ScrollView01" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:scrollbars="vertical" 
   android:scrollbarAlwaysDrawVerticalTrack="true" >

<ImageVieandroid:id="@+id/image"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentRight="true"
 android:layout_alignParentTop="true"
 android:layout_marginLeft="200dp"
 android:src="@drawable/ic_launcher" />

</RelativeLayout>

</ScrollView>

in java coding i have used below coding i want to put a image to close the dialog box please help

public void onClick(View v) 
{
// TODO Auto-generated method stub
   switch (v.getId())
   {
    case R.id.How_to_use:

 final Dialog dialog = new Dialog(this);
 dialog.setContentView(R.layout.description);
 dialog.setTitle("How to use");
 TextView text = (TextView) dialog.findViewById(R.id.description);
 text.setText(R.string.Descr_How_to_use);
 ImageView image = (ImageView) dialog.findViewById(R.id.image);
 image.setImageResource(R.drawable.ic_launcher);
 image.setOnClickListener(new OnClickListener() 
 {
  // @Override
  public void onClick(View v) {
  dialog.dismiss();
  }
  });
 dialog.show();
 break;
 default:
 break;
 }
TheFlash
  • 5,997
  • 4
  • 41
  • 46
Hardik Mer
  • 824
  • 4
  • 14
  • 26
  • 1
    May this help : http://stackoverflow.com/questions/8982678/how-to-have-activity-in-android-with-a-close-button-at-the-top-corner – Alexis C. Jun 18 '13 at 12:02
  • 1
    use custom dialog box and set button on corner , inflate in in dialog box and use it – abhi Jun 18 '13 at 12:04
  • i have created a custom dialog box, but i am not aware how to set the button on top corner of that dialog box, can u help me for that ? – Hardik Mer Jun 18 '13 at 12:12
  • Hi use following link reference http://stackoverflow.com/questions/5246138/how-to-get-that-cross-button-image-on-custom-dialog-boxs-boundary/22655042#22655042 – Amir Qayyum Khan Mar 26 '14 at 09:00
  • Does this answer your question? [How to have activity in Android with a close button at the top corner?](https://stackoverflow.com/questions/8982678/how-to-have-activity-in-android-with-a-close-button-at-the-top-corner) – Vega Sep 25 '20 at 11:35

3 Answers3

15

I know, I am late to answer this 2 yrs old question, but this one is for those who don't know a correct approach yet....

Use a Custom Dialog as (everyone has suggested).

Use a RelativeLayout as the main layout of custom_dialog.xml as you will have to float that cancel button on top corner (right/left) of the main window.

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="15dp"
        android:background="#F4F4F4">

        <!--Main Body of your custom dialog-->
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/llTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btnBookK"
            android:background="@null"
            android:src="@drawable/ic_close" />

    </LinearLayout>

</RelativeLayout>

In your custom dialog code use following line to make it transparent:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
aB9
  • 592
  • 3
  • 8
  • 25
2

customDialog.xml

<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
>

<LinearLayout
android:id="@+id/llTop"    
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:paddingBottom="10dp"

>


<Button
    android:id="@+id/btnClose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000" 
    android:text="Close"

    />

</LinearLayout>

 <ImageView
    android:src="@drawable/ic_launcher" 
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:text="Close"
    android:layout_centerInParent="true"
    android:layout_below="@+id/llTop"
    />



</RelativeLayout>

I have created 1 method whenever you want to display dialog just call this method.

private Dialog dialog; // class variable

private void showDialog
{
dialog = new Dialog(Activity.this);  // always give context of activity.
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.customDialog);


Button dialogButton = (Button) dialog.findViewById(R.id.btnClose);

        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) 
            {
                dialog.dismiss();
            }
        });

dialog.show();

}

TheFlash
  • 5,997
  • 4
  • 41
  • 46
  • You can refer this link for more clear explanation. http://stackoverflow.com/questions/20258730/custom-dialog-with-close-button – trueblue Dec 26 '14 at 11:27
1

Yo have to create custom dialog

import android.app.Dialog;
import android.content.Context;

public class CustomDialog extends Dialog 
{

public CustomDialog(Context context) 
{
    super(context);
    setContentView(R.layout.dialog);
}

}

create dialog.xml file. design according to your requirment.

TheFlash
  • 5,997
  • 4
  • 41
  • 46