Android ListView inside ListView Cut the data - android

In android application I use listview inside listview,
But the list view cut the data
the item is cut of
the other items I don't see at all
Here is the image shown that the single item is cut of, and the second item does not shown at all.
Any idea how can I change the height to be dynamic with the content
Thanks
First List View Item
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:padding="10dp">
<ImageView
android:id="#+id/approvedIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="5dp"
android:gravity="center_vertical"
app:srcCompat="#android:drawable/btn_star_big_on" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/travelLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/travelDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/countCompanies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/companiesListView"
/>
</LinearLayout>
</LinearLayout>
Second list view item
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal">
<TextView
android:id="#+id/cidTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:layout_marginRight="3dp"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="TextView" />
<TextView
android:id="#+id/comapnyAddedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="TextView" />
<Button
android:id="#+id/confirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:text="Accept" />
</LinearLayout>

First use RecyclerView.
If you show the XML it will be helpful.
For the dynamic height use wrap_content for the text

Related

Height of ListView in dialog in android not set as per the number of items in ListView; results in blank space when there are less number of options

Result is appropriate when there are enough options; image-> result is ok, But when there are less options in listview then there will be empty space; which I don't want to be there.image-> not ok result
I want to set the height like, when there are less options, there should not be any blank space bw. cancel button and list view.
Code for list_view.xml
<?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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:fontFamily="#font/quicksand_regular"
android:text="Please select country"
android:textAlignment="center"
android:textSize="14sp" />
<ListView
android:id="#+id/dialogList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:background="#drawable/listview_background"
android:choiceMode="singleChoice"></ListView>
<Button
android:id="#+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:background="#drawable/button_background"
android:fontFamily="#font/quicksand_bold"
android:gravity="center_horizontal"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:text="cancel"
android:textColor="#fff" />
</LinearLayout>
</RelativeLayout>
code for list_item.xml
<?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"
android:orientation="vertical">
<com.rohan.myvoice.CustomTextView
android:id="#+id/textViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Text"
android:textAlignment="center"
android:textColor="#drawable/option_select_color"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#color/line_light" />
</LinearLayout>
I can't use the other views like fragment and such, as then there will be lot of changes in my code.
You set the layout_weight to 10 which is not required here for list view we just want to use wrap_content in layout_height:-
<ListView
android:id="#+id/dialogList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:background="#drawable/listview_background"
android:choiceMode="singleChoice"></ListView>
Please check by Trying Recycler view instead of List View using my code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title_textView"
android:layout_width="250dp"
android:layout_height="30dp"
android:gravity="center"
android:layout_marginBottom="10dp"
android:text="Please select country"
android:textAlignment="center"
android:textSize="14sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/dialogList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" />
<Button
android:id="#+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:background="#drawable/button_background"
android:gravity="center"
android:text="cancel"
android:textColor="#fff" />
</LinearLayout>

Placing A ListView Below Other View in Android

I have been trying to Place A listview under some otherviews in Android.
For Example.
I have a Scrollview which I Place Other View Inside Including The ListView
I Want the List View to be Long as Long As 1000dp so that User can Scroll to the Listview and the Scroll the List Inside a Layout.
I have been on this for 2 days. Any Help will be highly appreciated.
This is My Code
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="50dip"
>
<TextView
android:id="#+id/tvheading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="#string/title_AllSensorhead"
android:textStyle="bold" />
<View
style="#style/Divider"
android:layout_above="#+id/listView" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:indeterminateDrawable="#drawable/progress"
android:visibility="gone" />
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/textSensorName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textSensorNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="1000dip"
android:layout_marginTop="5dip"
>
</ListView>
</LinearLayout>
Use a scroll view to enclose all the android xml file....the defining layout should be scrollable instead of linear layout....
there can be one possible solution for your problem..
As far i guess you just want to scroll whole screen that have a listview..
So, First create a different layout name like header_listview.xml
in that put your code apart of that listview in it..
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="50dip"
>
<TextView
android:id="#+id/tvheading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="2dp"
android:gravity="center"
android:text="#string/title_AllSensorhead"
android:textStyle="bold" />
<View
style="#style/Divider"
android:layout_above="#+id/listView" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:indeterminateDrawable="#drawable/progress"
android:visibility="gone" />
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/textSensorName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textSensorNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white"
android:textSize="15dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Now your listview layout can be simply like
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="1000dip"
android:layout_marginTop="5dip"
/>
In your java file when fatching the instance of list..
Add two line there
View headerView= LayoutInflater.from(this).inflate(R.layout.header_listview,null,false);
listView.addHeaderView(headerView;
You can get those TextViews & progressbar & chart from this headerview..like
TextView txt=(TextView) headerView.findViewById(R.id.tvheading);
from this approach the violation of scrollview with listivew wont be arrive,
Also you dont need to give 1000dip fix height to listview..
Please tell if need more assistance..
And appreciate if it helps ..

Listview items spreading till full width of screen

I use a listview to chat messages between two users. I want to set the background color of chat messages sent by the receiver as blue and some other color for the sender. I also want to limit the size of the listview item to wrap_content but it doesn't seems to be working, hence the background color spreads all across the width of the item.
Following is the code of the activity which contains listview:
<FrameLayout 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:background="#drawable/bg"
tools:context=".ChatScreen">
<include layout="#layout/toolbar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<io.codetail.widget.RevealFrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lMsgs"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/editText"
android:divider="#null"/>
<EditText
android:text=""
android:layout_width="wrap_content"
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/bSendMsg" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sendd"
android:id="#+id/bSendMsg"
android:layout_below="#+id/lMsgs"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<include layout="#layout/media_attach_menu" />
</io.codetail.widget.RevealFrameLayout>
</FrameLayout>
Code for listview item:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="left"
android:id="#+id/RelativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:text="Username"
android:id="#+id/tvUsername"
android:layout_alignParentTop="true"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvUsername"
android:id="#+id/innerRL">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="18sp"
android:textStyle="bold"
android:layout_alignParentTop="false"
android:text="Message"
/>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="gone"
android:contentDescription="#string/image"
android:scaleType="fitCenter"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:minHeight="50dp"
android:minWidth="50dp"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="14sp"
android:text="Timestamp"
android:id="#+id/tvTime"
android:layout_below="#id/innerRL"
/>
</RelativeLayout>
You set android:background="#drawable/bg" into your parentlayout means FrameLayout therefore it apply to your all view not single row remove from there and you can apply this background to your listview item layout
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:background="#drawable/yourbackground"
android:id="#+id/RelativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:text="Username"
android:id="#+id/tvUsername"
android:layout_alignParentTop="true"
/>
----

Align checkbox right most side in a listview

I want to allign the checkboxes in a list view to right most part of the screen.
my Layout is shown below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="vertical">
<TextView
android:id="#+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="30sp" />
<TextView
android:id="#+id/numberTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number"
android:textSize="25sp" />
</LinearLayout>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_weight="2"
android:gravity="right|center" />
But When i run this i get the output as checkboxes right next to the textview
Found mistake.. its in mainactivity.. i have put the the width of listview as wrap content instead of fill parent
Use RelativeLayout for ChechBox Section and Add android:layout_alignParentRight="true" .Try this way ,Let me inform .
Okay,You can set 2 Textview and ChechBox on same row using layout weight (Linear Layout). And set gravity,layout gravity right for ChechBox.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="30sp" />
<TextView
android:id="#+id/numberTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number"
android:textSize="25sp" />
</LinearLayout>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Check this one
<?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="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="30sp" />
<TextView
android:id="#+id/numberTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number"
android:textSize="25sp" />
</LinearLayout>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_marginRight="2dp"
android:gravity="right|center" />
</LinearLayout>

Android - LinearLayout like a table

I have a ListFragment that fills a LinearLayout with data.
In this layout I'm trying to make them looking like a table.
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/text_primary" >
<ImageView
android:id="#+id/homeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/home"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="25dp" />
<TextView
android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20dp" />
<TextView
android:id="#+id/visitor"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="25dp" />
<ImageView
android:id="#+id/visitorImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"/>
</LinearLayout>
</LinearLayout>
But I would like to make something like this:
Where the images, the teams and the score will show perfectly aligned and centered.
I'm new in android, and I'm having problems to make this working.
My question is how can I make this working like the picture?
I have modified your xml to the below. Since you need list of them, use List view and give the below layout as each list item layout. Tutorial to make list view is here. All the elements within the layout are simple view (you can modify this to ImageView with your image), TextView and hence this will not be editable by the user (to give the picture kind of effect).
Will this be useful to you?
<?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="100dp"
android:orientation="horizontal" >
<View
android:id="#+id/homeImage"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"/>
<TextView
android:id="#+id/home"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="25dp"
android:text="TEAM ONE"/>
<TextView
android:id="#+id/score"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20dp"
android:text="X - X"
/>
<TextView
android:id="#+id/visitor"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="25dp"
android:text="TEAM TWO"/>
<View
android:id="#+id/visitorImage"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/holo_blue_bright"/>
</LinearLayout>

Categories

Resources