Sorry for a noob question, I think it should be done with Spannable or something, but I can't google it right.
I need a text in a TextView with a drawable to the left of each line . How do I do it?
Try this..
By using XML
Use android:drawableLeft="#drawable/ic_launcher" to your TextView
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:drawableLeft="#drawable/ic_launcher" />
By using Code
Youe can see setCompoundDrawablesWithIntrinsicBounds
TextView textView = (TextView) findViewById(R.id.txt);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
Try this android:drawableLeft="#drawable/sampleImage" in TextView Tag in XML file.
see this property of TextView
android:drawableLeft="#drawable/yourIcon"
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/yourIcon"
android:ems="10"
android:text="TextView" >
<TextView
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:drawableLeft="#drawable/ic_launcher"
android:padding="10dip" />
you can even use a linearlayout with orientation vertical with one ImageView and Textview in that align textView layout to center
Related
I have a XML file with RelativeLayout, I also have Textview which has Gravity Center. In code I would like to get X coordinate of this Textview, where the TextView begins (length of TextView is not constant, it can change). I need a ImageView to be next to the TextView.
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView" /> `
countryText = (TextView) findViewById(R.id.textView6);
countryText.getLeft()
getLeft() return 0;
Add android:layout_centerInParent="true" to textview and add android:layout_toLeftOf="#id/textView6" to imageview
Simply get rid of the ImageView and add a compound drawable to the left of your TextView.
This is also more efficient, because it helps flattening your layout hierarchy.
I.e.:
<TextView
android:id="#+id/textView6"
android:drawableLeft="#drawable/my_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView"
/>
Official docs
How to make single line TextView appears if the screen small?, Because it's makes 3 dots of ellipsize without defining it in XML and the rest of text disappeared.
Example:
Please add:
android:singleLine="false"
OR
android:inputType="textMultiLine"
to your TextView
EDIT:
Your TextView can look something like this:
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/your_text"
android:maxLines = "1"
android:scrollbars = "vertical"/>
and add these two lines to your onCreate
TextView yourTextView = (TextView) findViewById(R.id.test);
yourTextView.setMovementMethod(new ScrollingMovementMethod());
Not too clean but does the job.
SECOND EDIT:
<TextView
android:text="#string/yourText"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever" <!-- Please change according to requirement -->
android:scrollHorizontally="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"/>
Hope this helps!
I need to right align my text in a TextView like this.
How to achieve this?
Below is my layout.xml file's textview
<TextView
android:id="#+id/sampleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/dummyTextView"
android:layout_alignLeft="#+id/dummyTextView"
android:gravity="right" />
Did you try?
<TextView
android:id="#+id/txtItemDay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|right"
android:text="Your text"
android:textColor="#android:color/white"
android:textSize="22sp" />
The problem is:
android:layout_width="wrap_content"
You could not align text in TextView if you set with is wrap_content, please set fill_parent or match_parent or specified witdh.
You're using android:layout_width="wrap_content" which means its not able to set the gravity because you don't have enough space for the text to align. Try using this instead:
android:layout_width="fill_parent"
I have a some ui widgets including textview inside RelativeLayout which is clickable. My problem is textview text color does not change when relativelayout gets focus although I have set textview color property correctly.
Is there any easy way to cascade focus to childview inside relative layout, same as listview items.
<RelativeLayout
android:id="#+id/top_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/selector_white_blue"
>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:textSize="#dimen/text_size_med"
android:textColor="#color/black_text_white_focused"
android:textStyle="bold"
android:text="Movie Name"
/>
</RelativeLayout>
Specify android:duplicateParentState='true' on your TextView.
Programmatically by textView.setDuplicateParentStateEnabled(true);
for your
TextView textView= new TextView(this);
I want to let it looks like this:
| two |
| lines |
Here is the current layout, not working at all.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two\nlines"
android:layout_gravity="center_vertical"
android:layout_centerInParent="true"/>
</RelativeLayout>
Any idea?
Thanks!
If you just want to center it (I'm assuming the \n is working to split the lines), just add android:gravity="center_horizontal" rather than layout_gravity.
Using layout gravity moves the actual TextView, using gravity affects the content of the TextView.
Had to add both gravity and layout_gravity to align both TextView and its content
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
if your width is wrap_content, you have to set gravity and layout_gravity both as "center_horizontal"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two\nlines"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"/>
However, if your width is "match_parent, you will only have to set gravity as "center_horizontal"
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="two\nlines"
android:gravity="center_horizontal"/>
Hope it helps!
You can use:
TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two"+"\n"+"lines"));
I use:
android:gravity="center_horizontal"
to center two line text
I think you must do it from Java:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="#+id/the_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_centerInParent="true"/>
</RelativeLayout>
Then:
TextView tv = (TextView) findViewById(R.id.the_text_view);
tv.setText(Html.fromHtml("two<br/>lines"));
Add property
android:lines="2"
where 2 is no of lines you can set this no as your requirment