I am trying to dismiss a Snackbar when a condition is satisfied.
snackbar.setAction("Retry", new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isConnected(getApplicationContext())){
toggleButtonsState(true);
snackbar.dismiss();
} else {
snackbar.show();
}
}
});
snackbar.show();
However, the Snackbar implementation do automatic dismiss of it when the action is clicked.
public Snackbar setAction(CharSequence text, final OnClickListener listener) {
TextView tv = this.mView.getActionView();
if(!TextUtils.isEmpty(text) && listener != null) {
tv.setVisibility(0);
tv.setText(text);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
listener.onClick(view);
Snackbar.this.dismiss();
}
});
} else {
tv.setVisibility(8);
tv.setOnClickListener((OnClickListener)null);
}
return this;
}
Generally, is there a way to modify a compiled gradle library so as to cope with my needs. ie. to remove this line Snackbar.this.dismiss();. I know I can search for the source and put in in libs folder and then I can modify it but is there a way to do so rather than this?
Unfortunately, you can't because Snackbar class is final and cannot override methods so it's behavior.
What you can try, is setting View.OnTouchListener to Snackbar view and prevent it getting any touch events only for the action view. I'm not sure if snackBar.getView() returns whole snackbar including the action views.
snackBar.getView().setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch (View v, MotionEvent event){
if(v instanceof Button) {
//This is the Action view event, do your logic.
if(isConnected(getApplicationContext())){
toggleButtonsState(true);
return false;
}
return true;
}
//not action view, proceed normally
return false;
});
Related
Long press
When I long-click on an item of the message, the item will display and the layout changes like the picture. I want to make this , but i don't have a keyword to find this solution. I need a keyword or some example to make it.
Many ways you can do. I am going to share one example.
Implement View.OnLongClickListener as follows
private void setupLongPress() {
imageButton.setOnLongClickListener(new View.OnLongClickListener(){
#Override
public boolean onLongClick(View v){
// here your staff
// we added dialog method here as follows
createPreviewDialog();
return false;
}
});
}
Now use LayoutInflater to inflate new layout as a popup windows
private Dialog createPreviewDialog() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_preview, null);
LinearLayout closeButton = view.findViewById(R.id.close);
closeButton.setOnClickListener (new View.OnClickListener (){
#Override
public void onClick ( View view ) {
dismiss();
}
});
View okButton = view.findViewById(R.id.ok);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
// here your staff
}
});
builder.setView(view);
return builder.create();
}
I can't handle clicks on recycerView body (NOT ITEMS),
but when I setOnClickListener on my recycler, it does not work.
I tried clickable="true" in xml, but does not work.
recycler.setOnClickListener {
Toast.makeText(context,"Click on recyclerView",Toast.LENGTH_SHORT).show()
}
Just use a Touch Listener :
recyclerView.setOnTouchListener { _, event ->
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
Toast.makeText(context, "Clicked", LENGTH_SHORT).show()
true
}
else -> false
}
}
You need to pass your click listener on "onBindViewHolder()"
That way you will be able to receive where the user clicked and perform something with it.
public void onBindViewHolder(LanguageAdapter.ViewHolder holder, final int position) {
final String name = names.get(position);
holder.main_item_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
}
And what you want to do is to set the click listener on your root layout from your items
I have a TextView and I put a OnClickListener on this TextView. I use this action to load custom view onto a LinearLayout.
But when I click on this TextView twice, custom view is repeating on the LinearLayout. I clear all custom views on this LinearLayout before I load new custom views on to this LinearLaout.
This is my OnClickListener on TextView,
TextView rejectedTitleTextView = (TextView) findViewById(R.id.roster_menu_rejected_title);
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rejectedTitleTextView.setBackgroundColor(getResources().getColor(R.color.acceptedPurpleColour));
newTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
acceptedTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
locationLinearLayout.removeAllViews();
rosterBottomLayout.setVisibility(View.GONE);
Log.d("CHECK_ACTION"," REJECTED_TEXT_VIEW ");
InternetConnectivity internetConnectivity = new InternetConnectivity();
final boolean isConnectedToInternet = internetConnectivity.isConnectedToInternet(context);
if(isConnectedToInternet==true) {
try {
Thread.sleep(1300);
} catch (Exception e) {
e.printStackTrace();
}
getDataFromServer("REJECTED");
}else{
Snackbar.make(mainView, "No Internet Connection", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
getDataFromServer("REJECTED");
is the method which I used to load custom view onto this LinearLayout.
How can I prevent this issue ?
Have any ideas ?
Inside onclickListener put
rejectedTitleTextView.setClickable(false);
and once finish your functionality make it as true because u need to click for next time .
rejectedTitleTextView.setClickable(true);
Inside setOnclickListener try below code:-
textView.setClickable(false);
Try this
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mSpinner.setEnabled(false);
mSpinner.postDelayed(new Runnable() { #Override public
void run() {
mSpinner.setEnabled(true); }
}
// do your stuff here
});
You can maintain boolean value like this
boolean isClick=false;
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isClick)
{
//do your Stuff on onCLick
isClick=true;
}else
{
//leave it blank if you do not want to do anything second time
}
}
});
I have LinearLayout and a Button, in layout I have a SeekBar and when click button I show or hide LinearLayout, I used View.GONE and View.Visible to hide and show.
It work in many devices but when I test it in Note Edge or cool pad It does not work.
What is happening here?
rlFont = (RelativeLayout) rootView.findViewById(R.id.rlFont);
ivFont.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ivFont.getDrawable().getConstantState().equals(getActivity().getResources().getDrawable(R.drawable.top_a001).getConstantState())) {
ivFont.setImageResource(R.drawable.top_a002);
rlFont.setVisibility(View.VISIBLE);
rlFont.requestLayout();
} else {
ivFont.setImageResource(R.drawable.top_a001);
rlFont.setVisibility(View.GONE);
rlFont.requestLayout();
}
}
});
This can solve your problem, with this is possible to toggle your button.
boolean isClicked = false;
rlFont = (RelativeLayout) rootView.findViewById(R.id.rlFont);
ivFont.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isClicked) {
ivFont.setImageResource(R.drawable.top_a002);
rlFont.setVisibility(View.VISIBLE);
rlFont.requestLayout();
isClicked = true;
} else {
ivFont.setImageResource(R.drawable.top_a001);
rlFont.setVisibility(View.GONE);
rlFont.requestLayout();
isClicked = false;
}
}
});
I am trying to add both OnClickListener and OnTouchListener to my image view. Following is how the image view is created
dialogImage = (ImageView)findViewById(R.id.dialogImage);
Following is how the listeners are set
dialogImage.setOnClickListener(dialogBoxClicked);
dialogImage.setOnTouchListener(imageViewSwiped);
Following is the listener method implementation
public OnClickListener dialogBoxClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
//To do has been removed because the code is too big
}
};
OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
{
public void onSwipeRight()
{
currentlyActiveQuestion++;
currentWord = words.get(currentlyActiveQuestion);
setUI();
}
public void onSwipeLeft()
{
currentlyActiveQuestion--;
currentWord = words.get(currentlyActiveQuestion);
setUI();
}
};
Here the OnTouchListener is implemented by a class called OnSwipeTouchListener to monitor left and right swipes. This class can be found here - https://stackoverflow.com/a/12938787/1379286
But the problem now is, when I set the OnTouchListener to the image view, the OnClickListener is not responding / do not do what it should do. ImageView is only responding to OnTouchListener. If I remove OnTouchListener then the OnClickListener works. I tested this in a virtual device WVGA5.1 and Galaxy Nexus in eclipse and not in a real phone because I do not have one.
How can I solve this?
EDIT
Any code example will be greatly appreciated
You may call View.performClick() when action_up. This results in the click event being called when an actual click happens. Hope it helps.
your_txtView.setOnClickListener(new TextView.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
} else if (MotionEvent.ACTION_UP == event.getAction()) {
v.performClick();
}
return true;
}
});
The OnTouchListener hooks the click-event. Handle the click event in it instead. Check out the answer on this question
From my experience, If you can't have both onTouchListener and onClickListener for the same view. If you want your onClickListener to work, set clickable="true" in the XML.