Writing text to certain positions in a TextView - java

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.

Related

Centering Object In LinearLayout (Android)

To StackOverFlow,
I am currently developing an Android App, and am facing a issue. The issue is that when I place two view items under a parent (LinearLayout) it will not center one view to use the entire width of the parent. The reason for this (I beleive) is because when the second view uses parent_fill it already takes the width of the previous view into account. So it centers the "empty space". The code for the layout is below.
http://i.stack.imgur.com/byqG3.png
As you can tell it is a simple layout. The problem is shown below.
http://puu.sh/jiKIV/4a30800776.png
The problem is that the Title Bar ("Mah App") is not centering on the entire Linear Layout Bar.
So my Question is how do I make the Title Bar center across the entire Linear Layout even though there are other views are on there as well?
Thanks,
Thomas.
Layout Weight is the concept you are missing here. Look at this:
http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
In your case, you need to define android:layout_weight=1 for the view you want to center. This will make that view more important than the rest on the parent.
But really the best way to center views is to use a RelativeLayout instead of LinearLayout. Aligning possibilities are much better

How can we use TextViews and fonts correctly?

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.

Android - Center text inside EditText programmatically

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?

relative positions in android UI

i have a problem in designing user interface and layouts. i want that some views relates to others. for example a button related to a TextView and when i sat margin top, it calculates from that TextView not screen top. how i can do this?
Have a look at Relative Layouts. They are the answer to your question. You can relate a view to another by setting it toLeftOf, below, alignParentTop or any other combination.
Use a RelativeLayout and Views will be positioned relative to other Views.
From what you have written it seems like you could group the TextView and Button into a container view, like a RelativeLayout, and fit them inside there with the appearance you would like. Then you can set the margin of the Relative layout.
Edit: Clarity

Alternative to dp for text view alignment with background

I have a text view on my android app which I want to place in a specific place on the screen. This is because there is a background on the screen and the text view has to show in line with the space I have made for it in the background.
I'm moving the text around using the margins and entering for example 20dp for top margin 10dp for right margin.
Is there any other method I can use in order to ensure that the text view will be displayed in the right place on all devices? As other dvices mas have a higher or lower res.
use relativelayout and you can control the position of a view in a much better way.
android:layoutalign_parentright=true tag makes the view to be the right always.
android:align_right="#id/someViewId" makes the right edge of this view aligned with the right edge of someViewId view.
It packs a lot of tags that enables you to do such stuff.

Categories