I am currently using a scroll view inside an Alert Dialog and need the scroll view to grow in height until the dialog reaches its maximum default height. It's a bit tough for me to explain so i have attached an illustration to help. Hope it does.
The issue i'm getting is the scrollview does not grow in height and even if i remove the app:layout_constraintBottom_toTopOf="#id/linearLayout4", it grows however the bottom part will be partially hidden by the button layout. This is my current code :
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/filter_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_box"
android:minHeight="300dp"
android:elevation="12dp">
<TextView
android:id="#+id/filter_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/filter"
style="#style/element_header"
android:textColor="#color/textColorDark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/filter_reset_btn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:text="#string/reset"
style="#style/text_info_nBold"
android:textSize="14sp"
android:textColor="#color/textColorDark"
app:layout_constraintBottom_toBottomOf="#+id/filter_header"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/filter_header" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#id/linearLayout4"
app:layout_constraintTop_toBottomOf="#id/filter_header"
app:layout_constraintVertical_bias="0.0">
<androidx.core.widget.NestedScrollView
android:id="#+id/filter_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_marginTop="16dp"
android:scrollbarFadeDuration="1000">
<!-- VIEWS INSIDE HERE -->
</androidx.core.widget.NestedScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_bottombar_layout"
android:orientation="horizontal"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/dialog_secondary_button"
style="#style/low_emphasis_btn"
android:layout_width="0dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:backgroundTint="#color/textColorDark"
android:text="#string/dialog_cancel"
android:textColor="#color/textColorDark"
android:visibility="visible" />
<Button
android:id="#+id/dialog_primary_button"
style="#style/high_emphasis_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:text="#string/apply" />
</LinearLayout>
Any help would be appreciated :)
Alert dialogs tend to wrap their content or can be forced to be full screen. A size in between the optimizes the screen real estate takes a some work, but it is not impossible.
One approach is to let the system lay out the alert dialog but, before it is displayed, use a ViewTreeObserver.OnGlobalLayoutListener to examine the resulting size of the dialog. In the layout listener, the size of the dialog can be adjusted up to fit the contents of the scrolling view or adjusted up to full screen if the scrolling view contents are too large for the screen.
Here is a demo app that shows how this can be done. Comments in the code explain more.
MainActivity.kt
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onClick(v: View) {
val text = when (v.id) {
R.id.customDialogShort -> getString(R.string.short_string)
R.id.customDialogMedium -> getString(R.string.lorem_medium)
else -> getString(R.string.lorem_long)
}
// Specifying the viewGroup as a parent to the inflater makes no difference.
val dialogView = LayoutInflater.from(v.context).inflate(R.layout.con_custom_view, null, false) as ConstraintLayout
(dialogView.findViewById(R.id.textView) as TextView).text = text
val alertDialog = AlertDialog.Builder(this).setView(dialogView).create()
val decorView = alertDialog.window!!.decorView
decorView.setBackgroundResource(R.drawable.alert_dialog_background)
// We need a layout pass to determine how big everything is and needs to be. Place a hook
// at the end of the layout process to examine the layout before display.
decorView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
decorView.viewTreeObserver.removeOnGlobalLayoutListener(this)
// Find out how much of the scrolling view is usable by its child.
val scrollingView: NestedScrollView = decorView.findViewById(R.id.filter_scroll)
val scrollingViewPadding = scrollingView.paddingTop + scrollingView.paddingBottom
val scrollingUsableHeight = scrollingView.height - scrollingViewPadding
// If the child view fits in the scrolling view, then we are done.
val childView = scrollingView.getChildAt(0)
if (childView.height <= scrollingUsableHeight) {
return
}
// Child doesn't currently fit in the scrolling view. Resize the top-level
// view so the child either fits or is forced to scroll because the maximum
// height is reached. First, find out how much space is allowed by the decor view.
val displayRectangle = Rect()
decorView.getWindowVisibleDisplayFrame(displayRectangle)
val decorViewPadding = decorView.paddingTop + decorView.paddingBottom
val decorUsableHeight = displayRectangle.height() - decorViewPadding - scrollingViewPadding
// Compute the height of the dialog that will 100% fit the scrolling content and
// reduce it if it won't fit in the maximum allowed space.
val heightToFit = dialogView.height + childView.height - scrollingUsableHeight
dialogView.minHeight = min(decorUsableHeight, heightToFit)
}
})
var buttonOk: Button = dialogView.findViewById(R.id.dialog_primary_button)
buttonOk.setOnClickListener { alertDialog.dismiss() }
buttonOk = dialogView.findViewById(R.id.dialog_secondary_button)
buttonOk.setOnClickListener { alertDialog.dismiss() }
alertDialog.show()
}
}
activity_main.xml
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/customDialogShort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Short text" />
<Button
android:id="#+id/customDialogMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Medium text" />
<Button
android:id="#+id/customDialogLong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Long text" />
</LinearLayout>
con_custom_view
Custom Layout for the AlertDialog.
<TextView
android:id="#+id/filter_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/filter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/filter_reset_btn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:text="#string/reset"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="#+id/filter_header"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/filter_header" />
<androidx.core.widget.NestedScrollView
android:id="#+id/filter_scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:padding="16dp"
android:scrollbarFadeDuration="1000"
app:layout_constraintBottom_toTopOf="#id/linearLayout4"
app:layout_constraintTop_toBottomOf="#id/filter_header">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="#string/lorem_long" />
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/dialog_secondary_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:text="#string/dialog_cancel"
android:visibility="visible" />
<Button
android:id="#+id/dialog_primary_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/apply" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I have create an android app with Kotlin.This app contains an interface, which contains a recyclerview to get all the list of product.
To fill this list of product, i create an adapter, this adapter is a cardview : Imageview and two textview.
In this interface, i added two buttons, one when i click on all the product display with the list and the other one display with grid disposition.The default value for grid disposition.
I wsant to change the width and the height of a cardview, to display all the product in the List.
The following code is anadapter item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardProductItem"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_marginTop="#dimen/spacing_medium"
android:layout_marginBottom="#dimen/spacing_medium"
android:layout_marginRight="#dimen/spacing_middle"
android:layout_marginLeft="#dimen/spacing_middle"
android:background="#color/grey">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/productImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_centerInParent="true"
android:layout_gravity="center"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/spacing_middle">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/productName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/spacing_medium"
android:fontFamily="#font/lato_regular"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/spacing_medium"
android:textStyle="bold"
android:fontFamily="#font/lato_regular"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:id="#+id/arrowDetailProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_margin="8dp"
android:visibility="gone"
android:layout_gravity="center|center_horizontal"
android:src="#drawable/ic_right_arrow"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
the following code is an extract from an xml file which cotains a recyclerview :
<ImageView
android:id="#+id/gridProducts"
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:scaleType="fitXY"
android:layout_toLeftOf="#+id/productNumber"
android:layout_margin="8dp"
android:src="#drawable/ic_divided_squares"/>
<ImageView
android:id="#+id/listProducts"
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:scaleType="fitXY"
android:layout_margin="8dp"
android:src="#drawable/ic_rounded_black_square_shape"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewProductByCategory"
android:layout_below="#+id/recyclerViewCategories"
android:layout_marginTop="66dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_middle"
android:layout_marginEnd="#dimen/spacing_middle"
android:orientation="horizontal"/>
the following code contains the onclick acion into the two buttom(Imageview) :
gridProducts.setOnClickListener {
recyclerViewProductByCategory.layoutManager =
StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
recyclerViewProductByCategory.adapter = products?.let { it -> ProductAdapter(it) }
gridProducts.visibility = View.INVISIBLE
listProducts.visibility = View.VISIBLE
}
listProducts.setOnClickListener {
recyclerViewProductByCategory.layoutManager =
LinearLayoutManager(this#CategoryByProduct, RecyclerView.VERTICAL, false)
recyclerViewProductByCategory.adapter = products?.let { it -> ProductAdapter(it) }
val layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT, // CardView width
LayoutParams.WRAP_CONTENT // CardView height
)
cardProductItem.layoutParams = layoutParams
listProducts.visibility = View.INVISIBLE
gridProducts.visibility = View.VISIBLE
}
after running my app an exception appear as the following :
java.lang.IllegalStateException: cardProductItem must not be null
could you please tell me where's the error and how can i correct it
Based on the code available, you are setting the value of cardProductItem outside of setOnClickListener. The error is stating that cardProductItem can not be set when a listProduct is clicked. Make sure that you are setting cardProductItem to a value before the UI is presented to the user.
For a more in-depth answer, I would need to look at the other places where cardProductItem is used.
I have two text views side by side in my RecyclerView item. One of the views has layout_weight="1" to keep another view visible if the view is wide.
The problem is that the the view sizes are not updating when the views got recycled, so to textView id:name width is not correct when i scroll the list.
Should I somehow force the view to update the layout?
My RecyclerView item:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1" />
<LinearLayout
android:id="#+id/code_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#drawable/background_code"
android:orientation="vertical">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>
Im using RecyclerView v. 23.4.0.
Update
1. I have tried to call my RecyclerView item layout root requestLayout at the end of the adapter onBindViewHolder().
This does not any have affect, my views are not updated.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
...
viewHolder.mLayoutRoot.requestLayout();
}
2. Maybe the problem is that the view is not yet completely drawn when I call requestLayout? So I tried to set GlobalLayoutListener and call requestLayout there. I think this helps little, but still if I have enough items, in my list not all views are updated. Owerall in my list I have around max 10 items.
ViewTreeObserver vto = viewHolder.mLayoutRoot.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
viewHolder.mLayoutRoot.requestLayout();
}
});
3. I tried to use TableLayout with shrinkColumns property to approach similar layout, but the result is same. Name text view widht is not always correctly set. Also, it looks like requestLayout has no affect at all (or am I calling it in wrong place?)
4. (28/8/2016)
I stripped down my code to bare minimum to reproduce this.
I managed to fix this using setIsRecyclable(false), like dosssik suggests, but I think it is not very good solution in terms of performance?
Here is my full list item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_marginLeft="16dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_stop_code_small"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"
android:textSize="11sp"/>
</LinearLayout>
My adapter code:
I also tried to call invalidate(), but can not see any affect.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Item item = getItem(position);
NormalViewHolder viewHolder = (NormalViewHolder) holder;
viewHolder.mName.setText(item.getName());
//viewHolder.mName.invalidate(); // No help
viewHolder.mCode.setText(item.getCode());
//viewHolder.mCode.invalidate(); // No help
}
Expected behaviour:
+----------------------------------------+
|[Name][Code] |
+----------------------------------------+
|[Name qwertyuiopasdfghjklzxcvb...][Code]|
+----------------------------------------+
This is how works right now if I scroll list up/down. So the layout is not updated and code is not alignet right to name. Also name is not taking always enough space.
+----------------------------------------+
|[Name][Code] |
+----------------------------------------+
|[Name qwer...] [Code] |
+----------------------------------------+
|[Name qwertyuiopa][Code] |
+----------------------------------------+
|[Name qwe] [Code] |
+----------------------------------------+
I share my solution which is not need to use
setIsRecyclable(false)
Use below custom TextView at your xml.
public class TextViewForRecyclerView extends android.support.v7.widget.AppCompatTextView {
public TextViewForRecyclerView(Context context) {
super(context);
}
public TextViewForRecyclerView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public TextViewForRecyclerView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
requestLayout();
}
}
This xml works for me without using setIsRecyclable(false);
In some android versions (like kitkat) the code: android:ellipsize="end" only works with android:singleLine="true", so add this to the first TextView "#+id/name" and it will works!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_marginLeft="16dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:maxLines="1"
/>
<TextView
android:id="#+id/code"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_stop_code_small"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"
android:textSize="11sp"/>
</LinearLayout>
The result:
As it seems code is of constant length, you can use following UI deisgn;
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:align_parentLeft="true"
android:layout_toLeftOf="#+id/code"
android:layout_marginRight="8dp"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_code"
android:gravity="center"
android:align_ParentRight="true"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"/>
</RelativeLayout>
The RecyclerView will reuse ViewHolder objects so the layouts of those will be inflated once and then reused through the lifetime of the list. Try calling requestLayout on your root LinearLayout.
Usage of forceLayout(), requestLayout() and invalidate()
You need to invalidate the two TextView views, and make sure you are doing it after you set the new text value to them:
#Override
public void onBindViewHolder(ItemHolder holder, int position)
{
...
holder.nameTextView.invalidate();
holder.codeTextView.invalidate();
}
As an alternative, if you don't mind the code view aligning to the right, you could replace the LinearLayout for a RelativeLayout and set the proper constraints between the name view and the code container:
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/code_container"
android:layout_toStartOf="#+id/code_container"
android:ellipsize="end"
android:maxLines="1" />
<LinearLayout
android:id="#+id/code_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/background_code"
android:orientation="vertical">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"/>
</LinearLayout>
I had very simillar problem with recyclerView.
Sometimes it works not properly, when items recycling, unfortunatly.
for me helped turning off recyclable.
setIsRecyclable(false);
you can do it in ViewHolder's constuctor like this:
class Holder extends RecyclerView.ViewHolder {
public Holder(View itemView) {
super(itemView);
setIsRecyclable(false);
}
}
Sure, that not awesome solution, but it's the only solution, that helped me.
Hope it will help you.
I finally got the right answer. As my test, you can just add two lines in you method onBindViewHolder, as below:
#Override
public void onBindViewHolder(ItemHolder holder, int position)
{
holder.mName.setSingleLine(false); //first line
holder.mName.setText("name");
holder.mCode.setText("");
holder.mName.setSingleLine(true); //second line
}
Hope it help you.
you should add weight to inner linear layout also then it will be
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="2"
android:ellipsize="end"
android:maxLines="1" />
<LinearLayout
android:id="#+id/code_container"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#drawable/background_code"
android:orientation="vertical">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"/>
</LinearLayout>
try this will work for you
I have had layout refresh problems in a chat-like Activity using RecyclerView: I've called a method from onBindViewHolder to adapt any chat message's margins, below a code snippet:
..
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) this.mItemView.getLayoutParams();
lp.setMargins(0,0,50,0);
try this i am not use editor but it will auto-correct if any problem with xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" //i changed this
android:layout_height="150dp"
android:layout_marginLeft="16dp"
android:weightsum="1"//added this
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true" />//added this
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_stop_code_small"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:singleLine="true"
android:textSize="11sp"/>
</LinearLayout>
the above will work
if you still insist of updating layout, always know if the update you need is measurement then you need to call View.invalidate() along with View.requestLayout()
Try using this, I had same issue, i resolved it using this :
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(context).inflate(R.layout.review_data, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new ViewHolder(rootView);
}
Set layout params in onCreateViewHolder like above.
setting weight on just one of them is wrong, because when the code is long the name will get disappeared.
I don't know what are you trying to achieve, but even with a simple ListView and a simple layout like below for its items they will never get disappeared and I think they look better:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:gravity="left"
android:layout_marginLeft="16dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:layout_marginRight="8dp"
android:ellipsize="end"
android:text="dfsdd45te5sf"
android:maxLines="1" />
<TextView
android:id="#+id/code"
android:layout_weight=".5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_dark"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp"
android:layout_marginRight="1dp"
android:text="ertertete5tte5tte5e5te5t"
android:singleLine="true"
android:textSize="12sp"/>
</LinearLayout>
I think you are expecting Out put simile like this
in the top container LinearLayout change "wrap_content" to "match_parent"
check this tutorial
http://wiki.workassis.com/android-recyclerview-example/
Well, I am not getting exact problem what you are facing...but one problem i can
see in your code is weight, you are using weight in a wrong way. weight will not effective if you use in one widget, at-least you have to use two widget to give proper weight between them, and use weight sum in their parent to work properly. for example see below your modified code:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="3">
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:text="qwertyuioopasdfsd" />
<LinearLayout
android:id="#+id/code_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:background="#drawable/background_code"
android:orientation="vertical">
<TextView
android:id="#+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:paddingBottom="1dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="1dp" />
</LinearLayout>
</LinearLayout>
I am making one chat application in this application I have to display all users names who are connected to login user,so I am showing this data in dialog,actually problem is different text views with different styles in relative layout which wiil be arranged automatically side by side,if layout width occupied by 3 text views remaining two text views should be in third line like that.please help me
ex:
car bus bicycle
train ship bike
aeroplane
Try this:
<LinearLayout
android:id="#+id/categoriesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#color/view_background"
android:orientation="vertical" >
<TextView
android:id="#+id/tagsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:text="#string/title"
android:textColor="#color/view_title"
android:textSize="18sp" />
<LinearLayout
android:id="#+id/tagsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:gravity="left"
android:orientation="vertical" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:padding="2dp" >
<Button
android:id="#+id/categoryIndications"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:background="#drawable/xml_category_count_bubble"
android:enabled="false"
android:minWidth="20dp"
android:padding="4dip"
android:textColor="#FFFFFF"
android:textSize="14sp"
android:textStyle="bold" />
<Button
android:id="#+id/categoryName"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:background="#drawable/xml_category_name_bubble"
android:enabled="false"
android:minWidth="40dp"
android:padding="4dip"
android:textColor="#color/view_text"
android:textSize="14sp" />
And this:
private void setTags(){
LinearLayout tagsContainer;
// Categories
tagsContainer = (LinearLayout) rootView.findViewById(R.id.tagsContainer);
// Container of the tags
tagsContainer.removeAllViews();
tagsContainer.refreshDrawableState();
// Indicate category layout
LinearLayout indicateLayout = (LinearLayout)
LayoutInflater.from(getActivity().getApplicationContext()).
inflate(R.layout.message_indicate_category, null);
if(YOUR_TEXTVIEW_STRINGS_ARRAY.size()>0){
Button textButton;
LinearLayout tag;
for(int i = 0; i<YOUR_TEXTVIEW_STRINGS_ARRAY.size();i++){
// View of the tag
tag = (LinearLayout)
LayoutInflater.from(getActivity().getApplicationContext()).
inflate(R.layout.customview_tag, null);
textButton= (Button)tag.findViewById(R.id.categoryName);
textButton.setText(YOUR_TEXTVIEW_STRINGS_ARRAY..get(i));
textButton = (Button)tag.findViewById(R.id.categoryIndications);
textButton.setText(YOUR_TEXTVIEW_STRINGS_ARRAY.get(i));
tagsContainer.addView(tag);
}
}
else{
// View of the tag
indicateLayout = (LinearLayout)
LayoutInflater.from(getActivity().getApplicationContext()).
inflate(R.layout.message_indicate_category, null);
tagsContainer.addView(indicateLayout);
}
}
Four views are using same xml. I want to show a linear layout for view 1 only.
I put android:visibility="gone" in xml. And then I am doing the following for view 1-
LinearLayout layone= (LinearLayout) view.findViewById(R.id.layone);
layone.setVisibility(View.VISIBLE);
But that doesn't set the visibility to visible.
Isn't it possible to show the view once its declared GONE in xml ?
I don't want to converse the logic by just doing,
layone.setVisibility(View.GONE);
in each of the three views except view 1.
Ideas or comments?
UPDATE:
My xml -
<TextView
android:id="#+id/layone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous Page"
android:textColor="#000000"
android:textSize="16dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:visibility="gone" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:tag="PrevEntries"
android:id="#+id/laytwo"
android:layout_marginTop="10dp"
android:background="#layout/roundedtext"
android:visibility="gone" >
<TextView
android:id="#+id/laythree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous Page"
android:textColor="#000000"
android:textSize="18dp"
android:gravity="center"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/layone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous Page"
android:textColor="#000000"
android:textSize="16dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:visibility="gone" />
layone is a TextView.
You got your id wrong.
LinearLayout layone= (LinearLayout) view.findViewById(R.id.laytwo);// change id here
layone.setVisibility(View.VISIBLE);
should do the job.
or change like this to show the TextView:
TextView layone= (TextView) view.findViewById(R.id.layone);
layone.setVisibility(View.VISIBLE);
Done by having it like that:
view = inflater.inflate(R.layout.entry_detail, container, false);
TextView tp1= (TextView) view.findViewById(R.id.tp1);
LinearLayout layone= (LinearLayout) view.findViewById(R.id.layone);
tp1.setVisibility(View.VISIBLE);
layone.setVisibility(View.VISIBLE);
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/activity_register_header"
android:minHeight="50dp"
android:orientation="vertical"
android:visibility="gone" />
Try this piece of code..For me this code worked..
Kotlin Style way to do this more simple (example):
isVisible = false
Complete example:
if (some_data_array.details == null){
holder.view.some_data_array.isVisible = false}
put this attribute in your xml view for Gone
android:visibility="gone"
put this attribute in your xml view for Show
android:visibility="visible"
Example
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/futura_medium_bt"
android:text="•Unlock all graphics"
android:textAllCaps="true"
android:visibility="gone"
android:textColor="#color/black"
android:textSize="#dimen/_12ssp" />