Android - Dynamically Add Views into View

Android - Dynamically Add Views into View

Use the LayoutInflater to create a view based on your layout template, and then inject it into the view where you need it.

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);

// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");

// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

You may have to adjust the index where you want to insert the view.

Additionally, set the LayoutParams according to how you would like it to fit in the parent view. e.g. with FILL_PARENT, or MATCH_PARENT, etc.

Android: How to dynamically add a Custom View into a linear layout

Use the LayoutInflator to create a view based on your layout template, and then inject it into the view where you need it.

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);

// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");

// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

You may have to adjust the index where you want to insert the view.

Source : Android - Dynamically Add Views into View

How can I add Views dynamically in a Relative layout or in any Layout

Try this:

//-----Main Layout-----\\
LinearLayout l1 = findViewById(R.id.myLinear);
ll.setOrientation(LinearLayout.VERTICAL);

//-----First layout-----\\
LinearLayout l11 = new LinearLayout(this);
l11.setBackgroundColor(android.R.color.holo_orange_dark);
l1.addView(l11, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

//-----Second layout-----\\
LinearLayout l12 = new LinearLayout(this);
l12.setBackgroundColor(android.R.color.holo_green_dark);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(-30, 20, 20, 0); //Negative margin top to superposition views
l1.addView(l12, layoutParams);

Add and Remove Views in Android Dynamically?

ViewParents in general can't remove views, but ViewGroups can. You need to cast your parent to a ViewGroup (if it is a ViewGroup) to accomplish what you want.

For example:

View namebar = View.findViewById(R.id.namebar);
((ViewGroup) namebar.getParent()).removeView(namebar);

Note that all Layouts are ViewGroups.

Dynamically adding a View to a LinearLayout with height of wrap_content doesn't display anything

You need to provide measurements of ImageCanvas with an onMeasure() override:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(icon.getWidth(),icon.getHeight());
}

See Custom View Components.

When you explicitly supply a height, that height is set in the custom component. With wrap content, the height gets set to zero since it is neither specified nor explicitly measured. The above code will provide the correct measurements.

(icon.getHeight() is what you really need and corresponds to wrap_content. You may need a different value for icon.getWidth() depending upon your design.)

Not sure why a NestedScrollView has any effect, but the code above will help.

adding the views dynamically with layout weight property in Android

Problem is with your layout params. You need to use like below

   private void addLayout(String textView1Text, String textView2Text, String checkBoxText) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
mLinearLayout.setLayoutParams(param);

TextView tv1 = new TextView(this);
tv1.setText(textView1Text);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 0.1f;
tv1.setLayoutParams(lp);
mLinearLayout.addView(tv1);


TextView tv2 = new TextView(this);
tv2.setText(textView2Text);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, 100);
lp1.weight = 0.6f;
tv2.setLayoutParams(lp1);
mLinearLayout.addView(tv2);

CheckBox cb = new CheckBox(this);
cb.setText(textView2Text);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(0, 100);
lp2.weight = 0.3f;
cb.setLayoutParams(lp2);
mLinearLayout.addView(cb);

}


Related Topics



Leave a reply



Submit