How to add a new TextView to a layout in run time? is it possible?
Complete solution:
View parent; //parent view where to add
ViewGroup layout=new LinearLayout(context);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tv1=new TextView(context);
tv1.setText("Test1");
TextView tv2=new TextView(context);
tv2.setText("Test2");
layout.addView(tv1);
layout.addView(tv2);
parent.addView(layout);
Programmatically adding TextView (or View in general) to layout (or ViewGroup in general) is possible, check ViewGroup's public void addView (View child, int index) method.
Related
I want to make textView programmatically and make them scrollable. I am calling this method again and again to get textView and adding it to the linearLayout.
TextView textView = addTextView(contents.paragraphs.get(i),false);
linearLayout.addView(textView);
But, it is not scrollable at all. The method is:
private TextView addTextView(String text,boolean type) {
TextView valueTV = new TextView(this);
valueTV.setText(text);
valueTV.setTextColor(getResources().getColor(R.color.colorTextPrimary));
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
valueTV.setMaxLines(1000);
valueTV.setVerticalScrollBarEnabled(true);
valueTV.setMovementMethod(new ScrollingMovementMethod());
return valueTV;
}
Change to:
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
If you use WRAP_CONTENT the TextView will increase height 'indefinitely'.
You can wrap the TextView into a ScrollView
ViewGroup linearLayout = findViewById(R.id.linear_layout);
TextView textView = addTextView(contents.paragraphs.get(i), false);
ScrollView scroller = new ScrollView(getApplicationContext());
scroller.addView(textView);
linearLayout.addView(scroller);
I have 2 TextViews and I want to add those to LinearLayout, but when I ran the project, only one TextView appreared.
Here's my code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Thank you, Jesus!");
textView.setTextColor(Color.BLACK);
TextView textView2 = new TextView(this);
textView.setText("Dont give up on me!");
textView.setTextColor(Color.BLACK);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.WHITE);
layout.addView(textView);
layout.addView(textView2);
setContentView(layout);
}
}
After running, textView2 was the only view present in the LinearLayout.
Can someone explain to me what was going on?
Use textView2 for calling setText and setTextColor method for textView2 because currently you are using textView :
TextView textView2 = new TextView(this);
textView2.setText("Dont give up on me!");
textView2.setTextColor(Color.BLACK);
Suggestion also set height/width for all views by calling setLayoutParams method
Another Suggestion : Add Orientation to Linear layout by using: layout.setOrientation(LinearLayout.VERTICAL);
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);
I want my TextView to appear below my ImageView in a RelativeLayout that is going into a GridView. I've tried:
public override View GetView(int position, View convertView, ViewGroup parent)
{
RelativeLayout rl = new RelativeLayout(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
ImageView imageView;
TextView tv;
imageView = new ImageView(context);
imageView.LayoutParameters = new GridView.LayoutParams(250, 250);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(8, 8, 8, 8);
imageView.SetImageResource(thumbIds[position]);
imageView.Id = position;
lp.AddRule(LayoutRules.Below, position);
lp.AddRule(LayoutRules.CenterHorizontal);
tv = new TextView(context);
tv.Text = stringIds[position].ToString();
tv.SetTextSize(Android.Util.ComplexUnitType.Dip, 20);
tv.SetTextColor(Android.Graphics.Color.WhiteSmoke);
tv.LayoutParameters = lp;
rl.AddView(imageView);
rl.AddView(tv);
return rl;
}
But the TextView always shows up on top of the ImageView.
Check out this question.
When you call rl.AddView(tv), you should include the LayoutParams rl.AddView(tv, lp).
You have to use
imageView.setLayoutParams(lp) in order to assign the params to your view.
How do you setLayoutParams() for an ImageView?
Since my content is not dynamic, I worked around the issue by simply editing my image in Paint and placing text below the image. Then I made that the background for the ImageView.
I want to dynamically add some views to a LinearLayout that is already defined in XML.
I'm able to add views to the screen but they are not being placed 'inside' the right LinearLayout.
How can I get a reference to this specific Layout from code, similar to getting a View by using findViewById()?
As Marcarse pointed out you can do
ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);
The LinearLayout class extends ViewGroup which itself extends the View class. This makes acquiring a reference to a layout as easy as getting a reference to another View.
This should work:
LinearLayout layout = (LinearLayout) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);
Better Version:
To add another layout in activity/Fragment
LayoutInflater mInflater = LayoutInflater.from(getActivity());
View mProgressBarFooter = mInflater.inflate(R.layout.spinner, null, false);
textLoader = (TextView) mProgressBarFooter.findViewById(R.id.footer_label);
Happy Coding(-: