I have popup window that contains some EditTexts. I want to create my custom numerical keyboard inside the popup window. So I have 10 buttons that represent digits 0-9. Inside buttons' onClickListener I trying to dispatch key event
public void onClick(View v) {
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0));
}
but it doesn't effect to EditTexts. I tried to do it with focusable equals true and false, but the result is the same. When I am trying to dispatch key event in the main layout of activity it works well, so what I should change to make my code work inside popup window?
Have you tried requesting focus on the EditTexts? Not only using
editText.setFocusable(true);
but also
editText.requestFocus();
I think it's also possible that is not working because you're not injecting a DOWN event first. Try the following:
Instrumentation mInstrumentation = new Instrumentation();
final Thread t = new Thread() {
public void run(){
mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
};
t.start();
This will inject an event which will be processed by the view that has the focus
Related
I am trying to change visibility of two buttons when pressing one of them.
So say I have an "on" and an "off" button. When I press the on button, I want the on button to hide itself and the off button to show, and vice versa.
How could one do this?
Basically, you have to inform on to point.
How can I handle the visibility ?
How can I listen to a click event ?
How can I handle the visibility ?
You can handle the visibility of a view by using the enter link description here property.
This property controls the visibility of the view.
Must be one of the following constant values.
GONE Completely hidden, as if the view had not been added.
INVISIBLE Not displayed, but taken into account during layout (space is left for it).
VISIBLE Visible on screen (the default value)
Now you know how you could display or not and element on your view.
How can I listen to a click event ?
Simply add on onClickListner on your button
Sample Code
btnOff, btnOn // your buttons
btnOff.setOnclickListner {
// handle visibility
btnOn.visibility = View.INVISIBLE
}
BTW. If you want an On/Off button, you should use Toggle Buttons there are like the switch buttons you used to have on settings screens
See this example in Kotlin, adapt it for your own needs:
val onButton = Button(this)
val offButton = Button(this)
onButton.setOnClickListener {
onButton.visibility = INVISIBLE
offButton.visibility = VISIBLE
}
offButton.setOnClickListener {
onButton.visibility = VISIBLE
offButton.visibility = INVISIBLE
}
Or in Java:
Button onButton = (Button) findViewById(R.id.onButton);
Button offButton = (Button) findViewById(R.id.offButton);
onButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onButton.visibility = INVISIBLE;
offButton.visibility = VISIBLE;
}
});
offButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onButton.visibility = VISIBLE;
offButton.visibility = INVISIBLE;
}
});
I wanted to know if its possible to have two onclick methods for one buttton..Im trying to have a button that can open a new activity and send a id token to the server for firebase purposes, if possible how do i go about it on android studio
I think you are getting the underlying concept wrong.
Buttons react to clicks.
The "ActionListener" that gets triggered on that click ... can do whatever it wants. There is nothing (conceptually) that prevents you in your code to just trigger various things. Of course, you have to understand what you are doing (things like: not blocking the UI thread for too long; or how to kick of things in background threads, and so on).
No. There is only one onClick method for a Button. But you can still perform two different purposes by one button.
I am using a button to hide and show a linear layout. The code is given below :
final int[] count = {2};
//here startTopics is the button....
startTopics.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count[0] %2==0)
{
topicLin.setVisibility(View.VISIBLE);
count[0]++;
}
else
{
topicLin.setVisibility(View.GONE);
//here topicLin is the linear layout
count[0]++;
}
}
});
It is one button and so you should apply only one onClick listener which performs the buttons job.
In your onClick-method you can just call another (private) method if you want to do multiple things without sacrificing code management.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendTokenToServer();
// Include your code to open the activity here or outsource it again into another private method
}
});
And your method to send the token to the server:
private void sendTokenToServer() {
// Your code here.
}
I dont like errors. I'm using a Popup Window to display a little hint above a TextView. the hint is for explanation of what the TextView is saying, for example RBI, will popup a hint saying "Runs batted in" I keep getting this error W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed. everytime i click outside of the popup hint to remove the popup, which is the way i want it to be setup, i get that error. I've tried setting the input to INPUT_METHOD_NOT_NEEDED and nothing
i have multiple hints to be shown, so i put everything in an TextView array and
i have a listener on each TextView item
method to run the popup
public void displayPopupWindow(View anchorView, String text) {
final PopupWindow popup = new PopupWindow(getApplicationContext());
View layout = getLayoutInflater().inflate(R.layout.test_popup, null);
((TextView) layout.findViewById(R.id.popup_text)).setText(text);
popup.setContentView(layout);
// Set content width and height
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
// Closes the popup window when touch outside of it - when looses focus
popup.setOutsideTouchable(true);
popup.setTouchable(false);
popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
// Show anchored to button
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAsDropDown(anchorView, 0, -225);
}
i create a listener
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = "";
...
//a bunch of code that just decides what the variable text should be
...
displayPopupWindow(v, text);
}
};
and the array of TextViews listeners is set to the listener object
//inside a for loop
TextViews[i].setOnClickListener(listener);
anyone know of anyway to rid of that error. i wouldnt want the logcat to fill up with those errors. anyone have any ideas how to fix this?
your error is a common error if you search on google,
you have to call your displaypopup method with an handler.
You can find more help here or here.
Hope i helps you
I've got a view which uses an AsyncTask to make a dynamic table. The only trouble is that during this I create some checkboxes, but once the user has checked them I want to disable the box. But due to the dynamic way they were created I can't get the reference to set them disabled.
So here is how I create the buttons in a loop:
while(it.hasNext()){
if(checkStatus(it.next())){
myCheckbox.setChecked(true);
myCheckbox.setEnabled(false);
}else{
myCheckbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buyU asyncTask = new buyU();
asyncTask.execute("value1");
}
});
}
}
So when the button is pressed my asyncTask runs. But I can't modify the checkbox as I don't have the reference for it. I can't pass a reference I don't think as I'm using the passing parameters to send a string with a few values. So I was thinking the easiest way was to just refresh the entire page for square one again.
What is a good way to do this?
Or alternatively can I pass the button as an object to the asyncTask as well as the string and cast them back to their types on the other end?
You can pass the Object, just add an instance variable and a constructor. Since we are executing this from onClick(), we'll cast v to a CheckBox:
Checkbox c;
public buyU (CheckBox checkbox)
{
c = checkbox;
}
#Override
protected void onPostExecute (Result result)
{
c.setChecked (false);
c.setEnabled (false);
}
Then call:
CheckBox check = (CheckBox) v;
buyU asyncTask = new buyU(check);
asyncTask.execute("value1");
If you're sure that the CheckBox should be disabled upon a click, you can skip the AsyncTask param passing and just set the CheckBox to disabled right in onClick().
public void onClick(View v) {
((CheckBox)v).setChecked (false);
v.setEnabled (false);
buyU asyncTask = new buyU();
asyncTask.execute("value1");
}
I'd also be weary of that enclosing loop, make sure it's not long running if it's running in the UI Thread.
As usual, Java classes start with a capital letter, buyU doesn't fit that specification.
I might be on the wrong track here, and should be thinking events/publish-subscriber, if so, please enlighten me.
I have an android project running, where I have a layout which acts as an on-screen menu. Implemented in several activities/"parent-views" with the use of '< include>'. Working nicely.
Now, some of the functionality is general and global. Like I have an "add"-button, which does something, that it should always do. Then I'd like the possibility to customize what it does in addition to this, based on the activity where the action originated.
I have seperated menulogic in a simple java class, with the constructor taking an activity as a parameter. From here, I can attach clicklisteners to the buttons in the menu fine, and do stuff on click.
What I'd like is something like:
private void addBtn(String text, String path) {
LinearLayout ll = (LinearLayout) parentActivity.findViewById(R.id.dynamicButtonLayout);
Button newButton = new Button(parentActivity);
newButton.setText(text);
newButton.setTag(path);
newButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
//Do Stuff.
fireDoneHandlingButtonClick();
} catch (Exception e) {
}
}
});
}
And then have a way of handling this method in the parent activity. Should I be thinking of events, or should I be thinking of a way to add a method as an argument to the addBtn method from the activity, which can be fired from inside the click-listener?
Look at How To Implement Your Own Listener in Android or Fire and Forget Messages (events) in Android