Unable to maintain the textsize in Android Textview - android

In my app, there is a textview. The textview is populated with the data from the DB. The following is the XML of the textview
android:id="#+id/ViewMessageOne"
android:layout_width="275dp"
android:layout_height="45dp"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp"
android:typeface="sans"
android:background="#drawable/back"
android:layout_marginLeft="35dp"
/>
The following is the lines of code to populate the data in the textview from DB
final TextView ViewMessageOne = (TextView) findViewById(R.id.ViewMessageOne);
ViewMessageOne.setText(Html.fromHtml(getMessages("MSG_ONE")+ "<br>" + "<small>" + getTimeStamp("MSG_ONE") + "</small>"));
When the data from DB is populated, the text size in the screen is small and is not taking the XML value. I checked this by increasing the size of the textview text. But it is not taking the text size from the XML. Can anyone guide me as how to get the value of the text size value from the XML and hence implement it using JAVA code. Thanks

you have follow line in your code:
android:textAppearance="?android:attr/textAppearanceMedium"
which probably overrides your custom textsize
remove that line and try again
Edit: to set text size programmatically:
viewMessageOne.setTextSize(16)

change this
android:layout_height="45dp"
to this
android:layout_height="wrap_content"

Related

In edittext hint need to scroll horizontally

In my app i have Edit text with fixed size but hint size is more than edit text so hint is not displayed fully.I want to display hint fully by scrolling horizontally but i don't have any idea how to do this.
If reduce the size of hint font it will help but i don't want to reduce font size.
Following is the example code which i use.
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:id="#+id/test_etx"
android:hint="#string/hint"
android:layout_below="#+id/bt3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"
android:layout_alignRight="#+id/etx"
android:layout_alignEnd="#+id/etx"
android:maxLength="10"
/>
Make your EditText width match_parent. Put that inside a horizontal scroll view. I tried and it works. If you want fixed width, give fixed width to your horizontal scroll view. Like this :
<HorizontalScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/bt3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="29dp"
android:layout_alignRight="#+id/etx"
android:layout_alignEnd="#+id/etx">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:id="#+id/test_etx"
android:hint="#string/hint"
android:maxLength="10"/>
</HorizontalScrollView>
First you can add a scroll in editText then put text in edit Text with grey font color instead of hint and then put a OnClickListener on the editText. If input text matches hint then set it to "" else process the data.
Make two arrays of EditText id and its corresponding hint in values res. Then fetch both arrays using getResource().
Use
String str = view.getResources().getResourceName(id);
str = str.substring(str.lastIndexOf(":id/") + 4);
to get view id.
match hint from stored array's hint to input text. I think it will be generalised for all the layouts. Hope it helped.

Set different text size in Android TextView

I have one TextView with only 1 line of text. Now I have to put an extra line to set the state of the user. So I set the property android:lines="2"
the textview looks like:
<TextView
android:id="#+id/providerName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:ellipsize="marquee"
android:fontFamily="sans-serif-light"
android:lines="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
The way I add the text is like:
mNameAndStatus.setText(providerNameText + "\n" + mUserStatus);
Image how is actually.
The problem is that the first lines must me in Large text, but the second line must be medium or small and I don't know if is it possible.
How can I achieve this?
try
String asHtml = firstLine+"<br/><small>"+secondLine+"</small>";
textView.setText(Html.fromHtml(asHtml));
or simply use Spannable (AbsoluteSizeSpan)
You need to do something like this :
mNameAndStatus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
where your text size is given in sp (24sp in the example).
Here are all TypedValues http://developer.android.com/reference/android/util/TypedValue.html

Text View Text analysis in Android

I have a textview and the text is loaded dynamically.
<TextView
android:id="#+id/txt_song_title"
style="?attr/txtcolorHightlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignTop="#id/frlyt_img_cover"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_toLeftOf="#id/img_audio_source_icon"
android:ellipsize="end"
android:fontFamily="#string/roboto_light"
android:maxLines="3"
android:text=""
android:textSize="#dimen/font_36" />
The textview has to show the entire text which is set (10 to 100+)characters. Based on text length, the font size should be changed inorder to fit the entire text. How to calculate num of words in the textview to change font size and display entire text without clipping dynamically?
I think you can do it from java code level. TextView class provides methods to implement this.
Following link may be useful.
Auto Scale TextView Text to Fit within Bounds
Try using a library, for example a simple google search gave me this:
https://github.com/grantland/android-autofittextview
I have not tried it but it seems ok :)

text entered in edittext with multiple new lines pressed when displayed in textview doesnt display whole text

i have an edit text inactivity one where i input text , the text could be a single sentence or multiple sentences entered by hitting enter, which takes to new line.
I am passing the text entered in edit text of activity one to activity two through intents and collecting the string value and displaying the edittext string value by setting it to text view.
What i see is it only displays single line and lines below line one are not getting displayed.
Suggestion please
the code is as follows
description=(EditText)findViewById(R.id.description);
send_description=description.getText().toString();
t_desc = (TextView) findViewById(R.id.descp);
<TextView
android:id="#+id/descp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/id5"
android:layout_marginTop="10dip"
android:gravity="left"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:scrollbars="vertical"
android:maxLines="10" />
Set properties of TextView like this
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="10"
android:scrollbars="vertical"
android:text="#string/message"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="bold" />
In your activity class use this code
TextView tv=(TextView) findViewById(R.id.tv);
tv.setMovementMethod(new ScrollingMovementMethod());
If the text you're putting in the TextView is short, it will not automatically expand to multiple lines. If you want the TextView to always have n number of lines regardless of the length of the text in it, set the android:lines
android:lines="n"// you can set n=2 and see the result
And, If you define your TextView's LayoutWidth to something like 120 dip then it will be multiline

TextView Superscript not showing correctly

I'm using the code found: Subscript and Superscript a String in Android in the first answer, but concatenating that from a previous string, so my code looks similar to:
TextView text = (TextView) findViewById(R.id.text);
text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));
Say, the contents of text was "3x", after setting the text using setText, it formats to "3x-2" with no subscript.
The XML for the TextView is:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="55dp"
android:layout_marginTop="210dp"
android:text="3x"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="14pt"
android:visibility="VISIBLE"
/>
Thanks for helping.
Instead of this line..
text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));
try like this
text.setText(Html.fromHtml(text.getText().toString()+"<sup>-2</sup>"));
Please check the below its working fine for me
"TEXT<sup><small>TM</small></sup> TEXT"
The above code will make the TM to Subscript and also make the text size small
Also for small size you can use-
text.setText(Html.fromHtml(text.getText().toString()+"<sup><small>-2</small></sup>"));
Put your whole text into only one Html.fromHtml("Your whole string")
From strings.xml
<string name="superscript_str">YourString<sup><small>SuperScriptString</small></sup></string>
If you want display small as super text use
text.setText(Html.fromHtml(text.getText().toString()+"<sup>small>-2</small>/sup>"));

Categories

Resources