2

I would like to ask on how to achieve to wrap a content in alertdialog? Because the current output of my dialog has an excess white field.

Hope someone can help me to understand this problem thanks.

enter image description here

Here is my activity_dialog.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:minWidth="10dp"
    android:minHeight="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="com.bloxofcode.toggle.MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="50sp"
            android:background="@color/colorHeaderDialog"
            android:textColor="@android:color/white"
            android:text="@string/select_gender"
            android:padding="15dp"
            android:gravity="center_horizontal|left"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:padding="15dp"
            android:text="Sample"
            android:id="@+id/editText" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ToggleButton
                android:text="ToggleButton"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/toggleButtonMale"
                android:textOn=""
                android:textOff=""
                android:focusable="false"
                android:background="@drawable/check_male"
                android:layout_weight="1" />

            <ToggleButton
                android:text="ToggleButton"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/toggleButtonFemale"
                android:textOn=""
                android:textOff=""
                android:focusable="false"
                android:background="@drawable/check_male"
                android:layout_weight="1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">
            <Button
                android:id="@+id/btnCancel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Cancel"
                android:textSize="20dp"
                android:padding="30dp"
                android:textColor="@android:color/white"
                android:layout_weight="1"/>
            <Button
                android:id="@+id/btnAccept"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Accept"
                android:textSize="20dp"
                android:padding="30dp"
                android:background="@color/colorDialogOK"
                android:textColor="@android:color/white"
                android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

Here is some of my implementation in the MainActivity.java:

AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.activity_dialog, null);
.....
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
iamcoder
  • 529
  • 2
  • 4
  • 23
  • [here is the details answer and what exactly is happening in alert Dialog](http://stackoverflow.com/a/14918253/7368826) – Anjal Saneen Jan 08 '17 at 11:40

2 Answers2

1

Try Creating a dialog like this. this works perfect and then right away your dialog logic is seperated from you activity logic

public class CustomDialog extends Dialog implements
        android.view.View.OnClickListener {

    public Activity c;
    public CustomDialog d;
    public Button yes, no;

    public CustomDialog(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_layout);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //dosomething
            }
        });
    }
}
vanlooverenkoen
  • 2,121
  • 26
  • 50
  • Hi sir thank you for quick respond but i already did that at first. The issue on using match_parent is, it stretch the images and too much padding inside. – iamcoder Jan 08 '17 at 11:50
  • Oke i tried some things out and this works great for me, I edited my answer – vanlooverenkoen Jan 08 '17 at 12:09
  • hi sir, thanks again for the response appreciate it, but how can i implement this? sorry for being newbie, here. – iamcoder Jan 08 '17 at 13:42
  • this is just a new class, you add this, and then in the onCreate, you change setContentView(R.layout.dialog_layout) to your layout, and then in the main activity you just call CustomDialog c = new Customdialog(this); and the call c.show(); – vanlooverenkoen Jan 08 '17 at 13:44
  • hi sir thanks again seems working fine using this code CustomDialog customDialog = new CustomDialog(MainActivity.this); customDialog.show(); calling on a button. – iamcoder Jan 08 '17 at 13:48
  • for listeners that is something you will need to set in the CustomDialog – vanlooverenkoen Jan 08 '17 at 13:49
  • Hi Sir, so all the listeners are all will be implemented on CustomDialog.java rather than in the MainActivity right? example the cancel and accept button. – iamcoder Jan 08 '17 at 13:51
  • yes indeed, like I said you now have splitted the dialog form the mainactivity, so lets say you want to use this dialog in the LoginActivity -> you jsut call CustomDialog c = new CustomDialog(this); c.show(); and you don't have to write something else, this is much better because android is a componentbased language (wich means just reusing components) – vanlooverenkoen Jan 08 '17 at 13:54
  • ow i see sir, thanks very much ,,appreciate your time to me sir, can i add you here in stackoverflow as a person of reference in the future? im currently working on a project and i think in the future i will facing more challenges and will need a helping hand? – iamcoder Jan 08 '17 at 13:57
0

Hope it is not correct to wrap the content of dialog as it might disrupt the view in tablets and larger devices.

Anyhow, basing upon your requirement.. hope this link works AlertDialog with custom view: Resize to wrap the view's content

Community
  • 1
  • 1
ChaitanyaAtkuri
  • 1,672
  • 11
  • 15