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
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);
Are you talking about gravitiy we can set this way, or a layout gravity you have tried actually?
tv.setGravity(Gravity.CENTER);
Try to set ViewFlippers width in xml as
android:layout_width="wrap_content"
I had an similar problem and this worked fine.
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);
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.
Related
Hi friends I am using LinearLayout with orientation horizontal to fill TextView's inside it programmatically.
Code:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout1);
for (int i = 0; i < 20; i++) {
TextView textView = new TextView(this);
textView.setText("Hello " + i);
linearLayout.addView(textView, i);
}
now, i am getting very weird look on the screen:
and I want this type of view:
views inside the LinearLayout should change their line according to device width. Anyone has any idea to achieve this, please help.
add text views to grid layout with setNumColumns().
I'm trying to put some TextView programmatically in my activity. The problem is that I cannot set their margins (top margin), so that there is some space between them.
The XML layout structure is the following:
<ScrollView>
<RelativeLayout_1>
<RelativeLayout_2>
<TextView/>
<EditText/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I omitted all the unnecessary information.
Then in my Activity I do
mLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativeParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.handicap_layout);
relativeParams.addRule(RelativeLayout.BELOW, R.id.handicap_layout);
TextView[] texts = new TextView[6];
for(int i=0; i<6; i++) {
texts[i] = new TextView(MyActivity.this);
texts[i].setText("Text " + (i+1));
texts[i].setTextSize(20);
relativeParams.setMargins(0, 10+(10*i), 0, 0); /* trying to increase the margin */
texts[i].setLayoutParams(relativeParams);
mLayout.addView(texts[i]);
}
where mLayout refers to RelativeLayout_1 and handicap_layout to RelativeLayout_2.
The problem is that the margins do not increase and the TextView are showed one above the other.
Has anyone a solution? Thank you!
EDIT: SOLVED
Ok, I managed to solve the problem: the LayoutParams have to be declared inside the for cycle.
Thanks.
Take one textview xml file. In that take margins. Once that is done inflate that textview.
TextView itemview = (TextView)getLayoutInflater().inflate(R.layout.equipment_item, null);
once that is done set your text to that layout.
I think you alredy find the relative layout right. Then add that itemview to relative layout like below.
equipmentdetails_layout.addView(itemview);
Then you can get the margins.
My question is how to set a textView style after adding it dynamically.
Here is the code:
LinearLayout layout = (LinearLayout)findViewById(R.id.linarLay);
TextView textView = new TextView(this);
textView.setText("TEST1");
layout.addView(textView);
I can see the text view that has been added but.. I need to style it now..
So far I tried this:
textView.setTextAppearance(getApplicationContext(),R.style.textStyle);
I tried this code after layout.addView(textView); and before it is just same doesn't change a thing..
Any idea/solution would be appreciated... Thanks
Style doesn't change because though you use the same TextView object to set style after adding it to Layout,it is not a part of layout. You have to get the View added,from layout using its id and when you change its style,it would directly be affected to your view on Layout.
Try this: (I have not tested but *should work)
LinearLayout layout = (LinearLayout)findViewById(R.id.linarLay);
TextView textView = new TextView(this);
textView.setText("TEST1");
textView.setId(999); // give some id
layout.addView(textView);
TextView tv=(TextView)findViewById(999);
tv.setTextAppearance(getApplicationContext(),R.style.textStyle);
I had a similar problem. I wanted to do the same with a button. You can set every property programmatically.
You can create a class with a set of methods like the one below:
private void setButtonStyle(Button b, String text)
{
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
b.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_button));
b.setGravity(Gravity.CENTER);
b.setText(text);
b.setLayoutParams(param);
b.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
b.setTypeface(null, Typeface.BOLD);
b.setShadowLayer(2, 1, 1, R.color.button_shadow_colour);
b.setTextColor(context.getResources().getColor(R.color.button_text_colour));
}
As you can see it is possible to set everything you need. As an example the param variable has 3 arguments in its contructor which are layout_width, layout_height and weight. So you can do the same with TextView.
deprecated since API 23:
textView.setTextAppearance(getContext(), R.style.Headline);
so choose:
textView.setTextAppearance(R.style.Headline);
So I want to center a text View inside a linear layout where I have 2 other objects that populate the linear layout. I have a imageView and another textView. How would I go about centering the textView so the text is in the middle of the screen? This is what I have so far.
View linearLayout = findViewById(R.id.rockLayout);
ImageView mineralPicture = new ImageView(this);
TextView mineralName = new TextView(this);
TextView mineralProperties = new TextView(this);
mineralProperties.setText("The properties are: " + Statics.rockTable.get(rockName).getDistinctProp());
mineralProperties.setId(2);
mineralName.setText("This mineral is: " + rockName);
mineralName.setGravity(Gravity.CENTER);
mineralName.setId(1);
mineralName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralProperties.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
mineralPicture.setId(3);
mineralPicture.setImageResource(R.drawable.rocks);
mineralPicture.setAdjustViewBounds(true);
mineralPicture.setMaxHeight(100);
mineralPicture.setMaxWidth(200);
mineralPicture.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
((LinearLayout)linearLayout).addView(mineralName);
((LinearLayout)linearLayout).addView(mineralProperties);
((LinearLayout)linearLayout).addView(mineralPicture);
I should mention I've tried doing such things as mineralName.setGravtiy(Gravity.CENTER); and it hasn't worked.
a) let the textview match_parent on the width and use gravity center
b) on the textview layout params set the layout_gravity to center (sorry for the terms I normally do this in XML)!
I have one table "TABLE_SUBJECT" which contain a number of subjects. I need to create
one horizontal scroll view with Subject.
How do I create a ScrollView with database items programmatically? If I enter 1o subject then it will be appear in scroll view as a button. Is it possible?
you may create it as below:
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);
if you have many elements first you need to wrap-up and add in the Scroll view; for example i need a many text view inside of scrollview, so you need to create ScrollView->LinearLayout->Many textview
ScrollView scrollView = new ScrollView(context);
scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textView = new TextView(context);
textView.setText("my text");
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.RIGHT);
linearLayout.addView(textView);
scrollView.addView(linearLayout);
this may help you.
HorizontalScrollView hsrll = (HorizontalScrollView)findViewById(R.id.hrsll);
b = new Button(this);
for (int i = 0; i < 5; i++) {
b.setWidth(LayoutParams.WRAP_CONTENT);
b.setHeight(LayoutParams.WRAP_CONTENT);
b.setText("b"+i);
b.setId(100+i);
hsrll.addView(b);
}
instead of for loop just modify the code as your need(no of records in db). but this the code for creating buttons in dynamically.
I was doing it like this:
Create xml with LinearLayout inside the ScrollView
Create xml as item in ScrollView
In activity set main content as xml with ScrollView
Loop through all table elements with adding new View to LinearLayout form main view
For me works fine.
In Kotlin you can use the below code
val scroll = ScrollView(context)
scroll.setBackgroundColor(R.color.transparent)
scroll.layoutParams = LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
)
scroll.addView(yourTableView)