Reputation: 5400
I am adding dynamic text view to view flipper like below. Everything is working perfectly, but how can i center each text view at center of View Flipper, i have looked for the gravity option but i think it doesn't support. As the text view contains different text length i want every text view to be at center of view flipper.
for(int i = 0; i < 10; i++){
TextView tv = new TextView(this);
vf.addView(tv, i);
}
Thanks
Upvotes: 1
Views: 2146
Reputation: 9870
Try to set ViewFlippers width in xml as
android:layout_width="wrap_content"
I had an similar problem and this worked fine.
Upvotes: 1
Reputation: 5400
Ok i got the answer, actually you need to add linearlayout to viewflipper then add children to linearlayout with giving gravity parameter to linear layout.
Upvotes: 0
Reputation: 34301
You can do this way,
TextView tv = new TextView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.gravity = Gravity.CENTER;
tv.setLayoutParams(param);
Upvotes: 0
Reputation: 4908
I think this may help :
ViewFlipper flipper = new ViewFlipper(this);
TextView textView = new TextView(this);
textView.setGravity(Gravity.CENTER);
textView.setText("Hello World");
flipper.addView(textView);
setContentView(flipper);
Upvotes: 3
Reputation: 128428
Are you talking about gravitiy we can set this way, or a layout gravity you have tried actually?
tv.setGravity(Gravity.CENTER);
Upvotes: 1