Adding a dynamic linearlayout to the current Android view

I'm trying to add linearlayout to scrolview, this is my code, the code is compiling, but it does not show me a new layout

this is the original layout (which I want to add to it)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:layout_margin="15dp" android:layout_marginTop="15dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:id="@+id/ViewHistoryImageLayout"> <ImageView android:id="@+id/HistoryImage" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="0.76" android:gravity="center" android:padding="10dp" android:src="@drawable/upload" android:contentDescription="@string/HistoryImage"/> <TextView android:id="@+id/TranslatedText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.12" android:paddingBottom="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="@string/translateImageButton" /> </LinearLayout> 

and this is the layout I want to add several times:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/TranslationMenuLayout" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> <RatingBar android:id="@+id/ratingBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numStars="5" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> 

and java code to add a new layout:

 setContentView(R.layout.activity_view_history_image); ScrollView sv = new ScrollView(this); LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE ); View ll = inflater.inflate(R.layout.translation_menu, null); sv.addView(ll); 

The code compiles fine and the application works, but nothing happens. Is there a problem with one of the .xml files?

Tnx

+4
source share
3 answers

you must use a blow, change:

 LinearLayout ll = new LinearLayout(R.layout.translation_menu); 

with

 LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE ); View ll = inflater.inflate(R.layout.translation_menu, null); sv.addView(ll); 

LayoutInflater creates a View object from the corresponding XML file.

+4
source
  LayoutInflater li = LayoutInflater.from(context); LinearLayout ll = (LinearLayout) li.inflate(R.layout.translation_menu, this) 

This is the right way.

+1
source

Use LayoutInflator .

Class

LayoutInflater used to instantiate an XML file into the corresponding View objects.

In other words, it takes XML as an input file and builds View objects from it.

 ScrollView sv = new ScrollView(this); LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE ); LinearLayout ll = (LinearLayout) li.inflate(translation_menu, this); sv.addView(ll); 
+1
source

All Articles