0

I need to set two texts(Text1 and Text2) in one single TextView.Following are my requirements.

Text1 should be bigger than Text2 in font size.

Text2 would below Text1. Both are centrally aligned.

Text1 should be given gravity: center so that it resembles other layout.Text 2 will always be below Text1

N J
  • 27,217
  • 13
  • 76
  • 96
  • 2
    I want to be sarcastic here but since everyone replies nicely here i will also be nice and say that is not possible, you have to create 2 textviews and set the properties accordingly OR use spaces or \n and \t but you cant display text1 bigger and other smaller. – rusted brain Jul 18 '15 at 12:28

4 Answers4

0

You cannot do that with only one TextView.

Basically one TextView keeps a text and some properties of that text. That properties are applied to the whole TextView, ie, to the whole text. If you want to have different properties for Text1 and for Text2 you must have two TextViews.

At the end any UI element in android is given by an xml, in this case a TexView is something like:

<TextView android:text="@string/title" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffd300"
            android:gravity="center_horizontal"
            android:textSize="40sp"
            android:id="@+id/textViewTitle"
            android:layout_gravity="center" />

Here in property text you will set your Text1 or your Text2. In this case we are using @string/title which means that we are using an string called "title" from the string resources.

Reference: http://developer.android.com/reference/android/widget/TextView.html

mayo
  • 3,845
  • 1
  • 32
  • 42
0

Combining two texts in a single TextView is not possible. It spoils the respect of the TextView itself.

You will need a Vertical LinearLayout with a background and border resembling to your TextView. Inside this layout, you will need to have two TextView's for text1 and text2 respectively. You can apply your desired properties to the individual TextView's

Jaseem Abbas
  • 5,028
  • 6
  • 48
  • 73
0

I'm still unclear about your question, do you want to display both texts at the same time or not?

If you're only going to display one 'style' at a time, mayo's answer is right. You can use a switch statement to implement it:

switch(textStyle){
    case 1: myTextView.setTextAppearance(getApplicationContext(), R.style.styleText1);
            break;
    case 2: myTextView.setTextAppearance(getApplicationContext(), R.style.styleText2);
            break
}

More info here.

But if you want to display both at the same time, a WebView is your only option:

Webview wv;
//Specify Text1 and Text2 as strings.
String text = "<html><body style=\"font-size:25px;\">"+"<p align=\"center\">"+ Text1 +"</p>"+"<style=\"font-size:15px;\">"+"<p align=\"center\">"+ Text2 +"</p>"+"</body></html>";
wv.loadData(""+text, "text/html", "utf-8");
Community
  • 1
  • 1
0

you can use html code in Textview

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:textSize="25sp" />

and in java file

TextView mTitle = (TextView) toolbar.findViewById(R.id.title);
        mTitle.setText(Html.fromHtml("<b>S T A C K </b><font color='white'>O V E R F L O W</font>"));
Tabassum Latif
  • 247
  • 2
  • 15