8

I have 2 xml file like this:

main.xml:

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    </LinearLayout>
</HorizontalScrollView>

items.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl"
android:layout_width="100dp"
android:layout_height="100dp">

   <ImageView
    android:id="@+id/img"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:contentDescription="@string/app_name"
    android:gravity="center"
    android:src="@drawable/icon_shop2" />

   <TextView
    android:id="@+id/providerName"
    style="@style/WhiteTextMedium"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img"
    android:layout_gravity="bottom"
    android:paddingLeft="10dp"
    android:text="Example" />

</RelativeLayout>

main.xml is used for MainActivity. My question is how to dynamically load 3 layouts items.xml to Linearlayout in the HorizontalScrollView of main.xml!

3
  • 1
    Inflate your Items layout and add one by one to your HorizontalView... Commented Sep 17, 2014 at 7:14
  • Can you give me source code so far...! Commented Sep 17, 2014 at 7:17
  • @HT No i have not..I just give you an idea...Now do the search on Google ....you'll get code..... Commented Sep 17, 2014 at 7:19

2 Answers 2

12

Try this way,hope this will help you to solve your problem.

LinearLayout ll;
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   ll = (LinearLayout) findViewById(R.id.ll);
   for (int i=0;i<3;i++){
        View view = LayoutInflater.from(this).inflate(R.layout.items,null);
        ImageView imageView =  view.findViewById(R.id.img);
        TextView providerName =  view.findViewById(R.id.providerName);
        // Assigning value to  imageview and textview here
        ll.addView(view);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help you can you please upvote ans if it is useful to you ?
1

There are two ways for doing so:

Way to Left (using xml if the number of items is fixed) :

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">

          <include
             android:id="@+id/item1"
             layout="@layout/items.xml"/>

          <include
             android:id="@+id/item2"
             layout="@layout/items.xml"/>

          <include
             android:id="@+id/item3"
             layout="@layout/items.xml"/>

    </LinearLayout>


</HorizontalScrollView>

Way to Right (In Activity using layout Inflator, if the number of items is dynamic) :

setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

View v = inflater.inflate(R.layout.items, ll);

for (short i=0;i<3;i++)
    ll.addView(v);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.