Michael Jones Michael Jones - 8 months ago 29
Android Question

Draw background height depending on a given value Android

so I have just started developing in Android about 3 days ago. So therefore, please bare with me.

Simply, I would like to have a background on my app that changes color depending on a given number. Let me elaborate some more. For example, I pass the number 40 out of 100, 2/5s of the screen should be x color, and the other 3/5s should be the other color. Here are two little diagrams:

40 / 100 40/60
+---+ +---+
| | 3/5 not filled in | | 1/3 not filled in
| | |...| 2/3 filled in
| | |...|
|...| 2/5 filled in +---+

So I was thinking I could go about making a dynamic background (giving a certain number) by drawing in the shapes. The problem is, I am not exactly sure how I would go about doing this. Where do I place the code to draw the shapes, and how exactly would I insert them into my XML file in the correct place (I already have a static color background that is up in my XML)?


Ok, you need to create an activity with two Linear Layouts. The width attribute will be set to match_parent and the height to 0dp. You will then be able to set the height with a layout weight of X %.

As an example, those layouts split the screen 25% 75% :





Give them an id to change values programmatically (weight and background color).

You can do it this way :

LinearLayout top = findViewById(R.id.top);

//Third Param stands for weight
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.MATCH_PARENT, 0, 1.0f);