Search code examples
androidarraylistintegerandroid-linearlayout

Android Programming Linear Layout ArrayList Integers


I have an arraylist of integers called numbers. How do I display these numbers horizontally and then vertically with the linear layout widget for my android app?

public class SecondActivity extends AppCompatActivity {
    private ArrayList<Integer> Numbers = new ArrayList<Integer>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Numbers.add(1);
        Numbers.add(2);
        Numbers.add(3);
        Numbers.add(4);
        Numbers.add(5);
        //Linear Layout code goes here
    }
}

Solution

  • In case if its mandatory create a custom table with row and column with the proper weight and repeat row wise with linear layout. if Not mandatory then use table Layout android. it will make your job simpler.

    Here I found Some old Code:

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/dms_background" >
    
        <!-- User Details -->
    
        <RelativeLayout
            android:id="@+id/report_title_layout"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/forty_dp"
            android:layout_alignParentTop="true"
            android:background="@color/dms_actionbar" >
    
            <TextView
                android:id="@+id/report_associate_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/retailer_report"
                android:textColor="@color/White" />
        </RelativeLayout>
    
        <RelativeLayout
            android:id="@+id/report_main_layout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/report_title_layout"
            android:layout_marginTop="@dimen/five_dp" >
    
            <!-- Table View -->
    
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="@dimen/ten_dp"
                android:shrinkColumns="*"
                android:stretchColumns="*" >
    
                <!-- Row 1 -->
    
                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1" >
    
                    <RelativeLayout
                        android:id="@+id/report_salesorder_layout"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:clickable="true" >
    
                        <ImageView
                            android:id="@+id/report_retailt_transaction_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/ic_sales" />
    
                        <TextView
                            android:id="@+id/report_retailt_transaction_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/report_retailt_transaction_image"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:text="@string/report_salesreport"
                            android:textColor="@color/White" />
    
                        <View
                            android:layout_width="100dp"
                            android:layout_height="2dp"
                            android:layout_below="@id/report_retailt_transaction_text"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/devider_horizontal" />
    
                        <View
                            android:layout_width="2dp"
                            android:layout_height="@dimen/ninty_dp"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:background="@drawable/devider_vertical" />
                    </RelativeLayout>
    
                    <RelativeLayout
                        android:id="@+id/report_salesreturn_layout"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:clickable="true" >
    
                        <ImageView
                            android:id="@+id/report_partner_search_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/ic_return" />
    
                        <TextView
                            android:id="@+id/report_partner_search_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/report_partner_search_image"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:text="@string/report_returnreport"
                            android:textColor="@color/White" />
    
                        <View
                            android:layout_width="100dp"
                            android:layout_height="2dp"
                            android:layout_below="@id/report_partner_search_text"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/devider_horizontal" />
                    </RelativeLayout>
                </TableRow>
                <!-- Row 2 -->
    
                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >
    
                    <RelativeLayout
                        android:id="@+id/report_paymentcollection_layout"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:clickable="true" >
    
                        <ImageView
                            android:id="@+id/report_route_plan_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/ic_payment" />
    
                        <TextView
                            android:id="@+id/report_route_plan_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/report_route_plan_image"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:text="@string/report_payreport"
                            android:textColor="@color/White" />
    
                        <View
                            android:layout_width="100dp"
                            android:layout_height="2dp"
                            android:layout_below="@id/report_route_plan_text"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/devider_horizontal" />
    
                        <View
                            android:layout_width="2dp"
                            android:layout_height="@dimen/ninty_dp"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:background="@drawable/devider_vertical" />
                    </RelativeLayout>
    
                    <RelativeLayout
                        android:id="@+id/report_performance_layout"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:clickable="true" >
    
                        <ImageView
                            android:id="@+id/report_new_partner_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/ic_performance" />
    
                        <TextView
                            android:id="@+id/report_new_partner_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/report_new_partner_image"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:text="@string/report_performancereport"
                            android:textColor="@color/White" />
    
                        <View
                            android:layout_width="100dp"
                            android:layout_height="2dp"
                            android:layout_below="@id/report_new_partner_text"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/devider_horizontal" />
                    </RelativeLayout>
                </TableRow>
    
                <!-- Row 3 with 2 columns -->
    
                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >
    
                    <RelativeLayout
                        android:id="@+id/report_stock_layout"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:clickable="true" >
    
                        <ImageView
                            android:id="@+id/report_report_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/ic_stock" />
    
                        <TextView
                            android:id="@+id/report_report_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/report_report_image"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:text="@string/report_stockreport"
                            android:textColor="@color/White" />
    
                        <View
                            android:layout_width="100dp"
                            android:layout_height="2dp"
                            android:layout_below="@id/report_report_text"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/devider_horizontal" />
    
                        <View
                            android:layout_width="2dp"
                            android:layout_height="@dimen/ninty_dp"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:background="@drawable/devider_vertical" />
                    </RelativeLayout>
    
                    <RelativeLayout
                        android:id="@+id/report_report_layout"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:clickable="true" >
    
                        <ImageView
                            android:id="@+id/report_empty_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:visibility="invisible"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/ic_reportsasso" />
    
                        <TextView
                            android:id="@+id/report_empty_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/report_empty_image"
                            android:layout_centerHorizontal="true"
                              android:visibility="invisible"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:text="@string/associate_reports"
                            android:textColor="@color/White" />
    
                        <View
                            android:layout_width="100dp"
                            android:layout_height="2dp"
                            android:layout_below="@id/report_empty_text"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="@dimen/ten_dp"
                            android:background="@drawable/devider_horizontal"
                           />
                    </RelativeLayout>
                </TableRow>
            </TableLayout>
        </RelativeLayout>
    
    </RelativeLayout>