GridView with buttons scale to size - android

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;
}

Related

How to create dialog with GridLayout?

How to create Dialog, which uses GridLayout and when a user clicks on the item of this layout and then click "choose" button. The item of GridLayout consist of an ImageView. How to realize this Dialog?
1 -Make the custom layout you want
2 - make myDialog class which extends DialogFragment and make it inflate the layout you made
3-in the fragment add the functionality you want
But I am using gridview , gridlayout is simpler but i suggest using gridview
Here the Dialog class
public class myDialog extends DialogFragment implements dis_listener {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_dialog_layout,container,false);
GridView gv = view.findViewById(R.id.dialog_grid);
grid_adapt adapt = new grid_adapt(getActivity(),this);
gv.setAdapter(adapt);
return view;
}
#Override
public void listen() {
this.dismiss();
}
}
grid_adapt :-
public class grid_adapt extends BaseAdapter {
private Context cxt;
//pass the data you want
//private ArrayList<Integers> imgs
private dis_listener img_dismiss;
grid_adapt(Context context,dis_listener dismisser){
this.cxt = context;
this.img_dismiss = dismisser;
}
#Override
public int getCount() {
//return the data length
//return imgs.length
return 3;
}
#Override
public Object getItem(int position) {
//return imgs.get(position);
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder vh;
if(convertView == null) {
convertView= LayoutInflater.from(cxt).inflate(R.layout.grid_item, parent, false);
vh = new viewHolder(convertView);
convertView.setTag(vh);
}else{
vh = (viewHolder) convertView.getTag();
}
vh.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(cxt,"Open",Toast.LENGTH_LONG).show();
}
});
vh.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
img_dismiss.listen();
}
});
return convertView;
}
static class viewHolder{
private Button button;
private ImageView img;
viewHolder(View view){
button = view.findViewById(R.id.dialog_item_button);
img = view.findViewById(R.id.img);
}
}
}
The interface has only one method called listen
result:
GridView gridView = new GridView(this);
gridView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, your_list));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do something here
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(gridView);
builder.setTitle("ALERT BOX WITH GRID");
builder.show();
Try this , This Based On the Single Column, Data Grid ..
If You want the Image, Please Follow this,
https://android-developers.googleblog.com/2012/05/using-dialogfragments.html

How to implement onClick() method on ImageView

I have dynamically populated ImageViews and Textviews.
Now I have small problem with implementing onClick method on ImageView. On some ImageView click it should switch to fragment and for some click onto activity.
here is my code - my main fragment:
public class MainFragment extends Fragment {
public static String[] gridViewStrings = {
"string1",
"string2",
"string3",
"string4",
"string5",
"string6"
};
public static int[] gridViewImages = {
R.drawable.delivery,
R.drawable.shipping_logs,
R.drawable.meassurement,
R.drawable.takeovers,
R.drawable.settings,
R.drawable.download_data
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_fragment, parent, false);
gridView = (GridView) view.findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(getActivity(), gridViewStrings, gridViewImages));
imageView = (ImageView) view.findViewById(R.id.gridview_image);
}
my custom gridview adapter:
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(final int p, final View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
QUESTION: How to implement onClick() on ImageView and switch to fragment and activity?
public DlgOutletListAdapter(Context context, List<WrapperOutlet> wrapperOutletList, View.OnClickListener listener) {
this.context = context;
this.wrapperOutletList = wrapperOutletList;
this.listener = listener; //---> pass in listener....
}
#Override
public int getCount() {
return wrapperOutletList.size();
}
#Override
public Object getItem(int position) {
return wrapperOutletList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_dlg_outlet_list, parent, false);
viewHolder = new ViewHolder(convertView);
viewHolder.ll_dlg_outlet_list_container.setOnClickListener(listener);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (!wrapperOutletList.isEmpty()) {
WrapperOutlet wrapperOutlet = wrapperOutletList.get(position);
Outlet outlet = wrapperOutlet.getOutlet();
if (outlet != null) {
if (TextUtils.isEmpty(outlet.getTel())){
viewHolder.ivCall.setImageResource(R.drawable.icon_call2);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.medium_gray));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(null);
} else {
viewHolder.ivCall.setImageResource(R.drawable.icon_call);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.fresh_teal));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(listener);
}
}
}
return convertView;
}
Pass in listener. Give you an example above. code below is where you call the adapter in fragment. My fragment implements View.OnClickListener
lv_dlg_outlet_list.setAdapter(new DlgOutletListAdapter(activity, wrapperOutletList, this));
Interface Callback:
public interface LaunchFragmentWithImage{
void launchFragmentWithImage(int imageID);
}
Give the adapter a reference of this class below so it can call the method. Wouldn't recommend giving the adapter itself a reference to the class because its not the adapters responsibility to know who recieves the image, thus violating the Single responsibility principle of OOD, but for the sake of learning it's okay.
public class ClassToLaunchNewFragment extends AppCompatActivity implements
LaunchFragmentWithImage{
#Override //Method from interface we've defined.
public void launchFragmentWithImage(int imageID){
FragmentToLaunch fragmentToLaunch = FragmentToLaunch.newInstance();
Bundle imageData = new Bundle();
imageData.putInt(imageID, "key");
fragmentToLaunch.setArguments(imageData);
getSupportFragmentManager().beginTransaction() //Call replace not add
.replace(containerID, fragmentToLaunch)
.commit();
}
}
In AdapterClass
imageViewVar.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
fragmentToLaunchInstanceThatWasPassedIn.launchFragmentWithImage(theImageResource);
}
});
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
why are you pointing the grid variable to two separate View objects?
if (convertView == null) {
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(choosedPosition){
case 0:do something;break;
case 1:do something;break;
...
}
}
});
}
It's just an example and can't running.

get swiped image position in swipe cards [Tinder Swipe]

I am working on tinder swipe cards library.So I referred this Sample.
My issue is, while swiping image I need to get the swiped image position.So far I tried like int idGet = flingContainer.getSelectedItemPosition(); But it is not working for me.
Below I have posted relevant code :
Logcat:
E:/ idGet : -1
CardsFragment.java:
public class CardsFragment extends Fragment {
private SwipeFlingAdapterView flingContainer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
flingContainer = (SwipeFlingAdapterView) rootView.findViewById(R.id.frame);
likeImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
likeNope();
flipMethodRight(80.00f);
}
});
}
}
void flipMethodRight(float scrollProgressPercent) {
flingContainer.getTopCardListener().selectRight();
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
public class MyAppAdapter extends BaseAdapter {
public List<GetImageData> parkingList;
public Context context;
private MyAppAdapter(List<GetImageData> apps, Context context) {
this.parkingList = apps;
this.context = context;
}
#Override
public int getCount() {
return parkingList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater =getActivity(). getLayoutInflater();
rowView = inflater.inflate(R.layout.adapt_card_frag, parent, false);
Log.e("AdapterCalling", "AdapterCalling");
// configure view holder
viewHolder = new ViewHolder();
viewHolder.DataText = (TextView) rowView.findViewById(R.id.bookText);
viewHolder.background = (FrameLayout) rowView.findViewById(R.id.background);
viewHolder.cardImage = (ImageView) rowView.findViewById(R.id.cardImage);
Log.e("ParkingList", "" +parkingList.get(position).getDescription() + "");
int idGet = flingContainer.getSelectedItemPosition();
Log.e("idGet", ""+idGet);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.DataText.setText(parkingList.get(position).getDescription() + "");
Glide.with(CardsFragment.this).load(parkingList.get(position).getImagePath()).into(viewHolder.cardImage);
return rowView;
}
}
I solved this issue with the help of below code:
String getImageFlingStr, GET_PRESENT_IMAGE;
getImageFlingStr = nameImgArrList.get(0).getImagePath();
presentImageShowing = getImageFlingStr;
GET_PRESENT_IMAGE = presentImageShowing;
Log.e("presentImageShowing", ""+presentImageShowing);

Image Gallery in Fragment not working (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);

Android FlipView Scrolling Back and Front Page with Button

I want to flip between two fragment with button click.and stop touch action.
using
android-flip,
issue link.
Sample of my code
public class FlipFragmeent extends Fragment{
private int currentFragmentIndex;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
flipView = new FlipViewController(inflater.getContext(),
FlipViewController.HORIZONTAL);*emphasized text*
//flipView.setFlipByTouchEnabled(false);
flipView.setAdapter(new BaseAdapter() {
#Override
public int getCount() {
return 4;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) battleActivity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(
R.layout.fragment_next, null);
((Button)convertView.findViewById(R.id.next)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
flipView.refreshPage(currentFragmentIndex);
currentFragmentIndex++;
if(currentFragmentIndex == 4)
currentFragmentIndex = 0;
}
}));
return convertView;
}
});
return flipView;
}
BUT that does not work any help?

Categories

Resources