Question

I have created a fixed size dialog in my application. While i tested with a phone of 240x320 pixel size, it shows perfect. But it shows not proper in a tablet. see the images below

enter image description here

image from phone

enter image description here image from tab

i tried with below code

public void showDialog(){
    // Create custom dialog object
    final Dialog dialog = new Dialog(MainActivity.this);
    // Include dialog.xml file
    dialog.setContentView(R.layout.dialog);
    // Set dialog title
    dialog.setTitle("Custom Dialog");

    // set values for custom dialog components - text, image and button
    TextView text = (TextView) dialog.findViewById(R.id.textDialog);
    text.setText("Custom dialog Android example.");
    ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
    image.setImageResource(R.drawable.ic_launcher);

    dialog.show();
    dialog.getWindow().setLayout(180, 150);

    Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
    // if decline button is clicked, close the custom dialog
    declineButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Close dialog
            dialog.dismiss();
        }
    });

}

dialog.xml

<?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" >

<ImageView
    android:id="@+id/imageDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dp" />

<TextView
    android:id="@+id/textDialog"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFF"
    android:layout_toRightOf="@+id/imageDialog"/>

 <Button
    android:id="@+id/declineButton"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text=" Submit "
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/textDialog"
    android:layout_toRightOf="@+id/imageDialog"
    />

</RelativeLayout>

how to solve this ?

thank in advance

Was it helpful?

Solution

Finally i found the solution

I removed the lines dialog.setTitle("Custom Dialog"); and dialog.getWindow().setLayout(180, 150); lines and added dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

Corrected code as follow

public void showDialog(){
    // Create custom dialog object
    final Dialog dialog = new Dialog(MainActivity.this);
    // Include dialog.xml file
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.dialog);

    // set values for custom dialog components - text, image and button
    TextView text = (TextView) dialog.findViewById(R.id.textDialog);
    text.setText("Custom dialog Android example.");
    ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
    image.setImageResource(R.drawable.ic_launcher);

    dialog.show();

    Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
    // if decline button is clicked, close the custom dialog
    declineButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Close dialog
            dialog.dismiss();
        }
    });

}

I made another change in layout file. Replaced to and lines android:layout_width="fill_parent" android:layout_height="fill_parent" to android:layout_width="180dp" android:layout_height="150dp"

Corrected layout as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="180dp"
android:layout_height="150dp" >

<ImageView
    android:id="@+id/imageDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dp" />

<TextView
    android:id="@+id/textDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFF" />

 <Button
    android:id="@+id/declineButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Submit "
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    />

</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top