0

I need to create i number of photo_view and give it a special amount every time (loop). number of photo_view in every time will be different. What exactly should I do in the code ?

my xml file :

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/tools"
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/black">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <com.github.chrisbanes.photoview.PhotoView
                    android:id="@+id/photo_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>
        </HorizontalScrollView>

        <ImageView
            android:id="@+id/exit_btn"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_margin="20dp"
            android:clickable="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_back_ltr" />

    </android.support.design.widget.CoordinatorLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Arman Download
  • 174
  • 1
  • 2
  • 14
  • What you are trying to do is called `Dynamically adding views to a viewgroup` it is done by code not in XML. take a look at this [thread](https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) – Atef Hares Nov 25 '17 at 17:43
  • What exactly should I do in the code؟ – Arman Download Nov 25 '17 at 17:46

1 Answers1

0

Dynamically adding Views to a ViewGroup is what you are looking for. which is done by code not by XML.

In general, there are two ways to add a view dynamically, either by inflating some layout file in a view then add this view to your parent viewgroup (LinearLayout for ex) by calling the add method on this parent view.

Or by defining a new object of that view and add all params needed to it then add it to the parent viewgroup.

See an example about way 1 here

See an example about way 2 here


in your case you could create a new layout.xml file contains only your custom view com.github.chrisbanes.photoview.PhotoView with all the needed params. then add an id for the parent linearlayout

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


        </LinearLayout>

And in your code, loop by the count and add the custom inflated view to parent. like this:

View inflatedView = View.inflate(context, yourViewXML, null);
LinearLayout parentViewGroup = findViewById(R.id.parentViewGroup);

for(int i = 0 ; i<count ; i++){
  parentViewGroup.add(inflatedView);
}
Atef Hares
  • 4,715
  • 3
  • 29
  • 61