Let's say I have a view structure like
<ConstraintLayout>
<CustomViewLayout>
...
...
</CustomViewLayout>
</ConstraintLayout>
This is simplified but I use the above in a bottom sheet and I sometimes change the height of the ConstraintLayout and the peek height of the bottom sheet depending on my CustomViewLayout height. My problem is that if part of the CustomViewLayout is cut off, that is - it is somewhat outside the screen because the ConstraintLayout isn't high enough - I'm no longer able to get a correct "full height" of it. I always only seem to get the visible part on screen of the view in that case.
So how can I get the full height of a view that is partly off screen?
Thanks!
Edit:
I should add that what I've tried is a globalLayoutListener, as well as a customViewLayout.post {} (and the usual customViewLayout.height of course). But none of these measure the full height when part of the view is outside the screen.
To check my previous answer, I developed a test project to examine the exact situation. It was successful to measure layout height when some part of it is outside of screen (layout height measured 4231px where the screen height was 1920px). If the view is long enough that is not completely shown by the screen, you should put it in a scrollable view. So I put the customViewLayout in a NestedScrollView to consume residual scroll amount after the bottom sheet has expanded. Here is the code and result:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="#+id/layoutBottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/customViewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/colorAccent">
<TextView
android:id="#+id/stateTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/sizeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:padding="24dp"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:padding="24dp"
android:text="#string/lorem"
android:gravity="center_horizontal"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
customViewLayout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
customViewLayout.viewTreeObserver.removeGlobalOnLayoutListener(this)
val height = customViewLayout.measuredHeight
val width = customViewLayout.measuredWidth
sizeTextView.text = "Height: $height px Width: $width px"
}
})
val sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet)
sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, offset: Float) {
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
BottomSheetBehavior.STATE_HIDDEN -> stateTextView.text = "State: Hidden"
BottomSheetBehavior.STATE_EXPANDED -> stateTextView.text = "State: Expanded"
BottomSheetBehavior.STATE_COLLAPSED -> stateTextView.text = "State: Collapsed"
BottomSheetBehavior.STATE_DRAGGING -> stateTextView.text = "State: Dragging"
BottomSheetBehavior.STATE_SETTLING -> stateTextView.text = "State: Settling"
}
}
})
}
}
Screen:
If you are looking for the view width and height, try this:
mCustomViewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
mCustomViewLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = mCustomViewLayout.getMeasuredHeight()
int width = mCustomViewLayout.getMeasuredWidth()
}
});
Related
I'm trying to implement the shimmer effect with the Height of wrap_content but the images are not loading, I know why it is not loading the images because the imageView has wrap_content and the shimmer also has wrap_content but I want the Height Should be wrap_content and not fixed.
After implementing a fixed height of eg 200dp in shimmer it works but after that images are not loading
I want to make it like Pinterest where the height is adjusted according to the image
XML Files
post_item_container_search.xml
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/imagePostSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="6dp"
android:contentDescription="#string/todo"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
post_item_containe_shimmer.xml
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/imageShimmer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:layout_marginTop="11dp"
android:background="#E7E7E7"
android:contentDescription="#string/todo"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
this how it looks like after adding minHeight to both or in actual imageView
By default, the wrapcontent doesn't have any size so it is 0dp you need to define 50dp or something for the height of shimmer then only you can see the shimmering.
Can refer this blog
or try to use this
post_item_container_search.xml
<com.google.android.material.imageview.ShapeableImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imagePostSearch"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitCenter"
android:layout_marginStart="6dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="6dp"
android:contentDescription="#string/todo"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
After implementing a fixed height of eg 200dp in shimmer it works but after that images are not loading
In that case, you should probably set an absolute width/height for the ImageView first. Later, you can set it back to WARP_CONTENT if you really need to. But first you'll need an estimated/absolute width/height for the view.
int height = bitmap.getHeight();
int width = bitmap.getWidth();
ShapeableImageView siv = findViewById(R.id.imagePostSearch);
ViewGroup.LayoutParams params = siv.getLayoutParams();
params.height = height;
params.width = width;
//To set it back to warp content or match parent:
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
Please keep android:minHeight for the ImageView as it will give you a minimum height and also android:height can be wrap_content so it grows if the image is larger than minHeight.
For example,
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/imageShimmer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:layout_marginStart="11dp"
android:layout_marginTop="11dp"
android:background="#E7E7E7"
android:contentDescription="#string/todo"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
You can create a dummy view for shimmer with fixed height, make its visibility to visible and show it until the image loads, and once the image is loaded make the dummy view visibility gone.
//XML
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<shimmerView //You can use your own
android:id="#+id/dummyShimmerView"
android:layout_width="100dp"
android:layout_height="100dp"/>
<ImageView
android:id="#+id/postImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</RelativeLayout>
//dummyShimmerView visibility is visible byDefault
Glide.with(context)
.load(url)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
//TODO: something on exception
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
Log.d(TAG, "OnResourceReady")
dummyShimmerView.visibility = View.GONE
postImageView.visibility = View.VISIBLE
return false
}
})
.into(imgView)
The app I am building requires a simple chat module. I've got everything to work except restricting the width of each chat bubble that appears in the chat screen which is a Recyclerview.
The design logic is that if the width taken up by the chat bubble is greater than or equal to 70% of the screen width then the view will be restricted to 70% of the width, otherwise it will take up as much width as needed. You can observe how the chat bubble width are restricted in this image. (It's a lot easier in iOS where the the UI is completely programmatically built.)
So what I did was define the initial case in the XML for a left:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/chat_message"
android:textColor="#color/black"
android:fontFamily="#font/nexabold"
android:textSize="16sp"
android:layout_margin="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#drawable/background_chat_left"
android:padding="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And then for the right:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/chat_message"
android:textColor="#color/fullWhite"
android:fontFamily="#font/nexabold"
android:textSize="16sp"
android:layout_margin="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#drawable/background_chat_right"
android:padding="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Finally, in my bind view holder method in the adapter for the Recyclerview I do the following. My thinking is that I leave the start (outgoing msg) /end (incoming msg) constraints open in the XML and populate the textview. If the textviews is more than 70% then restrict the width. This, however, isn't working and I am not sure why. I end up with the text spilling outside of the screen regardless.
#Override
public void onBindViewHolder(#NonNull MessagesViewHolder holder, int position) {
holder.tvMessage.setText(messagesArray.get(position).getBody());
Context context = holder.tvMessage.getContext();
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int screenWidth = displayMetrics.widthPixels;
ViewGroup.LayoutParams layoutParams = holder.tvMessage.getLayoutParams();
if (layoutParams.width >= screenWidth * 0.7) {
holder.tvMessage.setLayoutParams(new ViewGroup.LayoutParams((int) (screenWidth * 0.7), ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
I'm new to android development . I'm using this library https://github.com/chthai64/SwipeRevealLayout to implement swipelayout on my recyclerview.
However , it can only swipe to open and close but i want to implement when one item is swiped open then on click of any items will close the current opened one.
I just could't figure out how it works.
I'm sorry if my topic question doesn't match what i'm describing, I'm not a native english speaker.
I'm able to set ViewBinderHelper.setOpenOnly(true) . It can only swipe one item at a time.
I tried to test if swipelayout is open or close by holder.swipelayout.isOpen , but it doesn't do anything.
Any Ideas or Help would be extremely appreciated , thank you.
This is my Adapter Class
class ReturnAdapter(val context : Context , val deliveryinfo : MutableList<BookingInfo>) : RecyclerView.Adapter<ReturnAdapter.MyViewHolder>() {
private val viewbinderhelper = ViewBinderHelper()
init {
viewbinderhelper.setOpenOnlyOne(true)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view=LayoutInflater.from(context).inflate(R.layout.return_model_layout, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return deliveryinfo.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val deliveryinformation = deliveryinfo[position]
holder.setData(deliveryinformation)
viewbinderhelper.bind(holder.swipelayout, deliveryinformation.booking_id.toString())
}
inner class MyViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
val swipelayout : SwipeRevealLayout =
itemView.findViewById(R.id.swipelayout)
}
My XML file
<?xml version="1.0" encoding="utf-8"?>
<com.chauthai.swipereveallayout.SwipeRevealLayoutandroid:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:mode="same_level"
app:dragEdge="right"
android:id="#+id/swipelayout">
<!-- Your secondary layout here -->
<LinearLayout android:layout_width="70dp"
android:id="#+id/deletereturn"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:foreground="?
android:attr/selectableItemBackground"
android:gravity="center"
android:background="#color/red">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/ic_delete_white_36dp"/>
</LinearLayout>
<!-- Your main layout here -->
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginHorizontal="30dp"
android:gravity="center"
android:layout_centerVertical="true">
<pl.droidsonroids.gif.GifImageView
android:layout_width="36dp"
android:layout_gravity="center"
android:layout_height="36dp"
android:id="#+id/packaging"
android:src="#drawable/redmarkcheck"</LinearLayout></com.chauthai.swipereveallayout.SwipeRevealLayout>
What I need:
I tried doing this using a RecyclerView that sets the height of the item dynamically based on the model. I am using
Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center|bottom"
android:layout_weight="0">
<TextView
android:id="#+id/teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="5dp"
android:textColor="#android:color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/room"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/teacher"
android:padding="5dp"
android:textColor="#android:color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1976D2"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textColor="#ffffff"
android:textSize="13sp"
android:maxLines="3"
android:lines="1" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Adapter-code:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleViewHolders {
val layoutView = LayoutInflater.from(parent.context).inflate(
R.layout.rv_grid, null)
layoutView.getViewTreeObserver().addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
val lp = layoutView.getLayoutParams()
if (lp is StaggeredGridLayoutManager.LayoutParams) {
val sglp = lp as StaggeredGridLayoutManager.LayoutParams
when (viewType) {
1 -> sglp.isFullSpan = false
2 -> {
sglp.height = layoutView.height * 2;
layoutView.refreshDrawableState();
}
3 -> sglp.height = layoutView.height * 3;
}
layoutView.setLayoutParams(sglp)
val lm = (parent as RecyclerView).layoutManager as StaggeredGridLayoutManager
lm.invalidateSpanAssignments()
}
layoutView.getViewTreeObserver().removeOnPreDrawListener(this)
return true
}
})
return ViewHolder(layoutView)
}
Where it does multiplies with the number of hours the meeting takes.
The problem is that if there is a one-hour meeting after a 2-hour meeting, it gets shifted to the next day.
See:
With 2-hour meetings:
With 1-hour meetings:
How can I fix this in the adapter/how can I adapt the height without shifting the elements?
Import the library into your project.
Grab via maven
<dependency>
<groupId>com.github.alamkanak</groupId>
<artifactId>android-week-view</artifactId>
<version>1.2.6</version>
<type>aar</type>
</dependency>
Grab via gradle
compile 'com.github.alamkanak:android-week-view:1.2.6'
Add WeekView in your xml layout.
<com.alamkanak.weekview.WeekView
android:id="#+id/weekView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:eventTextColor="#android:color/white"
app:textSize="12sp"
app:hourHeight="60dp"
app:headerColumnPadding="8dp"
app:headerColumnTextColor="#8f000000"
app:headerRowPadding="12dp"
app:columnGap="8dp"
app:noOfVisibleDays="3"
app:headerRowBackgroundColor="#ffefefef"
app:dayBackgroundColor="#05000000"
app:todayBackgroundColor="#1848adff"
app:headerColumnBackground="#ffffffff"/>
Write the following code in your java file.
/
/ Get a reference for the week view in the layout.
mWeekView = (WeekView) findViewById(R.id.weekView);
// Set an action when any event is clicked.
mWeekView.setOnEventClickListener(mEventClickListener);
// The week view has infinite scrolling horizontally. We have to provide the events of a
// month every time the month changes on the week view.
mWeekView.setMonthChangeListener(mMonthChangeListener);
// Set long press listener for events.
mWeekView.setEventLongPressListener(mEventLongPressListener);
Implement WeekView.MonthChangeListener, WeekView.EventClickListener, WeekView.EventLongPressListener according to your need.
Provide the events for the WeekView in WeekView.MonthChangeListener.onMonthChange() callback. Please remember that the calendar pre-loads events of three consecutive months to enable lag-free scrolling.
MonthLoader.MonthChangeListener mMonthChangeListener = new MonthLoader.MonthChangeListener() {
#Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
// Populate the week view with some events.
List<WeekViewEvent> events = getEvents(newYear, newMonth);
return events;
}
};
I have a custom dialog set up and I have the layout's width set to wrap_content but it is still taking the entire screen width. I even tried setting the width to 100dp and it still uses the whole screen Why does this happen?
I have set up the dialog like this:
public class DialogGoToLine extends Dialog
{
//The root view of the dialog
private static View rootView;
public DialogGoToLine(Context context, int themeResId){
super(context, themeResId);
}
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Allow the dialog to be canceled by tapping out of its bounds
setCancelable(true);
//Inflate the custom layout
LayoutInflater layoutInflater = LayoutInflater.from(HotspotHelper.getAppContext());
rootView = layoutInflater.inflate(R.layout.dialog_go_to_line, null);
//Set up the layout
//setTitle(HotspotHelper.getAppContext().getString(R.string.clientDetails));
//setTitle("Go to line");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(rootView);
final EditText editText = (EditText) rootView.findViewById(R.id.editTextGoToLine);
Button buttonGoToLine = (Button) rootView.findViewById(R.id.buttonGoToLine);
buttonGoToLine.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View p1)
{
int line = Integer.valueOf(editText.getText().toString()) - 1;
((LinearLayoutManager)LogActivity.recyclerViewLog.getLayoutManager()).scrollToPositionWithOffset(line, (int)HotspotHelper.dpToPx(30));
dismiss();
}
});
}
}
And the layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewGoToLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:labelFor="#+id/editTextGoToLine"
android:text="Go to line: "
android:background="#color/accent"
android:textColor="#color/primaryText"
android:textSize="18sp"/>
<EditText
android:id="#id/editTextGoToLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_margin="4dp"
android:layout_centerVertical="true"
android:inputType="number"
android:gravity="center"
android:textSize="18sp"/>
<Button
android:id="#+id/buttonGoToLine"
android:layout_width="wrap_content"
android:text="Go"
android:layout_margin="4dp"
android:textSize="18sp"
android:layout_height="wrap_content"/>
Edit 1
If it makes a difference, this dialog has a style in which I declared the background to this drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/colorPrimary"/>
<corners
android:radius="2dp" />
</shape>
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 size
params.height = thumb_size;
params.width = thumb_size;
// set padding ,margins ,gravity as your desired
Use this setContentView(rootView, params);
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"?>
<LinearLayout xmlns: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">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
//put all your views inside this linear layout
<TextView......./>
<TextView......./>
<TextView......./>
</LinearLayout>
</LinearLayout>
Try this before showing the dialog:
dialog.getWindow().setLayout(context.getResources().getDimensionPixelSize(R.dimen.dialog_width), WindowManager.LayoutParams.WRAP_CONTENT)
dialog_width is 100dp.
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.
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.