Issue
I would like to add an unknown amount of progressbar to a linearlayout. (Like Whatsapp Status, and Instagram Stories) I can add progressbar to the linearlayout but the progressbar just shows max 7 progressbar.
My code so far:
while (i < 10){
i ++;
View child = getLayoutInflater().inflate(R.layout.stories_progress, null);
linearLayout.addView(child);
}
I would like to shrink the size of the progressbar programmatically, so when I have 2 progressbar, they fill the layout with 50%/50%, when 5, 10 or even 20 progressbar, they become smaller, all of them will be displayed on linearlayout I tried to get the current width of the linearlayout and divide by the number of progressbars, but the width of the linearlayout returns always 0;
My xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_layout_id"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
tools:context=".stories">
</LinearLayout>
My progressbar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ProgressBar
android:id="@+id/stories_progressBar_id"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ProgressBar>
</LinearLayout>
Solution
It is likely that you are not waiting for the top LinearLayout to have a size. You can wait for a size to be assigned by using a ViewTreeObserver.OnGlobalLayoutListener. Here is some code that should help:
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
linearLayout.removeAllViews();
int newWidth = linearLayout.getWidth() / 10;
int newHeight = linearLayout.getHeight();
int i = 0;
while (i < 10) {
i++;
LinearLayout child =
(LinearLayout) getLayoutInflater().inflate(R.layout.stories_progress,
linearLayout, false);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
lp.width = newWidth;
lp.height = newHeight;
linearLayout.addView(child);
}
}
})
Answered By - Cheticamp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.