Adding TextView with padding depending on another TextView - java

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.

Related

Android: adding text on compound drawable image in textview

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>

Android placing multiple bitmaps in imageview

I have multiple ImageViews which are transparent and are meant to over lap one another. I am aligning then to one another but one problem presist. How can I set the order in which they are layered? Ive tried revising the code in both xml and how the images are set in the actual java code...
I have tried FrameLayout but maybe Im not using it right...here is my implementation:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".RadarActivity" >
<ImageView
android:id="#+id/imageview_topo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:scaleType="center"
android:visibility="invisible" />
<ImageView
android:id="#+id/imageview_counties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:scaleType="center"
android:visibility="invisible" />
No matter what I do...imageview_topo is always placed on top. Even in the code i have instantated topo first and retrieved its image first!
Thank you!
I referred a site here that says:
If multiple child views exist, then they are drawn, in order, one atop
the other. This means that the first view added to the frame layout
will display on the bottom of the stack, and the last view added will
display on top.
You can check this site too: http://blog.neteril.org/blog/2013/10/10/framelayout-your-best-ui-friend/
May be i can help you more if you show the screenshot of your UI.

Two TextViews clashing in Android

I am trying to make two TextViews that will be on the same row. The first one should be left aligned and the second one right aligned, when one of them becomes to big the first one (the left aligned should go on new row). I was able to make the first one to go on new line when the second TextView (the right aligned one) became too big but the problem is that it doesn't display the whole information from it. So anyone has any ideas. I'd like it to be with RelativeLayout but any help would be appreciated.
You should play with android:layout_weight to acheive this , this is how it should be :
<LinearLayout .....
android:orientation="horizontal" >
<TextView android:id="#+id/txtView1"
android:layout_height="wrap_content"
android:gravity="left"
android:text="TextView Left"
android:layout_width="0dp"
android:layout_weight="1" />
<TextView android:id="#+id/txtView2"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="0dp"
android:text="TextView Right"
android:layout_weight="1" />
</LinearLayout>
You should try putting the two textviews into a linearlayout, set the layout_weight="1" on both textviews and their layout_width="0".
give weight = 1 for both text views....
if it does't work....change relative layout to linear layout or table row and give weight to 1 for both text view
Put your TextViews in LinearLayout and set layout_weight=1. it will solve your problem.

Basic clickable item in android

I've been searching for this for quite a while but can't seem to find it.
Is there any predefined widget in Android to implement a basic clickable item (with one of two lines of text) whose background turns to the theme's color when clicked, like the ones seen here, for example:
And I mean, without using the PreferenceActivity class, which implements this automatically. Does this exist or do I have to implement my own customized view?
Thanks!
Edit:
I've added a TwoLineListItem like this and it doesn't react to clicks (background doesn't change)
<TwoLineListItem
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
>
<TextView android:id="#android:id/text1"
android:layout_marginTop="1dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:text="Meh" />
<TextView android:id="#android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/text1"
android:layout_alignLeft="#android:id/text1"
android:paddingBottom="4dip"
android:includeFontPadding="false"
android:textSize="15sp"
android:textStyle="normal"
android:text="Moo" />
</TwoLineListItem>
Does it only work inside ListViews? Because that way I'll have to define my buttons in code and not in the XML, right?
Thanks!
The default layout for that is called android.R.two_line_list_item given by android.
All the items in a ListView support this transition.
Best wishes,
Tim

How to Align ListView Text to Right in Android?

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.

Categories