Displaying a ListView at the bottom of an activity - android

I'm new to developing on Android, so bear with me. I have an activity that displays some weather data for today's date, and I want to have a list of more weather data at the bottom of the activity for the next 7 days, like this:
However, I'm confused about what the best way to achieve this is. I was thinking of using a ListView but I'm confused about how to use them. From what I've gathered, I need to create a ListView layout in res/layout and then use a fragment on my main activity, which has its layout set to the ListView layout I just made. Is that correct? Or would an easier solution be to just have a ListView placed at the bottom of my main activity?
Or, am I using the completely wrong control for what I want to do?

You can make this layout very easily by this code:
<Relativelayout...>
<EditText android="#+id/edit1"
android:alignParentTop="true"
... />
<LinearLayout android:id="#+id/YOUR_MIDDLE_DATA"
android:above="#+id/listview1"
android:below="#+id/edit1" ...>
...
...
</Linearlayout>
<Listview android:id="#+id/listview1"
android:above="#+id/txt1"
... />
<Textview android:id="#+id/txt1"
...
android:alignParentBottom="true" />
</RelativeLayout>
You can use weight also for your customization.
Hope this will help you.

Consider the XML that follows as an example. In this (an actual XML from an app of mine), I have a smaller EditText above the ListView, but you can change it to something else that you would need. What this essentially illustrates is that you can have a ListView along with some other content at the top or at the bottom. (I have the Admob code after the ListView which I have removed)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/phone_thing_comment_bg"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="9dp"
android:paddingRight="9dp"
android:paddingTop="5dp" >
<EditText
android:id="#+id/editFilterList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#drawable/phone_thing_comment_box"
android:drawableLeft="#drawable/filter_search_icon"
android:drawablePadding="5dp"
android:hint="Type Friends Name"
android:inputType="text"
android:maxLines="1"
android:textColor="#ff333333"
android:textColorHint="#ff78797d"
android:textCursorDrawable="#null" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:divider="#000000"
android:dividerHeight="0dp"
android:fadingEdge="none"
android:persistentDrawingCache="scrolling"
android:scrollbars="none" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp"
android:text="#string/no_friends"
android:textColor="#f1f1f1"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="gone" >
</TextView>
</LinearLayout>
</LinearLayout>
The pros of using this method is that the ListView will scroll like it should but the Widgets above it will always be stationary. The cons of using this method is that on devices with smaller screens, there might not be enough room for the ListView contents to show and the user might have to scroll a lot.
Alternatively, you can separate the Widgets above the ListView, put them in a different XML file and add it to your ListView (in the Java code) as a Header. This makes the content above the ListView move along with the ListView contents.
So this piece of code goes in a different XML that will be added as a Header in your Java code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/phone_thing_comment_bg"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="9dp"
android:paddingRight="9dp"
android:paddingTop="5dp" >
<EditText
android:id="#+id/editFilterList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#drawable/phone_thing_comment_box"
android:drawableLeft="#drawable/filter_search_icon"
android:drawablePadding="5dp"
android:hint="Type Friends Name"
android:inputType="text"
android:maxLines="1"
android:textColor="#ff333333"
android:textColorHint="#ff78797d"
android:textCursorDrawable="#null" >
</EditText>
</LinearLayout>
The pros and cons are pretty much the opposite of the earlier example.
Either of the methods will work regardless of being used in a Fragment or an Activity.

Related

autoSizeText isn't displaying properly

I've got a layout that utilizes a table with a couple of rows. I'm attempting to use the API 26 autoTextSize functionality to ensure the text stays on a single line. The issue that I'm having is with the text being cut in half on first display in my list. If I scroll down the list, the text is fine, and then when I come back up to the top of the list my text is fine, but when it first displays it is cut in half. Here's the code I'm using for auto sizing.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:gravity="center">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="30dp"
android:gravity="center">
<TextView
android:id="#+id/AITAsymbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="30dp"
android:autoSizeMinTextSize="24dp"
android:autoSizeStepGranularity="2dp"
android:autoSizeTextType="uniform"
android:textAlignment="center"
android:textColor="#000000" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:id="#+id/received"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#ffffff" />
</TableRow>
</TableLayout>
Am I missing something with regards to how I should be using the auto sizing feature in Android?
You need to keep either layout_width or layout_height fixed for the Auto Text Sizing property to work.

Android Developers LinearLayout - I have a repeating list and want 1 button at the bottom of the screen, I keep getting one button per line

I am trying a task that should probably be simple..I want one button at the bottom across the bottom of the screen (floating preferably), while I have a scrollable list above that (I was able to do this in a tutorial with a simple listview and buitton).But, my list is a LinearLayout that I fill with a SimpleCursorAdapter and a viewBinder. Since I am using this LinearLayout I keep getting One button per line item, instead of one at the bottom of the screen. I have tried wrapping it with a table layout, relative layout, using two LinearLayouts, etc. Every time I get one button per line. Is this because of the way I am getting the data with a cursor adapter and filling it into the listview? Do I need to use a "merge" in my xml file? Is there a way to make two xml files and then call them both? Do I need to switch to a ListView or another way of displaying the data? This is my first app that I am trying start to finish on my own, so some of this stuff trips me up. I will include my code for the LinearLayout, please note that this is just the list without my extra button added (i deleted all my failed attempts). So I would be looking to modify the code below to contain one button that floats at the bottom of the screen all the time.
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="290dp"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:hapticFeedbackEnabled="true">
<Button
android:id="#+id/BtnToClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:drawableLeft="#+drawable/android_button"
android:gravity="left|center_vertical"
android:background="#android:color/transparent"
android:hapticFeedbackEnabled="true"
android:layout_weight=".1">
</Button>
<TextView android:text=""
android:id="#+id/tvViewRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
</TextView>
<TextView android:text="#+id/text11"
android:id="#+id/text11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
</TextView>
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="left|center_vertical"
android:layout_weight=".20"/>
<TextView
android:id="#+id/text9"
android:layout_column="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="center|center_vertical"
android:layout_weight=".1"/>
<TextView
android:id="#+id/text10"
android:layout_column="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="left|center_vertical"
android:layout_weight=".15"/>
<TextView
android:id="#+id/text12"
android:layout_column="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:padding="3dip"
android:gravity="left|center_vertical" />
<Button
android:id="#+id/BtnToClick2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler3"
android:gravity="center|center_vertical"
android:background="#+drawable/coup0a"
android:hapticFeedbackEnabled="true"
>
</Button>
Thanks in advance!
-Joe
You need to add the button as a footer or a header according to your needs. You can try this code .The R.layout.header and footer are separate xml layout files which you would have to define.
View header = inflater.inflate(R.layout.header,null);
View footer = inflater.inflate(R.layout.footer,null);
addHeaderView(header);
addFooterView(footer);
You should absolutely use a listview for this job. Listviews are highly optimized for displaying many entries. Just let your activity extend from a ListActivity and create a layout xml file with a listview widget that has the id "#android:id/list" and the listview activity will hook onto that list automatically. You are free to place other widgets in the layout aswell. Here is an example layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:id="#+id/chooseOther"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/add_fav"/>
</LinearLayout>
It has a list with a button sitting on the bottom of the screen at all times, even if you have a long list of items.

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.

Android - multiple EditText fields in window resize

In one of my views, I have three EditText fields. The first two are single-line, and the third is multi-line. I'm using android:windowSoftInputMode="stateVisible|adjustResize". however the third field collapses far too small in portrait mode when the IME comes up and it has focus.
Is there an option to set a minimum height that would force the window to scroll down to accommodate the third field?
I have tried setting android:minHeight="20dip" in the xml file, but this has no effect.
The EditText in question looks like:
<EditText
android:id="#+id/msgreplyarea"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="top"
android:layout_weight="1"
android:layout_marginLeft="10dip" android:layout_marginRight="10dip"
android:layout_marginTop="10px"
android:inputType="textCapSentences|textMultiLine"
android:imeOptions="flagNoEnterAction">
Thanks.
android:minHeight does work, but the parent view needs to be wrapped in a ScollView
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:scrollbarStyle="outsideInset"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/replyarea"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="top"
android:singleLine="false" android:layout_weight="1"
android:layout_marginLeft="10dip" android:layout_marginRight="10dip"
android:layout_marginTop="10px"
android:minHeight="120dp"
android:inputType="textAutoCorrect|textCapSentences|textMultiLine"
android:imeOptions="flagNoEnterAction" />
</LinearLayout>
</ScrollView>
Android documentation might help you with this. Or you can use a quick fix:
<activity name="EditContactActivity"
android:windowSoftInputMode="stateVisible|adjustResize">
...
</activity>
OR
android:windowSoftInputMode="adjustPan"

Categories

Resources