CardView width is not matching to width of the screen? - android

I am trying to match the CardView width to the screen width but CardView width is not matching to screen width. I have checked my code again and again and I've even made all the attributes width equal to match parent.
Recyclerview xml --
<?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"
tools:context=".DisplayTodayExercises">
<android.support.v7.widget.RecyclerView
android:id="#+id/exercises_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Card view xml --
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/activity_general_dimen"
android:orientation="horizontal">
<TextView
android:id="#+id/exer_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="40dp"
android:textStyle="bold"
android:gravity="center_vertical"
tools:text="Exercise Name"/>
<TextView
android:id="#+id/se_eps"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:layout_marginLeft="#dimen/sets_reps_dimen"
android:gravity="center_vertical"
android:layout_height="40dp"
tools:text="5 x 6"/>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_marginLeft="#dimen/sets_reps_dimen"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Inflating code
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.exercises_card_view_layout, null);
return new ExerciseContentViewHolder(view);
Here is the screenshot

The problem lies with your inflater. Check out the documentation for LayoutInflater, specifically for the inflate method.
Try this:
LayoutInflater.from(parent.getContext())
.inflate(R.layout.exercises_card_view_layout, parent, false);
Instead of supplying null, supply the parent view. This way the inflater knows what LayoutParameters to use. Supplying false parameter tells the LayoutInflater not to attach it to the parent just yet. The RecyclerView will handle this for you.

This is kind of known issue. You can wrap your CardView with eg RelativeLayout or try the solution from here: CardView layout_width="match_parent" does not match parent RecyclerView width

Related

RecyclerView wraps its conent

I am using the RecyclerView to inflate a Linear Vertical listViews, However, although the width of a child is set to match_parent, RecyclerView wraps that at runtime.
Here is a screenshot of the problem:
item_recyclerView
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tag_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:maxLines="2"/>
<TextView
android:id="#+id/tag_role"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/user_pic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:rotation="-45"
android:scaleType="centerCrop"
android:src="#drawable/ic_user"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.circleImageView" />
</LinearLayout>
When inflating a layout file, the layout_ attributes on the root view will be ignored unless you specify a parent ViewGroup in the inflate() call. You are not currently doing so.
In your onCreateViewHolder() method, replace this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater);
with this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);
Note also that you probably don't want to be using match_parent for the height of your RecyclerView's items:
android:layout_width="match_parent"
android:layout_height="match_parent"
This will give the appearance that only a single item is visible in your RecyclerView, since each item will be the full height of the screen. This is currently not a problem for the exact same reason that the match_parent width is being ignored.
give for your parent linearlayout weightSum 5
give for imageView weight 1 and do his width 0
and for child linearlayout weight 4
enter code here

Recyclerview not occupying whole width when used with weight

I have a
1. Framelayout(frame1) in which I am inflating a RecyclerView layout with weight 0.5
2. Framelayout(frame2) with visibility gone and weight 0.5
My Recyclerview is occupying only half the width though frame2 is gone. If I use listview instead of RecyclerView, I dont have this issue. Can anyone suggest how to make Recyclerview occupy whole width ?
EDIT:
If I scroll the recyclerview , it is taking up the whole width as expected.
Adapter
#Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
ListViewHolder tvh = new ListViewHolder (v);
return tvh;
}
main_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="0dp" // If I put match_parent here it works
android:layout_height="match_parent"
android:layout_weight="0.5"
/>
<FrameLayout
android:id="#+id/frame2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="gone" />
</LinearLayout>
frame1_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
recycler_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="5dp" >
<ImageView
android:id="#+id/imageIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_main" />
<TextView
android:id="#+id/textName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:layout_toRightOf="#+id/imageIcon"
android:textSize="14sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Test"
android:layout_below="#+id/textName"
android:textSize="12sp" />
</RelativeLayout>
Solved the issue by following this SO post . Any better solution will be appreciated.
Recyclerview with width 0dp
Solution is to basically call
recyclerView.setAdapter(adapter);
/* Make sure you call all the below stuff after you set the adapter or
in your loadFinished() incase you are using loading data using Loaders*/
recyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setAutoMeasureEnabled(false); // This is the key
recyclerView.setLayoutManager(llm);
Since you have two frame layouts with each having an equal weight, they will each occupy half the screen. Even if the visibility of the second frame layout is gone.
What do you need the second frame layout for? When are you planning to make it visible? And what do you want the first recycler view to look like when the second one is also visible?

Horizontal Recycler View with multiple item at once

I want to show multiple itemview at once like this
I am using recycler view 23.2.1
compile 'com.android.support:recyclerview-v7:23.2.1'
My recycler view xml
<android.support.v7.widget.RecyclerView
android:id="#+id/gallary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
Corresponding Java code
mRecyclerView = (RecyclerView) findViewById(R.id.gallary);
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
With this configuration I getting only one item at once. Like image below
It seems like wrap_content in recycler view is not working as the space between images are there. Is there anyway to remove the space between the items.
I found one bug related to wrap_contect in recycler view link which is fixed. Not sure if this is causing the problem. Any help to fix this will be appreciated
My row view:
<?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">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/facebook" />
<TextView
android:id="#+id/titletv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:text="Testing"
android:textSize="#dimen/fourteen_sp" />
<ProgressBar
android:id="#+id/mainimgloading"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerInParent="true"
android:layout_gravity="center" />
</RelativeLayout>
</LinearLayout>
Your parent LinearLayout for your row cannot be match_parent you must use wrap_content for your width or else you will get what you see there.
if you had a vertical recyclerview then you would be using wrap_content for your height and not your width

RelativeLayout width and height fill_parent doesn't work

This is my layout which is an info window, and its shown where there is new info to show to the user.
The layout should take the whole screen with some transparent background color, and a small text layout:
<?xml version="1.0" encoding="utf-8"?>
<!-- info fragment - used by the fragment to provide info on a specific fragment -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_dark_green" >
//this is the only layout i see in the result. not it's parent's layout...
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_info"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#drawable/background_info_window"
android:clickable="true" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_margin="5dp" >
<!-- The info textView -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_info"
style="#style/text_shadow_black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="Some Text "
android:textColor="#android:color/white"
app:isBold="true" />
</ScrollView>
<!-- Tap to close -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_info_textview_tap_to_close"
style="#style/text_shadow_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingRight="5dp"
android:text="#string/button_tap_to_close"
android:textColor="#android:color/white"
app:isBold="true" />
</RelativeLayout>
</RelativeLayout>
I'm adding this layout to another ViewGroup like this:
/**
* Show the info window
*/
public void show()
{
//if the window is not visible
if (!isVisible())
//add window's view
rootView.addView(infoWindowView);
}
this code add the info window into its root view container in the last index.
in result i only see the second RelativeLayout which contains the text but i don't see its parent root layout which supposed to be transparent...
Any ideas why ?
Update:
This is my container:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the main layout of the application -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_basic_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
I also had problems with a view in Relative layout that was dependent on the Relative layout's size.
I solved this problem problematically by updating the inner view size
private void update viewSize()
if(rootContainer == null || rootContainer.getLayoutParams() == null)
return;
rootContainer.measure(View.MeasureSpec.makeMeasureSpec(ScalingUtil.getScreenSize().getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY));
RelativeLayout.LayoutParams oldLayoutParams = (RelativeLayout.LayoutParams) rootContainer.findViewById(R.id.deals_card_center_bg).getLayoutParams();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rootContainer.getMeasuredWidth(), rootContainer.getMeasuredHeight());
layoutParams.setMargins(oldLayoutParams.leftMargin, oldLayoutParams.topMargin, oldLayoutParams.rightMargin, oldLayoutParams.bottomMargin);
rootContainer.findViewById(R.id.deals_card_center_bg).setLayoutParams(layoutParams);
How to not have the parent expand
Instead of match_parent on the child, align it to the sibling that dictates the height.
<RelativeLayout ...>
<View
android:id="#+id/element_that_should_match_its_parents_height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignBottom="#+id/element_that_dictates_height" />
<View
android:id="#+id/element_that_dictates_height"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Worked for me, hope it helps. The second View is usually some ViewGroup, that varies in height.
Try change
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_info"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#drawable/background_info_window"
android:clickable="true" >
to
<RelativeLayout
android:id="#+id/fragment_info_relativelayout_info"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#drawable/background_info_window"
android:clickable="true" >
Anyway, why the need of nested RelativeLayout ?
I see how do you add the view, so try also this:
infoWindowView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
rootView.addView(infoWindowView);

Framelayout with two Elements

I got a FrameLayout which has two elements, a TextView and a View with a Backgroundcolor.
Whithin eclips previw this shows up as expected, the view overlays the Textview.
Howewer when i am inflating this layout into another the colored view just disapears. any suggestions?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View
android:layout_width="wrap_content"
android:layout_height="10dp" android:background="#000" android:layout_gravity="bottom" android:id="#+id/viewActive"/>
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</FrameLayout>
This is the code for include
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup view2 = (ViewGroup) inflater.inflate(R.layout.frame_layout, null);
anotherViewGroup.addView(view2);
Your plain view's width is set to "wrap_content". That means the view should be as big as my content but it doesn't have any content so the width is effectively 0, making it invisible.
Try setting the width to a hardcoded value like "10dp" or "match_parent". That should do the trick.

Categories

Resources