I am doing adaptation of my app for Android TV.
I have RecyclerView with items. Each item includes three LinearLayouts.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/LLChannel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/item_touch_selector"
android:paddingLeft="#dimen/list_item_extra_padding"
android:paddingRight="#dimen/list_item_extra_padding"
android:weightSum="1"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/item_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.8"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="#dimen/abc_list_item_padding_horizontal_material"
android:paddingRight="#dimen/abc_list_item_padding_horizontal_material"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/LLFav"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.1"
>
</LinearLayout>
<LinearLayout
android:id="#+id/LLURL"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="0.1"
>
</LinearLayout>
</LinearLayout>
My question is: can I focus these three LinearLayouts separately instead of full item (LLChannel) focus? How?
UPD 17:12 10.07.2020:
Adding this stuff for each LinearLayout didn't help me:
android:background="#drawable/item_touch_selector"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusRight="#id/LLFav"
UPD 12.07.2020: Any ideas how it would be reached?
UPD 13.07.2020: This was easy. I will answer on my own question later.
All of LinearLayouts must be wraped in one more LinearLayout.
Related
I tried many many answers, but I think my ScrollView includes much more elements.
ScrollView doesn't scroll to the bottom
Android scroll view does not scroll all the way down
Even I tried with a NestedScrollView, margin bottom, padding, even with android:descendantFocusability="blocksDescendants" on child layout, and another big linear layout that includes every element in ScrollView, but no results...
The problem is that my ScrollView doesn't scroll to the bottom of it's
view. I have to say that the scrollview is below an horizontal
guideline that it's set at 35%, maybe could be one of the reasons to
this abbomination?
I'll insert a sample of XML code
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.35" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline"
app:layout_constraintVertical_bias="0.0">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linearLayout_dummy1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:orientation="horizontal" />
<!-- This is the part that's 85% of the total width.-->
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayoutInside"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/anotherlayout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:background="#drawable/rounded_shape"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.26"
android:orientation="horizontal">
....
....
much more linear and constraints layouts that opens then closes
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--Width 85% ends-->
<LinearLayout
android:id="#+id/linearLayout_dummy2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:orientation="horizontal" />
</LinearLayout>
</ScrollView>
Also, here's a "image"( don't laugh - not too hard :) ) of how the display would look without scrolling. If I would scroll it wouldn't reach it's bottom:
Your have some errors in your xml layout. Two of the close tags for linearlayout are missing and your scrollview should be inside the guideline widget.
Background
I have a header and footer (LinearLayout) with a RecyclerView between them.
The problem
I need that the footer stay fixed at the bottom of the screen and the RecyclerView fills the empty space
What I've tried
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context="br.com.sigane.coletordifal.activity.EnderecamentoActivity">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/produtos_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Footer" />
</LinearLayout>
</LinearLayout>
Also tried to use ConstraintLayout, but without success
Try this
Add android:layout_height="0dp" and android:layout_weight="1" to the RecyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/produtos_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Footer" />
</LinearLayout>
</LinearLayout>
I don't think you need to have separate linear layouts. I think just one is enough. Just set your weights like this for your recyclerview and footer.
<android.support.v7.widget.RecyclerView
android:id="#+id/produtos_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:scrollbars="vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Footer" />
I recommend using the constraint layout, it is a FrameLayout on steroids. If you are using android studio 3.2, right-clicking on the root element in the design view shows a menu with the option "convert to constraintlayout"
The reason you do not want to nest layouts like this, Each nested element results in additional measure passes, nested layouts are messy and harder to inflate than flat layouts.
Highly recommend spending an hour or so reading about the constraint layout and how to use it. You will not regret it, helped my improve productivity by 2x.
Hope this is helpful.
I set the ScrollView in my LinearLayout. I want a TextView to scroll. I tried to use ScrollView in xml like this. But, it's not working.
<LinearLayout 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:orientation="vertical"
android:padding="8dp"
tools:context=".SubpagesLayout.ReportResult">
<!-- first scrollView -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_corner_blue"
android:gravity="center"
android:text="#string/personalInformation"
android:textColor="#android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/birthDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" TestData" />
</LinearLayout>
<!-- second scrollView -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textOphthalmoscopy"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." />
</ScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
I tried another way like this, but it's still not working too.
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
I used
myTextView.setMovementMethod(new ScrollingMovementMethod());
So how to let TextView to scroll? If it is the second ScrollView.
You might consider using a NestedScrollView along with a ListView for showing the scrollable TextView.
The ListView will have a fixed size so that it'll not take the total height of the items inside.
So the layout may look like this.
<LinearLayout 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:orientation="vertical"
android:padding="8dp"
tools:context=".SubpagesLayout.ReportResult">
<!-- first scrollView -->
<NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_corner_blue"
android:gravity="center"
android:text="#string/personalInformation"
android:textColor="#android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/birthDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" TestData" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
<!--set a fixed height-->
android:layout_height="80dp"
.. Other attributes
</ListView>
</LinearLayout>
</NestedScrollView>
</LinearLayout>
And the list item of the ListView may contain the TextView you want to scroll. Set the height of the TextView to wrap_content.
<LinearLayout 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:orientation="vertical"
android:padding="8dp"
tools:context=".SubpagesLayout.ReportResult">
<TextView
android:id="#+id/textOphthalmoscopy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." />
</LinearLayout>
Now your ListView will have a single item and this will scroll with the default behaviour of list.
The whole layout will be scrolled via NestedScrollView.
Hope that helps!
Having a scrollView inside another scrollView is not a good prictice but ant a you check below links it may fix you problem.
Solution 1
Solution 2
I have a layout with listview and two buttons side by side at the bottom of the listview. Everything work fine until I have added the swiperefreshlayout for my listview.
My codes are as below.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFF"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/search_bar"
android:background="#color/colorPrimaryDark"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
>
<EditText
android:id="#+id/keyword_editText"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#drawable/search_edit_text"
android:hint="Keyword"
android:textColorHint="#color/list_item_title"
android:textColor="#000"
android:layout_centerVertical="true"
android:maxLines="1"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView_layout"
android:orientation="vertical"
android:layout_below="#+id/search_bar"
>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/RsListView"
android:dividerHeight="1dp"
android:divider="#D3D3D3"/>
</android.support.v4.widget.SwipeRefreshLayout>
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/req_history_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Request History"
android:layout_weight="1"
android:background="#drawable/request_history_button"
android:textColor="#drawable/button_text_color"
/>
<Button
android:id="#+id/apprv_history_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Approval History"
android:layout_weight="1"
android:background="#drawable/approval_history_button"
android:textColor="#drawable/button_text_color"
/>
</LinearLayout>
</LinearLayout>
Sorry for dumping my codes here as I can't figure out any other way for you guys to understand my code.
Please help
Inside LinearLayout usually all children have even dimension according to orientation. However, when you would like one child to take a little amount of place and another to fill the rest, you might be interested in layout_weight parameter. If the child that should fill the space gets android:layout_weight="1" it will fill the space making a place for other views.
In order to see the buttons add android:layout_weight="1" to SwipeRefreshLayout and set android:layout_height="0dp". That will make your buttons visible.
I have a problem in edit text its not getting expanded as I type.
I wanted it to show a maximum of 4 lines but when I click the exittext to type something, its shrinking, not even completely showing a single line.
the code used is.
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Comments"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.28"
android:background="#000000"
android:maxLines="1"
android:paddingEnd="20dp"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp"
android:layout_weight="10">
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="11">
<ListView
android:id="#+id/list2"
android:drawSelectorOnTop="false"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:footerDividersEnabled="true">
</ListView>
</FrameLayout>
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/linearLayout">
<EditText
android:id="#+id/enter_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:layout_weight="1"
android:inputType="textMultiLine"
android:maxLines="4"
android:minLines="1"
android:textColor="#android:color/black"
/>
<Button
android:id="#+id/send_button"
android:layout_width="45dp"
android:layout_height="30dp"
android:onClick="sendMessage"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
screen shot of whats happening..
thank you..
You have two framelayouts nested in a linearlayout and they are weighted. The first nested framelayout has a weight of 11
compared to 1. So the edittext will not be able to expand beyond it's the weighted width for the framelayout.
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp"
android:layout_weight="10">
The nested frames.
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="11">
<FrameLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
The total parent layout has been broken in 12 parts. The top frame has 11, the bottom 1. 1 is not much. There is not enough room to display all the text, plus you have a vertical scrollbar. The text will be added and be scrolling as you type. It would be impossible to read as it scrolls in such a short space.
I would even suggest not setting the weight for the bottom frame (it defaults to 0) and then try setting the top one to say 3? and increase it to see what works for you. Please try this, before commenting again.