I have LinearLayout in xml:
<LinearLayout
android:id="@+id/progress"
android:layout_width="fill_parent"
android:layout_height="@dimen/progress_height"
android:layout_alignParentBottom="true"
android:baselineAligned="false"
android:orientation="horizontal" />
and I would like to generate dynamically few another LinearLayouts and put them to "progress" equaly spaced, for example:
Every LinearLayout will have random background color I wrote something like this:
mProgress = (LinearLayout) findViewById(R.id.progress);
.
.
.
LinearLayout prog = new LinearLayout(this);
prog.setBackgroundColor(CommonUtils.getNextRandomColor());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
prog.setLayoutParams(params);
mProgress.addView(prog);
When user press the buttons, another LL will generate, with different color.
My method doesn't work. There is no background color in layouts.
Maybe there is another much simpler method to achive some kind of progress bar with colors sharing some space equally?
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
Double check that getNextRandomColor
is returning something like.-
getResources().getColor(colorResId);
and not just a colorResId
. If that's the case, you could try this.-
prog.setBackgroundColor(getResources().getColor(CommonUtils.getNextRandomColor()));
Anyway, if you're building a multicolor progress bar, you should consider changing the width of a single layout, and using a gradient color.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With