Text in TextView sometimes is shown vertical. How to prevent it? - android

I have this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutUsers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
>
</LinearLayout>
I add a few textviews into "layoutUsers" layout programmatically:
TextView textView1=new TextView(this);
textView1.setText("some text");
layoutUsers.addView(textView1);
And I have the following performance:
How to prevent text from being vertical?
How to show all text in a few lines?
Every text has own color, so I can't combine all textviews into one.
I guess we should do something with layout, not with textviews inside it...
Any ideas please?
Thanks!

setLayout Params for TextView programatically

I used a few textViews in layout, because they had different color and font.
I've discovered that I can use only one textView with HTML text.
Like this
String SPACE="nbsp;"
info+=SPACE+"user=<B><FONT color=#666666> someName+</FONT></B>";
textInfo.setText(Html.fromHtml(info));

Related

Android dynamic views

I have a TextView that displays an error message beside 2 Buttons. They are currently inside a horizontal LinearLayout. The problem is if the TextView is too wide, the 2 Buttons will be pushed off the screen. Is it possible to push the elements downwards in those cases?
If the text is short there are no problems:
(Textview text) (Button1) (Button2)|(Edge of screen)
If the textview is long, I want to push the 2 buttons down a "row"
(Realllllllllllly long text that may|(Edge of screen)
span 2 lines)
(Button1) (Button2)|(Edge of screen)
I think you need to keep one more Linear layout below to your horizontal linear layout and need to check text size runtime if it's width is greater than required two button space then need to hide horizontal linear layout buttons and need to show below layout buttons
to refer how to check text size runtime refer below link :
Refer this link
Try this way: Use FlowLayout
<org.apmem.tools.layouts.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</org.apmem.tools.layouts.FlowLayout>
Inside FlowLayout you can put your view's and it will auto move to next line if not fit.
Yes you can do that, flexbox-layout is the solution.
How to use
Gradle dependency
dependencies {
implementation 'com.google.android:flexbox:0.3.2'
}
And xml code
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_alignSelf="flex_end"
/>
</com.google.android.flexbox.FlexboxLayout>
There are few other attributes also [read documentation], which you can try and find what works more suitable in you case.
you can use the TextView predefined method, to gave validation to end user like this
TextView textView=(TextView)findViewById(R.id.text_view);
textView.setError("Your Text is very wide please provide short text");
setError put red mark on textview view, with that we can tell the end user. provided text is wide

changing part of text size with java for android

As I add text to my text view with java, I want some text to be smaller and some to be bigger. Is there a way I can change the text size as I add it with java?
You can Use Spannable String for this purpose and for Tutorial you can see it here
Each text view has a value for the size of the text contained in that view.
Depending on what you are trying to do, you could add several text views in a row with different text sizes to simulate what it sounds like you want.
Use a relative view, add a text view to it and align it however you want with wrap_content for width and then just add the other text views using an alignment toRightOf previous textview.
Use webview and html to make a richtextbox like view. Text widget don't have this feature. Maybe there would be 3rd party widget for this also.
Create 2 separate TextView's, place them next to each other and set different text size on each:
TextView myTextView1 = (TextView) findViewById(R.id.my_text_view_1);
TextView myTextView1 = (TextView) findViewById(R.id.my_text_view_2);
float textSize1 = 20.0;
myTextView1.setTextSize(textSize1);
float textSize2 = 30.0
myTextView2.setTextSize(textSize2);
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/my_text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 1"/>
<TextView
android:id="#+id/my_text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 2"/>
</LinearLayout>

Writting text above textview not inside it in android

I have a textview, created inside a shape. I want to write text above this text view not inside it. !
I have added image for it. The outer rectangle displayed is a dialog box and inner rectangle is textview. : http://i.stack.imgur.com/tLUgM.png
My xml looks like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_gravity="top"
android:background="#drawable/mydialogbox"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
Now I want to write text above this inner rectangle and inside outer rectangle which is a dilog box.
You can't do that. You will have to add other TextView dynamically if you don't want to add it in the xml file.
And then set the text to newly added TextView. But the TextView must be there to hold text
use two textview one for shape and other one for text.

Fit content of textview after change text size

I have text view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/item_table_light_grey"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
</LinearLayout>
When I change size of text using code
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setTextSize(adapter.getTextSize());
width and height of textView not changed though text size changed successfully.
What am I doing wrong?
Android does NOT refresh layout of views with "wrap_content" once it has been displayed.
So if you add a child view, or modify the content dynamically, you're screwed.
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with "wrap_content". The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Another solution is to set fixed values to layout_width and layout_height, add gravity="center" on the TextView, and remove the wrap_content
Hope it helps!
For anyone might need this now and in the future, I did a trick to solve this. If your TextVew is inside a LinearLayout, eg. with orientation="vertical", then put these:
android:layout_height="wrap_content"
android:layout_weight="1"
to your TextView will make its height updated when the text change.
I don't know what happened inside, but this worked for me.
Another solution to this issue would be to remove the TextView programatically and then add it again.
linearLayout.removeView(textView)
linearLayout.addView(textView)
I know it sound stupid but it works.
In my case calling invalidate didn't work, only this worked.
Depending on your implementation you need to take care of view index inside its parent

Two TextViews shows exclusively on a line using XML

My ListView item has two TextViews. I want to show one of them at a time using XML. I don't want to do this programmatically. How to do it?
For example:
When TextView A has text, TextView B disappears. When TextView A is empty, TextView B appears.
My code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/groupNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="#color/ForestGreen"
/>
<TextView
android:id="#+id/topRatedPlaceNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="#color/Azure"
/>
</LinearLayout>
Sorry, you cant do that with xml, by Programmatically in your getView() of List's Adapter check like
if(textview1.getText().toString.length()>0)
textview2.setVisibilty(View.GONE);
else
textview1.setVisibilty(View.GONE);
TextView A has a text then in TextView B set setVisibility(View.VISIBLE) or setVisibility(View.VGONE) and same for TextView B. you do this in your CustomAdapter Class getView Method.
Thanks
Will you ever have both of them shown with text at the same time ? If not, and if you don't have style difference (text size/color), you could just use a single text view ?
Else, if A and B have different size / color / font / whatever, and assuming they will never be having text at the same time, using wrap_content as you do should ensure that A gets a width of 0 when it has no text, and B will take all the space. Else, if B has no text, A will take as much space as needed by its content.

Categories

Resources