0

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
5
  • try setting appropriate LayoutParams to your ViewFlipper as well. Commented Mar 19, 2012 at 20:39
  • Please provide the Activity too, otherwise it is hard to tell. Commented Mar 19, 2012 at 20:44
  • Changing the layout while in onLayout looks 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. Commented Mar 19, 2012 at 21:15
  • @zapl Sorry I'm not sure where the right spot is to do it then. I haven't been able to find a good resource on this. Can you tell me where to do this instead? Commented Mar 19, 2012 at 23:49
  • What are you trying to achieve? If you want an Activity has has a ViewFlipper then do it in the Activities onCreate without 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. Commented Mar 20, 2012 at 0:12

2 Answers 2

4

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.

answered Mar 19, 2012 at 20:45
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.