An activity with two listviews in android

Aditya Prasun

MainAcitivity

public class MainActivity extends Activity {

ListView list, listYear;
String[] web = {
        "Google Plus",
        "Twitter",
        "Windows",
        "Bing",
        "Itunes",
        "Wordpress",
        "Drupal"
} ;
String[] year = {
        "2012",
        "2013",
        "2014",
        "2015",
        "2016",
        "2017",
        "2018"
} ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list=(ListView)findViewById(R.id.list);
    list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, web));
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
        }
    });

    listYear=(ListView)findViewById(R.id.listYear);
    listYear.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, year));
    listYear.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " + year[+position], Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

layout_activity

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:gravity="center_vertical"
                android:text="YEAR" />

            <ListView
                android:id="@+id/listYear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:dividerHeight="1dp"
                android:layout_marginTop="10dip"
                android:background="#B29090" >
            </ListView>

        </LinearLayout>

        <LinearLayout
            android:layout_below="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_marginTop="10dip"
                android:text="WEBSITE" />

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:dividerHeight="1dp"
                android:layout_marginTop="10dip"
                android:background="#B29090" >
            </ListView>
        </LinearLayout>

    </RelativeLayout>

</ScrollView>

What I want is to display both the activities in a single activity. The only problem that I am facing up is that it looks like i am not able to show the layout properly because it is showing only the 2nd layout with scroll-bars instead of the scrollview's scrolls

CAS

Your problem is with your RelativeLayout under your ScrollView.

Try replacing it with a vertical LinearLayout and give its 2 child LinearLayout a weight of 1 like this:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="10dip" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:gravity="center_vertical"
                android:text="YEAR" />

            <ListView
                android:id="@+id/listYear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:background="#B29090"
                android:dividerHeight="1dp" >
            </ListView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="10dip" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:gravity="center_vertical"
                android:text="WEBSITE" />

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:background="#B29090"
                android:dividerHeight="1dp" >
            </ListView>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Move items between two listviews in activity in Android?

From Dev

Android Two listviews and adapters

From Dev

Android display two full-size ListViews

From Dev

Android: how to populate two different listviews with database?

From Dev

How to add two listviews in a fragment in android layout?

From Dev

Android activity with two fragments

From Dev

TextView above two ListViews

From Dev

Two ListViews in a ScrollView

From Dev

Android : selectors for custom listviews

From Dev

Android - Listviews in Fragments

From Dev

android listviews matching layouts

From Dev

Android: 2 Listviews in NavigationDrawer

From Dev

Android ListViews Force Selection

From Dev

Android Two layout for one activity

From Dev

Android Two layout for one activity

From Dev

Two Options menu in android Activity

From Dev

Implementation of two listviews in one frame

From Dev

Joining two ListViews into one in flutter

From Dev

how to add two ListViews in parallel?

From Dev

Two listviews with highlighted selected items

From Dev

Implementation of two listviews in one frame

From Dev

A scrollable view with two ListViews inside

From Dev

Xamarin Forms - Two ListViews Stacked

From Dev

Android - Have two listviews share space if both open, else use all the space

From Dev

Android - Three Listviews one Fragment; First Listview controls other two; Highlight first Listview choice

From Dev

I was wondering if it is possible to create one arraylist for two different listviews across different activities in android studio?

From Dev

Saving lists of strings in listViews (Android)

From Dev

How to Sync Multiple ListViews Android

From Dev

Android ViewPager + Fragments with dynamic ListViews

Related Related

HotTag

Archive