Search code examples
androidandroid-layoutandroid-linearlayoutandroid-scrollview

adding a dynamic linearlayout to the current view android


I am trying to add a linearlayout to a scrolview this is my code the code compiles, but it doesn't show me the new layout

this is the original layout (that 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 that 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 the java code for adding the new layout is:

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 app is running but nothing happens is there a problem with one of the .xml files?

tnx


Solution

  • you should use an inflater, 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);
    

    The LayoutInflater creates a View object from the corresponding XML file.