-1

I add a custom view dynamically,but when I change the parameters (height,width), the view dissapear or if i just add it with the standard parameters it works normally.What's the problem? I'm trying to add the parts of this dynamically and affect new parameters to the parts.

xml code:

<RadioGroup
    android:id="@+id/prog"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:layout_gravity="center"
    android:layout_margin="10sp"
    android:orientation="horizontal"
    android:textAlignment="center" >
</RadioGroup>

Implementation code:

bar = (RadioGroup) getView().findViewById(R.id.prog);
int width = bar.getWidth();
RadioGroup.LayoutParams lparams = new RadioGroup.LayoutParams(width / 2,
                    height);
SegmentedControlButton part = new SegmentedControlButton(getActivity());
part.setLayoutParams(lparams);
bar.addView(part);

with one part it works but with 2 parts it disappears. I tried to check the value of width and it's 0 but i call the method in OnViewCreated so normally the view is prepared there.

PS: I use a fragment UPDATE: I tried this implementation in OnViewCreated but still 0:

ViewTreeObserver vto = bar.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                width = bar.getWidth();



            }
        });
user2137817
  • 1,825
  • 5
  • 28
  • 45
  • Some code demonstrating the problem would be very helpful here. – Bryan Herbst Sep 27 '13 at 14:59
  • I would use DDMS to dump the View hierarchy and check for your missing View. If it's there, what is the width an height? Is `bar.getWidth()` returning what you expect? – Bryan Herbst Sep 27 '13 at 15:17
  • yes because i tried 2 ways, the first one with buttons (+) and (-) to add and to remove parts, it works perfectly but when i use the classic way which is to simply call the method it disappears – user2137817 Sep 27 '13 at 15:22
  • You can get the width of the bar by `bar.getWidth()` and then make RadioGroup.LayoutParams with that `width` ( or `width/n`). – imranhasanhira Sep 27 '13 at 15:28
  • that's what i do, look at the code – user2137817 Sep 27 '13 at 15:29
  • Are you calling this in `onCreate()`, or any other method that runs before the views are actually measured and laid out? At that point, the width/height of them will be `0`. – Geobits Sep 27 '13 at 15:31
  • i call the method in OnviewCreated but i checked now that the width is null!! how to fix that? – user2137817 Sep 27 '13 at 15:32
  • try this : lp =part.getLayoutParams(); lp.setWidth()\Height(); part.setLayoutParams(lp); –  Sep 27 '13 at 15:45
  • ALready tried, the app crashes with a NPE – user2137817 Sep 27 '13 at 15:46
  • Sorry, I missed the `width` field. Geobits is right, the width & height properties are set once the views are created and get measured. You can use [ViewTreeObserver.OnGlobalLayoutListener](http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html) .Thanks. – imranhasanhira Sep 27 '13 at 15:58

1 Answers1

1

Firstly, use ViewTreeObserver to get the non-zero and non-null width of the bar. Example on how to do that is here.

You will have to replace

RadioGroup.LayoutParams lparams = new RadioGroup.LayoutParams(width / 2, height); 

with

RadioGroup.LayoutParams lparams = new RadioGroup.LayoutParams(0, height, 1.0);

By setting layout_weight property to 1, you don't have to do width/n etc. You just keep adding parts to the bar and the width of parts should automatically be set.

HTH.

Community
  • 1
  • 1
VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63