Android:Grid View - android

I want to ask how can i resize the size of the grid to a larger size according to my needs.
here is my xml file:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/grid_view"
android:columnWidth="150dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:padding="20dp"
android:gravity="center" />
</RelativeLayout>
here is my layout file:
package com.example.steve_jobs;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
image adapter class:
package com.example.steve_jobs;
import android.content.Context;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.view.ViewGroup;
/**
* Created by yash on 19/6/13.
*/
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sj_1,
R.drawable.sj_2,
R.drawable.sj_3,
R.drawable.sj_4,
R.drawable.sj_5,
R.drawable.sj_6,
R.drawable.sj_7,
R.drawable.nexus
};
}

You could remove or reduce all that padding!
edit:
try
android:stretchColumns="0,1,2"
0, 1 and 2 represent indexes of your columns;
so if you have 6 columns you would use "0,1,2,3,4,5"

you should use this
<GridView
android:id="#+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>

Related

ImageButton inside of GridView not clickable

Here is my code for dynamically creating ImageButtons inside of gridviews. When I run the activity, the ImageButtons are not clickable. I tried adding
android:descendantFocusability="blocksDescendants"
to the xml file but this did not fix the problem.
EDIT: I updated the adapter to be for ImageViews, not ImageButtons, but the problem persists.
Here is my custom adapter:
package com.example.myname.myproject;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
/**
* Created by myname on 3/31/17.
*/
public class ImageArrayAdapter extends BaseAdapter {
Context context;
int[] currentResources;
String[] currentIcons;
public ImageArrayAdapter(Context context, String[] currentIcons, int[] currentResources){
ImageArrayAdapter.this.context = context;
ImageArrayAdapter.this.currentIcons = currentIcons;
ImageArrayAdapter.this.currentResources = currentResources;
}
#Override
public int getCount() {
return currentResources.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
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(context);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setBackgroundColor(Color.TRANSPARENT);
} else {
imageView = (ImageView) convertView;
}
imageView.setClickable(true);
imageView.setImageResource(currentResources[position]);
return imageView;
}
}
Here is my xml file for the GridView.
<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.example.myname.myproject.AdminControlTabbed$PlaceholderFragment">
<GridView
android:id="#+id/templateGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
/>
Thanks for the help.
Why not ImageViews instead of ImageButtons. Then you have to only set an OnItemClickListener for your GridView in the activity class where you have initialized it. After you attach the adapter to it, you can attach a clickListener too like this:
myGridView.setAdapter(this, currentIconsArray, currentResourcesArray);
myGridViewsetOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//code for handling onClick event
}
});
Activity.dispatchTouchEvent(MotionEvent) dispach it to other listeners

How to add imageView dynamically to scroll view which has linear layout?

How to add imageView dynamically using java code to the LinearLayout with scroll view? I want my images side by side but when detected it's edge of the phone, add new row but I dont seem to find any answer.. all they say is just adding dynamically either vertically or horizontally but no newline..
anyone would want to help me? :( I'm new to this.
my XML code below :
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layoutWordsVert"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layoutWordsHori"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:orientation="horizontal">
<ImageView
android:id="#+id/saya"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/border_darkgreen1"
android:src="#drawable/me"
android:padding="1dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
EXPECTED VIEW:
|---------|---------|---------|
| image 1 | image 2 | image 3 |
|---------|---------|---------|
| image 4 | image 5 | image 6 |
|---------|---------|---------|
Here's what your MainActivity.java would look like,
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
ArrayAdapter<String> nosAdapter;
GridView gridView;
int images[] = { R.drawable.one, R.drawable.two, R.drawable.three,
R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven,
R.drawable.eight, R.drawable.nine, R.drawable.ten, R.drawable.zero,
R.drawable.blank };
MyGridViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.GridView);
adapter = new MyGridViewAdapter(this, images);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Toast.makeText(this, "Item at pos "+pos+" clicked", Toast.LENGTH_SHORT).show();
}
}
This would be your GridView Adapter,
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
public class MyGridAdapter extends BaseAdapter {
int[] images;
Context context;
public MyGridAdapter(Context context, int[] images) {
this.context = context;
this.images = images;
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
ImageView imageView;
LinearLayout outerLayout;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = (LinearLayout) inflater.inflate(R.layout.grid_my_images,null);
}
imageView = (ImageView) convertView.findViewById(R.id.ivImage);
imageView.setImageResource(images[pos]);
return convertView;
}
}
and finally the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="15dp" >
<GridView
android:id="#+id/GridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="3dp"
android:numColumns="3"
android:verticalSpacing="3dp" >
</GridView>
</LinearLayout>
layout file the grid, grid_my_images.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivGridMyApp"
android:layout_width="match_parent"
android:layout_height="100dp" /></LinearLayout>

How to at Click Image in GRIDVIEW Then Show name in EditText from drawable NOT from GridView Name

i have a problem in GridView, if im Click Image in GRIDVIEW Then Show name in EditText from drawable NOT from GridView Name. "NOT IMAGEVIEW Click BUT GRIDVIEW Image Click"
Example Image Ilutration
grid_view.xml :
enter code here
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<GridView
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="368dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
enter code here
ImageAdapter.Java
enter code here
package com.tes.butawarna;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.plat_ishihara_01,
R.drawable.plat_ishihara_02,
R.drawable.plat_ishihara_04,
R.drawable.plat_ishihara_05,
R.drawable.plat_ishihara_07,
R.drawable.plat_ishihara_08,
R.drawable.plat_ishihara_09,
R.drawable.plat_ishihara_10,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
AndroidGridLayout.java
enter code here
package com.tes.butawarna;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
public class AndroidGridLayoutActivity extends Activity {
String[] nama;
EditText namagambar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
namagambar=(EditText)findViewById(R.id.editText1);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), Training.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
After starting activity, just show different layout. Just ImageView and TextView.
The first editText1 is useless under the gridView. Put it in dofferent layout.

GridView is not shown in an example with Navigation Drawer

I've downloaded the Navigation Drawer example from the developers guide, I try to implement a GridView into an Activity of one of the options of the Drawer, but in execution time the GridView doesn't appears, the IDE doesn't show me any errors and in an example apart I can do it de GridView successfuly, How do I correct this problem?
My code is here:
In the MainActivity I use the same like the example with
Fragment fragment;
FragmentManager fragmentManager;
fragment = new inicioFragment(R.layout.inicio);
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
InicioActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
public class InicioActivity extends Activity {
TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inicio);
tv1=(TextView)findViewById(R.id.tv1);
GridView gv = (GridView)findViewById(R.id.gridView1);
gv.setAdapter(new ImageAdapter(this.getApplicationContext()));
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
String g=Integer.toString(position);
tv1.setText(g);
}
});
}
}
ImageAdapter.java
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(13, 13, 13, 13);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, R.drawable.image_5,
R.drawable.image_6, R.drawable.image_7,
R.drawable.image_0, R.drawable.image_1,
R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, R.drawable.image_5,
R.drawable.image_6, R.drawable.image_7,
R.drawable.image_0, R.drawable.image_1,
R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, R.drawable.image_5,
R.drawable.image_6, R.drawable.image_7
};
}
inicio.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="230dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:orientation="vertical" >
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Aqui van las notificaciones push de facebook" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="230dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<GridView
android:id="#+id/gridView1"
android:layout_width="156dp"
android:layout_height="192dp"
android:numColumns="2" >
</GridView>
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:text="InformaciĆ³n" />
</LinearLayout>
</RelativeLayout>
Thanks for your collaboration!!
Try to change InicioActivity into a fragment.
And check this question. I think you are facing the same problem.
The following worked for me. In Android Studio:
1) Create a new Navigation Drawer Activity from the list of activity templates.
2) Note that several layout files will be created for you, including a content_main.xml. Replace the XML in the content_main.xml file with your <GridView> XML.
3) Create the relevant XML files or Java classes for the GridView, eg. the ImageAdapter class and an XML file for the GridView item layout.

Place single image in multiple places without creating imageview

hi guys i need to place a single image in multiple places.
suppose i need to place one image in 5 places in a layout but using only one <ImageView> id
<ImageView
android:id="#+id/img"
android:layout_width="340dip"
android:layout_height="240dip"
android:layout_marginBottom="60dip" />
how to acheive this please help.
Use This code for which uses Grid View :
Main.java:
package sra.gri.vie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon,
R.drawable.icon,
};
}
}
main.xml: // the layout
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Lat me know if this post is of any help :)
you can do like this
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50; params.topMargin = 60;
rl.addView(iv, params);
and then add this r1 in your main view then again change margin, height and other param and add again like this you do n no of times
Do you try include tag for it?
img.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_home"/>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="#layout/img"/>
<include layout="#layout/img"/>
<include layout="#layout/img"/>
<include layout="#layout/img"/>
</LinearLayout

Categories

Resources