I am creating a program that has the mainMenuButton button (created in onCreate) disabled by default.
The user first has to check a few boxes and select something from the spinner before the mainMenuButton button becomes enabled. Currently, I can't even get the button to enable after using the spinner.
I've tried both
#Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.main_menu_button:
openMainMenu();
break;
case R.id.building_spinner:
Button mainMenuButton=(Button)findViewById(R.id.main_menu_button);
mainMenuButton.setEnabled(true);
break;
}
}
and
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
View mainMenuButton=(View)findViewById(R.id.main_menu_button);
mainMenuButton.setEnabled(true);
}
but to no avail. I've also tried replacing View with Button, but that didn't enable the mainMenuButton either. What exactly am I supposed to do to enable the button after something on the spinner has been selected?
Also, would there be much of a big difference to implement it for checkboxes as well?
EDIT2 :
If I comment out the if and else statement, the button remains permanently disabled. If I leave as is, the button is never disabled to begin with and never disables, which I don't understand because I haven't even touched the spinner, so it should be disabled by default.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String buildingString = parent.getItemAtPosition(position).toString();
if(buildingString !="Select Building")
{
Button mainMenuButton=(Button)findViewById(R.id.main_menu_button);
mainMenuButton.setEnabled(true);
}
else
{
Button mainMenuButton=(Button)findViewById(R.id.main_menu_button);
mainMenuButton.setEnabled(false);
}
}
Im not sure if this affects it, but the spinner strings was formatted like this :
<string-array name="building_array">
<item>Select Building</item>
You've not registered a listener on spinner.
Add this in your onCreate
spinner.setOnItemSelectedListener(this);
and then write you code in onItemSelected.
Related
I am inflating edit text in adapter class within of a list view. View is looking cool multiple edit text are appearing properly but when I do focus on the edit text or if I try to type something it loosing focus I tried to adding listeners, changing manifest file but nothing works for me.
following code is not working for me
#Override
public View getChildView(int position, int i1, boolean b, View view,
ViewGroup viewGroup) {
EditText comment = view.findViewById(R.id.txtRecordComment);
comment.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (lastFocussedPosition == -1 || lastFocussedPosition == position) {
lastFocussedPosition = position;
edittext.requestFocus();
}
}
}, 200);
} else {
lastFocussedPosition = -1;
}
}
});
return view;
}
please help me to resolve this issue
Since you are using a ListView, it is bound to happen with your current code implementation.
You must remember that if your EditText is in a ListView, every key down will result in the edit text losing and getting focus because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object.
To get the expected behaviour, declare a variable in your adapter: int focusedRow. In getView method of your adapter, add an onFocusChanged listener to the EditText and when that edit text gains focus, set focusedRow = whatever row the focused EditText happens to be in.
Also set any edit text that is in the currentlyFocusedRow to be focused.
Update:
If you have multiple EditTexts, add an onFocusChanged listener to each edit text.
Add below code in Android Manifest file
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
Hope this will work
I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}
I want to change the background view when it's pressed (its working). My problem is, If i press the other view (not the same one) in the list, i want to set my background to Black of the newly selected view and change the background to White of the previous selected view. Here is my Implementation
for(final TotalPlayers player : this.playerData){
final ArrayList<View> addedPlayerViews1 = getPlayerView(player);
dropPlayersListView.addView(addedPlayerViews1.get(0));
addedPlayerViews1.get(0).setOnClickListener(new OnClickListener() {
boolean highlight = false;
#Override
public void onClick(View v) {
if (!highlight)
{
addedPlayerViews1.get(0).setBackgroundColor(Color.BLACK);
highlight=true;
}
else {
addedPlayerViews1.get(0).setBackgroundColor(Color.WHITE);
highlight=false;
}
}
});
}
}
addedPlayerViews1.get(1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addedPlayerViews1.get(0).setBackgroundColor(Color.WHITE);
highlight=false;
}
});
I assumed that you can get the other view by get(1).
The problem of your code was you only handled the onClick event of your view where you want to change the background. but you also need to handle the onClick of other view too.
I have created custom View called Color. I use object of Color to fill GridView (same issue is in ListView also).
My task is to let user choose one color and highlight it.
Previously I do something like that but I used only in building widgets and everything worked. This time I use my own.
This cose is for item clicking:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int color = (int)id;
Log.d(TAG, "Selected color: " + id);
view.setSelected(true);
view.invalidate();
}
Color.onDraw:
#Override
public void onDraw(Canvas canvas) {
if (isSelected()) {
Log.d(TAG, "color draw selected");
} else {
Log.d(TAG, "not selected " + color);
}
}
Also I set setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); to this GridView (AbsListView.CHOICE_MODE_MULTIPLE don't help also)
In log I see:
Selected color: 8355711
not selected 16711680
not selected 16743680
not selected 8355711
I am sure, Color items is not recreating more than on time.
I tried change GridView to ListView, user default widget (overrided only onDraw()). Nothing helps. Maybe I forgot something?
In my opinion GridView somehow drop selected status.
If you need more info just tell.
Addition:
I checked with debugger. View with color 8355711 is same object in both functions.
I used setOnItemSelectedListener(this); to track item selection. But nothing happen in this listener.
The method isSelected() is comming from the GridView which is a child of View.
Basically when calling isSelected() you're saying: "is the gridview selected" which is not what you want.
What you want is: "is there any selected view in the grid view ?"
Which could be achieved using getSelectedView()
As the documentation says, you will get a reference to the selected view or null if none is selected.
Also make sur your GridView is properly initialized to handle item selection.
EDIT : Ok i understand that isSelected() is called from the Color view. My first guess is then useless.
But I think you should try to make your item selected using the setSelection() of the GridView object.
Add something like:
myGridView.setSelection(position);
I fix this issue my self by making my own select. Its workaround.
In Color class added:
private boolean selected = false;
public boolean isSelected2() {
return selected;
}
public void setSelected2(boolean selected) {
this.selected = selected;
}
#Override
public void onDraw(Canvas canvas) {
....
if (isSelected2()) {
//draw selected state
} else {
....
}
On ColorChooser grid:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
int i = getChildCount();
while (--i >= 0)
((Color)getChildAt(i)).setSelected2(false);
((Color)view).setSelected2(true);
....
}
Reason why I not to override natice setSelected(boolean state) is some other code in grid every time make all items in deselected state. I don't know why, because in other places same more native code work very fine.
I got a tricky problem here:
in my app, there's a list (using a ListView) and each item contains text and a checkbox aligned to the right hand side.
Now, clicking the checkbox itself works, I installed a handler (OnClickListener) for it and perform an internal action. The visible state of the checkbox is just as expected - clicked the first time the tick is there, clicked again the tick disappears.
Now I simply wanted to "extend" the clickable area to the complete list entry - my approach was to install an OnItemClickListener using ListView.setOnItemClickListener() and within this listener I do the following:
resultView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.performClick();
}
});
That sounds simple, but what happens is that sometimes (not every time) the tick in the checkbox does not appear after that! I added a checkBox.invalidate() but that did not help.
How can I have a checkbox "react" to the click of the complete list entry item???
Thanks!
Rather than use performClick() use checkBox.setChecked( !checkBox.getChecked() ); instead. performClick() will cause the onClickListener to be called again potentially causing a recursion, which could explain the odd behaviour that you're seeing.
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelected);
TextView tv = (TextView) v.findViewById(R.id.tvName);
// pi = (PackageInfo) arg0.getItemAtPosition(position);
cb.performClick();
if (cb.isChecked()) {
checkedValue.add(tv.getText().toString());
} else if (!cb.isChecked()) {
checkedValue.remove(tv.getText().toString());
}
}