Search code examples
javaandroidxmlandroid-widgetremoteview

RemoteViews and addView (horizontal) : only the first item (view) is showing


I working on a widget and I have a trouble with adding view into my RemoteView.

Only the first item (view) added is showing.

PS : I need to add views horizontally.

This is my loop for adding views into my RemoteViews :

RemoteViews mainView = new RemoteViews(getPackageName(), R.layout.widget);
mainView.removeAllViews(R.id.view_container);
for (int i = 0; i < Constant.max; i++) {
   RemoteViews newView = new RemoteViews(getPackageName(), R.layout.item);
   newView.setImageViewBitmap(R.id.logo, Constant.data[i].getLogo());
   mainView.addView(R.id.view_container, newView);
   Log.e("Item added : ", Constant.data[i].getName());
}

widget.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="center">

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

   </LinearLayout>

</RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="@dimen/margin_between"
       android:layout_marginRight="@dimen/margin_between"
       android:background="#F0F0F0"
       android:orientation="vertical"
       android:padding="2dp">

       <ImageView
           android:id="@+id/logo"
           android:layout_width="48dp"
           android:layout_height="48dp"
           android:layout_gravity="center"
           android:src="@drawable/ic_android_black_24dp" />

   </LinearLayout>

</LinearLayout>

If you have any idea to solve this problem.. thanks in advance.


Solution

  • Try to change the width of your parent layout in item.xml to wrap_content.

    Because with fill_parent it will cover the whole width of your container (view_container) and there wont be an available space for the other items.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="fill_parent">
    
       <LinearLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="@dimen/margin_between"
           android:layout_marginRight="@dimen/margin_between"
           android:background="#F0F0F0"
           android:orientation="vertical"
           android:padding="2dp">
    
           <ImageView
               android:id="@+id/logo"
               android:layout_width="48dp"
               android:layout_height="48dp"
               android:layout_gravity="center"
               android:src="@drawable/ic_android_black_24dp" />
    
       </LinearLayout>
    
    </LinearLayout>
    

    PS: Maybe it's better to use a HorizontalScrollView if you have many images