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.
Related
I am creating an animation that would move, change content and size of Views in the Layout. I am using constraint layout. I have two xml files one for regular layout, one for layout that I need to transition to.
I need to change: Contraints, Text, Tex Size and Visibly of multiple views.
I tried using:
ConstraintLayout rootLayout = (ConstraintLayout) findViewById(R.id.main);
ConstraintSet newConstraint = new ConstraintSet();
newConstraint.load(this, R.layout.activity_selected);
TransitionManager.beginDelayedTransition(rootLayout);
newConstraint.applyTo(rootLayout);
However this doesn't update the text size, as it is not part of the ConstraintLyout class
I also tried using
selectedScene = Scene.getSceneForLayout(sceneRoot, R.layout.activity_selected, this);
TransitionManager.go(selectedScene, set);
But this does not produce a nice animation and required additional complications such as creating up setEnterAction() etc
Is there an elegant way to animate Constraint Layout change while changing text and text size of the views
When using constraint sets, you can only change the constraints of a view and not its properties like the text size, color etc. In order to animate those properties, I would prefer you to use the object animator instead. There you have lots of possibilities.
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
I need to draw custom borders in android to all of the views on my screen. Every view will have different parameters. To do this, I thought about making new CustomBotton, CustomTextView etc. classes and redefine their onDraw() methods. But the methods will contain the same code, so it's not nice to
make new classes for those Views and
rewrite the same onDraw() method with the same code.
Is there a more elegant/faster way to do this?
Do something like this for simple and fast solution (Doing things quick always has a performance trade off).
Dont deal with onDraw for anyview.
Make a single class extending LinearLayout.
2.1 Set Background of this Linearlayout as color of your border.
2.1.1 - More better read border color attribute from xml at runtime.
2.2 Set its padding as width of your border.
2.3 Set its width and height as wrap_content and wrap_content respectively.
Add single view to this LinearLayout. Either programatically or via xml.
<com.example.BorderLinearLayout >
<ImageView /> // or whatever. But a single view or viewgroup only
</com.example.BorderLinearLayout>
Hope this helps
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.
Please advice how to set fixed heigh (in dip) for the child view of ListView component?
I am using relative layout as root layout for the child view
when I set backgoround image to relative layout it becomes very height (maybe because backgoround picture is large) and I want to set precisely the height in dp.
Stumbled on this problem while looking for an answer to a somewhat related question.
Anyway, the problem is with inflating the view. In your getView (or newView if you're using CursorAdapter), when inflating a new layout, instead of doing
inflater.inflate(R.layout.your_layout, null)
do this instead:
inflater.inflate(R.layout.your_layout, parent, false)
Passing the parent view will make the child honor it's parents bounds, and false means you're instructing the inflater to NOT attach it to the parent. Setting a fixed height in dip will work after you do this.
For specifying the child height in dip via java code see following discussions:
1. setWidth in dip
2. how to specify padding in dip
3. Correct way to specify dimensions in Java code