I have a list view in an Android application used to display an ArrayList containing Strings and I can't find any properties of the ListView that would allow me to align the text of the ListView Items to the right of it instead of the left. Any help would be great, thanks.
The answer depends on how exactly do you build the list. If you use the the standard ArrayAdapter then you could use the constructor:
ArrayAdapter<String>(Context, R.layout.aligned_right, values);
where R.layout.aligned_right is a xml layout like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="right"/>
This is because these parameters are not defined in the ListView, but in the ListAdapter instead. When you specify an adapter for the list, you should set it to return such a layout which aligns items on the right.
Add the below to the layout
android:layoutDirection="rtl"
This should suffice
android:layout_gravity="right"
For Expandable list view we create two row XMLs namely
One is group_row.xml and Other is child_row.xml.
make group_row.xml as
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/row_group_name"
android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:layout_height="wrap_content" />
That is, a Text view aligned to parentRight inside a Relative Layout (which is match parent).
After 2 hours of struggling, I achieved this a moment ago.
image after
Pay attention to one small thing.
If you use listitem inside the listview, the gravity attribute of the listitem might not work if the listview layout:width is not set to match_parent.
Related
I am working on a text view which shows the number of selected items (from a multiple choice list) with the number having a circle around it. I thought to add the circle by using the setCompoundDrawablesWithIntrinsicBounds (as I don't want to have 2 separate text views to achieve the desired look), but my question is if it's possible to somehow apply some text (in this case the number) over the circle image or if not, what would be the best solution for the label I have in mind?
I need to implement something looking like this:
What is your advice? Thanks for the help in advance!
You can use LinearLayout for that instead of single TextView implement click of LinearLayout.
For XMl use this:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/line">
<TextView
android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_bg"/>
<TextView
android:id="#+id/txtDropDown"
android:layout_width="0dp"
android:weight="1"
android:layout_height="wrap_content"
android:drawable_right="#drawable/arrow_down"/>
</LinearLayout>
As a beginner in Android, I'm trying to display a tree. I have already found how to add programmatically new TextViews but I don't know how to place them, I mean how to set their padding/margin depending on the father node.
Thanks a lot for your answers,
Baptiste
Dear first Use relative layout as parent and then add text view child.i did not add relative layout add relative layout as parent.
<TextView
android:id="#+id/txttxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="node"
android:textColor="#color/white"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/txttxt12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_below="#+id/txttxt1"
android:text="Child"
android:textColor="#color/white"
android:layout_alignLeft="#id/txttxt1"/>
this is sample code i made for you. you can use according to your requirement.
I've set up a ListView and assigned a custom layout to make the ListView items better looking. However, I'm looking for a way to add a button that would appear with each element in it's top right corner, like Youtube's videos: they all have that little options button with things like add to, share, etc. The problem is that you cant put a widget inside of a widget, at least I don't know a way to do it. The custom layout contains a TextView which gives the ListView items the looks. Can someone suggest a way on how should I approach this?
Use FrameLayout for your custom ListView items. like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<Button android:id="#+id/button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
NB - when widgets overlaps in layout, last written widget will be topmost.
Here is a link where you will find what you want.
It shows you how to add button in list view.
You will need to change some XML code and adjust the onClickListeners but it's similar to what you are looking for.
My app requires the ability to add several views on one line in a user selected order.
I found this tutorial which seems to accomplish what I want with a bit of modification.
http://www.androidpeople.com/android-custom-listview-tutorial-example/
Having followed the tutorial and made the required changes, the code works except for one strange issue. The position increments but when it hits ~9 it returns to zero and then re-adds views that are already in the list and thus never reaches the >9 ones.
Also, if I scroll down to the bottom and then back up the very first entry has changed! It may change more but I haven't checked that.
Through some tests I have discovered that the textSize has some effect. If I set it small enough so that all 'rows' will show on screen at once then they appear fine.
This is my listview layout that gets inflated into the main 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:minHeight="60dip"
style="#style/DefaultTheme">
<TextView
android:id="#+id/Line01"
android:layout_width="5dip"
android:layout_height="fill_parent"
android:background="#F00" />
<TextView
android:id="#+id/Line02"
android:layout_width="5dip"
android:layout_height="fill_parent"
android:background="#0F0" />
<TextView
android:id="#+id/Line03"
android:layout_width="5dip"
android:layout_height="fill_parent"
android:background="#00F"
android:layout_marginRight="5dip"/>
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="2dip">
<TextView android:id="#+id/Name"
android:text="Name"
style="#style/Name" />
<TextView
android:id="#+id/Status"
android:text="Status"
style="#style/Status" />
</LinearLayout>
<ImageView
android:id="#+id/StatusImage"
style="#style/StatusImage" />
</LinearLayout>
I change background colours and text but that is all. Any ideas what is the problem?
Thanks!
That's because ListView recycles Views. It creates as many Views as required then the one which isn't available will be recycled as next.
So you have to set the colors/attributes in the getView or bindView methods of the adapter.
And btw. what you posted doesn't inflate the ListView, as there is no `' object in it. At best it could be a single ListView item, but not the listview itself ^^
I feel dumb for asking this now and not going back over the tutorial completely.
I was setting the values in my layout I place in the ListView on my main layout only when a new ViewHolder was created. Thus when I scrolled, the views were recycled, but never updated with the correct View settings.
All working fine now!
Cheers
I have a main menu screen with a simple ListView that contains "links" to further screens in my app (Browse, Bookmarks, Settings, About, etc.). Underneath the ListView there is a TextView (more accurately, a TextSwitcher that rotates TextViews) that changes every 10 seconds to display a new "tip".
In portrait mode, this works fine. There are my five list items in the ListView , and my tip label underneath. However, when I switch to landscape mode, the ListView is taller than the screen. The ListView scrolls normally, but I cannot scroll past the end of the ListView to see the TextView underneath.
I have tried every possible combination of Layouts, wrappers, ScrollViews, and layout_height parameters and I simply cannot get it to behave.
Here is the simplest code I can use to get the result pictured above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/ListLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ListView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:id="#+id/TipLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="#+id/ListLayout">
<TextSwitcher android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/TipSwitcher">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="7pt"
android:id="#+id/Tip1TextView" android:text="Tip: Hello, Android!"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tip: This is the second TextView in the TipSwitcher!"
android:id="#+id/Tip2TextView" android:textSize="7pt"></TextView>
</TextSwitcher>
</LinearLayout>
</RelativeLayout>
Like I've said, I've already tried so many different combinations that I can't list them, and for the most part I was randomly inserting XML in an attempt to get something to work the way I wanted. So I'd greatly appreciate suggestions as to how I would go about doing this the right way.
Thanks.
EDIT: Something I forgot to mention, this may or may not be relevant. My MainMenuActivity is extending ListActivity. According to the docs, "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen." But, "If you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate()." So I don't think the ListActivity is interfering.
Put the TextSwitcher in the ListView itself. You can use addFooterView() for this.