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

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

Related

Make TextView appear above another TextView

Hello I am trying to make 2 TextViews apear above each other through code.
My question is:
How can I stack them over each other?
Sample Code
private TextView DurrationView;
private TextView DurrationViewoverall;
DurrationView = new TextView(getContext());
DurrationView.setText("");
addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("");
addView(DurrationViewoverall);
I am trying to have DurrationView appear above DurrationViewoverall.
The class they are in extends Linear Layout.
Add LayoutParams to the textview and make your linear layout as orientation vertical
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
DurrationView = new TextView(getContext());
LinearLayout.LayoutParams durrationViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationView.setLayoutParams(durrationViewParams);
DurrationView.setText("Text1");
linearLayout.addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
LinearLayout.LayoutParams durrationViewoverallParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationViewoverall.setLayoutParams(durrationViewoverallParams);
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("Text2");
linearLayout.addView(DurrationViewoverall);
addView(linearLayout);
Check that the LinearLayout is set to have a vertical orientation. If that is set properly, you may need to set the LayoutParams of the TextViews programatically as well.

To add textview dynamic at run time 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.

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 multiple different text views in linear layout

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

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