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>
Related
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
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));
I have a widget that has a TextView of variable size based on the size of the parent, which ultimately descends from the size of the widget as adjusted by the user. I'd like the TextView to display as much text as possible, and then show ellipsis for the text that overflows the size of the TextView.
As far as I can tell, the android:ellipsize property only works if the number of lines is limited. If the lines aren't limited, then the text simply flows off the end of the TextView and is cut off.
However, since the size of the TextView is variable, limiting the number of lines using the
android:lines
or
android:maxLines
properties doesn't work for me.
I've seen a solution that involves adding a GlobalLayoutListener that dynamically calculates the number of lines that fit and then setting the lines to match, but that doesn't work in my case because the TextView is on a widget and I can't add a GlobalLayoutListener.
Is there any way that I can support multiple widget sizes/screen sizes/font sizes while simultaneously not allowing the text to overflow the container?
Edit: Here is my layout, in edited relevant part (I've removed irrelevant lines like color and moved in some style information like textSize):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="top"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="vertical"
android:background="#drawable/widget_frame"
android:paddingTop="3.0dip"
android:paddingBottom="5.0dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:textSize="#dimen/widget_text"
android:id="#+id/widget_text_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/widget_loading"
android:textSize="14.0sp"
android:ellipsize="end" />
</LinearLayout>
</FrameLayout>
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.
I've observed a behavior with layout_weight that I can't explain. The following is a trivial example:
<?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="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="This is a very very very very very very very very very very very very very very very long string."
android:layout_weight="1"
/>
<View
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:background="#ffffffff"
/>
</LinearLayout>
In a QVGA display, the TextView wraps the text. The white square is displayed to the right of the text.
However, if I remove android:layout_weight="1" from the TextView, the TextView now takes up the entire display width. The white square is no longer displayed.
Why would layout_weight in the TextView affect whether or not the white square is displayed? Shouldn't the View with the white background always be assigned 32dpx32dp first? (It makes no difference if the view were any other types - ImageView or TextView).
The problem I was working on is that I want the white square to always be displayed to the right of the TextView (whether or not the text is wrapped), but I don't want any empty space between the TextView and the white square. (If I add android:layout_weight="1" to the TextView, then there is a gap if the text is not wrapped.)
Any help would be appreciated!
To answer my question #1: One thing I learned by looking at the source for LinearLayout: Not only does layout_weight assign unused space to a child, it also shrinks a child with layout_weight if the child extends beyond the bounds of the LinearLayout. That explains why a TextView with wrapped text is shrunk in my layout.
As for the answer to my question #2, I think you meant android:toRigthOf instead of android:layout_alignRight. Using a RelativeLayout instead of a LinearLayout doesn't change the layout behavior. The tricky part is placing a view immediately to the right of a TextView, without gaps, whether or not the text is wrapped. Setting a maxWidth would limit the TextView's width, but that solution doesn't scale across portrait/landscape and different display dimensions.
Solution - Looks like Dyarish's solution is the best available. My layout problem exists regardless of the layout you use. The key is to set a maxWidth for the TextView so that it doesn't take up the all of the horizontal space in the layout. Because hardcoding a android:maxWidth value in the TextView doesn't scale across different displays, setting the maxWidth at runtime, as Dyarish suggested, is a good solution.
Hopefully this is what you are looking for.
First off, here is a great resource I found for Creating UI's.
layout_weight - Specifies how much of the extra space in the layout to be allocated to the View.
If you want to ensure that the white square is always to the right of the textview, you can use a Relative View, and add the parameter to the view. android:layout_alignRight="+id#yourTextViewID". This should always make the box appear right beside the textView area. You should probably also add something like android:maxWidth="250px" This will ensure that you don't push the white box completely out of the screen.
Here is a code sample:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:maxWidth="250px"
android:id="#+id/TextForWhiteBox"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:text="This is a very very very very very very very very very very very very very very very long string."
/>
<View android:background="#ffffffff" android:layout_width="32dp" android:layout_height="32dp" android:id="#+id/view1" android:layout_toRightOf="#+id/TextForWhiteBox"></View>
</RelativeLayout>
You could also add to the View:
android:layout_alignTop="#+id/TextForWhiteBox" android:layout_alignBottom="#+id/TextForWhiteBox"
to make the white box the same size as the TextView.
Firstly I've tested the code from my other answer and it does exactly what you've described you've wanted. (unless I'm misunderstanding what you are asking for). You definitely do not want to use the android:layout_alignRight which is not what is in the code sample. That would simply keep the box on the right hand of the screen and not be affected by the textview at all. This sample uses android:layout_toRightOf="#+id/TextForWhiteBox" which is possible due to it being a relative layout. Since the Relative Layout allows you to place objects in relation to others. That line will always place the box just to the right of the textview with no gaps.
As for the screen orientation changes:
When the orientation changes it creates a new instance of the view.
Here is a simple solution.
//Add to oncreate in your Activity
private TextView textStatus;
textStatus = (TextView) findViewById(R.id.TextForWhiteBox);
// This get's the width of your display.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
// Now you know the screen orientation, and it's width. So just set the maxwidth of the text view to match the display width - the pixels of your white box.
textStatus.setMaxWidth(width - 32); // 32 is here because you already know the size of the white box. More logic is needed to dynamically get this value, because you would need to wait for the activity to be fully created.
}
Here is the main.xml I used:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:id="#+id/TextForWhiteBox"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:text="This is a very very very very very very very very very very very very very very very very very very very long string."
/>
<View android:background="#ffffffff" android:layout_width="32px" android:layout_height="32px" android:id="#+id/view1" android:layout_toRightOf="#+id/TextForWhiteBox"></View>
</RelativeLayout>
You might need some additional logic to keep screen values.
This code has been tested, you should be able to literally copy and paste this to work as you asked.
Also depending on your logic you could use something like this to return the screen orientation.
int orient = getResources().getConfiguration().orientation;
Hope this helps!
If this helped you, please click the accepted button. =) Cheers!