I have a problem. If I set clearFocus() in setOnItemClickListener it's not work.
Focus set nearby input.
How I can clear focus in autocomplete, after I clicked item?
textView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
arrAuto = (String) ((TextView) view
.findViewById(R.id.item_auto)).getText();
findViewById(R.id.editText1).clearFocus(); //it's not work
}
});
You need to set android:windowSoftInputMode="stateHidden" to your activity in AndroidManifest in order to keep the keyboard hidden when the activity is started.
Related
I'm in the process of making a grocery list app and I wrote the code to where when I click an item, it'll mark it off.
This is my code for that section:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView text = (TextView) view;
if (!text.getPaint().isStrikeThruText()) {
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}else{
text.setPaintFlags(text.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
}
});
}
It works exactly like i want but when I add another item after an item is marked off, all of the items that are marked off, the marks disappear.
When I add an item, it's like it resets. it doesn't delete any of my items, just the strike_thru part of it. any help would be greatly appreciated! thanks
Your ListView's Adapter contains a method called getView, which is called when a list view item needs to be displayed in an actual View. The Views in your ListView will be discarded if you scroll too far off screen, or invalidate the whole ListView.
My guess is that adding an item is invalidating the ListView.
Your getView method should set the paint flags on the view that it returns. Assuming your list view is displaying a String[], you will also need a boolean[] to hold whether or not an item is complete. You would need to initialize this to all falses, add a completed[i] = !completed[i] at the beginning of your onItemClick. Then you can check competed[i] instead of isStrikeThruText in your if statement, later in that method. Finally, your getView can look like this
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
}
TextView textView = (TextView) convertView;
textView.setText(items[position]);
if (completed[position]) {
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
textView.setPaintFlags(textView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
return textView;
}
I have a listview in which previously every item was clickable, and it worked fine. But as soon as I added a feature to have all items long clicked, the onClick feature stopped working. I stripped down all my code for debugging purposes which I will submit here:
listview = (ListView) findViewById(R.id.workout_listview);
listview.setAdapter(new WorkoutListViewAdapter(this, new String[] {...}));
listview.setClickable(true);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.print("CLICK");
}
});
listview.setLongClickable(true);
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("LONG CLICK");
return true;
}
});
This code causes nothing to happen when a list item is clicked and displays "LONG CLICK" when one is long clicked.
I found this question, which seems to have the same problem but the code is structured very differently, and my code is already following the answer's guidelines of implementing the listener on the ListView rather than the row.
As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});
I have a ListViewin my ListView show ImageButton, ImageView, Button, TextView
I set ListView.OnItemClickListner. When I run my project, it shows my ListView.
But when i click on listview it's not working
what is wrong? and I want button, ImageButon,ImageView click events should call Activity or dialog ? Help me?
This is the code:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Toast.makeText(getActivity(), "sdadad",1).show();
}
});
Add one tag in parent layout of your custom layout
android:descendantFocusability="blocksDescendants"
I have a spinner that opens programaticly, and when the user chose an option from the spinner, it closes... is there a way to be notified, or a listener that tells you, when the user chose his choice?
the onItemSelected gets the default item that is chosen automaticly when the spinner is open.
set setOnItemSelectedListener to your spinner...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object obj = (Object) parent.getSelectedItem();
//get clicked position from position
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
//this method is call when nothing choosed by you
}
});
Add an OnItemSelectedListener to your Spinner.
I am not sure if I understand your question correctly but let me try to answer it. The normal and straight forward way would be to add an OnItemSelectedListener to the spinner i.e
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Do whatever you want here
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
but this is such a basic thing that I feel like a fool for pointing it out. Anyway here is a tutorial on spinner from Android Developer resources. It creates the listener in step 5 and adds it to spinner on step 6.