To add textview dynamic at run time Android - android

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.

Related

How to add to linear layout in java (without XML)

I want to add a line of texts inside linear layout(Vertical)
and I want to add them in function in java
How to do this?
To add dynamically in Java write like this :
LinearLayout myLayout = new LinearLayout(MainActivity.this) ;
Do it like this :
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
//add layout params
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//add TextView inside the vertical LinearLayout
TextView textView = new TextView(this);
textView.setTextSize(16); //set the text size here
textView.setTextColor(Color.BLACK); // set the text color here
textView.setText("Texts"); // set the text here
textView.setTypeface(Typeface.BOLD); // set the text style here
linearLayout.addView(textView, p); // add the TextView to the LinearLayout
You can add any views inside the LinearLayout as much as you need.
Use addView :
LinearLayout layoutCard = new LinearLayout(getApplicationContext());
TextView text = new TextView(getApplicationContext());
text.setText("hello");
layoutCard.addView(text);

TextView inside layout with fixed height cutting off

I have a calendar view with events inside a table. An event is created with a LinearLayout and a textview inside:
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//My label
TextView label = new TextView(context);
label.setEllipsize(TruncateAt.END);
label.setPadding(4, 0, 0, 10);
label.setText(text);
ll.addView(label, layoutParams);
When I add my LinearLayout to the calendar view, I got this:
My label is cutted off because of the linearlayout height. I dont want to change the height of my event. So I have set the property ellipsize with TruncateAt.END in my label in order to avoid this issue, but the problem persists. How can I have my label truncated at the end with (...)?
Add setIncludeFontPadding() to your TextView...this will remove all default padding from TextView.
label.setIncludeFontPadding(false);
One more thing, you have added ellipsize to your TextView but you didn't specify the length of the TextView after which the ellipsize should take effect. You can set that condition using any one of the following...
label.setSingleLine(true);
or
label.setMaxLines(2); //here maximum line 2

How to add textview dynamically in android at particular positiion

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)

how to align text view to the right of Image view within Linearlayout Dynamically in android

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout3);
TextView tv = new TextView(this);
ImageButton imb=new ImageButton(this);
ll.addView(tv);
tv.setSingleLine();
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setHorizontallyScrolling(true);
tv.setFocusableInTouchMode(true);
tv.setText(myString1);
This is my code and I'm able to display data in textView now. But I want to put one imageButton to the left of textView so that will work as cancel strip. but I'm having a problem. How can I set imageButton left of text view programatically?
You have two solutions for your question. You can use the below code if you just want to show image on the right of textview.
TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon,0, 0,0);
or else
set orientation of your linearlayout as Horizontal and add the imageview first and then textview second. So they will be aligned as your requirement.
Below is the example.
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton imb=new ImageButton(this);
imb.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(imb);
ll.addView(tv);

How to center a textView with java code inside a linear layout

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)!

Categories

Resources