1

I am working on list view..list view items display in vertical.but I am display items in horizontal but not show the items in horizontal. and simple_list_item_1 is not working..I am changing gravity left or right but items show vertically..here is my code

here is content_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

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



        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_centerHorizontal="true" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Searach Now"
            android:id="@+id/button"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/editText" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">



        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:layout_below="@+id/headingText">


        </ListView>

    </LinearLayout>

</RelativeLayout>

here is simple_list_item. XML

 <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"></TextView>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
       ></TextView>

</Relativelayout>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sana Mirza
  • 11
  • 2

2 Answers2

0

Must use way: As per Android Documentation RecyclerView is the BEST way to organize the items in listview and to be displayed horizontally

Advantages:

  1. Since by using Recyclerview Adapter, ViewHolder pattern is automatically implemented
  2. Animation is easy to perform
  3. Many more features

Sample:

survivingwithandroid.com

Just add the below block to make the ListView to horizontal from vertical

Code-snippet

LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);

Reference:Horizontal ListView in Android?

Another way:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="horizontal">

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:background="#f00"
            android:padding="10dp"
            android:text="Button 1"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:background="#0f0"
            android:padding="10dp"
            android:text="Button 2"
            android:textColor="#fff"
            android:textSize="20sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:background="#00f"
            android:padding="10dp"
            android:text="Button 3"
            android:textColor="#fff"
            android:textSize="20sp" />

    </LinearLayout>

</HorizontalScrollView>

</RelativeLayout>

Result code:

enter image description here

Problem with HorizontalScrollView is it shows horizontal bar and it is not recommended to use.

I hope it will help you.

0

I know you are a beginner, recyclerView offers LayoutManagers for this purpose but as you are new it is good to know about listview as it is the base concept and simpler to setup than recyclerview.

Use Horizontal scrolling view and embed your list view in that. What this does is instead of positioning your list items in vertical it will do so in horizontal way with scroll option.

KingKongCoder
  • 600
  • 4
  • 15