Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom dialog in Android

Tags:

android

dialog

I am trying to create a custom dialog in Android. But whatever I tried to do, I am not able to change the width of the dialog. It just remains the same. The following is the view that I am setting as the content for the dialog.

<RelativeLayout  
android:id="@+id/current_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:visibility="visible">
<ImageView android:id="@+id/player_image" 
    android:src="@drawable/person"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>
<TextView   
    android:id="@+id/player_name"
    android:layout_below="@id/player_image"
    android:layout_centerHorizontal="true"
    android:text="Raja Ram"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<ImageButton android:id="@+id/dialog_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:background="#00000000"
    android:src="@drawable/close_button_selector"/>
<ImageButton android:id="@+id/dialog_flip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:background="#00000000"
    android:src="@drawable/rotate"/>
</RelativeLayout>

As you can see, everywhere I am using wrap_content in this code sample. Also I tried the following options

1) Setting a custom style while creating the Dialog like this.

dialog = new Dialog(this,R.style.Theme_Dialog);

And the style as follows

<resources>
<style name="Theme" parent="android:Theme">
</style>
<style name="Theme.Dialog">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
</resources>

2) Setting the parameters for the view in the onCreateDialog() like this

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.player_info, null, false);
ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.setContentView(v,p);

3) I also tried to set the Window parameters like this in the onCreateDialog() method

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width=WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(params);

But again no luck. Can someone help me out with this issue. Am I doing something wrong?? Also can you please suggest me how to set the x and y postions for the Dialog window?

Thanks

like image 615
rajaramyadhav Avatar asked Aug 03 '10 18:08

rajaramyadhav


People also ask

How do I create a custom dialog box?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How many types of dialog are there in android?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)

How do I show custom dialog in flutter?

Flutter custom Alert Dialog If you want to implement more advanced custom Dialog, you can use Dialog widget for that. Instead of the AlertDialog , in here we return Dialog widget. The showDialog method will remain the same. You can use a Container widget to set relevant height for the Dialog.


2 Answers

        dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);

        dialog.setContentView(R.layout.custom_dialog);

         LayoutParams lp=dialog.getWindow().getAttributes();    
         lp.x=100;lp.y=100;lp.width=100;lp.height=200;lp.gravity=Gravity.TOP | Gravity.LEFT;
         lp.dimAmount=0;            
         lp.flags=LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_TOUCH_MODAL;
    //   dialog.getWindow().setAttributes(lp);

         dialog.show();

This worked well for me....

like image 156
Jithin Avatar answered Sep 28 '22 12:09

Jithin


Is your problem that it takes the full width of the screen, or that you can't control how much of the screen is it taking up?

To make your activity act as a dialog, and not take up the full with you should set your activity to have the dialog theme in the manifest:

<activity android:theme="@android:style/Theme.Dialog">

The dialog will be as large as is required by your layout. If you want it to be wider, you need to make your layout wider. (Adjust size of images, add padding, etc).

I'm not aware of a way to position the dialog window.. I think it is always centered, I could be wrong though.

like image 30
Cheryl Simon Avatar answered Sep 28 '22 13:09

Cheryl Simon