I have a LinearLayout that I am extending and I want to add a ViewFlipper and Views in it dynamically. Can anyone tell me why this is showing a blank screen?
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
ViewFlipper vf = new ViewFlipper(context);
vf.layout(l, t, r, b);
this.addView(vf);
TextView tv = new TextView(context);
tv.setText("FOO");
tv.setBackgroundColor(Color.WHITE);
vf.addView(tv, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
asked Mar 19, 2012 at 20:29
user895400
3681 gold badge7 silver badges19 bronze badges
2 Answers 2
I made this sample code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
ViewFlipper vf = new ViewFlipper(this);
vf.layout(10, 100, 100, 150);
LinearLayout ll = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("Prueba");
ll.addView(tv);
ll.addView(vf);
TextView tv2 = new TextView(this);
tv2.setText("FOO");
tv2.setBackgroundColor(Color.WHITE);
vf.addView(tv2, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
setContentView(ll);
}
And obtained the desired result, not the one you found out. Maybe you should check the filling type stuff inside you layouts.
Sign up to request clarification or add additional context in comments.
Comments
Have you overriden the onMeasure() method? I think you need to do this otherwise the View is not correctly sized. Someone please correct me if I am wrong
answered Mar 19, 2012 at 20:39
Mimminito
2,8533 gold badges23 silver badges28 bronze badges
Comments
default
LayoutParamsto your ViewFlipper as well.onLayoutlooks like a bad idea to me. Are you sure that you want to have different layouts depending on the size of your custom LinearLayout. Currently you would add new ViewFlippers to your layout whenever it has to re-layout itself which can happen several times.onCreatewithout extending a LinearLayout (I guess that is what you should do) - if you want a custom View or Widget (like a special ProgressBar) / ViewGroup (like a special LinearLayout that somehow manages it's childviews) then have a look here.