Android: ListView fixed height of child view - java

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

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

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.

Android ViewAnimator height changing base on child

By default, the max size of all children is used as the VeiwAnimator's layout size.
How to create ViewAnimator, which height changes depending on the size of displaying child?
Either by specifying the android:measureAllChildren="false" as a ViewAnimator XML attribute or viewAnimator.setMeasureAllChildren(false) in code.

Off set android view while using android:layout_centerVertical="true"?

I'm trying to get a view center vertically in android, but I have a imageView at the top that is 75dps, so I need my centered view to be offset from the top by 75dps so it gets centered! Is there a way to do this? I tried using android:layout_marginTop="75dp" but it didn't work! Any ideas?
You could wrap your view into a ViewGroup (e.g. LinearLayout), then make the LinearLayout centered, and add the marginTop to your view. It should work.
Anyway, it would be helpful if you post also your layout xml.

What View subclass has include?

When I use include in a layout, I have to set width and height both to include tag and to the layout it is linked to. Otherwards it won't work correctly. So, it seems, that include is not a link, but a subclass of View, if it has its own width, height, id and so on. But on developer's site there is not such class as include. Who are you, Mr. include?
The <include> tag can be thought of as a copy/paste of the internal layout. It's part of the xml parsing that's in the View Inflater classes. It effectively takes the layout ID, inflates the xml object you pass in, then adds it to the main layout that it's inflating based on the attributes of the included layout. You can use it on anything that inherits from View.
You can overwrite any of the attributes that have layout_ in them. You don't have to overwrite the width and height attributes if they are included in the root of the layout your including. However, you do have to overwrite those parameters if you are overwriting any other attribute. It's part of a bug in how <include> is parsed. If you want to change, say, layout_marginLeft, you would have to overwrite layout_width and layout_height to the same values that are in the layout in order for the parser to overwrite the left margin attribute.
include doesn't actually need a width and height if you specify the width and height of the root element of the included layout. In other words, this is entirely valid:
<include layout="#layout/my_included_layout" />
So long as my_included_layout has a root element with a width and height it will be happy.

Categories