How to create Grid view using drawable images in android
First create a simple app with empty activity
Now create grid view via xml
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:id="@+id/gview"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:id="@+id/gview"
/>
Now initialize in java file
GridView gridView;
gridView = (GridView) view.findViewById(R.id.gview);
Create new layout resource .xml file for thumbnails
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:id="@+id/thumbimg"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/thumbtxt" /> </LinearLayout>
Now Create Grid Adapter Class
package com.example.user.goruntu; 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; import java.util.ArrayList; import java.util.HashMap; public class GridAdapter extends BaseAdapter { ArrayList<HashMap<String,String>> data; Context cnt; public GridAdapter(Context cnt, ArrayList<HashMap<String,String>> data){ this.data = data; this.cnt = cnt; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View view, ViewGroup viewGroup) { HashMap<String,String> item = data.get(position); ViewHolder viewHolder = new ViewHolder(); LayoutInflater inflater = LayoutInflater.from(cnt); view=inflater.inflate(R.layout.griditems,viewGroup,false); viewHolder.thumbimg=(ImageView) view.findViewById(R.id.thumbimg); viewHolder.thumbtxt=(TextView) view.findViewById(R.id.thumbtxt); if(item.get("imgname").equalsIgnoreCase("flw1")){ viewHolder.thumbimg.setImageResource(R.drawable.flw1); viewHolder.thumbtxt.setText(item.get("imgtxt")); }else{ viewHolder.thumbimg.setImageResource(R.drawable.flw2); viewHolder.thumbtxt.setText(item.get("imgtxt")); } return view; } class ViewHolder{ ImageView thumbimg; TextView thumbtxt; } }
Go to Main activity class and assign grid view with above
adapter
package com.example.user.goruntu; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.WindowManager; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { GridView gridView; ArrayList<HashMap<String,String>> list;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); gridView = (GridView) view.findViewById(R.id.gview); list = new ArrayList<>(); for(int i=0;i<=10;i++) { HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("imgtxt",Integer.toString(i) + ".) Flowers"); if(i%2==0) { hashMap.put("imgname", "flw1"); list.add(hashMap); }else{ hashMap.put("imgname", "flw2"); list.add(hashMap); } } GridAdapter adp = new GridAdapter(view.getContext(),list); gridView.setAdapter(adp);
}
}
No comments:
Post a Comment