When we have a TextView with a text of a certain size, it seems that if we add padding to the enclosing layout then the text wraps around to remain the same font size.
But does this mean that setting specific fontsize could cause issues in different screen sizes etc?
Because I was expecting somehow the text would be "resized" to stay in one line even if the font size was affected. Also it seems that if my text gets to have more characters the line will also wrap around and mess my layout.
How could I use then font and not mess up my layout if the characters displayed are more?
Update:
Basically my question is, how do we deal with TextViews that can take a string of arbitrary length and using fonts appropriately?
Generally speaking, I find that setting the layout_width and/or layout_height to wrap_content meets most of my needs. There are situations where the text length can get too long, though. In this case, there are a few different strategies that people use. The one you choose depends on your layout and what you are trying to achieve.
Dynamic resizing of text
You mentioned font size in your question so I will cover it first. You can use different values or dimensions to change the font size for different device sizes and orientations.
Font size of TextView in Android application changes on changing font size from native settings
Text size and different android screen sizes
How to set text size of textview dynamically for different screens
Doing this doesn't necessarily make the font fit the TextView, though. So some people try to resize the font programmatically. This doesn't seem to be a very mainstream solution in Android but here is a discussion:
Auto Scale TextView Text to Fit within Bounds
Auto-fit TextView for Android
How to adjust text font size to fit textview
Truncate the text
If the text is too long to fit in the TextView's layout and it isn't necessary to see the whole thing you can just cut it off at the end. You also have several options with android:ellipsize. Read these for more details:
Android: TextView automatically truncate and replace last 3 char of String
Android: Something better than android:ellipsize="end" to add "..." to truncated long Strings?
TextView: Get it to truncate without respect to spaces between words
Use a ScrollView
When I have a TextView that can potentially contain a lot of text I usually put it in a ScrollView. See this answer for more details:
https://stackoverflow.com/a/8532016/3681880
Write code like this.
At first declare a LinearLayout then declare a TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:gravity="center"
android:text="Hi I am Jay"
android:textSize="16sp" />
</LinearLayout>
Here gravity for LinearLayout denotes the TextView is in the center of LinearLayout and the gravity fot TextView denotes the text will be in center of TextView. You can change the textSize as 18sp or 20sp by writing this in textSize field.
Related
What's the recommended approach for ensuring an ImageView button is the correct size according to its parent?
I have a VideoView set to "match_parent" (screen width) and an ImageView over the VideoView that is 60dp x 60dp, which is fine on the screen I'm testing.
Problem is, if I rotate the phone and the VideoView is much smaller, the ImageView is no longer proportional and in the correct position. Same issue is true for larger screen sizes such as tablets.
I want the ImageView to always be the same proportional size and appear in the same position relative to the VideoView (its parent).
You should consider using layout_sum in android:
Since I don't know how you are exactly coding this, I can only provide assumptions based code. I am assuming that you would want to proportionately align an VideoView and ImageView in a parent. (Assuming you required 30% of the space to be occupied by the ImageView and the rest 70% of the space to be occupied by the VideoView).
You can code the XML file as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical"
android:layout_gravity="center">
<VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7"/>
</LinearLayout>
This will embed the VideoView and ImageView into the LinearLayout (Parent view).
Try to use RelativeLayout,wrapContent and avoid using hardCoded sizes.
Or better define a separate layout for Landscape and Portrait View that may solve your issue.
Even if your issue is not solved try using this library i frequently use it in my projects-SDP
I ended up doing this programmatically using ViewTreeObserver GlobalLayout listener. It works great and I only need it for one view on the screen, so not a big deal. Button appears in the correct position relative to its parent size even with the phone is rotated.
var topPadding = Convert.ToInt32(_videoView.Height * .25);
_buttonLayout.SetPadding(_buttonLayout.PaddingLeft, topPadding, _buttonLayout.PaddingRight, _buttonLayout.PaddingBottom);
So I've defined a simple Button in xml like so:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/home_button_student"
android:textSize="#dimen/abc_text_size_headline_material"
android:onClick="buttonClick"
android:id="#+id/home_button_student"
android:drawableLeft="#drawable/ic_action_emo_cool"/>
I have several of these in a LinearLayout and as expected they fill the screen, each taking up an equal amount of space, each one looking similar to this: http://i.imgur.com/fMNldop.jpg
The problem I'm having here is as you can see the drawable is only the same height as the text, I'd like to know if there's a way to make the drawable scale to the height of the button itself WITHOUT using a workaround like creating my own Button using another LinearLayout, I'd like to stick to using a Button.
I have different versions of the image in my res folder under the correct folders (xxhdpi, xhdpi, and so on) but it only seems to want to use the smallest one.
EDIT: It seems like this may be due to the image supplied being too small, as I said just above I have the image in a whole range of sizes, how can I tell android to use a larger one on a larger screen instead of defaulting to the smaller one? I thought that was the whole point of having multiple sizes in the first place.
The drawable will only be the size the bitmap is. Just provide larger image and it should be fine.
For that you need to change your layout_height from fill_parent to wrap_content like this
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/home_button_student"
android:textSize="#dimen/abc_text_size_headline_material"
android:onClick="buttonClick"
android:id="#+id/home_button_student"
android:drawableLeft="#drawable/ic_action_emo_cool"/>
If you want a button real big in height, then make sure you have an equal heighted drawable image.and also there's no need to give weight here,
you can use your custom layout for that like drawable left or right whatever you want just use linear layout put in this imageview and button for the view. so you can put image that you wants
I'm writing a custom Android view which contains an EditText. I want to be able to center the text vertically through Java code but it's not working.
As you can see from the image above, the text is not centering vertically. I set the EditText background to a light red so you can see the layout bounds of it. I have an EditText on the left of it rendered through XML code and it it working correctly.
How do I get the text to show correctly?
.
Here is my code for the middle EditText:
txtSearch = new EditText(context);
txtSearch.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f));
txtSearch.setGravity(Gravity.CENTER_VERTICAL);
txtSearch.setBackgroundColor(context.getResources().getColor(R.color.nowl_red_transparent_12));//Color.TRANSPARENT);
txtSearch.setHint("Search");
txtSearch.setHintTextColor(context.getResources().getColor(R.color.text_hint));
txtSearch.setInputType(InputType.TYPE_CLASS_TEXT);
txtSearch.setMaxLines(1);
txtSearch.setHorizontalScrollBarEnabled(true);
txtSearch.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
txtSearch.setTextSize(16f);
txtSearch.setEllipsize(TextUtils.TruncateAt.END);
txtSearch.setText("Y u no center");
container.addView(txtSearch);
and here is the code for the EditText on the left which renders correctly:
<EditText
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#color/transparent"
android:hint="test" />
I tried practically everything. I used setGravity(Gravity.CENTER_VERTICAL), removed/added properties, and more. Nothing of which worked. Is this a bug with Android or is there something I'm missing here? It looks like this in the UI Preview and in actual Android devices as well.
From the looks of it, it does look aligned to some degree. If it was top aligned, it would be on the far top with maybe 1dp of margin. If it was bottom aligned, it would be so high either.
I drew a green box on top of your original image to show what I think is what it is aligned relative to.
Given you have replaced the original background with a custom color, It might be possible that Android is pulling some unexpected default settings either from your context object or the system defaults and adds a padding (although I don't remember Android having any padding by default). Have you tried txtSearch.setPadding(0, 0, 0, 0) just in case?
It works well on my side with your code. The only difference is I don't know what is your contain like.
My advice is :
Try to remove the white background image which must be a 9 patch image
Open your device's developer options: Settings->Developer Options->Show layout bounds to debug your view. In this way, it'll be much easier for you to debug.
Add android:gravity="top" attribute to your EditText in the xml file.
How do I place the text inside an EditText widget at the top?
I have an android LinearLayout with a background image (9 patch). Inside the Layout there is a TextView.
How can I make the textView and its parent respectively when the text has more content
(as a result of translation and localization) ?
It should be enough to use "wrap_content" as width and height.
My application has a TextView, used for showing lots of data for the user. The data would be best represented in a structured, "semi-spreadsheet way", ie. some data would need to be always shown on topmost line of the view, some data on bottom-left corner, some on bottom-right corner, and the "middle" of my view would need to be reserved for other stuff.
As a minimum, I would thus need to reserve:
topmost row for certain data
bottom left corner for some
bottom right corner for some
the rest of my view to a rapidly changing data stream
Is there any UI component which would allow me to position my txt freely around the component, to achieve my goal? I need to keep this view (currently TextView) as one, since I need the view to be clickable for other purposes, and dividing the area into millions of small TextViews really is not the answer I'm looking for.
You can use RelativeLayout creatively to achieve what you need. There is no specific layout that will satisfy your needs. But RelativeLayout is something i can see that would serve your purpose if used creatively.
You can use this in your xml file for alinment.
android:gravity="left"
OR in java you can do
TextView textView = (TextView)findViewById(R.id.mytextviewid);
textView.setGravity(Gravity.RIGHT);
For aligning view inside relative layout you can do any of this
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
You have to use Relative layout in your layout xml file and position different textviews according to your need using align properties as described above.
Note: Android does not provide facility to write text in textview at particular position, what at most android provide is align text within textView to left/right/middle position using layout_gravity parameter, but this applies to whole text in textview.