How to make a TextView multiline programmatically in Android (Java) - java

I want to make my textview multiline.
Image : http://s13.postimg.org/y0q78e1yv/Capture.png
But how can I make my text multiline ?
Which atribut ?
TextView txt_tweet = (TextView) View.inflate(this, R.layout.special_textview, null);
special_textview
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:inputType="textMultiLine"
android:scrollHorizontally="false">
</TextView>

I did it following way:
tv.setElegantTextHeight(true);
tv.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tv.setSingleLine(false);

You want to show to different texts in the same textview? if so, use two text views like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</LinearLayout>
Remote android:inputType="textMultiLine", this is an EditText Atribute.
If you just want to use more then one line in the same text view:
android:maxLines="5"//optional to set max numbers of lines
android:minLines="2"//optional to set min numbers of lines
android:singleLine="false"//set false to allow multiple line
android:lines="2" //or more
If this textview you want to use belongs to a ListView, just use:
android.R.layout.simple_list_item_2
It will give you two texts views to work on.

First replace "\n" with its Html equavalent "<br>" then call Html.fromHtml() on the string. Follow below steps:
String text= model.getMessageBody().toString().replace("\n", "<br>")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))
This works perfectly.

Adding to the answers: the order matters!
Make sure you call setInputType before setMinLines!

You can do it like this:
txt_tweet.setSingleLine(false);
txt_tweet.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);

The TextView must have the 'singleLine' attribute set to false. Also you should set the 'ellipsize' to wrap the text:
android:singleLine="false"
android:ellipsize="end"

in layout
android:lines="8" //Total Lines prior display
android:minLines="6" //Minimum lines
android:maxLines="10" //Maximum Lines

Just remove this line:
android:inputType="textMultiLine"
inputType for EditTexts

Related

How to place vertical scrollbar of textView from right to left?

I applied vertical scrollbar on my textview in an android app but it is by default showing at the right side of the textview. I want to move it (place it) to the left side of the textView. here is the code, please tell what modification needed:
TextView abc.setText("sfihogwhgo \n lwhgieowehgwegoihwgoe \n wfeliwegh \n woihgi");
abc.setMovementMethod(new ScrollingMovementMethod());
Also, in the layout xml:
<TextView
android:id="#+id/textView_ghazal"
android:layout_width="match_parent"
android:layout_height="514dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:lineSpacingExtra="20sp"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold"
android:scrollbars="vertical"
/>
There are 2 ways to do this, one is through xml:
android:verticalScrollbarPosition="left"
or through Java code:
abc.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT);

Android way to add new xml layout

I have an activity with a layout. After a GET request to a server, I want to dynamically add new elements to that layout.
I want to add those elements multiple times, using a for-structure.
The elements I want to add are the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#drawable/outer_border"
android:padding="2dp"
android:layout_marginTop="20dp">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/orange"
android:height="40dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="TW"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="70px"
android:width="60dp" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_toLeftOf="#+id/checkBox1"
android:text="inca 6 zile"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
I've tried this:
for(int i = 0; i < homeworkList.size(); i++){
LinearLayout linearLayout = (LinearLayout) currentActivity.findViewById(R.id.linearLayout2);
RelativeLayout newLayout = new RelativeLayout(currentActivity, null, R.style.HomeworkLayout);
TextView text = new TextView(currentActivity);
TextView text1 = new TextView(currentActivity);
text1.setText("da");
text.setText("nu");
newLayout.addView(text1);
newLayout.addView(text);
linearLayout.addView(newLayout, relativeParams);
}
But no result, those textview were added but on top of each other, and the relative layout I just added in that for doesn't have any of the style I added using R.style.HomeworkLayout.
What is the best way to add the elements with so much styling? Why isn't this working?
those textview were added but on top of each other
That's what you told RelativeLayout to do. If you wanted to specify positioning rules, you would have passed instances of RelativeLayout.LayoutParams to addView() when you were adding the TextView widgets.
What is the best way to add the elements with so much styling?
Well, probably, the answer is to use ListView or RecyclerView. That being said, the simplest solution that keeps your vertical LinearLayout would be to inflate the rows:
LinearLayout linearLayout = (LinearLayout) currentActivity.findViewById(R.id.linearLayout2);
for(int i = 0; i < homeworkList.size(); i++){
View row=getLayoutInflater().inflate(R.layout.row, linearLayout, false);
// call findViewById() to retrieve your TextView widgets and fill them in
linearLayout.addView(row);
}
This assumes that the layout you show in your question is named R.layout.row; adjust the inflate() call as needed if that is not the name. This also assumes that the code snippet is in a method on the activity that is hosting this UI.
If you want to use a layout which is repeating why don't you prefer using a custom liner layout.
A simple and basic solution is mentioned on this link
http://android-coding-tuts.blogspot.in/2012/02/custom-listview-with-sliding-view-for.html
You should look up Fragments for this. They have a separate control-view structure and you can just create a new fragment for each subview.
Check it out here:
http://developer.android.com/guide/components/fragments.html

abbreviations in android via xml or java

first of all.
i have tried searching my question in stackoverflow and not founded the exact answer there are some related questions but those are not the answer of my questions.
may be i dont know what we call that thing which i gonna ask you.
i am creating an xml file in eclipse in which i am going to show abbreviations of words.
for example
fb: facebook //no new line added for bigger screen
gm: gmail
sof: stack over flow
these are just examples.
in my code i want to create 2 textviews or something like that
first textview will show the abbreviations and 2nd will show the exact English for that abbreviation.
but some times on small screens the abbreviation may go out of screen or may include /n or new line and my all other abbreviations will be wrong like this example.
fb: face //a new line added for small screen
gm: book
sof: gmail
:stack over flow
basically i want you to tell me that how to arrange the code so the textview should either scroll and do not include the new line.
or if textview2 include enter than textview1 should have an enter too.
please help me
You should use TableLayout class and HorizontalScrollView
Just try something like this
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height=wrap_content"
android:fillViewport="true">
<TableLayout
android:layout_width="wrap_content" <!-- text should go along one line for each row-->
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="fb:"/>
<!-- when singleLine atrubutte set : ) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="facebook"/>
</TableRow>
</TableLayout>
</ScrollView>

Eclipse not displaying TextView

Okay, so the thing is that I'm trying to display text but it won't let me. the code is :
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textAlignment="center"
android:textColor="#000000"
android:textColorLink="#android:color/black"
android:textStyle="normal" />
What have I done wrong? (the text won't appear on screen)
At your string resources app_name might be empty or null.
There is another view that prevents your TextView to be displayed at your screen, take a look at your layout file.
The value for TextView modified to a null/empty value in your code.
Try this
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Flights"
android:textColor="#android:color/white" />
Your code is correct as it is working for me. The probable mistake you have done is that your String resource variable "app_name" is empty or you can provide the full xml code for us to have a better look at this problem.

Dynamically including an XML relative layout multiple times in a parent view

I have an XML RelativeLayout snippet that I would like to include several times (from a loop) in my main View. The problem seems to be -- is there a way to avoid hard-coding the parent of the RatingBar, since each time I include the RelativeLayout snippet my elements will need to have different ids?
As far as I can tell, the recommended way is to get the layout snippet and then override the android:id for each element to be unique, and then override the android:layout_below manually for each element that has relative positioning. This seems a little kludgy -- is there any way to have these bindings get done automatically?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:id="#+id/relativeView">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="Label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1" />
</RelativeLayout>
you just need to change the id of the RelativeLayout
like
int BASEID=200;
View v = mLayoutInflator.inflate(R.layout.myRelativeLayout, null);
for (int i;i<10;i++){
v.findViewById(R.id.relativeView).setId(i+BASEID);
}
mRootView.addView(v,...);
then when you need to get the RatingBar for suppose the 4th RelativeLayout you added you can call
RatingBar mRatingBar = (RatingBar)mRootView.findViewById(BASEID+3).findViewById(R.id.ratingBar1);

Categories

Resources