0

 //Java code
 
 public class PassengerHomeActivity extends AppCompatActivity implements OnMapReadyCallback {

RecyclerView my_recycler_view;
    ArrayList<carmodel> array_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

CarCategory();
        array_list = new ArrayList<>();
        my_recycler_view.setHasFixedSize(true);
        adapter = new CarAllAdapter(PassengerHomeActivity.this, array_list);
        my_recycler_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        my_recycler_view.setAdapter(adapter);
}

private void CarCategory() {
        if (array_list != null) array_list.clear();
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URLUtils.APP_BASE_URL + "carcategorylistApi", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                try {
                    JSONObject jObj = new JSONObject(response);
                    Log.w("CarCategory_customer", "<><><>" + response);

                    if (jObj.getString("ack").equals("1")) {
                        JSONArray jarr = jObj.getJSONArray("cardetails");
                        for (int i = 0; i < jarr.length(); i++) {
                            JSONObject jsonObject = jarr.getJSONObject(i);
                            String id = jsonObject.getString("id");
                            String name = jsonObject.getString("name");
                            String image = jsonObject.getString("image");
                            String hover_image = jsonObject.getString("hover_image");

                            carmodel objmodel = new carmodel();
                            objmodel.setId(id);
                            objmodel.setName(name);
                            objmodel.setImage(image);
                            objmodel.setHover_image(hover_image);

                            array_list.add(objmodel);


                        }
                        adapter.notifyDataSetChanged();

                    } else if (jObj.getString("ack").equals("2")) {
                        Toast.makeText(PassengerHomeActivity.this, "" + jObj.getString("msg"), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(PassengerHomeActivity.this, "" + jObj.getString("msg"), Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    // JSON error
                    System.out.println("mono_JSONError " + e);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error instanceof TimeoutError) {
                    Toast.makeText(PassengerHomeActivity.this, getString(R.string.tooslow), Toast.LENGTH_LONG).show();
                } else if (error instanceof NoConnectionError) {
                    Toast.makeText(PassengerHomeActivity.this, getString(R.string.nointernet), Toast.LENGTH_LONG).show();
                } else if (error instanceof AuthFailureError) {
                    System.out.println("mono_AuthFailureError " + error);
                } else if (error instanceof ServerError) {
                    System.out.println("mono_ServerError " + error);
                } else if (error instanceof NetworkError) {
                    System.out.println("mono_NetworkError " + error);
                } else if (error instanceof ParseError) {
                    System.out.println("mono_ParseError " + error);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(PassengerHomeActivity.this);
        requestQueue.add(stringRequest);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                10000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


    }
}

I'm showing a horizontal scrolling recyclerview with images and text as shown in screenshot.

What I want is to have equal width for each row item of recyclerview which will fit the screen if the number of rows are less than 4 and if greater than 4 then it'll have normal scroll. I can count the number of row items.

I just want to know how to stretch the rows with equal width if the count is 4 or less.

This image is from the current code.

current image

This is what I want.

desired image

//Main xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/top_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".PassengerHomeActivity">

    <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/projectColor">


    </android.support.v7.widget.Toolbar>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/ll_buttomAction">

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

                    <RelativeLayout
                        android:id="@+id/rl_mapcontainer"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <fragment
                            android:id="@+id/maps_home"
                            class="com.google.android.gms.maps.SupportMapFragment"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent" />

                        <ImageView
                            android:id="@+id/pinLocation"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:src="@drawable/pin_pickup" />


                    </RelativeLayout>

                    <LinearLayout
                        android:id="@+id/ll_pick"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="10dp"
                        android:background="@drawable/pickup_text"
                        android:orientation="vertical"
                        android:padding="5dp">

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

                            <com.example.nits_34.zeomobile.CustomView.IonicFont
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/ion_circle"
                                android:textColor="#19F029" />

                            <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:text="Pickup from" />
                        </LinearLayout>

                        <TextView
                            android:id="@+id/txt_pickup"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:ellipsize="end"
                            android:maxLines="1"
                            android:minLines="1"
                            android:textColor="#2B2B2B" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/ll_pick"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="5dp"
                        android:background="@drawable/pickup_text"
                        android:orientation="vertical"
                        android:padding="5dp">

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

                            <com.example.nits_34.zeomobile.CustomView.IonicFont
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/ion_circle"
                                android:textColor="#ff4d4d" />

                            <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:text="Drop at" />
                        </LinearLayout>

                        <TextView
                            android:id="@+id/txt_drop"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="5dp"
                            android:ellipsize="end"
                            android:maxLines="1"
                            android:minLines="1"
                            android:textColor="#2B2B2B" />
                    </LinearLayout>

                </RelativeLayout>


                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="16dp"
                    android:theme="@style/MyFloatingButton"
                    app:fabSize="mini"
                    app:layout_anchor="@+id/rl_mapcontainer"
                    app:layout_anchorGravity="bottom|right|end"
                    app:srcCompat="@drawable/my_locationblack" />

            </android.support.design.widget.CoordinatorLayout>

            <LinearLayout
                android:id="@+id/ll_buttomAction"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="vertical">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/my_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:scrollbars="none" />

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

                    <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                        android:id="@+id/txt_RideNow"
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        android:layout_weight="1"
                        android:background="#1A222D"
                        android:gravity="center"
                        android:text="RIDE NOW"
                        android:textColor="@color/projectColor" />

                    <View
                        android:layout_width="1dp"
                        android:layout_height="45dp"
                        android:background="#58789E" />

                    <com.example.nits_34.zeomobile.CustomView.ExoticFontRegular
                        android:id="@+id/RideLeter_Txt"
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        android:layout_weight="1"
                        android:background="#1A222D"
                        android:gravity="center"
                        android:text="RIDE LATER"
                        android:textColor="@color/projectColor" />

                </LinearLayout>
            </LinearLayout>


        </RelativeLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_header"
            app:itemIconTint="#ff1b6bae"
            app:itemTextColor="#333" />


    </android.support.v4.widget.DrawerLayout>


</RelativeLayout>
//Row layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#324457"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/ll_singleCar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@drawable/no_image"  />


        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/itemImage"
            android:gravity="center"
            android:padding="2dp"
            android:text="Sample title"
            android:textColor="@color/projectColor"
            android:textSize="12sp" />


    </LinearLayout>

</LinearLayout>
{
  "ack": 1,
  "cardetails": [
    {
      "id": "3",
      "name": "Luxury Cards",
      "image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/suv.png",
      "hover_image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/Luxury_hover.png"
    },
    {
      "id": "4",
      "name": "SUV's",
      "image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/limousine.png",
      "hover_image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/SUV_hover.png"
    },
    {
      "id": "6",
      "name": "Regular Cars ",
      "image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/taxi.png",
      "hover_image": "http:\/\/111.333.88.222\/team6\/zoemobile\/car_images\/reguler_hover.png"
    }
  ]
}
Monojit
  • 3
  • 6

2 Answers2

0

activity_main.xml : change layout_width="match_parent" to "wrap_content"

<android.support.v7.widget.RecyclerView
                    android:id="@+id/my_recycler_view"
                    android:layout_width="wrap_content" // change match_parent to wrap_content
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:scrollbars="none" />

row.xml : please check below source. I changed layout_width="match_parent" to "wrap_content" on two places

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#324457"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/ll_singleCar"
        android:layout_width="wrap_content" // change match_parent to wrap_content 
        android:layout_height="wrap_content" 
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@android:drawable/btn_default"  />


        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content" // change match_parent to wrap_content 
            android:layout_height="wrap_content" 
            android:layout_below="@id/itemImage"
            android:gravity="center"
            android:padding="2dp"
            android:text="Sample title"
            android:textColor="#ffffff"
            android:textSize="12sp" />


    </LinearLayout>

</LinearLayout>

And if you want to have the same width size of each row item when the Adapter has less than 4 items, you can change the ll_singleCar's width programmatically in onBindViewHolder method in your Adapter file. Please check this link https://stackoverflow.com/a/11344000/7973751 .

Sunhee
  • 101
  • 8
  • I think that main.xml you posted above is just part of main.xml. Please post all source, main.xml. – Sunhee May 23 '17 at 05:47
  • Please check this link, https://stackoverflow.com/a/41894547/7973751 It says to change android:layout_width="match_parent" to "wrap_content" of RecyclerView . – Sunhee May 23 '17 at 05:55
  • If I use android:layout_width="wrap_content", It does not fix's the whole screen. – Monojit May 23 '17 at 06:03
0

I believe you can use ItemDecoration class to solve problem. In method getItemOffsets you can calculate the size of each item and add space left or right as you needed base screen size which should be easy to get with RecyclerView.getWidth or measure width.

And make sure your RecyclerView width is match parent.

vsatkh
  • 179
  • 2
  • 10
  • how to calculate the size in width? – Monojit May 23 '17 at 06:38
  • Hmm, currently I don't have access to my computer. So you can use RecyclerView to get it width. – vsatkh May 23 '17 at 08:27
  • Hmm, currently I don't have access to my computer. So you can use RecyclerView to get it width. So the option is to provide fix space of the left and right example: 32dp. The the rest will be the 3 item. Which mean divide it by 3 is the size of each item. To calculate the Rect size base on item size that we calculated above you can use layout manager from RecyclerView to find the view in the current position and get its width there. – vsatkh May 23 '17 at 08:36