How to Programmatically Add Views to Views

How to add view programmatically

Try this

View v = new View(activityContext);
v.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT), GetPixel(1, activityContext));
v.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));

and

// To convert pixels to dp units
public int GetPixel(float f , Context context)
{
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int)((f * displayMetrics.density) + 0.5f);
}

Adding views programmatically to LinearLayout but they don't appear

I believe the problem lies in this section of code:

public void generatePDF() {
LayoutInflater inflater = getLayoutInflater();
View pdfLayout = inflater.inflate(R.layout.simple_pdf_example,
findViewById(android.R.id.content),
false);

All of the rest of your code uses this inflated pdfLayout view:

LinearLayout linearLayoutView = pdfLayout.findViewById(R.id.linearLayoutView);

for (int i=0; i<10; i++) {
View v = getLayoutInflater().inflate(R.layout.printed_order_element2, null);
linearLayoutView.addView(v);
}
pdfLayout.draw(page.getCanvas());

The problem is that your pdfLayout view is never put on the screen. Your setContentView() call in onCreate() inflates a different layout, and the inflated pdfLayout is never attached to any view!

When you call inflate(int, View, boolean) and pass false as the third argument, you are telling the system to treat the second argument (the View) as the parent only in order to parse LayoutParams on the root of the inflated layout. The inflated layout is not added to the parent! You have to manually call parent.addView() with the inflated view.


How to fix it? It's hard to say, exactly, since I've got a few questions. But maybe just asking the questions will reveal the answer.

It's strange to see both a call to setContentView() and a call to inflate() that passes in android.R.id.content as the parent. Could you just call setContentView(R.layout.simple_pdf_example) in your onCreate(), and avoid the second inflate() call altogether?

If so, then you'd replace all calls to pdfLayout.findViewById() with direct findViewById() calls (since what was previously pdfLayout is now just your Activity's main content).

Add multiple custom views to layout programmatically

You can inflate the layout2.xml file, edit the texts, and add it to the first layout:

public class MyActivity extends Activity {

private ViewGroup mLinearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
mLinearLayout = (ViewGroup) findViewById(R.id.linear_layout);
addLayout("This is text 1", "This is first button", "This is second Button");
}

private void addLayout(String textViewText, String buttonText1, String buttonText2) {
View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, mLinearLayout, false);

TextView textView = (TextView) layout2.findViewById(R.id.button1);
Button button1 = (Button) layout2.findViewById(R.id.button2);
Button button2 = (Button) layout2.findViewById(R.id.button3);

textView1.setText(textViewText);
button1.setText(buttonText1);
button2.setText(buttonText2);

mLinearLayout.addView(layout2);
}
}

You may want to change android:layout_height of the layout2.xml root view to wrap_content.

If you are using ViewBinding, here is how it would look like for the addLayout function :

MyLayoutBinding binding = MyLayoutBinding.inflate(getLayoutInflater(), mLinearLayout, false);
binding.getTextView1().setText(textViewText);
binding.getButton1().setText(buttonText1);
binding.getButton2().setText(buttonText2);

mLinearLayout.addView(binding.getRoot());

How to programmatically add views and constraints to a ConstraintLayout?

I think you should clone the layout after adding your ImageView.

ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();

ImageView view = new ImageView(this);
view.setId(View.generateViewId()); // cannot set id after add
layout.addView(view,0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);```

Programmatically Adding views in inflated layout

Your LinearLayout needs to have height greater than 0 to add views at run-time otherwise it won't be able to calculate the height.

For example make it wrap_content

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="SUBSCRIPTIONS"
android:textColor="#F5D90A"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

Add views inside Constraint Layout and set them full screen PROGRAMMATICALLY

the issue is you didn't set width and height to your image view so by default wrap_content is taken for both. So setting them both 0dp will fix the issue . Add these lines to set the width and height to 0dp.

 mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));

One more thing , instead of getting parent id as constraintLayout.getId() you can use ConstraintSet.PARENT_ID. So final code looks like

 mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));
constraitLayout.addView(mImage,0);
ConstraintSet set1 = new ConstraintSet();
set1.clone(constraitLayout);
set1.connect(mImage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set1.connect(mImage.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set1.connect(mImage.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set1.connect(mImage.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set1.applyTo(constraitLayout);

hope this helps



Related Topics



Leave a reply



Submit