2

Trying To Achieve

  • Add Button After GridView which has layout_height="wrap_parent" & layout_width="fill_parent" to provide better UIx.

Constraints:

  • Adding ImageViews which fetched from Facebook-Graph-Api to GridView which consists of ImageView.

  • Add Button atlast of GridView once the Image's fetched.

Tried

  • 1. Setting Visibility of Button to Visible at onPostExecute() of AysncTask which helps me to fetched the Image's

  • 2. Setting Visibility of Button to Visible at ScrollListener which would run if it Scrolling reaches end of GridView i.e Last Image inside GridView

  • But Button is not showing at the end of GridView. Dunno Why??

My XML Layout::

<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"
tools:context=".MainActivity" 
android:id="@+id/rootview"
>
<Button
    android:id="@+id/fbconnect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/connectfb" 
    android:layout_centerHorizontal="true"
    android:visibility="visible"
    />
<GridView 
    android:id="@+id/gridphoto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:columnWidth="80dp"
    android:numColumns="auto_fit" 
    android:verticalSpacing="2dp"
    android:horizontalSpacing="2dp"
    android:stretchMode="columnWidth"
    />
<Button 
        android:id="@+id/loadmore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_centerHorizontal="true"
        android:layout_below="@id/gridphoto"
        android:visibility="gone"
        android:text="@string/load_more"
    />
</RelativeLayout>

Code :: Fetching Images via AysncTask and Setting Visibility at onPostExecute to add LoadMore Button at end of GridView.

 protected void onPostExecute(Void result) {
  loadMore.setVisibility(View.VISIBLE); // Load More Button
 }

Code :: Implementing ScrollListener on GridView -- > Checking If it reaches bottom or not then changing Visibility of Load More button.

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
 if (gridView.getLastVisiblePosition() + 1 == increasingItem) // Checking if it reaches bottom of GridView or not. It prefectly runs. Checked via Logging into StackTrace.
  {
     loadMore.setVisibility(View.VISIBLE); // Load More Button
  }
}

1 Answer 1

4

I ran through it with this code :

MainActivity :

 public class MainActivity extends Activity implements OnScrollListener {




        ArrayAdapter<String> adapter;

        GridView gridphoto;
        Button loadMore;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            gridphoto = (GridView) findViewById(R.id.gridphoto);
            loadMore = (Button) findViewById(R.id.loadmore);
            gridphoto.setOnScrollListener(this);
            adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
            for (int i = 0; i < 80; i++) {
                adapter.add("dummy data " + i);

            }
            gridphoto.setAdapter(adapter);

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if (gridphoto.getLastVisiblePosition() + 1 == 80) {
                loadMore.setVisibility(View.VISIBLE); // Load More Button
            }
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }
    }

and activity_main.xml

<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/gridphoto"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:columnWidth="80dp"
            android:gravity="center"
            android:horizontalSpacing="2dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="2dp" />

        <Button
            android:id="@+id/loadmore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Load More"
            android:visibility="gone" />
    </LinearLayout>

</RelativeLayout>

It works like expected, hope it 'll help

Sign up to request clarification or add additional context in comments.

4 Comments

Tried to wrap my RootViewinto LinearLayout. But as my GridView adapter filled from webservices doesn't give me the o/p of adding Button after GridView.
I was not so clear sorry ! Don't wrap your RootView but just your GridView and Button. And pay attenton to gridvie attibutes . android:layout_height="0dp" android:layout_weight="1" The problem in your original code is that even if you set height to wrap_content, if your Gridview has too much content, it will be taller than it's parent, you can check this behavior by adding just one item to your gridview and setting the visibility of button to visible. Et Hop ! At reuntime you will see your button.
Whatever you are suggesting would definitely works. But wat if i had content in GridView way long then parent?? Is there any way to resolve these??
I've made a short project to check the answer I gave you was really solving the issue. And it does,I edited the post to give you the whole thing as input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.