Programmatically add Views inside a (Linear)Layout (that is inside a ScrollView) - android

I have an app that, after some clicking, shows activity with news contents. I want at a bottom of that to show comments, which are dynamically loaded in async task.
One approach is to use ListView and custom ArrayAdapter, but, i would have to put ListView inside a ScrollView, and it is is a problem, even if i manually override ListView's height. It shows one list item, and one part of the following one.
The other approach is to define LinearLayout as a holder for comments, than inflate another LinearLayouts defined in it's own xml file, populate it's contents, attach to holder's view. It works fine, from program's point of view, except it pushes comments below contents of the news. It's like, it creates another scrollable view of comments underneath contents od news (contents overlap it).
Is there any other way of doing this that has nothing to do with lists, or how can i make the other approach to work.
The relevant code snippet from xml layout that holds news contents is:
<LinearLayout> //global layout
<LinearLayout>
//views that hold title, date, content
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/commentsHolder"
android:orientation="vertical">
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#ed1b24"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="KOMENTARI"
android:layout_marginLeft="5dp"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#ed1b24"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:id="#+id/comments_holder_view"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</LinearLayout>
and code that corresponds to one comment is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comments_holder_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/comment_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/comments_back_details"
android:text="Ovde ide tekst komentara, koji se, naravno, dinamicki dodaje."/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/comment_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/categoryStyle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" | "
style="#style/categoryStyle"
/>
<TextView
android:id="#+id/comment_pubdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/categoryStyle"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:orientation="horizontal"
android:layout_margin="5dp"
android:background="#d1d1d1">
</LinearLayout>
Thank you very much for any reply. This is quite important for me.

I am not very sure how you inflate your comments, but here is how I would do it.
Declare the comments Layout as a LinearLayout and give it an id, and then get a reference to that LinearLayout :
LinearLayout commentsLayout=(LinearLayout)findViewById(R.id.commentsLayoutId);
and then just add child Views to this layout :
commentsLayout.addView(newComment);

Related

Create a clickable item like button with a particuliar view inside

I'm starting in android but I'm working on a project that asks me to do the same thing as the pictures below. However I don't know how to analyze this view to be able to produce something similar.
From what I see in the activity I have a "button" and when I click on it, the elements present in the "button" change. Could someone explain to me how to cut out the view and the elements to do the same thing? Thank you in advance for your answer.
Pictures :
based view
view after click on "button"
I gonna give you a rough idea..
its not a button its a layout containing 2 textviews, 2 imageviews and one seekbar and i will suggest you to use a linearlayout.add a clicklistner on it.
to change the elements after clicking on layout you have to get the references of its elements textview etc as mentioned and change them by yourself. ex- seekbar.setprogress(30) approximately as your picture after click.
xml..
Linearlayout
textview
imageview and textview
imageview
seekbar
\Linearlayout
#Miina Neko Use card view inside cardview use a linear layout (or anyother layout you like) inside cardview use below code i made a layout for you
<androidx.cardview.widget.CardView
app:cardCornerRadius="#dimen/_5sdp"
app:cardElevation="#dimen/_5sdp"
app:cardBackgroundColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:padding="#dimen/_5sdp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/refresh_btn"
android:layout_width="#dimen/_10sdp"
android:layout_height="match_parent" />
<TextView
android:layout_marginLeft="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="arrrarrr-raaar"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="#dimen/_3sdp"
android:layout_marginBottom="#dimen/_3sdp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_marginRight="#dimen/_5sdp"
android:src="#drawable/refresh_btn"
android:layout_width="#dimen/_10sdp"
android:layout_height="#dimen/_10sdp" />
<ImageView
android:layout_marginLeft="#dimen/_5sdp"
android:src="#drawable/refresh_btn"
android:layout_width="#dimen/_10sdp"
android:layout_height="#dimen/_10sdp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:layout_marginRight="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_5sdp"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminateDrawable="#android:drawable/progress_indeterminate_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
NOTE: "#dimen/_sdp" is a library i am sharing link and chage your drawable accrouding to your choice and make ids and in java or kotlin change then programmatically when clicked or any action performed like in second picture hope answer your question..
SDP library link https://github.com/intuit/sdp

list view taking up more space than needed

This is my custom dialog which consist of the following
list view,button and finally a textview used when the list is empty.
My problem is the listview seems to take up more space than it needs to when only 1 item exists.
The image below shows this.
My XML code
<?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" >
<ListView
android:id="#+id/lvRaffles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bCloseDialog"
android:layout_alignParentTop="true" >
</ListView>
<Button
android:id="#+id/bCloseDialog"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/alert_dialog_button_style"
android:text="Close" />
<TextView
android:id="#+id/tvRaffleEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="No Raffle Code Found With That Search"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="gone" />
</RelativeLayout>
I have tried the following.
List View height on wrap_content, 0dp does not help.
I know you fixed this with LinearLayout but to answer your question, the reason it appears as if your listview is taking up too much space is because you used layout_alignParentBottom="true" if you want the button to appear below the listview you would need to use layout_below="#id/lvRaffles" You also have two elements aligned to parentTop, which can cause the elements to overlap.

Android - List view with single choice radio buttons

I have a list view with 2 radio button items. When inflated it from a layout, it is happening to be scrollable even if there is a lot of space as shown below.
As shown in screenshot, I have 2 radio buttons.
1. Distance
2. Rating
But the "rating" radio button is not visible and I have to scroll to see it. Why is this happening. I tried to setting the layout height to wrap content, fill parent and match parent. But that didn't solve the problem. Any idea why is this happening?
here i am referencing the list view :
ListView radio_list = (ListView) view.findViewById(R.id.RadioList);
radio_list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
radio_list_items));
radio_list.setItemsCanFocus(true);
radio_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
my xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SEARCH"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:id="#+id/searchTextLayout"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/searchTextButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#685E5C"
android:contentDescription="Sample"
android:scaleType="fitCenter"
android:src="#drawable/abs__ic_search" />
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/searchTextButton"
android:background="#drawable/background_black_border_full"
android:padding="8dp"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SORT BY"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/RadioList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</LinearLayout>
Your layout xml file is weird,
first of all you should simplify it :
there is no point of having one single element in a LinearLayout => it means the corresponding linear layout is useless.
second point : if there is 2 child in a linear layout, and the first one has a height of "match_parent" without any layout_weight set, the second one will be pushed out because there isn't any space left. ( its probably your problem here)
third point : your are using a relative layout as a linearlayout, proof is the android:orientation parameter you try to use on it. it is useless. position of relative layout childs are given by relative positioning between them. in your case you can use a LinearLayout with orientation horizontal instead of the RelativeLayout
best advise to help you : simplify your layout xml.
More on this topic.

can't view a listview inside a listview correctly in android app

I want to display data from a very generic JSON string in my android app. One view/screen shall display an arbitrary number of "details" where each detail has an arbitrary number of "values".
I'm trying to use a ListView inside a Listview, and all the data is nicely put into each list, but the lists won't display fully (see image below).
In the lower part of the screen is the first ListView. It contains a number of TextView labels (e.g. Usage and Info), which in turn has a number of values (units, sw version etc). The values is put in a new list which displays a TextView.
The top part is also a ListView, but is displayed ok.
Looks like this:
First part, status tab with the two lists, first (fine) and second (problem):
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="#+id/tabStatus">
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:id="#+id/listThingStatus"/>
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:id="#+id/listThingDetails"/>
</LinearLayout>
The lower ListView with the label and the second ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="70px">
<LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:id="#+id/textDetails" />
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/listThingValues"/>
</LinearLayout>
</LinearLayout>
And finally the TextViews inside the second ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="70px">
<LinearLayout android:orientation="horizontal" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center_vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:id="#+id/textValuesLabel" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30px" android:gravity="right|center" android:id="#+id/textValuesUnits" />
</LinearLayout>
</LinearLayout>
The result is this (see image), where the content in the inner lists is mostly "hidden" (SW version and text below etc). I use "wrap_content" almost everywhere but the content is too wrapped here :(. How and where do I format my XML to make the content in the inner lists display properly?
Image (from emulator, but same result on phone):
http://i.imgur.com/8mcDq.png
Placing a ListView inside a ListView will only bring you headaches.
Try using an ExpandableListView instead.

How to hide listview dividers dynamically?

I have this ListView whose items i'd like to hide depending on the selection of a RadioGroup. Currently I'm passing a boolean to the ListAdapter due to the RadioGroup having only two options. My items contain a checkbox and i want to either show the entire list or just the ones with the check boxes checked. I'm succeeding at hiding the items but the dividers still show, how can i fix this?
Look how it looks like
http://www.mediafire.com/i/?wa2s0ngq027vjwr
http://www.mediafire.com/i/?9i6ggj2fdsns2da
(I'm new, so i can't upload images here)
The xml for my row would be:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="1dip" android:gravity="center_vertical"
android:background="#FFF">
<CheckBox android:id="#+id/dispositivo_tv"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000000" android:textSize="15dip"
android:layout_alignParentLeft="true" />
<LinearLayout android:id="#+id/botones"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:gravity="center_vertical">
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/button_foto"
android:src="#drawable/camera" android:background="#FFF"
android:paddingRight="15dip" android:visibility="invisible"></ImageButton>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/button_comentario"
android:src="#drawable/comment_add" android:background="#FFF"
android:paddingRight="15dip"></ImageButton>
</LinearLayout>
</RelativeLayout>
and the xml block for the ListView would be:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal"
android:padding="5dip" android:background="#layout/list_box">
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:cacheColorHint="#00000000"
android:headerDividersEnabled="false" android:footerDividersEnabled="false
</ListView>
</LinearLayout>
and what i use to hide the row when the boolean i told you about is set FALSE is:
wrapper.getDispositivo().setVisibility(View.GONE);
wrapper.getFoto().setVisibility(View.GONE);
wrapper.getComentario().setVisibility(View.GONE);
PS: wrapper is the instance of the class where i have all the elements of the row, i.e. the checkbox (getDispositivo()), and a couple of image buttons (getFoto(), getComentario())
Thanks in advance...
How about using custom dividers in your relative layout and setDivider(null); so once you hide the layout the dividers are hidden as well. I wanted to actually add this as a comment. But it comes only after 50 reps so had to put it as a answer.

Categories

Resources