I am new with android. I have add textview dynamically in .java file with text apple but I want to add it on the top. How can I do that.
This is my code......in .jave file
TextView lblname;
LinearLayout linearlayout;
lblname = new TextView(this);
linearlayout = (LinearLayout) findViewById(R.id. linearlayout);
linearlayout.addView(lblname);
Thank You in advance..
modify your last line to:-
linearlayout.addView(lblname, index);
replace index with the position you want to add the view i.e, 0 if in the beginning or linearlayout.getChildCount()-1 if prior to the last view in you layout
Try this
lblname = new TextView(this);
lblname.setText("APPLE");
lblname.setId(5); // id should be unique
lblname.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linearlayout.addView(lblname,index);//specify your index
Simply specify the index where you want to add the child, as a second parameter for the addView.
For example, to add it at the top:
linearlayout.addView(lblname, 0);
See reference:
http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, int)
Related
I want to add Text-view at run time on Android to display the contents of my database on a activity,which can change every time.
Please Help
You could create a TextView dynamically like this for example:
//create a TextView with Layout parameters according to your needs
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//if your parent Layout is relativeLayout, just change the word LinearLayout with RelativeLayout
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
//get the parent layout for your new TextView and add the new TextView to it
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
linearLayout.addView(tv);
And in the tv.setText(...); line you could set the text, that your got from your database.
Hi I was want to add 2 different text views to my linear layout but somehow when i try to add both of them only the first one appears why is this the case? Here is my code :
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// create the text view for the main string to
// be displayed
TextView displayMainText = new TextView(this);
displayMainText.setTextSize(15);
displayMainText.setText(mainString);
displayMainText.setLayoutParams(layoutParams);
displayMainText.setPadding(0, 20, 0, 0);
// Create the text view for the optional string to
// be displayed
TextView displayOppText = new TextView(this);
displayOppText.setTextSize(15);
displayOppText.setText(optionalString);
displayOppText.setLayoutParams(layoutParams);
displayOppText.setPadding(0, 20, 0, 0);
// add text views to the layout
LinearLayout studyTLayout = (LinearLayout) findViewById(R.id.study_time_layout);
studyTLayout.addView(displayMainText);
studyTLayout.addView(displayOppText);
setContentView(studyTLayout);
With my code it only adds the first text view correctly
It seems like you didn't set the orientation of the LinearLayout to Vertical.
studyTLayout.setOrientation(LinearLayout.VERTICAL);
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);
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)
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.