2

I am working on a WearOS app. I am using a ListView to show a list of strings. Right now, the ListView looks like this:

enter image description here

I want a single row to take up the entire screen. Once a user swipes up, then Row 2 takes up the entire screen. Swipe up again and Row 3 takes up the entire screen, etc. So like this: enter image description here

I came across this link, which is exactly what I want to do, but the first method doesn't work and the second method suggests a library, but I don't want to use a library for what seems like a pretty simple task. I don't want to use RecyclerView. Thank you for your help.

Here is my XML file for the main activity, which is where the ListView is.

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_grey"
    android:padding="@dimen/box_inset_layout_padding"
    tools:context=".MainActivity"
    tools:deviceIds="wear">

    <!-- Change FrameLayout to a RelativeLayour -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/inner_frame_layout_padding"
        app:boxedEdges="all">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />
    </RelativeLayout>
</android.support.wear.widget.BoxInsetLayout>
Logan Phillips
  • 660
  • 2
  • 10
  • 23
  • Possible duplicate of [Simple Android RecyclerView example](https://stackoverflow.com/questions/40584424/simple-android-recyclerview-example) – InsaneCat Jun 25 '18 at 13:16
  • @user8035311 I added the XML. I am not using a RecyclerView. Just a ListView because according to the Android tutorials RecyclerView is best for larger lists. My list will have at maximum 5-10 entries. – Logan Phillips Jun 25 '18 at 13:16
  • @InsaneCat I actually haven't come across that answer, I'll look at it. – Logan Phillips Jun 25 '18 at 13:18
  • hope this could help you... https://stackoverflow.com/a/47025423/2404063 – Kannan_SJD Jun 25 '18 at 14:21

1 Answers1

1

Try the following (The idea here is to programmatically set the height of the textView to the height of the screen):

1) MnnnnnnnActivity.class:----------

public class MnnnnnnnActivity extends AppCompatActivity {

private ListView lv;
private CustomAdapter customAdapter;
private String[] s = new String[10];

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout6);

    for (int i = 0; i < 10; i++) {
        s[i] = "ROW " + String.valueOf(i + 1);
    }

    lv = (ListView) findViewById(R.id.lv);
    customAdapter = new CustomAdapter(MnnnnnnnActivity.this, s);
    lv.setAdapter(customAdapter);

}

public int getScreenHeight() {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.heightPixels;

}
}

2) CustomAdapter.class:--------

public class CustomAdapter extends ArrayAdapter<String> {

private String[] s;
private WeakReference<MnnnnnnnActivity> mActivity;

public CustomAdapter(MnnnnnnnActivity activity1, String[] s) {
    super(activity1.getApplicationContext(), R.layout.list_view_item, s);
    this.s = s;
    mActivity = new WeakReference<MnnnnnnnActivity>(activity1);

}

@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if(mActivity != null) {
        MnnnnnnnActivity activity = mActivity.get();
        if (activity != null) {
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(activity);
                convertView = inflater.inflate(R.layout.list_view_item, null);
                holder = new ViewHolder();

                holder.tv = (TextView) convertView.findViewById(R.id.tv);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(s[position]);
            holder.tv.setHeight(activity.getScreenHeight());


        }
    }

    return convertView;

}

private class ViewHolder {
    TextView tv;
}

}

3) layout6.xml:-----------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv">
</ListView>

</android.support.constraint.ConstraintLayout>

4) list_view_item.xml:----------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tv"
android:singleLine="true"
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"
    android:gravity="center"
android:textSize="20sp"
android:textStyle="bold">
</TextView>

</LinearLayout>

5) Note: Although this is tested on phone, the same idea can be used to achieve something similar on a wareable. Also, a better approach would be to set the height of the linearLayout (which contains the textView) to the screen height.

6) Output:

Output

Brainnovo
  • 1,749
  • 2
  • 12
  • 17