Image Gallery in Fragment not working (Android) - android

I am trying to create image gallery in fragment but its not working.when i run the app its showing white screen in my emulater but there is no error in my logcat. i am using this tutorial for reference tutorial.this is my code
public View onCreateView(LayoutInflater inflater, ViewGroup container, AttributeSet attrs, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.imagegallery, container, false);
Gallery gallery = (Gallery) rootView.findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(getActivity()));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView <? > parent, View v, int position, long id) {
Toast.makeText(rootView.getContext(), "pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
// display the images selected
ImageView imageView = (ImageView) rootView.findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
return rootView;
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Activity activity) {
// sets a grey background; wraps around the images
TypedArray a = getActivity().obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
what i am doing wrong?

You should return rootView; in your Fragment in onCreateView(...)
return rootView;
and remove
super.onInflate(attrs, savedInstanceState);
and also change
gallery.setAdapter(new ImageAdapter(this));
to
gallery.setAdapter(new ImageAdapter(getActivity());

Try this
public class HomeFragment extends Fragment {
public HomeFragment(){}
private Gallery gallery;
private ListView listview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
gallery = (Gallery) rootView.findViewById(R.id.gallery1);
listview = (ListView) rootView.findViewById(R.id.listView1);
gallery.setAdapter(new GalleryViewAdapter(getActivity()));
String values[] ={"item1","item2","item1","item2"};
ArrayList<String> listValue = new ArrayList<String>();
for(int i=0;i<values.length;i++){
listValue.add(values[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);
Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.fly_in_from_center1);
gallery.setAnimation(anim);
anim.start();
gallery.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//view.setBackgroundResource(R.drawable.list_item_bg_pressed);
Animation vanish =AnimationUtils.loadAnimation(getActivity(),R.anim.vanish);
view.startAnimation(vanish);
Log.i("pos :: ","pos :: "+position);
//Toast.makeText(gridView.getContext(), "pos :: "+position, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
this is Adapter
public class GalleryViewAdapter extends BaseAdapter {
private Context context;
public GalleryViewAdapter(Context c) {
context = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 15;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = LayoutInflater.from(context).inflate(R.layout.row,null);
v.setLayoutParams(new Gallery.LayoutParams(250,250));
}
else {
v = convertView;
}
ImageView imageview = (ImageView)v.findViewById(R.id.row_img);
TextView txtview = (TextView)v.findViewById(R.id.row_title);
txtview.setText("Hello"+position);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setPadding(6, 6, 6, 6);
return v;
}
}

getActivity() is working inside of pageadapter .
GalleryAdapter pageAdapter = new GalleryAdapter(getActivity(), proName, proImage);
ViewPager pager = (ViewPager)getView().findViewById(R.id.galery);
pager.setAdapter(pageAdapter);

Related

Display image full screen from gridView

i have a GridView with a photos.
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_me, container, false);
TextView t = (TextView) view.findViewById(R.id.textName);
name = MySharedPreferences.getInstance(getActivity()).getName();
t.setText(name);
gridView = (GridView) view.findViewById(R.id.gridPhoto);
img = new ImageAdapter(getActivity(), path);
gridView.setAdapter(img);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//code
}
});
requestPath();
return view;
}
In requestPath() set the ImageAdapter with the url of the images.
In onItemClick I would like to get the image in the grid
This is ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context context;
private final LayoutInflater inflater;
private List<PathImage> path;
private String username = null;
public ImageAdapter(Context c, List<PathImage> path){
context = c;
this.path = path;
inflater = LayoutInflater.from(context);
}
public ImageAdapter(Context c, List<PathImage> path, String username){
context = c;
this.path = path;
inflater = LayoutInflater.from(context);
this.username = username;
}
#Override
public int getCount() {
return path.size();
}
#Override
public Object getItem(int position) {
return path.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ImageView imageView;
if (v == null) {
v = inflater.inflate(R.layout.gridview_item, parent, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
}
imageView = (ImageView) v.getTag(R.id.picture);
String url = "";
if(username == null) {
url = "http://example.com/image/" + MySharedPreferences.getInstance(context).getUsername() + "/";
}else{
url = "http://example.com/image/" + username + "/";
}
Glide.with(context).load(url + path.get(position).getPath()).into(imageView);
return v;
}
public void updatePath(List<PathImage> p){
path = p;
this.notifyDataSetChanged();
}
}
I would like to display the images in another fragment when I click on gridview.
How do I move an image from one fragment to another?
in ImageAdapter, implment a custome callback like below:
private ImageViewClickListener mImageViewClickListener;
public interface ImageViewClickListener {
void onImageClicked(int position);
}
public void setImageViewClickListener (ViewClickListener viewClickListener) {
mImageViewClickListener = viewClickListener;
}
Now, in your activity, implement ImageViewClickListener interface and call imageAdapter.setImageViewClickListener(this) and implement below code in getview:
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mImageViewClickListener.onImageClicked(position);
}
});

How to add item to gridview using data from bundle

I am using this tutorial to make gridview:
https://developer.android.com/guide/topics/ui/layout/gridview.html
I have changed the mThumbIds from array to List<Integer> and I am trying to initialize it with data that came to dataGridFragment from MainActivity via Bundle but it doesn't work. It works if I add element manually. I think that the bundle thata comes too late but have no idea how to synchronise it.
public class GameFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sudoku_game_fragment,
container, false);
ImageAdapter adapter = new ImageAdapter(getActivity());
List<Integer> array ;
GridView gridview = (GridView) view.findViewById(R.id.gridview);
gridview.setAdapter(adapter);
if(getArguments() != null) {
array =getArguments().getIntegerArrayList("values");
adapter.mThumbIds.add(0, R.drawable.zero);
}
adapter.notifyDataSetChanged();
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getActivity(), "" + position,
Toast.LENGTH_SHORT).show();
}
});
return view;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public List<Integer> mThumbIds = new ArrayList<Integer>();
// public Integer[] mThumbIds;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.size();
}
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) {
// populateImages();
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
imageView.setImageResource(R.drawable.one);
} else {
imageView = (ImageView) convertView;
}
//imageView.setImageResource(R.drawable.two);
// Log.d("getView", String.valueOf(values[position]));
imageView.setImageResource(mThumbIds.get(position));
return imageView;
}
}

Adding gridview with images and texts

I am trying to create a gridview in Android Studio with images and texts. I found a couple of solutions but they were specific to the a single activity and not with activity with fragment. I can populate the text in grid using the following code. But I want images as well.
What should I do? I have a little experience in android development and I need to submit this project in 2 weeks from now.I have commented out some probable solutions but even they don't work. Please check the whole code.
This is my mainactivity with a placeholder fragment.
public static class PlaceholderFragment extends Fragment {
private ArrayAdapter<String> gridAdapter;
//gridView gridAdapter;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final String[] mainMenuArray = {
"Create a new class",
"Take Attendance",
"Check Up Status",
"Update/Modify Students",
"Delete/Update class",
"Add students"
} ;
int[] imageId = {
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
};
List<String> mainMenu = new ArrayList<String>(Arrays.asList(mainMenuArray));
// gridAdapter = new gridView(getActivity(), mainMenuArray, imageId);
gridAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.grid_items, // The name of the layout ID.
R.id.grid_text, // The ID of the textview to populate.
mainMenuArray);
GridView grid;
//gridView gridAdapter = new gridView(getActivity(), mainMenuArray, imageId);
grid=(GridView)rootView.findViewById(R.id.grid);
grid.setAdapter(gridAdapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " + mainMenuArray[+position], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
And for the adapter.
public class gridView extends BaseAdapter{
private Context mContext;
private final String[] mainMenuArray;
private final int[] Imageid;
public gridView(Context c,String[] mainMenuArray,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.mainMenuArray = mainMenuArray;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mainMenuArray.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_items, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(mainMenuArray[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
public static class PlaceholderFragment extends Fragment {
private ArrayAdapter<String> gridAdapter;
gridView gridAdapter;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView grid=(GridView)rootView.findViewById(R.id.grid)
final String[] mainMenuArray = {
"Create a new class",
"Take Attendance",
"Check Up Status",
"Update/Modify Students",
"Delete/Update class",
"Add students"
} ;
int[] imageId = {
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
0,0,0,0
};
gridAdapter = new gridView(getActivity(), mainMenuArray, imageId);
grid.setAdapter(gridAdapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " + mainMenuArray[+position], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Try this Let me know :)

GridView with buttons scale to size

I have problem with my gridview filled with button. Size of buttons are different when I user match_parent, when I set fixed size it looks good, but not on all screen sizes. Maybe I should use other component than buttons? This is my code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.myfragment, container,
false);
GridView myfragment_grid = (GridView) rootView.findViewById(R.id.myfragment_grid);
myfragment_grid.setAdapter(new GridAdapter(getActivity()));
return rootView;
}
public class GridAdapter extends BaseAdapter {
public GridAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 12;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
String[] labels = getActivity().getResources().getStringArray(
R.array.grid_labels);
Button menuButton;
if (convertView == null) {
menuButton = new Button(mContext);
menuButton.setBackgroundColor(getResources().getColor(
R.color.grid_item_color));
menuButton.setTextColor(getResources().getColor(R.color.white));
menuButton.setTextSize(15);
menuButton.setText(labels[position]);
menuButton.setLayoutParams(new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
menuButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
switch (position) {
...
}
}
});
} else {
menuButton = (Button) convertView;
}
return menuButton;
}
private Context mContext;
}

Issues with gridview in fragment

I have been trying to apply a gridview to a fragment but I am having issues with applying the adapter
public class Playerstats extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
//super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.playerstats, container, false);
GridView gridview = (GridView) view.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(getActivity()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
}
});
return view;
}
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.sample_2, R.drawable.sample_3,
};
}
I am getting the issues:
The method setAdapter(ListAdapter) in the type GridView is not applicable for the arguments (ImageAdapter)
and
The constructor ImageAdapter(FragmentActivity) is undefined
Any suggestions?
Use this : new ImageAdapter(getActivity().getApplicationContext()). Your construct uses a Context instead of an FragmentActivity (returned by the getActivity() call)
Here is the full code
public class Playerstats extends Fragment{
GridView gridview;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
//super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.playerstats, container, false);
gridview = (GridView) view.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}

Categories

Resources