Search code examples
androidandroid-buttonandroid-gridview

Adding Button end to GridView dynamically


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

Solution

  • 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