Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Do you know how to call the onClick method assigned to a button in the XML layout file without clicking the button? I've tried the performClick method already.
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
Button signUpButton = findViewById(R.id.signUpButton);
try {
signUpButton.performClick();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
Step 1. Extract code performed in onClick to a method
Step 2. Call this method in place of button.performClick()
try callOnClick:
findViewById(R.id.hello).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.hello).callOnClick();
Do you know how to call the onClick method assigned to a button in the XML layout file without clicking the button?
You can call a function for when the user clicks the button and call that function independently.
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
OnbuttonPressed();
}
return false;
}
....
public void OnbuttonPressed(){
Button signUpButton = findViewById(R.id.signUpButton);
try {
signUpButton.performClick();
} catch (Exception e) {
e.printStackTrace();
}
}
Then you can call the function "OnbuttonPressed()" anywhere else.
Related
I am new to Android. I am trying to make a text box and on pressing done key, it should take the value to the java code. For this I am using setOnEditorActionListener.. I searched on how to do this and got many answers on how to implement it. Example:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
I need to ask where should I write this thing? In which method? I tried doing it in onCreate but it threw some error. I somehow made it work using this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unlock);
Log.i(TAG, "onCreate");
editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(this);
}
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
Log.i(TAG, "button pressed");
Toast.makeText(this, "Hey you just clicked the DONE button", Toast.LENGTH_SHORT).show();
handled = true;
}
return handled;
}
Here I used this keyword, and I don't understand why have I used it.
Question 1. Please help me understand, why have we used this keyword..
Question 2. Why wasn't it working in the below code?
public void checkInput() {
Log.i(TAG, "Enter checkInput method");
final EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i(TAG, "Enter onEditorAction");
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
Log.i(TAG, "button pressed")
handled = true;
}
return handled;
}
});
}
I called this checkInput method from onCreate.
To answer Question 1:
Here I used this keyword, and I don't understand why have I used it. Question 1. Please help me understand, why have we used this keyword..
You're telling Java to look into the Activity class for implementations of methods required by the TextView.OnEditorActionListener interface. So for all interactions with your soft keyboard, Java would look into your class for the method: onEditorAction
In order for the above to work, your activity needs to defined like:
public class MyActivity implements TextView.OnEditorActionListener {}
For question 2:
Question 2. Why wasn't it working in the below code?
To check for the "Done" action, your if statement should be:
if (actionId == EditorInfo.IME_ACTION_DONE) { ... }
Hope that helps.
I have an activity where the whole screen is dedicated to sending one message. Being one EditText on the top half, and the SoftKeyboard always visible on the bottom half.
When i press back, the SoftKeyboard hides and i have to press back again to leave the activity.
The behavior that i'm struggling to get is : finishing the activity right away when i press the back button, instead of hiding the keyboard.
You can find this behavior in the twitter app for example, when writing a new tweet.
I tried with overriding the onBackPressed() function, but seems like when the keyboard is visible, the function is not called.
#Override
public void onBackPressed() {
finish();
}
Any help would be really appreciated!
So after trying many things, here something that worked :
Subclass EditText and override the onKeyPreIme() function to send a call back.
Here's the code for the subclass :
OnKeyPreImeListener onKeyPreImeListener;
public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
this.onKeyPreImeListener = onKeyPreImeListener;
}
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if(onKeyPreImeListener != null)
onKeyPreImeListener.onBackPressed();
Log.d(TAG, "HIDING KEYBOARD");
return false;
}
return super.dispatchKeyEvent(event);
}
public interface OnKeyPreImeListener {
void onBackPressed();
}
Then in your activity for each of your TextView :
EditTextGraphee.OnKeyPreImeListener onKeyPreImeListener =
new EditTextGraphee.OnKeyPreImeListener() {
#Override
public void onBackPressed() {
Log.d(TAG, "CALL BACK RECEIVED");
MyActivity.this.onBackPressed();
}
};
editText.setOnKeyPreImeListener(onKeyPreImeListener);
new answer:
so apparently you don't receive the onBackPressed callback, but that doesn't mean you can't detect the keyboard closing.
Using the technique described here: How to check visibility of software keyboard in Android?
you can detect when the keyboard open/close, so when the keyboard closes you call finish();
deprecated, original answer:
simply override the back press event in the activity:
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
I assume that since the soft keyboard is visible probably an edittext has a focus. So you can catch the back pressed event by adding an OnEditorActionListener on that EditText and finish activity.
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP){
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
finish();
}
}
return false;
}
});
You nee to extend EdtText class and implement onKeyPreIme method.
public class MyEditText extends EditText {
/* Must use this constructor in order for the layout files to instantiate the class properly */
public MyEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
// do your stuff here.
return true;
}
}
Override onBackPressed() method like this :
#Override
public void onBackPressed() {
hideKeyboard();
finish();
}
For hideKeyboard() function please search in the Internet .
I need to click once to implement the methods in Onclick but the problem here that in counter it won't count until i click button constantly and i have several check conditions that print text to the user if true these texts don't show up until i click the button one more time , how to handle that issue ? i want the button to be clicked once then all the code inside Onclick implemented properly
Life.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{ counterLife();
Help.setVisibility(View.INVISIBLE);
Reset.setVisibility(View.VISIBLE);
// other code
.............
void counterLife() //To count
{
if (a && T2 ==0 && T3 == 0)
{
if(countLife == 6)
{ //code
text.setText("You Completed 6 Lives ");
}
else
countLife ++ ;
}
The first time you click you should use the OnFocusChangeListener to get the event.
After foucus is on the View all extra clicks will invoke the OnClickListener event.
You can also do this:
setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// call your onclick method
}
}
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What I am seeking is to have button that has a x value and after pressing another button to change the value of the first. e.x first button value is, when pressed to open flashlight constantly, then I press the second button and now first's value button has changed, when pressed flashlight blinks!
Thanks in advance
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
// playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
Change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
// i want it to start blinking
} else {
//wait till opens to start blinking
}
}
}
}
and on clicking again button change, everything to return to normal.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
public void onClick(View v)
{
if (v.getId()== R.id.but1 && v.getId()== R.id.but2)
{
Intent intent=new Intent(First.this,Second.class);
startActivity(intent);
}
}
There isn't such event that can be associated to two controls. event handlers only associated to one control and that is different than assigning the same listener to two button. listener will receive a call from every button separately.
Also, listeners will never be triggered together because both run in the same thread (The UI thread). It's impossible to catch on click event for both controls at some moment. one listener will be triggered and then the other. Even if we assumed that the user managed to click those together at the same millisecond or so in the perfect world. Any way who can decide that when they are clicked at same millisecond they considered to be clicked to gather! why not same nanosecond. and why not the same hour :)
Ok, it's enough explaining the click event.
What we need is the touch event and it can be played as follow (the code will also explain how the touch event work):
Activity Class Members:
public boolean b1Down = false, b2Down = false;
onCreate Method Code:
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
b1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
boolean consume = false;
if (event.getAction() == MotionEvent.ACTION_UP)
{
b1Down = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b1Down = true;
if (b2Down)
{
// both are clicked now //
Toast.makeText(MainActivity.this, "Both are clicked now!", Toast.LENGTH_SHORT).show();
}
consume = true;
}
return consume;
}
});
b2.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
boolean consume = false;
if (event.getAction() == MotionEvent.ACTION_UP)
{
b2Down = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b2Down = true;
if (b1Down)
{
// both are clicked now //
Toast.makeText(MainActivity.this, "Both are clicked now!", Toast.LENGTH_SHORT).show();
}
consume = true;
}
return consume;
}
});