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
Related
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.
I've four textviews and a button in listview. when user clicks on the button i want to get the value of these four textviews and pass to the database. I'm confuse how to save the data of selected item to sqlite.
public class PlannningListViewAdapter extends BaseAdapter{
Context context;
public ArrayList<Planning> planArraylist;
private static LayoutInflater inflater = null;
public Activity planActivity;
public PlannningListViewAdapter(Context context,
ArrayList<Planning> planArraylist) {
this.context = context;
this.planArraylist = planArraylist;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return planArraylist.size();
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_lv_item, null);
}
Planning p = planArraylist.get(position);
TextView tvDocCode = (TextView) convertView.findViewById(R.id.Plan_no);
TextView tvDocName = (TextView) convertView.findViewById(R.id.doc_name);
TextView tvmon = (TextView) convertView.findViewById(R.id.mon);
TextView tvAdr = (TextView) convertView.findViewById(R.id.adr);
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setOnClickListener(ButtonClickListener);
tvDocCode.setText(p.getDocCode());
tvDocName.setText(p.getDocName());
tvmon.setText(p.getTerrCode());
tvAdr.setText(p.getAdr());
return convertView;
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
// Log.v(TAG, "item clicked, row %d", position);
}
};
}
You can use setTag()and getTag() method on your button.
Please look at the code below:
You have to change getItem method:
#Override
public Planning getItem(int arg0) {
return planArraylist.get(arg0);
}
Update your getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
Button btn_save =(Button)convertView.findViewById(R.id.button1);
btn_save.setTag(position);
...
}
private OnClickListener ButtonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = (int)v.getTag();
Planning planning = getItem(position);
yourDatabaseHelper. getRuntimeExceptionDao(Planning.class).createOrUpdate(planning);
}
};
One more thing. You should implement ViewHolder please read about this pattern, and the best solution for you is use RecyclerView, getting position is already implemented there in RecyclerView.ViewHolder.
Firstly, sorry for my english, I am french.
So, I have a CustomArrayAdapter (ReservationAdapter), and I would remove an item from this class. I have tried many things, but they didn't work.
public class ReservationAdapter extends ArrayAdapter<Reservation> {
public ReservationAdapter(Context context, ArrayList<Reservation> reservations) {
super(context, 0, reservations);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Reservation reservation = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ReservationHolder holder;
if (convertView == null) {
holder = new ReservationHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.reservation_fragment, parent, false);
holder.date = (EditText) convertView.findViewById(R.id.dateReserv);
holder.imgDelete = (ImageButton) convertView.findViewById(R.id.less);
convertView.setTag(holder);
} else {
holder = (ReservationHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
holder.date.setText(reservation.date);
holder.imgDelete.setOnClickListener(deleteReservListener);
// Return the completed view to render on screen
return convertView;
}
private View.OnClickListener deleteReservListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Where I try to remove an item
notifyDataSetChanged();
}
};
public static class ReservationHolder {
private EditText date;
private ImageButton imgDelete;
}
You have to remove the item from reservations and then call notifyDataChanged().
Before of all you have to implement a OnItemClickListener on your ListView (outside of your adapter, of course):
AdapterView.OnItemClickListener itemCLickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
reservations.remove(i);
((ReservationAdapter)listView.getAdapter()).notifyDataChanged();
}
}
e voilĂ
It works, merci !
Meanwhile, I'm still looking for a solution, and have find this :
holder.date.setText(reservation.date);
holder.imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
remove(reservation);
notifyDataSetChanged();
}
});
I just put the OnClickListener into the GetView method.
Thanks again ! :)
Add position vatiable in your ReservationHolder, and also construcror as:
public ReservationHolder(int position)
{
this.position = position;
}
public int getCurrentPosition()
{
return position;
}
and in getView(): ReservationHolder holder = new ReservationHolder(position);
Change your method:
private View.OnClickListener deleteReservListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Where I try to remove an item
notifyDataSetChanged();
}
};
to:
private View.OnClickListener deleteReservListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//Where I try to remove an item
ReservationHolder holder = (ReservationHolder)v.getTag();
reservations.remove(holder.getCurrentPosition());
notifyDataSetChanged();
}
};
I'm trying to code a simple Boggle app. There's a "Shuffle Board" button which is supposed to shuffle all the cubes and show the new board.
The problem is: even though I update the LetterAdapter (inside LetterAdapter#shuffle, I call notifyDataSetChanged) AND use any combination of refreshDrawableState(), invalidate(), invalidateViews() inside onClick I can't get the view to being updated. The only working way I could find is to reassign the adapter to the GridView.
Why don't refreshDrawableState(), invalidate(), invalidateViews() or notifyDataSetChanged work? Why don't they have any effect visually?
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GridView letterGrid = (GridView) findViewById(R.id.letter_grid);
final LetterAdapter letterAdapter = new LetterAdapter(this);
letterGrid.setAdapter(letterAdapter);
Button shuffleButton = (Button) findViewById(R.id.shuffle_button);
shuffleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
letterAdapter.shuffle();
// --------------- RELEVANT PART
// letterGrid.refreshDrawableState();
// letterGrid.invalidate();
// letterGrid.invalidateViews();
letterGrid.setAdapter(letterAdapter);
// --------------- END
}
});
}
public class LetterAdapter extends BaseAdapter {
private Context mContext;
private BoggleBoard mBoggleBoard;
public LetterAdapter(Context context) {
mContext = context;
mBoggleBoard = new BoggleBoard();
}
public void shuffle() {
mBoggleBoard.shuffle();
notifyDataSetChanged();
}
#Override
public int getCount() {
return BoggleBoard.NUM_OF_CUBES;
}
#Override
public Object getItem(int position) {
return mBoggleBoard.getLetter(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Button letterButton;
final String curLetter = mBoggleBoard.getLetter(position);
if (convertView == null) {
letterButton = new Button(mContext);
letterButton.setText(curLetter);
} else {
letterButton = (Button) convertView;
}
return letterButton;
}
}
}
NOTE: I have read about a dozen relevant questions/answers but couldn't find any satisfying answer for my case.
The problem is in your getView method. You should call letterButton.setText(curLetter); outside the condition.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Button letterButton;
final String curLetter = mBoggleBoard.getLetter(position);
if (convertView == null) {
letterButton = new Button(mContext);
} else {
letterButton = (Button) convertView;
}
letterButton.setText(curLetter);
return letterButton;
}
in my app i use list view. in the list view i have three image buttons(play,detail,buy). each image button has individual actions. how can i perform onclick action for each image button in list view.
my code:
public class AndroidThumbnailList extends ListActivity{
..........
public class MyThumbnaildapter extends ArrayAdapter<String>{
public MyThumbnaildapter(Context context, int textViewResourceId,String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
public View getView(int position, View convertView, ViewGroup parent) {
.........
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_contentUri = MEDIA_EXTERNAL_CONTENT_URI;
initVideosId();
setListAdapter(new MyThumbnaildapter(AndroidThumbnailList.this, R.layout.row, _videosId));
}
}
how to write action for my list view. please help me.
You will need to write your own Adapter that inflates the view you want to use, and then assigns an OnClick listener to each of the images. Here is some sample code from one of my projects that does something similar (but with a single checkbox that I add a listener to).
public class GroupListAdapter extends BaseAdapter {
private List<Group> groups;
// ... constructors here
#Override
public int getCount() {
return groups.size();
}
#Override
public Group getItem(int position) {
return groups.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final Group group = getItem(position);
final View view;
if (convertView == null)
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.group, null);
else
view = convertView;
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do stuff when the rest of the view is clicked
}
});
TextView tv = (TextView) view.findViewById(R.id.group_name);
tv.setText(group.getName());
final CheckBox check = (CheckBox) view.findViewById(R.id.group_checkbox);
check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do stuff when clicked
}
});
return view;
}
}