TextViews in a container - java

Okay so I have 3 TextViews (top, divider, botom) that form a stacked fraction (like 3/4 but the 4 is below the 3 with a horizontal line dividing them).
The problem is the horizontal line... because all the dynamic creation of these elements is done in the onCreate method, I cannot use .getWidth() and .setWidth() to limit the size of the line (it is supposed to be as long as the longest textview, either above or below it).
Now my question is: is it possible to make a container with the three TextViews stacked neatly one above the other, that could be easily added to an existing layout, like a box that would contain the three views and that would function as it's own layout in which I could set the line width, the textview's text align to center etc?

As was suggested by #Inn_vita, you can add all three to a Linear Layout (as opposed to, say, a Relative Layout) and then treat the layout separately. However, just be careful in that nesting layouts within each other unnecessarily can create overhead and slow your application.
Note, though, that you can change the size of your TextViews programmatically after your setContentView(R.layout.activity_main).
I think this code snippet may help with what you are looking for:
setContentView(R.layout.activity_main);
TextView numerator = (TextView) findViewById(R.id.firstBox);
TextView fractionBar = (TextView) findViewById(R.id.secondBox);
TextView denominator = (TextView) findViewById(R.id.thirdBox);
fractionBar.setWidth(denominator.getWidth());
You can do all this after your setContentView() within the onCreate() method.

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

Writing text to certain positions in a TextView

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.

Programmatically set view width

I am developing an android app. There are six main long variables that are displayed on-screen at any time. These are continually incremented using a timer on a separate thread. The user can watch these variables increase on screen. Each of these variables has a maximum amount.
What I'd like to do is draw a 'progress bar' for each of these variables. At the moment, I am using a View with a solid red colour.
Please see below for prototype:
The red bar on the left would represent a variable that has reached it's maximum amount, whilst the others are empty and the bar has a width of 0, so is invisible. Each variable is placed in a RelativeLayout (which represents one 'section'), which is then placed in a LinearLayout. At a future point I may need to add/remove some - so the solution needs to not rely on hard-coded layout positionings.
My question is how can I programmaticaly set the width of these 'progress bar's in code, whilst not hard-coding the layout co-ordinates of any of the variables?
You should check http://developer.android.com/reference/android/widget/ProgressBar.html ProgressBar. It will ease you with displaying the progress.
In the case of LinearLayout (and any ViewGroup for that matter) you can generate your own LayoutParams programatically:
LayoutParams params = new LayoutParams(width,height) // in px
yourLinearLayout.setLayoutParams(params);
you will need to cast the LayoutParams to the according ViewGroup type.
The regular way would be to use the ProgressBar, but if you want to use your custom view, use this:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(withInPx, heightInPx);
yourView.setLayoutParams(lp);
This assumes you use LinearLayout, but you should edit that if you use a different layout (i.e. RelativeLayout)
The heightInPx will be fixed in your case, but the withInPx you can vary, according to the progress.

edittext inside an imageview

i have a edittext image that i created in photoshop. then i bring it into my app and put a edittext inside it(my imageview is a frame for my edittext). in fact, i put these two inside a relativelayout and my problem is that when i test my app in twho screen sizes, these two (imagebox and edittext) doesnt resize in the same value and always my edittext resizes much more than my imageview. i want these two in every situation stay together.
The best way in my opinion is to convert your image to a 9-patch, with the padding information matching the part of your image containing the actual input zone.
You can then set the 9-patch as the background of your TextView or EditText. You don't need any container to hold the image. You can even use a style for re-use.
The Android system will hande the resizing of the background, so you just don't have to do anything special after that
You want to be using a FrameLayout rather than a RelativeLayout if you're planning on using images.

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

Categories