Expand layout_weight with animation from 0.3 to 0.8 - java

I have fragment who slide up to 0.3 of height of screen...
now i want to animate it from 0.3 to 0.8 on button. But how do it? Can I animate a layout_weight? I need show more information after click on buton that why to rise height. objectAnimator didnt work.
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:gravity="bottom">
<LinearLayout
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:animateLayoutChanges="true"
android:background="#drawable/shape_details"
android:clickable="true"
android:orientation="vertical"
android:padding="15dp"
android:paddingBottom="32dp"
android:paddingTop="32dp">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text=""
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight=".1"
android:src="#drawable/time" />
<TextView
android:id="#+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".9"
android:text="TextView"
android:textColor="#android:color/darker_gray"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

You can accomplish this by using a ValueAnimator to animate a float from 0.3 to 0.8, and then use an AnimatorUpdateListener to update the weight value in your details view's LayoutParams.
LinearLayout details = findViewById(R.id.details);
ValueAnimator animator = ValueAnimator.ofFloat(0.3f, 0.8f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) details.getLayoutParams();
params.weight = value;
details.setLayoutParams(params);
}
});
Now you can call animator.start() whenever you want to animate the view from 30% height to 80% height.

Related

TextView not appearing in embedded LinearLayout

I am creating a simple tasklist app, and I want to programmatically add a TextView to an embedded LinearLayout. However, it is not showing up for some reason.
I try to add the TextView in MainActivity (irrelevant code removed).
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}
private void processExtraData(){
Integer hours = 4;
Integer minutes = 35;
Integer seconds = 0;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
TextView txt1 = new TextView(this);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) (64 * scale + 0.5f);
ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(0, pixels, 1f);
txt1.setLayoutParams(params); txt1.setText(hours.toString()+":"+minutes.toString()+":"+seconds.toString());
linearLayout.addView(txt1);
}
The embedded LinearLayout is declared in activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Super Reminder App!"
android:textAppearance="#style/TextAppearance.AppCompat.Large" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="horizontal">
<EditText
android:id="#+id/taskAdd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.5"
android:hint="New Task" />
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="Add Task" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="12.5"
android:orientation="horizontal">
<ListView
android:id="#+id/tasks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"/>
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
</LinearLayout>
<Button
android:id="#+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Clear All" />
</LinearLayout>
I have already tested and made sure that onNewIntent and processExtraData are called. No errors are thrown either. Could this be a GUI issue? Any help would be greatly appreciated.

Check recyclerview scroll state inside nestedscrollview

I'm using a recyclerview inside NestedScrollView and I want to pin my search bar to action bar after scrolling and recyclerview items must show under searchbar, I need to make lazy load for my recyclerview items (load next items from server after scrolling to end) for this I need to check recyclerview scroll change state and I can't do this perfectly when I use recyclerview inside nestedscrollview. I tried using nestedscrollview scroll state change listener and it doesn't give me what I want and doesn't work right.
Nested scroll view is not working if it's placed inside recyclerview. I have lazy load recyclerview but same layout contain other layouts like slider, menus then recyclerview. I want to scroll full layout and when recyclerview ends onscroll then onload execute to get more item from internet and load in recylerview.
Here you see my codes:
my layout xml code
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/main_activity_background">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#color/colorPrimaryDark"
app:expandedTitleTextAppearance="#style/TextAppearance.AppCompat.Headline"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/bg_gradient"
android:orientation="vertical"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/spacing_large"
android:paddingRight="#dimen/spacing_large"
android:paddingTop="#dimen/spacing_mxlarge">
<LinearLayout
android:id="#+id/layout_dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" />
</RelativeLayout>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetStartWithNavigation="0dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#drawable/searchbox_stroke_bg"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="0.9"
android:layout_height="40dp"
android:hint="#string/search"
android:textDirection="locale"
android:textAlignment="viewStart"
android:drawablePadding="10dp"
android:layout_gravity="start"
android:paddingStart="15dp"
android:textColor="#color/searchbox_stroke"
android:maxLines="1"
android:singleLine="true"
android:maxLength="25"
android:textSize="17sp"
android:paddingEnd="5dp"
android:background="#android:color/transparent"
android:theme="#style/MainSearchEditTextTheme"
android:drawableStart="#drawable/ic_search"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="16dp"
android:src="#drawable/ic_filter"
android:layout_gravity="center_vertical" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:id="#+id/main_nested_scrollView"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollingCache="true"
android:nestedScrollingEnabled="false"
android:id="#+id/main_recyclerview"
android:layout_marginBottom="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/more_items_progress"
android:visibility="gone">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progress_bar"
android:indeterminate="true"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lyt_progress"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:orientation="vertical">
<com.armanjafari.raimon.widget.ViewLoadingDotsBounce
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:background="#color/colorAccent">
</com.armanjafari.raimon.widget.ViewLoadingDotsBounce>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
My code to check scroll state and it didn't work
nested_content.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
if (scrollY < oldScrollY)
{ // up
((MainActivity)getActivity()).animateNavigation(false);
}
if (scrollY > oldScrollY)
{ // down
((MainActivity)getActivity()).animateNavigation(true);
}
if (scrollY == ( v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight() ))
{
if (!is_loading)
{
if (current_page < all_pages)
{
//not work right scroll state
}
}
}
}
});
i got the answer, if any one have my problem see below...
for detect nestedscrollview is at end correctly use this code:
nested_content.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
if (scrollY < oldScrollY)
{ // up
((MainActivity)getActivity()).animateNavigation(false);
}
if (scrollY > oldScrollY)
{ // down
((MainActivity)getActivity()).animateNavigation(true);
}
View view = (View) nested_content.getChildAt(nested_content.getChildCount() - 1);
int diff = (view.getBottom() - (nested_content.getHeight() + nested_content
.getScrollY()));
if (diff == 0)
{
if (merchantList.size()>0)
{
if (!is_loading)
{
if(current_page<=all_pages)
{
// nestedscrollview at end
getMerchants(current_page);
}
}
}
}
}
});

Increasing scrollview by adding buttons in a relativelayout (or linearlayout ) from bottom to top

Could really use some help with this one.
I'm trying to start with a single button at the bottom of my screen. When my counter++ button is clicked, a second button will be dynamically created above the button at the bottom of the screen. I was able to get this far, the problem is that the height of my scrollview doesn't increase when my buttons reach the top of the screen. Please see my example below.
Example
I'm assuming the scrollview only increases when items are added below the current view although if that's the case, is there no way I can create several buttons starting at the bottom going upwards?
Here my code.
XML:
<RelativeLayout 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:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.bbetzner.ttt.Map">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++"
/>
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Java:
public void addfloor(){
Button myButton = new Button(this);
myButton.setText(""+floor);
margincount += 100;
RelativeLayout ll = findViewById(R.id.layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, R.id.floor0);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.bottomMargin = margincount;
ll.addView(myButton, lp);
}
I've also tried doing this in a linear layout to see if the scrollview would increase but I couldn't figure out how to stack buttons from bottom to top in a linear layout. :S
Thanks in advance, yall are awesome!
#Texas use below code using that you are achieving what you want:
<RelativeLayout 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:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:id="#+id/lladdViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="0" />
</LinearLayout>
</ScrollView>
</LinearLayout>
and here is Java File:
public class SampleActivity extends Activity {
LinearLayout lladdViews;
Button count;
int floor = 0;
private int margincount = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
lladdViews = (LinearLayout) findViewById(R.id.lladdViews);
count = (Button) findViewById(R.id.count);
count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addfloor();
}
});
}
public void addfloor() {
Button myButton = new Button(this);
myButton.setText("" + floor);
margincount += 10;
RelativeLayout ll = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, R.id.floor0);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.bottomMargin = margincount;
ll.addView(myButton, lp);
lladdViews.addView(ll);
}
}
Here is your designing part happy coding.....
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
<Button
android:id="#+id/floor1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="7"
/>
<Button
android:id="#+id/floor2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="6"
/>
<Button
android:id="#+id/floor3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="5"
/>
<Button
android:id="#+id/floor4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="4"
/>
<Button
android:id="#+id/floor5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/><Button
android:id="#+id/floor6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="3"
/><Button
android:id="#+id/floor7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="2"
/>
<Button
android:id="#+id/floor8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="1"
/>
<Button
android:id="#+id/floor9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>

RecyclerView within ScrollView wrap_content height not working as intended

I have a RecyclerView embedded within a ScrollView. Here is the complete layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<fragment
android:name="devarshi.sample.view.ProductPortfolioFragment"
android:id="#+id/fragmentProductPortfolio"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="230dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textViewProductName" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textViewProductDescription" />
<LinearLayout
android:id="#+id/linearLayoutProductDetails"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/product_detail_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textViewProductDetails" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/linearyLayoutOtherProductDetails"
android:layout_height="100dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutPrice"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewProductPrice"
android:layout_width="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/colorDiscountedPrice"
android:layout_height="wrap_content"
android:text="1500"/>
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewOldProductPrice"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="200"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewQuantity"
android:layout_width="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/colorDiscountedPrice"
android:layout_height="wrap_content"
android:text="Quantity"/>
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewQuantityValue"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="200"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewBrand"
android:layout_width="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/colorDiscountedPrice"
android:layout_height="wrap_content"
android:text="Brand"/>
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewBrandValue"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="200"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutFreeShipping"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/product_detail_free_shipping_icon"
android:id="#+id/imageViewFreeShipping" />
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewFreeShipping"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="#string/product_detail_free_shipping"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutCashOnDelivery"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/product_detail_cash_on_delivery_icon"
android:id="#+id/imageViewCashOnDelivery" />
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewCashOnDelivery"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="#string/product_detail_cash_on_delivery"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutUnit"
android:layout_height="wrap_content">
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewUnitLabel"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="#string/product_detail_unit"/>
<Button
android:text="-"
android:layout_width="#dimen/product_detail_counter_button_width"
android:layout_height="#dimen/product_detail_counter_button_height"
android:id="#+id/buttonDecrement" />
<TextView
android:id="#+id/textViewUnitValue"
android:layout_width="27dp"
android:textSize="14sp"
android:gravity="center"
android:textColor="#color/colorActualPrice"
android:layout_height="#dimen/product_detail_counter_button_height"
android:text="0"/>
<Button
android:text="+"
android:layout_width="#dimen/product_detail_counter_button_width"
android:layout_height="#dimen/product_detail_counter_button_height"
android:id="#+id/buttonIncrement" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewProductDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Problem is -
Though I have set layout_height of RecyclerView as wrap_content its height is partially wrapping the content i.e. ideally after wrap_content its height should be 150dp but it is currently being 70dp.
Another interesting scenario is -
If I move linearyLayoutOtherProductDetails below recyclerViewProductDetails then it shows RecyclerView height accurately as 150dp. Seems like when the RecyclerView is loaded on the layout it is calculating height based on the number of items (within it) currently being visible on the layout i.e. the height is equal to 5 items in place of 9 items.
Any ideas on how can I have a RecyclerView of height completely wrapping its content in first scenario?
Note: I am using com.android.support:recyclerview-v7:25.0.0
Using RecyclerView inside a ScrollView is not a very good practice. You might consider keeping them all inside a NestedScrollView which might serve your purpose. Here's the documentation from developers android.
In your case, you might consider having the linearyLayoutOtherProductDetails as the header of your RecyclerView and this should be the best solution. Get rid of ScrollView or NestedScrollView and add the custom layouts as the header or footer of your RecyclerView.
Check my answer here about how you can add a header/footer in your RecyclerView.
I faced the same problem, and finally I found there maybe a bug in the measure logic of RecyclerView and LinearLayoutManager when using heightMeasureSpec of UNSPECIFIED mode and not zero size.
I have reported this bug to google issuetracker, you can see the details.
NestedScrollView may also lead to the same problem when it's child has not zero vertical margin, because it may pass heightMeasureSpec of UNSPECIFIED mode and not zero size to it's child, then the child may pass similar heightMeasureSpec to RecyclerView. Related code of NestedScrollView:
#Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
To fix this bug, you can simplly use the inherited RecyclerView below:
public class FixRecyclerView extends RecyclerView {
public FixRecyclerView(#NonNull Context context) {
super(context);
}
public FixRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public FixRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthSpec, int heightSpec) {
if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED && MeasureSpec.getSize(heightSpec) != 0) {
super.onMeasure(widthSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
} else {
super.onMeasure(widthSpec, heightSpec);
}
}
}
Make linearlayoutOtherProductdetails header (first item) of recyclerview and remove scrollview. Because scrolling view inside another scrolling view does not work properly.
Another solution is use custom layout manager which set the height of recycler view equals to the height of its content. But this may result in poor scrolling .

The image background of my LinearLyout, containing a webview, doesn't appear

I have a fragment containing a LinearLayout containing a webview, when a change the html string in my webview, sometimes the background of my LinearLayout doesn't appear.
My fragment:
`
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
tools:context=".NewsFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/bg_item_single"
android:orientation="vertical"
android:padding="#dimen/list_margin" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textColor="#000"
android:textSize="20sp" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/transparent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
`
If I have a linearLayout in a other linearLayout is just to have a space on the bottom of my scrollView, isn't the problem.
My code:
`
private void populate() {
textView.setText(grant.getName());
// Configure w`enter code here`ebview
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setJavaScriptEnabled(false);
webview.setBackgroundColor(Color.argb(1, 0, 0, 0));
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webview.loadDataWithBaseURL("file:///android_asset/", createFullHtmlPage("styles.css", grant.getGrantDescription(), 0), "text/html", "UTF-8", null);
}
`
I don't see were is the problem.
Try setting android:layout_height="wrap_content", and remove the transparent background of this view.
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/transparent" />
I fixed my problem. I used a shape as background of my LinearLayout, but if the webview is too big, for unknown reason, the shape don't work and don't appear. Therefore now I use a color background and 2 imageview for creat a bottom corner and top corner.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
tools:context=".NewsFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="50dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_item_top" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bg_item_default"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textColor="#000"
android:textSize="20sp" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/list_margin" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_item_bottom" />
</LinearLayout>
</LinearLayout>
</ScrollView>
How I undestud problem is in this line
webview.setBackgroundColor(Color.argb(1, 0, 0, 0));
try this
webview.setBackgroundResource(color.blue);

Categories