0

I want to add two child views to a horizontal scroll view. The two views are a ImageView and a TextView and the TextView should be below the `ImageView, both the view should scroll horizontally Is it possible to achieve this? How can it be added? I am new to android.

Thanks in advance.

in my fragment:

LinearLayout lv = (LinearLayout) v.findViewById(R.id.textl);
        for (int i=0;i<5;i++){
            ImageView iv = new ImageView(getContext());
            TextView tv = new TextView(getContext());
            tv.setText(text[i]);   //defined text and images
            iv.setImageResource(images[i]);
            lv.addView(iv);
            lv.addView(tv); 

xml:

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:id="@+id/hs">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/textl"/>

        </LinearLayout>

    </HorizontalScrollView>
sun
  • 1,832
  • 4
  • 19
  • 31

4 Answers4

1

you should use LinearLayout with vertical orientation inside horizontal scroll view then use image and text view inside LinearLayout.

<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hsv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     >
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />


</LinearLayout>
</HorizontalScrollView>
PRIYA PARASHAR
  • 777
  • 4
  • 15
1

The best possible way is to use RecyclerView. This is best way to use it as it scale itself and also elements can be loaded on the fly. if you are in activity then in onCreate method

// recyclerView is id mentioned in xml file
 RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

LinearLayoutManager mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);

mRecyclerView.setLayoutManager(mLayoutManager);

** your adapter init goes here **
mRecyclerView.setAdapter(**adapter object**);

for details post your code - will help you. For Fragments you need to do same with slight changes

Note: you should also define your recyclerview in your xml file

MobDev
  • 464
  • 4
  • 14
0

All three questions answers :

Textview below Imageview as shown below,

Yes, it is possible

Yo can check below xml code

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="100dip">

    <HorizontalScrollView
        android:id="@+id/hsv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:fillViewport="true"
        android:measureAllChildren="false"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/innerLay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iV1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/next_"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_below="@+id/iV1"
                android:gravity="center"
                android:hint="hello"/>
        </LinearLayout>
    </HorizontalScrollView>
</RelativeLayout>
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • I think if we add like this I think it get error - The specified child already has a parent. You must call removeView() on the child's parent first when adding text and image to those one – sun Apr 30 '16 at 06:46
  • No, it will not throw error , please check http://stackoverflow.com/questions/18656949/how-to-implement-horizontalscrollview-like-gallery – Amit Vaghela Apr 30 '16 at 06:47
  • yeah I am adding image and text to the both imageview and textview dynamically but it getting issue The specified child already has a parent. You must call removeView() on the child's parent first. – sun Apr 30 '16 at 06:59
  • check http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first and http://stackoverflow.com/questions/28071349/the-specified-child-already-has-a-parent-you-must-call-removeview-on-the-chil – Amit Vaghela Apr 30 '16 at 07:02
0

Try This:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="Hello" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp"
                android:padding="2dp"
                android:text="Hello"
                android:textSize="20sp" />

        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>
AbhayBohra
  • 2,047
  • 24
  • 36