How to force GridView to load cells - android

GridView is scrolled programmatically, and no new item appears coming up from the bottom.
I tried to update with the following line, but it does not force GridView to load new items.
imageAdapter.notifyDataSetChanged();
gridview.invalidateViews();
gridview.setAdapter(imageAdapter);
Simplified the app, now the scrolling can be fired with button click, but the upcoming empty items are still appearing. Here is some code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CustomGridView gridview = (CustomGridView) findViewById(R.id.gridView1);
final ImageAdapter imageAdapter = new ImageAdapter(this);
gridview.setAdapter(imageAdapter);
gridview.setNumColumns(3);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)gridview.getLayoutParams();
linearParams.width=66*3;
gridview.setLayoutParams(linearParams);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridview.scrollBy(0, 44);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 300;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(66, 66));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0); // 8 8 8 8
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(R.drawable.asdf);
return imageView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.j4nos.moviebuffs12.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="198dp"
android:layout_height="match_parent"
android:layout_marginTop="66dp"
android:layout_marginLeft="0dp"
android:layout_marginBottom="0dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.j4nos.moviebuffs12.CustomGridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:columnWidth="66dp"
android:horizontalSpacing="0dp"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="horizontal"
android:stretchMode="none"
android:verticalSpacing="0dp"
android:listSelector="#null"
android:scaleType="centerCrop">
</com.j4nos.moviebuffs12.CustomGridView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/* ADD THIS */
#Override
public int computeVerticalScrollOffset() {
return super.computeVerticalScrollOffset();
}
}

I think the problem is with the way you are scrolling:
gridview.scrollBy(0, 44);
This moves the gridview, not the content of the gridview. You can verify this by adding the following into your layout inside the CustomGridView component:
android:fastScrollAlwaysVisible="true"
Now you can see it's not the gridview content that is being interacted with on the button press.
Instead, I suggest you try to use:
gridview.scrollListBy(44);
if your API level allows it.

Related

How to set WearableListView item height

I make WearableListView list. Problem is that setting android:layout_height="20dp" doesn't help
How to set height in this case? In Android Wear sample projects Notifications and Timer they also just set atribute android:layout_height="80dp". But I tried to set in the projects android:layout_height="20dp" but it didn't help! (below is my project source code):
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<base.mobitee.com.mobiteewatch.adapter.HolesListItemLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center_vertical" >
<TextView
android:id="#+id/text_hole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:textSize="#dimen/list_item_text_size" />
</base.mobitee.com.mobiteewatch.adapter.HolesListItemLayout>
HolesListItemLayout.java:
public class HolesListItemLayout extends LinearLayout
implements WearableListView.OnCenterProximityListener {
private TextView mName;
private final int mFadedTextColor;
private final int mChosenTextColor;
public HolesListItemLayout(Context context) {
this(context, null);
}
public HolesListItemLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HolesListItemLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mFadedTextColor = getResources().getColor(R.color.grey);
mChosenTextColor = getResources().getColor(R.color.black);
}
// Get references to the icon and text in the item layout definition
#Override
protected void onFinishInflate() {
super.onFinishInflate();
mName = (TextView) findViewById(R.id.text_hole);
}
#Override
public void onCenterPosition(boolean animate) {
mName.setTextSize(18);
mName.setTextColor(mChosenTextColor);
}
#Override
public void onNonCenterPosition(boolean animate) {
mName.setTextColor(mFadedTextColor);
mName.setTextSize(14);
}
}
HolesListAdapter.java:
public class HolesListAdapter extends WearableListView.Adapter {
private final Context mContext;
private final LayoutInflater mInflater;
public HolesListAdapter(Context context) {
this.mContext = context;
mInflater = LayoutInflater.from(context);
}
#Override
public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new WearableListView.ViewHolder(
mInflater.inflate(R.layout.list_item_hole, null));
}
#Override
public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
TextView text = (TextView) holder.itemView.findViewById(R.id.text_hole);
text.setText(mContext.getString(R.string.hole_list_item) + " " + (position + 1));
text.setTextColor(mContext.getResources().getColor(android.R.color.black));
holder.itemView.setTag(position);
}
#Override
public int getItemCount() {
return Preferences.HOLES;
}
}
The WearableListView is hard-coded to only display three items at a time - it measures the item height by dividing the list view's height by three ... so there isn't a practical way of doing what you want.
I suggest making the text font larger ...
I've got idea. Insert list into FrameLayout container. And by changing height of container list item height is changed. Result:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_centerInParent="true">
<android.support.wearable.view.WearableListView
android:id="#+id/wearable_list_game_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:scrollbars="none"/>
</FrameLayout>
</RelativeLayout>
Add one LinearLayout inside and set
android:layout_height="50dp"
android:layout_margin="5dp"

setImageBitmap in gridview does not scale the image to fit

Note: This problem only happens in gridview, if you setimagebitmap in a single imageview, it works well.
See the following pictures, The left picture is what I want, and the picture on right is what actually I get.
The only difference is the way I set the image, if I set the image as below, I get the correct result(left picture above).
holder.image.setImageResource(imagesArray.get(position));
But if I set the image with bitmap as below, the image not scaled as I want(the right picture above)
holder.imageView.setImageBitmap(bitmap);
Why I set the image as bitmap instead of drawable?
because my images were encrypted, I need to decrypt them before load into the imageview. the basic step is:
depryct the image into byte arrya.
create bitmap from the bytes.
load the bitmap into imageview.
Note, the problem was not caused by the decryption, I have test with an normal image without decryption, it does not work either.
Here is the code
GridViewActivity
public class GridViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
GridView gridView = (GridView)findViewById(R.id.grid_view);
ImageAdapter imageAdapter = ImageAdapter.getInstance();
imageAdapter.setContext(this);
gridView.setAdapter(imageAdapter);
for (int i = 0; i < 50; i++) {
int resourceId = getResources().getIdentifier("myimage", "drawable", getPackageName());
imageAdapter.imagesArray.add(resourceId);
}
}
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
private static ImageAdapter imageAdapter = null;
private Bitmap bitmap;
private Context mContext;
public ArrayList<Integer> imagesArray = new ArrayList<Integer>();
private ImageAdapter() {
}
public static ImageAdapter getInstance() {
if (imageAdapter == null) {
imageAdapter = new ImageAdapter();
}
return imageAdapter;
}
public void setContext(Context context) {
mContext = context;
}
#Override
public int getCount() {
return imagesArray.size();
}
#Override
public Object getItem(int position) {
return imagesArray.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
row = inflater.inflate(R.layout.row_grid, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView)row.findViewById(R.id.image);
row.setTag(holder);
}
else {
holder = (ViewHolder)row.getTag();
}
// Create bitmap from image in assets folder.
String imageFileName = "myimage.jpg";
AssetManager assetMgr = mContext.getAssets();
try {
InputStream fis = assetMgr.open(imageFileName);
bitmap = BitmapFactory.decodeStream(fis);
}
catch (Exception ex) {
Log.i("zdd", ex.getLocalizedMessage());
}
holder.imageView.setImageBitmap(bitmap);
//holder.image.setImageResource(imagesArray.get(position));
return row;
}
static class ViewHolder {
ImageView imageView;
}
}
SquareImageView
public class SquareImageView extends ImageView
{
public SquareImageView(Context context)
{
super(context);
}
public SquareImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
Here is the layout
activity_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7f7f7f"
android:numColumns="4"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</GridView>
</RelativeLayout>
row_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
Try changing the row item's parent size to "match_parent".
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#ffffff">
<com.jiyuzhai.setimagebitmaptest.SquareImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
Try removing "padding" property from linear layout (or setting it to 0dp) and setting scaleType to fitXY in SquareImageView.

Making a clickable image to open another activity

Here i practice some tutorial of making gridview with auto resize image. i'm adopted this tutorial to my activity. what i'm gonna do next is how to make the image clickable. so that when users clicked on each image it goes to some class/activity.
Here is several code from the program I experimented :
activity_main.xml to throw a gridview into layout,stretch mode and column width :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:numColumns="2"
/>
</FrameLayout>
MainActivity.java custom ImageView that maintains its aspect ratio:
public class MainActivity extends ImageView {
public MainActivity(Context context) {
super(context);
}
public MainActivity(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MainActivity(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
This is a layout for a grid item :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.conversa.MainActivity
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="#android:color/white"
android:background="#55000000"
/>
</FrameLayout>
This is an adapter for gridview :
public class MyAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
items.add(new Item("Conversation 1", R.drawable.pic1));
items.add(new Item("Conversation 2", R.drawable.pic2));
items.add(new Item("Conversation 3", R.drawable.pic3));
items.add(new Item("Conversation 4", R.drawable.pic4));
items.add(new Item("Conversation 5", R.drawable.pic5));
items.add(new Item("Conversation 6", R.drawable.pic6));
items.add(new Item("Conversation 7", R.drawable.pic7));
items.add(new Item("Conversation 8", R.drawable.pic8));
items.add(new Item("Conversation 9", R.drawable.pic9));
items.add(new Item("Conversation 10", R.drawable.pic10));
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return items.get(i).drawableId;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if(v == null) {
v = inflater.inflate(R.layout.gridimage, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item {
final String name;
final int drawableId;
Item(String name, int drawableId) {
this.name = name;
this.drawableId = drawableId;
}
}
}
And this is where the adapter set into gridview :
public class Gridpiw extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
}
}
What should I add or maybe modify so that the image can be clicked to show new activity?
You should do it like this:
Firstly, create you activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>
</FrameLayout>
Secondly, change your Gridpiw to MainActivity classname:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
}
}
Thirdly, change your MainActivity extends ImageView to MeasuredImageView extends ImageView, like this:
public class MeasuredImageView extends ImageView {
public MeasuredImageView(Context context) {
super(context);
}
public MeasuredImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MeasuredImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}
Then, include it into your grid item layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.conversa.MeasuredImageView
android:id="#+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scaleType="centerCrop"
/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="#android:color/white"
android:background="#55000000"
/>
</FrameLayout>
NOTE: your ImageView with id "picture" should have attribute android:clickable="true".
Then in your adapter you should store the context, passed as the parameter.
public class MyAdapter extends BaseAdapter {
...
private Context context
...
public MyAdapter(Context context) {
this.context = context;
//your actions...
}
}
And finally, in your getView() function of the adapter class, you should setOnClickListener to your ImageView:
...
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if(v == null) {
v = inflater.inflate(R.layout.gridimage, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SecondActivity.class);
context.startActivity(intent);
}
});
return v;
}
...
Hope this helps.
Add This Kind Of Code For ImageViews to Your Code Inside Your Adapter Inside getView()
Add onClickListener()
picture.setOnClickListener(new OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Inten(SampleMainActivity.this,SecondActivity.class);
startActivity(mainIntent);
}
}

images not appearing in gridView

I have a gridView here:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_drag_and_drop_app"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SlidingDrawer
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:layout_marginTop="200dp"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/arrow" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
<!-- Editext for Search -->
<EditText android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/Search_applications"
android:inputType="textVisiblePassword"/>
<ListView
android:id="#+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</SlidingDrawer>
<Button
android:id="#+id/btnLinkToPersonalize"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#null"
android:text="#string/Personalize"
android:textColor="#21dbd4"
android:textStyle="bold" />
<GridView
android:id="#+id/GRIDVIEW1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dip"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
>
</GridView>
<ImageView
android:id="#+id/trash_can"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/trashcanDescription_Delete"
android:padding="40dip"
android:src="#drawable/trashcan"
android:visibility="gone" >
</ImageView>
</RelativeLayout>
and then have this coding:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class Drag_and_Drop_App extends Activity {
//For GridView
private int draggedIndex = -1;
private BaseAdapter adapterGV;
ArrayList<Integer> drawables = new ArrayList<Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// GridView
Log.d("GridView", "onCreate called");
drawables.add(R.drawable.pattern1);
drawables.add(R.drawable.pattern2);
android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);
// Instance of Adapter Class
gridView.setAdapter(new GridViewAdapter(this));
// gridView.setOnItemLongClickListener(this);
and this adapter:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context mContextGV;
// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();
// Constructor
public GridViewAdapter(Context c){
mContextGV = c;
}
#Override
// How many items are in the data set represented by this Adapter
public int getCount() {
return drawables.size();
}
#Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
return drawables.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Try to reuse the views
ImageView view = (ImageView) convertView;
// if convert view is null then create a new instance else reuse it
if (view == null) {
view = new ImageView(mContextGV);
}
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
}
}
But the drawables pattern1 and pattern2 never show up in my gridView.
I made sure that the gridView onCreate() method is being called (which it is).
What can I do to fix this?
NEW CODING:
Drag_and_Drop_App:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// GridView
Log.d("D&D", "onCreate called");
android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);
// Instance of Adapter Class
gridView.setAdapter(new GridViewAdapter(this));
// gridView.setOnItemLongClickListener(this);
My gridViewAdapter:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context mContextGV;
// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();
// Constructor
public GridViewAdapter(Context c){
mContextGV = c;
Log.d("GridViewAdapter", "Constructor is set");
}
#Override
// How many items are in the data set represented by this Adapter
public int getCount() {
return drawables.size();
}
#Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
return drawables.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Try to reuse the views
ImageView view = (ImageView) convertView;
// if convert view is null then create a new instance else reuse it
if (view == null) {
view = new ImageView(mContextGV);
Log.d("GridViewAdapter", "new imageView added");
}
drawables.add(R.drawable.pattern1);
Log.d("GridViewAdapter", "pattern1 added");
drawables.add(R.drawable.pattern2);
Log.d("GridViewAdapter", "pattern2 added");
drawables.add(R.drawable.trashcan);
Log.d("GridViewAdapter", "trashcan added");
drawables.add(R.drawable.ic_launcher);
Log.d("GridViewAdapter", "ic_launcher added");
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
}
}
The closing tag of the gridview in the xml file
</gridview>
is before the imageview, therefore your image will not be in the gridView.
More generally speaking, if an image do not show, make sure you have the image with the resolution for the screen of the device you are using.

Creating grid view with clickable images in Android

I want to create a grid view with clickable images.
Whenever an image is clicked, a corresponding value will be shown below that grid view.
The problem I am facing is in the design part, I don't know how to design a grid view like this. Every time I try to do that I get some bad results. I have no android UI design experience as of now.
How can I accomplish this?
Try this
Main activity
public class MainActivity extends AppCompatActivity {
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"
} ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageAdapter adapter = new ImageAdapter(MainActivity.this,web,
imageId);
GridView grid=(GridView)findViewById(R.id.grid_view);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns: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">
<GridView xmlns: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>
ImageAdapter class
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
private final int[] Imageid;
private final String[] web;
public ImageAdapter(Context c,String[] web,int[] Imageid )
{
mContext = c;
this.Imageid = Imageid;
this.web=web;
}
#Override
public int getCount()
{
return Imageid.length;
}
#Override
public Object getItem(int position)
{
return position;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup
parent)
{
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null)
{
gridView = new View(mContext);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid_layout, null);
// set value into textview
TextView textView = (TextView)
gridView.findViewById(R.id.grid_item_label);
textView.setText(web[position]);
// set image based on selected text
ImageView imageView = (ImageView)
gridView.findViewById(R.id.grid_item_image);
imageView.setImageResource(Imageid[position]);
}
else
{
gridView = (View) convertView;
}
return gridView;
}
}
grid_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="10dp"
>
</ImageView>
<TextView
android: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>

Categories

Resources