Android Dialog Wrap_content Not Working
Solution 1:
Use have to use this method setContentView(View view, ViewGroup.LayoutParams params)
instead of setContentView(rootView);
ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//ViewGroup.LayoutParams params= new ViewGroup.LayoutParams(// 300,// 300);//for coustom sizeparams.height = thumb_size;
params.width = thumb_size;
// set padding ,margins ,gravity as your desired
Use this setContentView(rootView, params);
Solution 2:
I faced same issue, and i came across a solution. If you give fixed width or wrap_content to your root of your layout, it doesn't consider that. I don't know the reason behind this. Put one more parent layout inside your root and give required width and height to the inner parent, put all views inside this parent. I hope it helps.
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPrimary"android:gravity="center_vertical"android:orientation="horizontal"><LinearLayoutandroid:layout_width="100dp"android:layout_height="wrap_content"android:orientation="horizontal">
//put all your views inside this linear layout
<TextView......./><TextView......./><TextView......./></LinearLayout></LinearLayout>
Solution 3:
Try this before showing the dialog:
dialog.getWindow().setLayout(context.getResources().getDimensionPixelSize(R.dimen.dialog_width), WindowManager.LayoutParams.WRAP_CONTENT)
dialog_width
is 100dp
.
Solution 4:
For some reason, removing the theme that I applied to the dialog fixed the wrap_content issue. The only thing the custom theme did was set the background of the dialog, but removing it has solved the issue.
Solution 5:
Try changing the android:orientation="vertical"
in your main Linear Layout because You are using linear layout with horizontal orientation so all the widgets are aligned horizontally and they are acquiring their assigned space horizontally that's why it cover the whole screen.
Post a Comment for "Android Dialog Wrap_content Not Working"