Boolean to check if a view is being clicked - android

I have a question concerning the views in android studio:
Is there a method which returns a Boolean to indicate if a "view is on a longclick"

By setting an onClickListener on it, then taking action when the listener's onClick() method is called, e.g.:
View btnView = view.findViewById(R.id.btnView);
btnView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something
}
});

Related

How to achieve on first click listener in android?

I have a customView , i want to set onClick which will only be called on the very first click. In which i want to start a thread which will start a counter on other TextView , with simple onClickListener with each click a new threads starts which is a problem . How can i achieve such task ?
Another option is in your onClick() method do set a null listener, i.e.
#Override
public void onClick(View view) {
// disable any other clicks from now on
customView.setOnClickListener(null);
...
}
I think this is only logic problem, So I solve this problem by using a boolean variable for the first click:
boolean isFristClick = true;
#Override
public void onClick(View v) {
if (isFristClick) {
// Start your counter Thread here
isFristClick = false;
} else {
// Do nothing
}
}
What about making a workaround for that?!! like assigning a boolean value to tell if it's the first click:
private boolean first_click = true;
your_view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(first_click){
first_click = false;
// Do something on first click
}else{
// Do another thing on later clicks
}
}
});

Android - How to increase EditText value?

I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}

Android: From a popup window, how do I call a method in the object that initiated the popup window?

I have an object "A"
"A" initiates the display of a popup window
That popup window has a button within it.
I want that button in the popup window to initiate a call to a method in "A".
I want to initiate the call from the line of code below that says:
"// RIGHT HERE I WANT TO CALL A METHOD IN "A" from which this popup was declared"
Generally speaking how can I call a method in the object that declares the popup window, from that popup window? this seems like it would be soo easy, but I am soo Newbie with this OO stuff.
If this explanation is confusing I will be happy to embellish.
public EventsOverlay A = new EventsOverlay(a, b))
class EventsOverlay extends ItemizedOverlay<OverlayItem> {
private PrePopupPanel panel=new PrePopupPanel( R.layout.preview1);
#Override
protected boolean onTap(int index) {
panel.show(true);
return true;
}
...
} end the EventsOverly class
class PrePopupPanel {
View popup;
boolean isVisible=false;
PrePopupPanel(int layout) {
ViewGroup parent=(ViewGroup)mapView.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
ImageButton infobtn = (ImageButton)popup.findViewById(R.id.button1);
infobtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
hide();
// RIGHT HERE I WANT TO CALL A METHOD IN "A" from which this popup was declared
}
});
... other methods like show(), hide() etc... copied from someone else
Your class is inside the other one, you may have access to all member method of your parent.
Use an Interface to do this.
-- class A should implement the interface
-- Pass this to PrePopupPanel.
-- call the method through the interface object.
Interface
package com.demo.interface;
public interface ICallHandler {
public void show(String show);
}
Class
class EventsOverlay extends ItemizedOverlay<OverlayItem> implements ICallHandler{
private PrePopupPanel panel=new PrePopupPanel( R.layout.preview1,this);
#Override
protected boolean onTap(int index) {
panel.show(true);
return true;
}
PrePopupPanel
Class PrePopupPanel {
View popup;
boolean isVisible=false;
ICallHandler mHandler;
PrePopupPanel(int layout, ICallHandler callHandler) {
mHandler = callHandler;
ViewGroup parent=(ViewGroup)mapView.getParent();
popup=getLayoutInflater().inflate(layout, parent, false);
popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hide();
}
});
ImageButton infobtn = (ImageButton)popup.findViewById(R.id.button1);
infobtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
hide();
// RIGHT HERE I WANT TO CALL A METHOD IN "A" from which this popup was declared
mHandler.show();
}
});
Use Android Dialog methods.
In onCreateDialog you can pass a reference to your custom object and trigger any methods you want when user clicks a button. Then anywhere in your activity code call showDialog()

OnclickListener for individual elements in a row from a ListActivity using SimpleCursorAdapter to Bind to DB not working properly

Please help.
As I have stated in the title I am trying to make that individual elements of a row of a List adapter launch different actions depending on what the user click.
It "kind of" works but it takes LONG for it to react to user clicks. What is it that I am doing wrong?
Thanks in advance,
So I tried the following code in
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Cursor c = (Cursor) this.getListAdapter().getItem(position);
// c.moveToNext();
prescription_id = c.getString(0);
TextView pName = (TextView) v.findViewById(R.id.text2);
TextView paName = (TextView) v.findViewById(R.id.text3);
TextView rDateLabel = (TextView) v.findViewById(R.id.textView1);
TextView rDate = (TextView) v.findViewById(R.id.text4);
TextView rLeftLabel = (TextView) v.findViewById(R.id.text5);
TextView rLeft = (TextView) v.findViewById(R.id.text6);
ImageView callPhone = (ImageView) v.findViewById(R.id.Call_Pharmacy);
pName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
pa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDateLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeftLabel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
rLeft.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
goToPDetails();
}
});
callPhone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Some Code
}
});
}
All those onClick listeners (those on single sub-views of one ListView element) probably shouldn't be here in the onListItemClick method, but in the getView method of your Adapter instead (with proper use of the convertView argument).
The way you do it seems quite wrong, maybe your onListItemClick method isn't even needed if you correctly implement the various onClick listeners at the right place.
Using an xml based layout for your list item is key here. Set each individually clickable View with two attributes android:clickable="true" and android:onClick="<your click handler>" the method will need to be implemented with this signature: public void <your click handler> (View v) {...} in your Activity. A side note is that you'll have to make a design decision to implement a click handler to overlap handling (one click hanlder for more than one View) or a single view handler per View, the former is best for when click are substantially similar in function and the latter is when they are different.
The next step is to implement the click handler, the key here is to use ListView.getPositionForView(View v) so you can associate the row, the data, and the View clicked.
Don't forget to implement ListActivity.onListItemClick() as a catch-all for clicking on the root layout of the list item and as a catch-all for Views that don't have their own onClick handler set.
The above technique will have good performance and makes use of several Android API's to speed your development.
If you decide to implement the listeners in code, please study getView() closely (as darma mentioned) and for the sake of performance (if you have several items in your list) reuse the click listeners with the above discussion about how to associate the data and row.

How to prevent button onClick firing repeatly?

I have a Button which sets both onLongClickListener & onClickListener :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
return true;
}
onClickListener :
#Override
public void onClick(View v) {
do something else ...
}
When I long click the button, onLongClick fires repeatly
(sometimes onClick fires too when I release the button, it's weird ##")
What I want is to make the onLongClick be triggered only once for one long press.
So I modified the code :
onLongClickListener :
#Override
public boolean onLongClick(View v) {
do something ...
myButton.setLongClickable(false);
return true;
}
onClickListener :
#Override
public void onClick(View v) {
myButton.setLongClickable(true);
do something else ...
}
Unfortunately, the onClick callback was locked too after onLongClick fires!
I cant unlock the button anymore :|
Whats wrong with my code? Also, why onClick sometimes works when I release my button after a long click?
I've got the code you need, give me a minute to post it.
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//DO STUFF GRAH!
}
});
btn.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
//OTHER STUFF
return true;
}
});
This worked fine for me. I made an int and onLongClick added one and displayed it in a toast. Always incremented by one, and didn't do the onClick (reset it to 0).

Categories

Resources