Adding 25+ textviews programmatically to 5 linearlayouts in numerical order - android-studio

Here i have 25+ names and i have created 5 linearlayouts which have vertical orientation.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical
android:layout_weight="1"
android:id="#+id/ll_one" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:id="#+id/ll_two" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:id="#+id/ll_three" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:id="#+id/ll_four" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:id="#+id/ll_five" />
</LinearLayout>
and i have names data in arraylist i. e.
ArrayList<String> data = new ArrayList<>();
data.add("1. temp1");
data.add("2. temp2");
data.add("3. temp3");
...
data.add("25. temp25");
now i want to set this arraylist strings into textviews and add those textviews in above linearlayouts like i want to add 1. temp1 textview into first linearlayout, 2. temp2 textview into second linearlayout, 3. temp3 textview into third linearlayout, 4. temp4 textview into fourth linearlayout , 5. temp5 textview into fifth linearlayout, 6. temp6 textview into first linearlayout, 7. temp7 textview into second linearlayout, and so on like this...

Related

Why does the ScrollView leave a lot of unnecessary space when scrolling down?

My content should end on a button. That is, the button is the last bottom element.
Since there is a lot of text I had to use ScrollView. And when scrolling down, the button goes up and starts to go empty space.
I tried to set the height of the ScrollView to wrap_content. Nothing has changed.
<androidx.core.widget.NestedScrollView
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:theme="#style/ThemeOverlay.Smoke.FullscreenContainer"
tools:context=".LectureFragment"
android:background="#color/dark_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#drawable/day1"
android:layout_weight="20"
android:layout_marginEnd="#dimen/margin_regarding_the_parent"
android:layout_marginStart="#dimen/margin_regarding_the_parent"
android:layout_marginBottom="#dimen/margin_between_something"
android:scaleType="centerInside"
/>
<TextView
android:id="#+id/textView"
android:text="#string/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/margin_regarding_the_parent"
android:layout_marginStart="#dimen/margin_regarding_the_parent"
android:layout_weight="10"
android:textColor="#color/white" />
<com.google.android.material.button.MaterialButton
android:text="#string/submit_lecture_rus"
android:layout_width="match_parent"
android:layout_height="#dimen/height_button"
android:layout_marginEnd="#dimen/margin_regarding_the_parent"
android:layout_marginStart="#dimen/margin_regarding_the_parent"
android:layout_marginBottom="#dimen/margin_regarding_the_parent"
android:backgroundTint="#color/coral"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
you probably can set the height of the Linear layout to be wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="100">
...
</LinearLayout>

Child Views do not expand to the same height as their parent

I've got the following setup:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/list_item"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
<!--otherwise nothing important is happening here -->
</LinearLayout>
<LinearLayout
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/list_item"
android:layout_alignBottom="#+id/list_item"
android:visibility="gone"
android:orientation="horizontal" >
<TextView
android:id="#+id/undo_acknowledge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="fill_vertical|start"
android:layout_weight="0.5"
android:textSize="20sp"
android:text="#string/undo"
android:singleLine="false"
android:textColor="#color/black" />
<TextView
android:id="#+id/acknowledge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="fill_vertical|end"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textSize="20sp"
android:text="#string/delete"
android:singleLine="false"
android:textColor="#color/black" />
</LinearLayout>
</RelativeLayout>
Some background:
I've got a Recycler View containing these layout as list items. When I slide an item, the LinearLayout (I've tried also RealtiveLayout) "sliding_layout" becomes visible and it's two TextViews appear.
The "list_item" layout's height is calculated using wrap_content and therefore the "sliding_layout" uses
android:layout_alignTop="#+id/list_item"
android:layout_alignBottom="#+id/list_item"
to match it's height.
Up to this point it works fine. But I want the two child TextViews of "sliding_layout" to have the same height as their parent (and therefore as "list_item"). As you can see, I tried to achieve this by multiple ways (all of them separate and combined, as far as possible):
android:layout_height="match_parent"
android:gravity="fill_vertical"
android:layout_alignParentTop="true" & android:layout_alignParentBottom="true" (with RealtiveLayout as parent)
But none of those seems to do anything: The vertical size of the TextViews seems to be calculated according to the text size.
My Build configuration is the following:
minSdkVersion 16
targetSdkVersion 21
Do you have any suggestions what I could do to achieve getting my TextViews the same height as their parent?

android layout - need to match the height of a table row to match width using layout weights

I want to display 18 buttons in a grid 0f 6x3. I am using six table rows and each row has 3 buttons. the width of each button is set to 0dp and given a layout_weight=1.
as the background image I want to use is a circular object I need the width and height of the button to be same! How can I achieve this.
here is the basic xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tableRow">
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageButton4"
android:layout_weight="1"
android:background="#drawable/sh_commonroom" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageButton5"
android:layout_weight="1"
android:background="#drawable/sh_bedroom" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageButton6"
android:layout_weight="1"
android:background="#drawable/sh_kitchen" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
........................
Currently it looks like this.

Unable to right-align text in Android ListView row layout

I am trying to design a ListView row layout with the following:
A check box
An image
3 text items. 2 at the top, 1 at the bottom. Where one of the top text items is right aligned.
An image
It would look something like this:
Chk Img Left aligned small text Right aligned small text Img
Box Btn1 Left aligned medium text Btn2
No matter what I try, I cannot get the right aligned text to work properly. If I put the 2 top text items into a RelativeLayout and set the 2nd one to android:layout_alignParentRight="true" it gets aligned to the edge of the row, overlaying the 2nd image. The following is one of my many attempts:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/cbSelect"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="#+id/imgType"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_toRightOf="#id/cbSelect"
android:src="#android:drawable/btn_star_big_on"/>
<LinearLayout
android:id="#+id/itmDetails"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_toRightOf="#id/imgType"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="8dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtTopLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textSize="12sp"
android:text="Top Left"/>
<TextView
android:id="#+id/txtTopRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#android:color/black"
android:textSize="12sp"
android:text="Top Right"/>
</RelativeLayout>
<TextView
android:id="#+id/txtMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Item Text"
android:textColor="#android:color/black"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:id="#+id/imgStar"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:src="#android:drawable/btn_star"/>
</RelativeLayout>
Any suggestions?

Android-Aligning a element + margin with relative to parent element

To align a element with respect to an other element using their I can use.
android:layout_alignLeft etc..
But how to add a margin or a padding:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/students"
android:textSize="30sp"
android:typeface="normal"
android:layout_marginTop="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:id="#+id/student" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/student"
android:paddingLeft="20dp"
android:paddingTop="30dp"
android:text="#string/stud_describe"
android:textSize="10sp"
android:textStyle="italic"
android:typeface="sans" />
</RelativeLayout>
Padding top in the second TextView is not having any effect.
Looks like you're getting padding and margin mixed up. Change the second TextView to:
layout_marginTop="30dp"
instead of
paddingTop="30dp".
Padding pads the View itself, making it bigger. Margin changes the position of the TextView within its container (RelativeLayout) and doesn't alter its size.

Resources