1

I create a Dynamic GridView with ArrayList Items and I want to create the Controls for shapes Some thing like this GridView GridView Example

and This Is my Code This ArrayList

ArrayList<String> alphabets1;
alphabets1 = new ArrayList<String>();
alphabets1.add(rs.getString("Name"));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, alphabets1);

and this is My Grid View

    final GridView gridView = new GridView(this);
    gridView.setVerticalSpacing(3);
    gridView.setHorizontalSpacing(3);
    gridView.setLayoutParams(new GridView.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
    gridView.setNumColumns(4);
    gridView.setAdapter(adapter);
    gridView.setLayoutParams(linearLayoutParams);

this is my GridView enter image description here

2
  • Do you want to show image dynamically in a gridview ? Commented Feb 20, 2017 at 9:40
  • I don't want to show Image I want to show just a TextView with a shape like I published @tahsinRupam just like a GrivView with text fields Commented Feb 20, 2017 at 9:44

2 Answers 2

3

GridView is used to display data in two dimension. In this tutorial we are going to show you how to implement custom GridView in Android with Images and Text.

Creating Layout:

The Main layout for our project is “activity_main” which has a GridView to display text with images.

activity_main.xml

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

    <GridView
         android:numColumns="auto_fit"
         android:gravity="center"
         android:columnWidth="100dp"
         android:stretchMode="columnWidth"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:id="@+id/grid"
         /> 
</LinearLayout>

Next step is to create a layout for the grid item that is to be displayed in GridView. Create the layout as grid_single.xml which has a TextView to display the text which is stored in the Array and a ImageView to display set of images in each grid item.

grid_single.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="50dp"
        android:layout_height="50dp">
    </ImageView>

    <TextView
        android:id="@+id/grid_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:textSize="9sp" >
    </TextView>

</LinearLayout>

Creating Activity:
Before Creating the MainActivity we must create a CustomGrid class for our custom GridView which is extended to BaseAdapter.

CustomGrid.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGrid extends BaseAdapter{
      private Context mContext;
      private final String[] web;
      private final int[] Imageid;

        public CustomGrid(Context c,String[] web,int[] Imageid ) {
            mContext = c;
            this.Imageid = Imageid;
            this.web = web;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return web.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View grid;
            LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {

                grid = new View(mContext);
                grid = inflater.inflate(R.layout.grid_single, null);
                TextView textView = (TextView) grid.findViewById(R.id.grid_text);
                ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
                textView.setText(web[position]);
                imageView.setImageResource(Imageid[position]);
            } else {
                grid = (View) convertView;
            }

            return grid;
        }
}

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends Activity {
    GridView grid;
    String[] web = {
            "Google",
            "Github",
            "Instagram",
            "Facebook",
            "Flickr",
            "Pinterest",
            "Quora",
            "Twitter",
            "Vimeo",
            "WordPress",
            "Youtube",
            "Stumbleupon",
            "SoundCloud",
            "Reddit",
            "Blogger"

    } ;
    int[] imageId = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
            R.drawable.image7,
            R.drawable.image8,
            R.drawable.image9,
            R.drawable.image10,
            R.drawable.image11,
            R.drawable.image12,
            R.drawable.image13,
            R.drawable.image14,
            R.drawable.image15

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
        grid=(GridView)findViewById(R.id.grid);
                grid.setAdapter(adapter);
                grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

                    }
                });

    }

}

reference link https://www.learn2crack.com/2014/01/android-custom-gridview.html

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

1 Comment

Thanks Alot it's really helpful I already Created a Dynamic GridView with with Just 1 Item Duplicate from my Database I just want add attributes to be shaped like a button and each button spacing from another some thing like Image I post I'll post my grid View now have a chack :)
0

same as we create listview just use gridView instead of list view in xml

1 Comment

You mean that create a GridView with XML File?

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.