I'm new to Stack Overflow. I'm looking forward to making a calculator in the XML and Kotlin combination. I'm aware I should use a TextView or EditText above and RecyclerView below but "how to add 3 buttons in one row in RecyclerView StackOverflow". in all the tutorials I see they show only one element per row. but I have to add 3 elements. I tried to google search "how to add 3 buttons in one row in RecyclerView StackOverflow" but got no good results.
I'm well aware of the working of RecyclerView, so I don't want full code. I just need which part when altered gets me 3 buttons in RecyclerView.
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/calcTv"
android:layout_marginTop="9dp"
android:textSize="36dp"
android:textAlignment="center"
android:text="5+4"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_below="#+id/calcTv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Along with the above code, I have a RecyclerView adapter and XML for layout inflating.
Sorry in advance if the question is not clear and for my way of asking.
Add layoutManager and spanCount attributes to your RecyclerView:
<androidx.recyclerview.widget.RecyclerView
android:layout_below="#+id/calcTv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:spanCount="3"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>
layoutManager for making the list grid or linear
spanCount is the number of columns
Related
I want to achieve this design shown below. All I can think of is having a LinearLayout as parent layout with android:orientation="vertical" and add listViews with horizontal scroll dynamically inside the linear layout, but it seems very inefficient solution.
What can be the best possible approach to achieve this UI? Any suggestion will be highly appreciated.
If I were you, I will create a RecyclerView witch adds the rows of the Textview and another 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvCards"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
The second recyclerview will contains columns of those cards. Of course the second recyclerview will contain the LinearLayoutManager like that:
LinearLayoutManager linearLayoutManager
= new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false);
I'm not sure if that solution will be the most efficient one. I would like to read more comments to find a better solution, if it exists.
I need to show embeded slider (marked green) inside of the list of recyclerview items, like on image below:
I am thinking that I need to change something in my recyclerview layout manager, but I have no idea what exactly and guess it is quite complicated. In addition I am using FlexboxLayout here.
So, could you please help me with the selection of the best approach of solving this problem?
You can inflate different view for that position , since you're inflating a different layout there .
This answer will help :- RecyclerView with multiple view type
The basic gist is to use the getItemViewType and return a flag , say 1 for the first red box , 2 for the second and 3 for the third and inflate layout on depending on the viewType param ( in the signature )
you can resolve it easily to make the layout like this.
<?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="wrap_content"
android:orientation="vertical"
android:background="#color/recycler_view_item_background_color">
<!-- Red layout-->
<LinearLayout
android:id="#+id/red_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/item_performance_list_padding"
android:orientation="vertical">
<TextView
android:id="#+id/performance_date_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
<!-- Blue Layout-->
<LinearLayout
android:id="#+id/blue_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/text_item_divider" />
</LinearLayout>
you can show or hide the blue layout for every item according to your need.
LinearLayout blueLayout = (LinearLayout) findViewById(R.id.blue_layout);
blueLayout.setVisibility(View.GONE); // hide
blueLayout.setVisibility(View.VISIBLE); // show
I have a background image with a long width which I want to be in back of a Recyclerview with 8 item in it(Recyclerview). This is possible only with a horizontal-scrollview which I coded below but Is there any way to not use horizontal scrollview and have a same result?
<HorizontalScrollView
android:id="#+id/HSV"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/back"
android:paddingLeft="2.5dp"
android:paddingRight="2.5dp"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</HorizontalScrollView>
with the above code I have the desired output, but as I remove the HorizontalScrollView The image is compressing in width and loose its shape I want the image scroll as I scroll the recyclerview
That is The picture I want:
![in Background is the aforementioned image and this eight Item are in recycler view and I want as the items finishing the background image is finishing too.
]1
Design like this in layout.And pass layout of card in image to adapter in Android.Set background to Constraint layout ore Recyclerview. When you have Recyclerview no need to add Horizontal Scrollview. Just pass Layout manager as Horizontal
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#color/secondaryColor"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>idget.ConstraintLayout>
I wonder if I have got your query right, so you want your RecyclerView to be scrolling horizontally. I would suggest to use the LinearLayoutManager for the same, so that you can pass the Scrolling constant to Horizontal in the arguments of LinearLayoutManager.
To read about it, please go ahead and read it LinearLayoutManager
And when you get it, just do it like this in you Java Class.
RecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
I have this youtube link for you too, if you don't get it what you want. Here you will get the full demo of how to do it, and achieve it. Horizontal RecyclerView
I hope that will answer your query. Thanks :)
I'm facing a tricky situation here and I don't know how to solve this problem.
In my project I have a custom BottomSheetDialogFragment and in the layout a FrameLayout to add or replace Fragments.
Now I have a Fragment and inside I have a RecyclerView with the height:="wrap_content" because I want the BottomSheetDialogFragment only use the necessary space. Everything looks great, the problem appear when I put another view inside of the same layout and set the RecyclerView bellow or above of that view.
The RecyclerView ignores the size of the other view (or views) and always grows to the max screen size, and then it's no possible to see a few elements and even scroll.
I saw a solution, some developers are suggesting to add paddingBottom equals to the height of the view. But in my case doesn't works because I want to have a dynamic solution.
Above I'll share a few images of the problem and GitHub Repository with a sample.
Thanks for your attention!
I've manage to do what you need just need to use this as your fragment_sample.xml:
<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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rclItems"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
<Button
android:id="#+id/btnAddMoreItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/rclItems"
android:text="#string/add_1_item"/>
</LinearLayout>
Explanation
Using a LinearLayout gives you the ability to work with weight, and the vertical orientation allows you to place an item below the other. The weight on the recyclerview will increase the height of it as needed until filling the screen. The next item you add would be added to the recyclerview but you'll need to scroll the list to see it
The android developers blog says that :-
The scrolling containers in your bottom sheet must support nested scrolling .
Try changing your fragment_sample.xml as below to make the recyclerview scroll working and to make the add button persistent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="wrap_content">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="#+id/next"
android:layout_above="#id/btnAddMoreItems"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/rclItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.v4.widget.NestedScrollView>
<Button
android:id="#+id/btnAddMoreItems"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:text="#string/add_1_item"/>
</RelativeLayout>
Note: making bottomsheet layout a child view of CoordinatorLayout will allow you to get the implement BottomSheetBehavior and recieve its transitions callbacks .
I have a listView which takes data from a rest API server and displays it. I need to add a textView on the side of it describing the type of data.
The right half of the screen has the listView and the left half has the textView.
The problem arises when the textView moves to accommodate the different screen sizes. I need the textView to be fixed and to be shown in conjunction with listView, and be in a straight line with the listView, regardless of the screen size. Cheers.
Edit: Image added of the sketch
It sounds like a ListView is not what you want to use. You cannot align Views with the children of a ListView. You would need to either put the TextViews within the ListView's children or not use a ListView.
The simplest way to do this would be a vertical LinearLayout containing many horizontal LinearLayouts.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"/>
<Item that was in the ListView />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"/>
<Item that was in the ListView />
</LinearLayout>
...
</LinearLayout>
The ListView is really for scrolling and when you don't know how many items there will be.
Perhaps you already have an adapter in which you are handling the creation of the items and that's why you are using a ListView. You can still create the LinearLayout's children programmatically by inflating each item individually and adding it to the container view.
Original Answer:
You probably want the TextViews to scroll with the ListView as well. So I would think you should use one ListView, then add the TextView to the left side of the ListView item.
You should use weightSum,
Here is an example.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="horizontal">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World"/>
</LinearLayout>