2

I am just a beginner in android, and wish to have your suggestions at places where i could improve my code.

For our project we created a grid view, which loads users at runtime, now the issue with this is it doesnot scroll at times and is very hard to do so.

Also we have used this grid view, making the view visible and gone depending upon the circumstances required by the app.

Here's my xml file:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <!-- Title Layout -->

    <RelativeLayout
        android:id="@+id/newGroupTitleLayout"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:background="@drawable/topbar_bg" >

        <ImageButton
            android:id="@+id/newGroupCancelButton"
            android:layout_width="60dip"
            android:layout_height="30dip"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:background="@drawable/buttonanim_cancel_button"
            android:contentDescription="@string/todo" />

        <TextView
            android:id="@+id/setupPrefTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/new_group"
            android:textColor="@color/yellow"
            android:textSize="20sp" />
    </RelativeLayout>

    <!-- Group Name -->

    <LinearLayout
        android:id="@+id/groupNameLinearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/newGroupTitleLayout"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="15dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/groupNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:text="@string/group_name"
            android:textColor="@color/yellow" />

        <EditText
            android:id="@+id/groupNameEditText"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/full_textfield"
            android:ems="10"
            android:inputType="text"
            android:nextFocusRight="@+id/stateEditText"
            android:paddingLeft="15dp"
            android:singleLine="true" >
        </EditText>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/addMemberLayout"
        android:layout_width="wrap_content"
        android:layout_height="290dp"
        android:layout_below="@+id/groupNameLinearLayout"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/membersbox" >

        <!-- View used when more than 1 member present -->

        <GridView
            android:id="@+id/mebersListGridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fastScrollAlwaysVisible="true"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:padding="10dp"
            android:verticalSpacing="10dp"
            android:visibility="gone" />

        <!-- View when there are no members -->

        <RelativeLayout
            android:id="@+id/zeroMembersLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true" >

            <ImageButton
                android:id="@+id/addMemeberImageButton"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="70dp"
                android:background="@drawable/buttonanim_addmembersmall_button"
                android:contentDescription="@string/todo" />

            <LinearLayout
                android:id="@+id/memberCountStatementTextViewtLayout"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/addMemeberImageButton"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/this_group_has_"
                    android:textColor="@android:color/white"
                    android:textSize="19sp" />

                <TextView
                    android:id="@+id/groupMembersCount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/_0"
                    android:textColor="@color/yellow"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/_members"
                    android:textColor="@android:color/white"
                    android:textSize="19sp" />
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/memberCountStatementTextViewtLayout"
                android:layout_centerHorizontal="true"
                android:text="@string/tap_to_add_friends"
                android:textColor="@android:color/white"
                android:textSize="16sp" />
        </RelativeLayout>
    </RelativeLayout>

    <!-- Create group button -->

    <ImageButton
        android:id="@+id/creategroupbutton"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_below="@+id/addMemberLayout"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/buttonanim_creategroup_button"
        android:contentDescription="@string/todo" />
</RelativeLayout>
</ScrollView>

Could anyone help me out with this?

Your help is appreciated.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Kailas
  • 7,350
  • 3
  • 47
  • 63

3 Answers3

4

The problem with scrolling is a consequence of putting a GridView inside a ScrollView (which is the root layout in your case). ScrollView allows yout to scroll it's children vertically. GridView scrolls vertically too. So you have to find another solution. Using GridView has one drawback. It doesn't have header and footer views like ListView has. So might want to replace the GridView with a ListView.

GareginSargsyan
  • 1,877
  • 15
  • 23
  • Not exactly, I wish to have a grid view itself. The point you mentioned is right, as the parent scrollview shall consume the scroll action on the screen and the child view may not be able to get the scroll events. Well, thanks for suggestion. – Kailas Dec 24 '13 at 09:30
  • You can use a ListView and make it function almost like GridView. Each row of the GridView can bit made a single child of ListView. – GareginSargsyan Dec 24 '13 at 10:13
1

Try to use ExpandableHeightGridView instead of simple gridView:

public class ExpandableHeightGridView extends GridView{

boolean expanded = false;

public ExpandableHeightGridView(Context context)
{
    super(context);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs,
        int defStyle)
{
    super(context, attrs, defStyle);
}

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
}}



<com.your_package_here.ExpandableHeightGridView
        android:id="@+id/mebersListGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fastScrollAlwaysVisible="true"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:padding="10dp"
        android:verticalSpacing="10dp"
        android:visibility="gone" />

From: this

Community
  • 1
  • 1
Kulibin
  • 444
  • 4
  • 12
0

Can't see your closing ScrollView tag..

 </ScrollView>

You should never nest ListView or GridView inside ScrollView because both of these views provide scrolling as soon as your data is more than visible area of the view.

Sulabh Gupta
  • 642
  • 1
  • 7
  • 20
  • Nice observation. Actually, I didn't forget it. Unfortunately, it din't appear for you. I have edited the content, to make it visible. Thanks for your suggestion. – Kailas Dec 24 '13 at 09:32