How to implement onClickListener only when is image click? - android-imageview

I have image like pieces of pizza, and I want implement onClickListener only when I click on pizza?

pizzaImageView.setClickable(true);
pizzaImageView.setOnClickListener(pizzaClickListener);
private OnClickListener pizzaClickListener = new OnClickListener() {
public void onClick(View view){
//Do code here
}
};

Related

highlight items in recyclerview android

I am trying to select items in recylerview. When I click an item, a checkbox appears on that item and it is highlighted. But the problem is that when I scroll, the item which is highlighted goes to its original color but the checkbox image remains as it should. Why the highlighted color is gone but the image remains after scrolling, I want the items to keep their state after scrolling.
public static List<Model> item = new ArrayList<Model>()
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int
position) {
holder.bind(item.get(position));
//
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.check);
public void bind(Model model) {
if (model.isChecked()){
imageView.setVisibility(View.VISIBLE);
itemView.setBackgroundColor(Color.GRAY);
model.setChecked(true);
} else {
imageView.setVisibility(View.GONE);
itemView.setBackgroundColor(Color.TRANSPARENT);
model.setChecked(false);
}
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Model s = item.get(getAdapterPosition());
if (!selectList.contains(s)){
selectList.add(s);
model.setChecked(true);
} else {
selectList.remove(s);
model.setChecked(false);
}
notifyItemChanged(getAdapterPosition());
}
});
public class Model {
private boolean isChecked = false;
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}
Don't manually change the design in the onclick listener, just update your model (as you are already doing: model.setchecked(...) depending on state)
Then call Adapter.notifyItemChanged(getAdapterPosition())
This will force the adapter to reload the item, calling onBindViewHolder, where the design should be updated properly
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Model s = item.get(getAdapterPosition());
if (!selectList.contains(s)){
selectList.add(s);
model.setChecked(true);
} else {
selectList.remove(s);
model.setChecked(false);
}
adapter.notifyItemChanged(getAdapterPosition())
}
In the above example, replace "adapter" with a reference to your recylerview adapter

How to open new fragment on Recyclerview Item Click?

I am using recycler view in a fragment. Now I want to open a new fragment on recycler view item click. I have used the following code in my recycler view adapter. Please solve my problem.
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.img.setImageResource(mData.get(position).getPhoto());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) v.getContext();
MiscellaneousFragment miscellaneousfragment = new MiscellaneousFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.linerr, MiscellaneousFragment).addToBackStack(null).commit();
}
});
But I am getting this error in Logcat.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at androidx.fragment.app.FragmentTransaction.doAddOp.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MiscellaneousFragment miscellaneousfragment = new MiscellaneousFragment();
FragmentManager fragmentManager =currentfragment.getFragmentManager()
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction()
fragmentTransaction.add(R.id.linerr, miscellaneousfragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}
});

Item click is not working, while auto scroll in recyclerview

I have used the following code to autoscroll the Horizontal recyclerview.
private final Runnable SCROLLING_RUNNABLE = new Runnable() {
#Override
public void run() {
recyclerViewDate.smoothScrollBy(pixelsToMove, 0);
setDateValue();
mHandler.postDelayed(this, duration);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_center_item_finder);
txtSelected=(TextView)findViewById(R.id.txtSelected);
getRecyclerviewData();
mHandler.postDelayed(SCROLLING_RUNNABLE, 100);
}
But I'm not able to perform click on recycler items, while scrolling.
Found this solution, created a custom recyclerview. Android - the item inside RecyclerView can't be clicked after scroll
Still click not working.

Saving button's enable disable state and getting this last state on create

I'm newbie in android and I'm trying to develop some applications. But last two weeks I've been stuck in this code. In short, I have two buttons in a layout. These buttons are Add and delete button. If I clicked the add button, add will be disabled and delete will be enabled. Or if I clicked the delete, delete will be disabled add will be enabled. I want to save state of these two buttons (which one is clicked) and get the last clicked state on create. But the problem is it's not working. When I open the app both buttons are show disable state. Or not saving either. Never working.
public class UserVideoInfoActivity2 extends AppCompatActivity {
SharedPreferences.Editor editor;
SharedPreferences preferences;
SharedPreferences.Editor editor2;
SharedPreferences preferences2;
Button buttonadd;
Button buttondelete;
private static final String ADD = "ADD_KEY";
private final static String DELETE = "DELETE_KEY";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_video_info_layout_2);
buttonadd = (Button) findViewById(R.id.buttonaddlayout);
buttondelete = (Button) findViewById(R.id.buttondeletelayout);
preferences = getSharedPreferences(ADD, Context.MODE_PRIVATE);
preferences2 = getSharedPreferences(DELETE, Context.MODE_PRIVATE);
buttonadd.setEnabled(GetState());
buttondelete.setEnabled(GetState2());
buttonadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonadd.setEnabled(false);
buttondelete.setEnabled(true);
SaveState(buttonadd.isEnabled());
}
});
buttondelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonadd.setEnabled(true);
buttondelete.setEnabled(false);
SaveState2(buttondelete.isEnabled());
}
});
}
private void SaveState(boolean isChecked) {
editor = preferences.edit();
editor.putBoolean(ADD, isChecked);
editor.commit();
}
private void SaveState2(boolean isChecked2) {
editor2 = preferences2.edit();
editor2.putBoolean(DELETE, isChecked2);
editor2.commit();
}
public boolean GetState() {
return preferences.getBoolean(ADD, false );
}
public boolean GetState2() {
return preferences2.getBoolean(DELETE, false);
}
}
<Button
android:id="#+id/buttondeletelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="delete" />

Android App: Replace default button background with custom background in a Dialog Fragment

What I'm trying to do is change the default backgrounds of a custom DialogFragment that I have written. Normally, I would do this by changing the XML layout file, however, for a DialogFragment these buttons don't exist in the layout file.
In essence, I'm trying to get access to the setPositiveButton, setNegativeButton and setNeutral buttons in order to modify them. Alternatively, I would next try to do this by getting them by id, however, since they aren't defined in a layout file, I do not have a corresponding id for them. I've found plenty examples of how to modify the rest of the layout, but I can't find anywhere that positive/neutral/negative buttons are modifiable.
Ideally, I could do this in the following block of code:
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
...
}
})
Thanks in advance.
Here is the code ... The button instance is valid only after the dialog created. Hope this helps you.
public static class CustomDialog extends DialogFragment
{
public static CustomDialog newInstance()
{
return new CustomDialog();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
super.onCreateDialog(savedInstanceState);
Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog dialog = builder.create();
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return dialog;
}
#Override
public void onStart()
{
super.onStart();
Button pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
Button nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(R.color.Blue));
nButton.setBackgroundColor(getResources().getColor(R.color.Green));
}
}