I've tried implementing an onClickListener on a CameraFragment, however, it never seems to be called. I am probably missing something quite simple. Does anyone have any ideas?
public class CWACCameraFragment extends CameraFragment implements OnClickListener {
//...
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
takePicture();
Toast.makeText(getActivity(),"click",
Toast.LENGTH_LONG).show();
}
Is there a way to ensure that the onClick event occurs?
In the demo app, I added the following to DemoCameraFragment:
#Override
public void onStart() {
super.onStart();
getView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e(getClass().getSimpleName(), "got here");
}
});
}
Log messages showed up just fine. Hence, AFAICT, your approach works, so perhaps there is some bug in how you wired in the click listener.
Related
I have been looking for my question on Google and inside the forum but I cannot an answer so far. I am using android studio to code an app in Java
The thing is : inside a class, I have overrided the OnClick() method because I have to implement a lot of views.
Now I want to call a function just before OnClick() is called.
What I mean is, for instance, if the user taps on a button, before OnClick() is called I want one of my methods to be called.
Does anyone know how to do this ?
Thank you in advance
I want to call a function just before OnClick() is called
You can use a logic inside onClick() like,
if(userhaspermission())//your method to check if the user has permission
{
//your onclick operation code
}
Make your checking permission method with return type as boolean
Implement your custom OnClickListener as follows
public abstract class MyOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
//Do common action
if(condidtionSatisfied){
performClick(v);
}
}
public abstract void performClick(View v);
}
Set onClickListener to any component as follows:
button.setOnClickListener(new MyOnClickListener() {
#Override
public void performClick(View v) {
//Execute post click action
}
});
This will ensure your common code will be called for all the views and it would be much cleaner approach.
Override the onClick() method and write your first logic which you want.
Write your own Listener like Sagar said
This logic is quite similar to Sagar's Answers.
Write abstract class as given below
public abstract class OnClick implements View.OnClickListener {
public void beforeClick(View v) {
}
#Override
public void onClick(View v) {
beforeClick(v);
performClick(v);
afterClick(v);
}
public void performClick(View v) {
}
public void afterClick(View v) {
}
}
NOTE : See in above code it implements View.OnClickListener, so this logic will works for views which extends View super class. If you want above logic for object's which needs Dialog interface onClick then you need to modify it for DialogInterface.OnClickListener.
So you can use above logic as below
//Let say you need it for button with id button
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClick() {
#Override
public void beforeClick(View v) {
Toast.makeText(MainActivity.this, "Before", Toast.LENGTH_SHORT).show();
}
#Override
public void performClick(View v) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
}
#Override
public void afterClick(View v) {
Toast.makeText(MainActivity.this, "After", Toast.LENGTH_SHORT).show();
}
});
I searched on Google and StackOverflow but I did not find really what I need. I want to disable a button runtime in a click event but it does not work. I have 2 buttons whose names are ButtonA and ButtonB
My code:
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setEnabled(false);
ButtonA.invalidate();
ButtonB.setEnabled(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setEnabled(false);
ButtonB.invalidate();
ButtonA.setEnabled(true);
ButtonA.invalidate();
}
});
I use onclick listener events in my list adapter class
If I refresh the screen it works but I want to do it runtime. When I click the button I want to see it will be disabled.
How can I fix this?
You can use clickable() method for this.
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setClickable(false);
ButtonA.invalidate();
ButtonB.setClickable(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setClickable(false);
ButtonB.invalidate();
ButtonA.setClickable(true);
ButtonA.invalidate();
}
});
Or you can try this:
Button b=(Button) findViewById(R.id.btnB);
b.setEnabled(true);
The thing is :
it should be defined by final,
like this;
final Button ButtonA,ButtonB;
I write an android app
I set an editText.setOnClickListener(...)
but i see that when the user clicks, the soft-keyboard is opened and only
when the user clicks on a keyboard key - the onClick() is called.
how to catch the click before the soft keyboard is opened?
I want to avoid the keyboard opening.
here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phone_login);
initMembers();
setOnClickListeners();
initFieldsTexts();
setKeyboardVisibilityListener();
}
private void setOnClickListeners() {
mPhoneNumberField.setInputTextOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
}
and:
public class PhoneLoginFillInField extends LinearLayout {
..
public void setInputTextOnClickListener(OnClickListener onClickListener)
{
mInputText.setOnClickListener(onClickListener);
}
during debugging i see this line is called twice
mPhoneNumberField.setInputTextOnClickListener(new OnClickListener() {
though it's called only from onCreate and there setOnClickListeners(); is called once
You mean EditText should not gain focus; it needs to respond only to click events. Do this:
editText.setFocusableInTouchMode(false);
And then carry on:
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Your code...
}
}
Set an onTouchListener to the EditView. Remember to return true to avoid the event propagation.
How can I write command in onclicklistener() so that I can move to next activity as well as my post status dialog will appear on that new activity. I am using intent for switching to next activity and also using postTowall() method. But these two doesn't perform simultaneously. I am using this method:
public void onClick(View v) {
// TODO Auto-generated method stub
postTowall();
Intent intent= new Intent(Frnd.this,Logout.class);
startActivity(intent);
}
private void postTowall() {
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
public void onComplete(Bundle values) {
String sh = null;
Bundle params = new Bundle();
params.putString("caption", sh);
}
public void onCancel() {
// TODO Auto-generated method stub
}
You can use thread for posting. Use asynctask and inside that post the message to face book. Or you may just write a simple thread and start it.
This asynctask link may help you. it has usage example also:
http://developer.android.com/reference/android/os/AsyncTask.html
You should set other activity to open post dialog. Send via intent a signal if it should or not...
My custom audio application starts to play when the Main activity begins, but I'd like it to start playing when the user hits the "Play" button. Just for the record I'm not using the Android MediaPlayer, but my own player.
When I simply use startPlayer() it runs fine, when the Activity starts, but when I put the startPlayer() invokation inside the setOnClickListener() it doesn't work.
The method startPlayer() in being called (it's logging fine), but the audio player doesn't fire up. Why?
Here is the code:
play = (Button) findViewById(R.id.playbutton);
play.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startPlayer();
}
});
}
private void startPlayer() {
try {
player.playAudio();
} catch (Exception e) {
e.printStackTrace();
finish();
}
}
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startPlayer();
}
});