Create a custom dialog in Android

I am trying to create a user dialog in Android. But no matter what I tried to do, I cannot change the width of the dialog. He remains unchanged. Below is the view that I set 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 use wrap_content in this code example. I also tried the following options

1) Setting up a custom style when creating a 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 parameters for the view in 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 setting window options 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 with this problem. Am I doing something wrong ?? Can you also suggest me how to set the x and y posts for the dialog box?

thank

+14
android dialog
Aug 03 '10 at 18:38
source share
5 answers

I understood the problem after a long time. The problem was how I used the Relative layout. I guess I did not set the relative position properly. When I changed this to a linear layout, it worked fine. He did not use the entire screen, but only what was required.

+1
Aug 05 '10 at 23:41
source share
  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 ....

+31
Nov 29 '10 at 12:59
source share

Is your problem that it occupies the full width of the screen or that you cannot control how much of it occupies the screen?

In order for your activity to act as a dialogue, and not engage in full, you must configure your activity to have a dialogue topic in the manifest:

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

The dialog box will be as large as required in your layout. If you want it to be wider, you need to make your layout wider. (Adjust image size, add padding, etc.).

I do not know how to place the dialog box. I think that he is always focused, I could be wrong.

+2
Aug 03 '10 at 18:43
source share

I could change the width, height and position of the dialog using getWindow (). setAttributes (params)

0
Nov 28 '10 at 12:58
source share

in the onCreate() method of your custom dialog class, do the following

 @Override protected void onCreate(Bundle savedInstanceState) { getWindow().setLayout(x, y); } 
-3
Feb 23 '12 at 17:22
source share



All Articles