2

I am using Android Studio. My project involves receiving some data through BLE and displaying it on the screen on a Linear Layout.

My main activity code for adding View:

dataLayout.addView(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

Function is defined as:

addView(dataEdit, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));

In my xml file, the required portion looks like:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1"
    android:id="@+id/dataLayout"
    android:fillViewport="true">
</LinearLayout>

EDIT: The whole xml file looks like:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView">

  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:focusable="true"
      android:focusableInTouchMode="true"
      android:baselineAligned="true">

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:text="Find Device"
        android:layout_marginTop="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scanStatus" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Scan"
        android:id="@+id/scan"
        android:layout_gravity="center_horizontal"
        android:enabled="false" />

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView4"
        android:layout_marginTop="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/deviceInfo" />

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:text="Connect Device"
        android:layout_marginTop="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/connectionStatus" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Connect"
        android:id="@+id/connect"
        android:layout_gravity="center_horizontal"
        android:enabled="false" />

    </LinearLayout>

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView7"
        android:text="Receive"
        android:textColor="#0B0101"
        android:layout_marginTop="5dip" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Clear Data"
        android:textColor="#FFFCFC"
        android:id="@+id/clearData"
        android:layout_gravity="center_horizontal"
        android:singleLine="false" />

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView9"
        android:text="Graphs"
        android:layout_marginTop="5dip" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Plot"
        android:id="@+id/Plot"
        android:layout_weight="1"
        android:layout_marginTop="2dip"
        android:singleLine="false"/>

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

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Distance:"
        android:textColor="#0B0101"
        android:id="@+id/textView8" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="1"
        android:id="@+id/dataLayout"
        android:fillViewport="true">
    </LinearLayout>

  </LinearLayout>

</ScrollView>

The problem is that only first 9 values of received data are being displayed on the screen. After that, even when I scroll up the screen, I can't see any values. I want to scroll up the screen and see all the values being received.

Any help would be appreciated.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
user3863537
  • 101
  • 1
  • 12
  • I've updated my answer, let us know if it works! – bonnyz Sep 22 '15 at 08:05
  • @bonnyz I have tried the code you updated. It has made the layout more centralized and better looking but the problem of displaying only the first few values exists. – user3863537 Sep 22 '15 at 19:27

2 Answers2

1

Just wrap your root layout with a ScrollView (how to).

UPDATE

Try to update your layout in the following way:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView">

  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:focusable="true"
      android:focusableInTouchMode="true"
      android:baselineAligned="true">

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:text="Find Device"
        android:layout_marginTop="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scanStatus" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Scan"
        android:id="@+id/scan"
        android:layout_gravity="center_horizontal"
        android:enabled="false" />

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView4"
        android:layout_marginTop="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/deviceInfo" />

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:text="Connect Device"
        android:layout_marginTop="5dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/connectionStatus" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Connect"
        android:id="@+id/connect"
        android:layout_gravity="center_horizontal"
        android:enabled="false" />

    </LinearLayout>

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView7"
        android:text="Receive"
        android:textColor="#0B0101"
        android:layout_marginTop="5dip" />

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Clear Data"
        android:textColor="#FFFCFC"
        android:id="@+id/clearData"
        android:layout_gravity="center_horizontal"
        android:singleLine="false" />

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView9"
        android:text="Graphs"
        android:layout_marginTop="5dip" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Plot"
        android:id="@+id/Plot"
        android:layout_weight="1"
        android:layout_marginTop="2dip"
        android:singleLine="false"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Distance:"
        android:textColor="#0B0101"
        android:id="@+id/textView8" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
        android:id="@+id/dataLayout">
    </LinearLayout>

  </LinearLayout>
</ScrollView>
Community
  • 1
  • 1
bonnyz
  • 13,458
  • 5
  • 46
  • 70
  • I have added the scroll View but it has no effect. I have edited my question to add complete .xml file. Could you please have a look? – user3863537 Sep 21 '15 at 18:12
1

Wrapping your LinearLayout in a ScrollView should solve it. However, use a ListView or RecyclerView instead of adding views yourself. These recycle the views that are not visible to save memory.

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • I have wrapped my entire code in scroll view but it has no effect. Kindly have a look at the edited version of my question which contains the whole .xml file – user3863537 Sep 21 '15 at 18:20
  • @user3863537 Have you considered a `ListView` or a `RecyclerView` ? – An SO User Sep 22 '15 at 12:21