Skip to content Skip to sidebar Skip to footer

Creating Gridview Having Clickable Images,android

I want to create a gridview having clickable images.. When ever an image is clicked a corresponding value will be shown below that grid view. The problem I am facing is in desig

Solution 1:

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.

For an introduction to how you can dynamically insert views using an adapter, read Building Layouts with an Adapter.

http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

and These are good GridView tutorials will help you

http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

http://www.mkyong.com/android/android-gridview-example/

and

http://developer.android.com/guide/topics/ui/layout/gridview.html

Solution 2:

Solution 3:

Try this

  1. Main activity

    publicclassMainActivityextendsAppCompatActivity {
     List<String> list;
       int[] imageId = {
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.d,
        R.drawable.e,
        R.drawable.f,
         };
          String[] web = {
        "Google",
        "Github",
        "Instagram",
        "Facebook",
        "Flickr",
        "Pinterest",
        "Quora",
        "Twitter",
        "Vimeo",
        "WordPress",
        "Youtube",
        "Stumbleupon",
        "SoundCloud",
        "Reddit",
        "Blogger"
    
        } ;
       @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       ImageAdapteradapter=newImageAdapter(MainActivity.this,web, 
       imageId);
        GridView grid=(GridView)findViewById(R.id.grid_view);
       grid.setAdapter(adapter);
      grid.setOnItemClickListener(newAdapterView.OnItemClickListener() {
    
        @OverridepublicvoidonItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
        }
    });
    
     }
     }
    
    1. activity_main

      <?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.mypc.grid.MainActivity"><GridViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/grid_view"android:layout_width="fill_parent"android:layout_height="fill_parent"android:numColumns="2"android:columnWidth="90dp"android:horizontalSpacing="10dp"android:verticalSpacing="10dp"android:gravity="center"android:stretchMode="columnWidth" ></GridView></LinearLayout>
    2. ImageAdapter class

      publicclassImageAdapterextendsBaseAdapter
      {
       private Context mContext;
       privatefinalint[] Imageid;
       privatefinal String[] web;
          publicImageAdapter(Context c,String[] web,int[] Imageid )
        {
         mContext = c;
         this.Imageid = Imageid;
           this.web=web;
      }
      
      @OverridepublicintgetCount()
      {
           return Imageid.length;
       }
      @Overridepublic Object getItem(int position)
      {
           return position;
        }
        @OverridepubliclonggetItemId(int position)
      {
          return0;
         }
       @Overridepublic View getView(int position, View convertView, ViewGroup 
         parent)
        {
           LayoutInflaterinflater= (LayoutInflater) 
            mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View gridView;
            if (convertView == null)
            {
               gridView = newView(mContext);
               // get layout from mobile.xml
               gridView = inflater.inflate(R.layout.grid_layout, null);
              // set value into textviewTextViewtextView= (TextView) 
              gridView.findViewById(R.id.grid_item_label);
              textView.setText(web[position]);
             // set image based on selected textImageViewimageView= (ImageView) 
             gridView.findViewById(R.id.grid_item_image);
             imageView.setImageResource(Imageid[position]);
         }
         else
       {
            gridView = (View) convertView;
       }
          return gridView;
       }
      }
      
    3. grid_layout

      <?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="5dp" ><ImageViewandroid:id="@+id/grid_item_image"android:layout_width="100dp"android:layout_height="100dp"android:layout_marginRight="10dp"
        ></ImageView><TextViewandroid:id="@+id/grid_item_label"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@+id/label"android:layout_marginTop="5px"android:textSize="15px" ></TextView></LinearLayout>

Post a Comment for "Creating Gridview Having Clickable Images,android"