I need to have 3 text in layout. the textView in the center is red in color and the other 2 are black. so i added it to a relativelayout and set the layout as textView2 to the right of textView1 and textView3 to the right of textView2
But when the text in the 3rd text view is bigger it should come textview one. insted I get this.
now what should i do to get somthing like this?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/rel_lay"
android:background="#DFDFDF"
android:padding="2dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:text="TextView"
android:textColor="#000000" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:text="TextView"
android:textColor="#FF0000" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_toRightOf="#+id/textView2"
android:text="TextView when the third text is longer"
android:textColor="#000000" />
</RelativeLayout>
this is the relative layout with 3 textView.
Just use one TextView and set text as per you need. You know which text to set for Red color. So what you need to have is index of starting text and ending text.
Use following code.
SpannableString spanText = new SpannableString("My Name is Chintan Rathod");
spanText.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 5 /*start index*/, 10 /*end index*/, 0);
textView.setText(spanText);
Here, I have passed 5 and 10, but you can pass your own indexes. But make sure index within range which will not fire IndexOutOfBoundException
You might have better luck with using Spanned classes and one TextView. The way you are trying to implement it, TextView #3 would have to fill_parent on the width and be able to know where TextView #2 ends.
Related
i am trying to align multiple (variable) textviews in one line. It should look like this:
- Textview1 | Textview2 | TextView3
Since the textviews vary regarding length I am having a problem aligning them properly(the only thing I know is that Textview1 will be the shortest one).
Here is my code (it is placed inside a Linear Layout):
<RelativeLayout
android:id="#+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" >
<TextView
android:id="#+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="3dp"
android:text=" Title "
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/persondetails_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name "
android:textSize="#dimen/font_big"
android:textStyle="bold"
android:layout_toEndOf="#id/persondetails_title"
android:layout_toRightOf="#id/persondetails_title" />
<TextView
android:id="#+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/persondetails_name"
android:layout_toRightOf="#id/persondetails_name"
android:text=" Surname " />
</RelativeLayout>
Now, as long as these 3 Textviews fill out one line on the screen everything is OK, but as soon as one of them gets bigger I get some problems. For example, when I put the surname field very long, it pushes the other 2 views out of the screen (they are not visible) and takes all the space (but just one line!).
What I want is that these views are aligned to the right side, each of them to the right of the previous one, and when it is needed to linebreak into a new line (no mather which textview it is) and in the new line the following textviews should align to the right of it.
So what do I need to change in my code, so that these Textviews are aligned next to each other, and moved properly when one of them is getting bigger ?
Thank you
This is how I implemented Elltz suggestions:
<LinearLayout
android:id="#+id/persondetails_namen_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/persondetails_wert_titel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
<TextView
android:id="#+id/persondetails_wert_vorname"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="#id/persondetails_text_vorname"
android:gravity="right"
android:layout_gravity="right"
android:text=" "
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"/>
<TextView
android:id="#+id/persondetails_wert_nachname"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="#id/persondetails_text_vorname"
android:layout_gravity="right"
android:text=" "
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"/>
</LinearLayout>
But still not working. Actually I removed the "singleLine=true" because in case when one of the 3 textviews gets too long I need it break (don't have a problem with breaking).
Regarding breaking, I want every textview to take as muche space as it needs, the only thing is that all the other views shoud be moved properly.
The property "single line" cannot be right because it is possible that one of these views gets too long for one line, but I have tested it with single line and without:
a) with "singleline=true"
b) without "singleline=true"
In bothe cases I get a wrong output, but case 2 is closer to what I want. But if you look at it you will see that textview2 is being pushed and it breakes in its bounds but not on the entire lenght. Textview 3 takes the most space. But what i want is that every view is fully streched and if needed breaked into new line (textview1 is this "Mag.")
This is how I would like it to look like:
Change the relative layout to a linear layout and then set the orientation to horizontal something like this.
<LinearLayout:id="#+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<TextView
android:id="#+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:text=" Title "
/>
<TextView
android:id="#+id/persondetails_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name "
android:textSize="#dimen/font_big"
android:textStyle="bold"
/>
<TextView
android:id="#+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Surname " />
</LinearLayout>
You should try and do a LinearLayout and weight each element.
<LinearLayout
android:id="#+id/persondetails_names_horizontal_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" >
<TextView
android:id="#+id/persondetails_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="3dp"
android:text=" Title "
android:layout_weight= "1"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/persondetails_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name "
android:textSize="#dimen/font_big"
android:textStyle="bold"
android:layout_weight= "1"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/persondetails_surname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight= "1"
android:layout_centerVertical="true"
android:text=" Surname " />
</LinearLayout>
To answer why your code line is giving you that problems is a RelativeLayout positions its Views relative to each other, a View will need another View to be layed out if not the parent itself. So TextView 1 is positioned to the left of TextView 2 when TextView two gets bigger since its to the left it will be to the left but not on screen layout or space get it?
To answer you
TextView 1 will be the shortest Using the LinearLayout Approach with weightsum and weight tweak it this way first set the weightsum to two on the LinearLayout and the two i am going to be long TextViews give them a width of 0dp and weight 1 each the shortest TextView will have wrap_content as width and that's all life's is good. now the two long TextViews will calculate their sizes and the rest will be left for the shorter one. I do not not specifically how you want your TextView to long in terms of straight horizontal line but after you have done that you can add these elements to your TextView in xml or you could do it in java to ensure your straight lined TextViews
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:overScrollMode="always"
android:scrollHorizontally="true"
android:singleLine="true"
EDIT
this is what i mean
int width = getWindow().getDecorView().getMeasuredWidth();
//i am getting the overal width of the screen
Toast.makeText(getApplicationContext(), String.valueOf(width), Toast.LENGTH_SHORT).show();//toast it
for(int i =0; i < ((LinearLayout)findViewById(R.id.l)).getChildCount(); i++){
//looping through the parent container
TextView child = (TextView) ((LinearLayout)findViewById(R.id.l)).getChildAt(i);
child.setSelected(true);
child.setEllipsize(TruncateAt.MARQUEE);
child.setHorizontallyScrolling(true);
child.setSingleLine(true);
if(i+1 == 3){ //the last textview
child.setLayoutParams(new LinearLayout.LayoutParams(
((LinearLayout)findViewById(R.id.l)).getChildAt(1).getRight(),
LinearLayout.LayoutParams.WRAP_CONTENT));
}else{ // the first two take 1/3 of the width of the screen
child.setLayoutParams(new LinearLayout.LayoutParams(width/3, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
}
Hope it helps
I am trying to wrap a second text around the first text, both in relative layout and textview such that :
<RelativeLayout
android:id="#+id/headlineRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:paddingRight="2dp"
foo:customFont="proxima-nova-bold.ttf"
android:textColor="#color/orange"
android:textSize="#dimen/title"
android:gravity="left"
/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:layout_toRightOf="#id/text1"
foo:customFont="proxima-nova-regular.ttf"
android:textColor="#color/green"
android:textSize="#dimen/title"
android:gravity="left"
/>
</RelativeLayout>
So, what I am currently getting is :
TEXT1 (TEXT 2 STUFF IS A SENTENCE WHICH
SHOULD WRAP AROUND)
bUT WHAT i expect is :
TEXT1 (TEXT 2 STUFF IS A SENTENCE WHICH
WHICH SHOULD WRAP AROUND)
Any clue?
I may have missed something here, but what you re getting is what I would expect. Each TextView is a rectangular box into which the text is rendered. If you want, you can overlay the boxes, but then the 'TEXT 1' and 'TEXT 2' would lie on top of each other.
To achieve what you want, you will have to think more carefully about what you are doing, and perhaps have three TextViewss:
<TextView 1><TextView 2>
<..... TextView 3 .....>
Whenever you set the text in TextView 2, set it to only show one line and find out how far it got. Then send the rest of the string to TextView 3.
I have something like "There are 200€" but I want to style the value (ex. textColor Red)
Is there any way of having two textViews to seem like an entire one?
A textView inside a textView or a textViewContainer or something.
I can achieve it on the run like this answer: Change text color of one word in a TextView
But I want to do from the layout file. Any chance?
Thanks
EDIT
<TextView
android:id="#+id/lastIncome"
android:text="Tu último ingreso fueron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_font_size"/>
<TextView
android:id="#+id/lastIncomeValue"
android:text="200€"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_value_font_size"
android:textColor="#color/greensea"
android:layout_gravity="right"/>
You can achieve this with a horizontal LinearLayout. The LinearLayout is the container for the two side by side TextViews. This layout can be placed in any other container (LinearLayout, RelativeLayout, etc.) in your XML.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There are " />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200€"
android:textColor="#ff0000"/>
</LinearLayout>
You can use a easy approach to do this within one textview using SpanableString Builder Click here to get this approach
i am suppose to place a TextView right of a multiline TextView.
please check the code below .
<RelativeLayout
android:id="#+id/rltest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rlanswer"
android:layout_marginLeft="#dimen/grdiviewspacing"
android:layout_marginRight="#dimen/grdiviewspacing"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/lblanswer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:lines="1"
android:text="#string/strbookmarkmessage"
android:textColor="#color/black"
android:textSize="#dimen/listviewlocationsize"
android:textStyle="bold" />
<TextView
android:id="#+id/btnyesiwillcollectitby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#id/lblanswer2"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/stryesiwillcollectitby"
android:textColor="#color/linkcolor"
android:textSize="#dimen/listviewlocationsize" />
</RelativeLayout>
i have tried the problem with the above code is its not showing the second TextView. where us if i try layout_below - its showing the second text view. can anyone please assists me..
i want something like this - blue textview text to read textview.
Use LinearLayout instead of RelativeLayout and give layout_weight=1 to both the TextView you will get result whatever you want.
If you want to keep using a RelativeLayout, you will have to set the width in dip (instead of wrap_content) for the TextView "lblanswer2", otherwise it will always take the whole place if your text is long.
If using a RelativeLayout is not a necessity, you can follow pratik's answer and implement a LinearLayout, this way your 2 TextViews will share the width equally.
I have two text views(TV_1,TV_2).I want to set the TV_2 is immediate right of the TV_1.
It means
suppose TV1 occupy the one entire line in the screen then TV2 will set below the TV1.
Suppose Tv1 occupy the one and half line then tv2 will occupy the rest of the space. How to implement this? i tried but i am not getting. It is similar to one paragraph.
<RelativeLayout
android:id="#+id/xrays_dicom_alert_TnC"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="20dp">
<CheckBox
android:id="#+id/xrays_dicom_alert_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/xrays_dicom_alert_TnC_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:text="I have read I agree to the following"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/xrays_dicom_alert_statement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:layout_below="#id/xrays_dicom_alert_TnC_text"
android:layout_alignRight="#id/xrays_dicom_alert_TnC_text"
android:text="Statement"
android:textColor="#color/black"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thats impossible act in Android.
A View occupies a rectangular area on the screen.
http://www.androidadb.com/class/android/view/View.java.html
Or you have to do something like this
To make clickable some text in TextView one can go with Spannable concept.
http://developer.android.com/reference/android/text/Spannable.html
use URLSpan http://www.androidadb.com/class/ur/URLSpan.html
I think it's not possible in the pattern what you have asked.