This question already has answers here:
How to find out which view is focused?
(6 answers)
Closed 6 years ago.
My app uses keyboard control and when moving around I've noticed that occasionally you get what I'll refer to as a "Mystery View" - I say that because nothing visible seems to be selected, yet nothing visible is focussed either
Is there any way to find out what I'm focussed on at any given time? eg. a listener that fires on a view selection, etc.
Thanks
** edit - some code for ntc **
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
if(col1.getFocusedChild() != null) {
Log.d(TAG, col1.getFocusedChild().toString());
}
}
});
col1.addChild(btn);
getWindow().getCurrentFocus();
You can probably use this method for every view. Maybe this may gets bigger and hectic but is sure to work:
if(yourChildView.isFocused()) {
yourChildView.setFocusable(false);
}
Related
This question already has answers here:
Remove an onclick listener
(10 answers)
Closed 6 years ago.
I have around 18 views and a button that has onclicklisteners. My goal is to disable the listeners on the 18 views once the button is clicked to avoid them from getting flipped over again once they are pressed.
My problem: after i have initialized the onclicklisteners those 18 views still has their listeners and stil doing the method being called once they are clicked. I have tried setting click listeners to null and setting clickable to false and they are still clickable.
Any way to disable theses views from being pressed?
Method to disable:
private void makeUnclickable() {
for (int x = 9; x < 9; x++) {
front[x].setOnClickListener(null);
back[x].setOnClickListener(null);
front[x].setClickable(false);
back[x].setClickable(false);
}
}
Take a boolean variable and store the state of the button you want to enable or disable.Then:
public void onClick(View v)
{
if(isEnabled)
{
//write your click listeners here
}
else
{
return true;
}
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
Click Event
public void buttonOnClick(View v) {
Button button=(Button) v;
((Button) v).setText("clicked");
Changes BackGround to an image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.BackGround);
rl.setBackgroundResource(R.drawable.mapwork);
Makes Certain Items Disapear
button.setVisibility(View.INVISIBLE);
EditText mText2 = (EditText) findViewById(R.id.input);
mText2.setVisibility(View.INVISIBLE);
Changes what is in the output if they put in a certain number
EditText mText = (EditText) findViewById(R.id.output);
if("8" == mText.getText().toString()){
mText.setText("That does not work!");
}
}
When I test the application the If statement never happens even when the requirements are met. I have tried look for an answer and have not found one. Thank you for helping.
Now it will work
if(mText.getText().toString().equals("8")){
mText.setText("That does not work!");}
My situation is: I have a EditText, and under it I have a button, called "select all". It's purpose is to let the user select all the text by pressing it.
When the button is clicked, I invoke selectAll() on the EditText, but instead of selecting all text, in some cases (generally, when the cursor is already positioned within the text somewhere), the cursor is moved to position 0 (start of text) and text remains unselected. Second click on the button will then select all. But of course it should happen on first click.
From this issue: Android EditText selectAll() doesn't works if one taps on the same field in android 4.x only it seems that this is a bug in android 4.0 and above. (Found no mention in Google issue tracker).
Does anyone know about a way to overcome this problem? It seems that the probelm is also in other selection methods and not only selectAll().
(p.s. This question is sort of duplicate of the issue I mentioned above. But I opened another question, because the author of that issue was satisfied and selected a partial answer (of setting android:selectAllOnFocus="true"), while in my case and scenario it does not help me and does not solve the problem.
Thanks.
Problem caused by IME. If showed cursor drag pointer then selection must be zero width.
You need cancel drag pointer. It can be doned by change text. For example replace:
Editable text = edit.getText();
if (text.length() > 0) {
text.replace(0, 1, text.subSequence(0, 1), 0, 1);
edit.selectAll();
}
We replace first symbol of text with same symbol. It cause cancel drag pointer and allow make selection without bug.
I was having a similar issue. I solved it (using Xamarin in C#, but the equivalent Java will work) with the following code:
private void InitFocus()
{
Java.Lang.Runnable rable = new Java.Lang.Runnable(()=>
{
EditText maleCount = FindViewById<EditText>(Resource.Id.txtMaleCount);
maleCount.RequestFocus();
maleCount.SelectAll();
});
new Handler().Post(rable);
}
I have 2 controls: One is a dropdown that lets you select a chicken house and then a set of buttons for each day of the week. Whenever you change the day of week or the chicken house, I want to set focus in the txtMaleCount EditText and then I want the value in that box selected (since it's a number and they're presumably going to replace it).
Clearly, the non-intuitive part was the need to Post it. Doing it directly (on the UI thread) didn't seem to have any effect.
Try this:
yourEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
});
Or This:
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
#Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.setSelection(editText.getText().length()-1); // selects all the text
}
});
Or this:
theEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.performLongClick();
}
});
Hope this helps .. :)
I want to create an application based on quiz which consists of 14 questions.
Can anyone tell me how do I need to go from one question to another by clicking on next button.If I use an Intent then I am afraid that I will be creating 14 Activities :(
I don't think that is the programmatic procedure too.
You can stay in the same Activity and keep track of the question.
You might want to use a TextSwitcher to add a fade in/fade out animation when swapping the question's text.
You could have the button click just update the questions text to be the next question.
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
questionTextView.setText(questionTwo);
}
});
#Vivek you can use view flipper
this might help you
http://www.bogotobogo.com/Android/android9TabWidgetFlipper.html#SlidingDrawer
http://www.warriorpoint.com/blog/2009/05/29/android-switching-screens-by-dragging-over-the-touch-screen/
Change the text of your textviews where the questions are.
#Override
protected void onCreate(Bundle savedInstanceState) {
Button nextButton = (Button) findViewById(R.id.yourButtonId);
TextView questionTextView = (TextView) findViewById(R.id.yourTextViewId);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
questionTextView.setText("Your Next Question");
}
});
}
A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity.
Has its own lifecycle;
Receives its own input events;
Can be added or removed while the activity is running;
Believe me that is the best method. I have been introduced to fragments having an application with quotes. The list of the quotes in left and the quotes that were changing on left when an author have been selected.
I'm using android.widget.ViewFlipper that contains views where one view contains TextView (question) and "input control" for answer (selectboxes, date/time widget, radiogroup, etc).
This question already has answers here:
Get text from pressed button
(8 answers)
Closed 9 years ago.
for( i=0; i<26; i++) {
btnAlpha[i] = new Button(this);
btnAlpha[i].setBackgroundColor(Color.TRANSPARENT);
btnAlpha[i].setTextColor(Color.GREEN);
btnAlpha[i].setText(Character.toString ((char)(j+i)));
btnAlpha[i].setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//Want to get the text from the current button.
btnAlpha[i].getText();
//But it gives error that "i" cannot be accessed.
}});
I get the error "cannot refer to a non-final variable inside an inner class defined in a different method". But I need to get the text at that right time. How to do it? Is there any other way to do it?
Button is a subclass of View, so the argument to onClick, v, is the Button being clicked. Try
public void onClick(View v) {
((Button) v).getText();
}
create a new variable final int x = i; in the for loop and use x in place of i in the onClick method
edit: actually im not sure this will work correctly. oops. Kype P's answer looks pretty good.