When I click on each row in my ListView, I would like the TextView inside the row to become bold, and that works perfectly, but the problem I am facing is that when I click on another item in my ListView, the other row item is still bold as well as the one that was just selected
How can I put the last selected ListView row TextView's typeface back to NORMAL after another has been selected? thanks.
Here's my code:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
TextView name = (TextView) v.findViewById(R.id.songTextView);
TextView artist = (TextView) v.findViewById(R.id.artistTextView);
name.setTypeface(null, Typeface.BOLD);
artist.setTypeface(null, Typeface.BOLD);
}
One easy way to achieve this is to add a member to the adaper with the position of the bolded row, init it with -1.
At your onItemClickListener call the adapter and set the field to the pressed position, and then call notifiyDataSetChanged. At you adapter on the getview check if the current position is bold or by the bold_position field in the adapter.
some code:
adaper:
public class CustomAdapter extends ...{
int mBoldPos = -1;
public void setBoldPos(int newPos)
{
this.mBoldPos = newPos;
}
fragment-
mListView.setOnItemClickListener(new ....)
{
onItemClick(AdapterView<?> parent, View view, int position, long id)
{
((CustomAdapter) mListView.getAdapter()).setBoldPos(position);
((CustomAdapter) mListView.getAdapter()).notifiyDataSetChanged();
}
}
adapter:
getView(..)
{
if (pos == mBoldPos)
{
// Bold me up
}
}
Related
I am working on a project which uses a TableLayout, and the user can add a new row to the table layout. Each new row is inflated as a new view and then the view is added to the table layout. One of the controls within the row is an AutoCompleteTextView.
The user can start typing into the AutCompleteTextView and then select one of the items in the suggestion list, when the user selects the item, the selected item is added to the text box as expected, but I want to then set the cursor position as the user can then change the value of the text. For example, the selected item might be sometext() but they can amend the text after selecting it to become sometext(25), so I am trying to set the position of the cursor within the brackets.
This is working fine for one AutoCompleteTextView in the layout but I can't figure out how to do it when its dynamically generated.
I'm finding the AutoCompleteTextView from the inflated layout and creating a set on item click listener, and using the view parameter in the OnItemClick function to ensure I am using the correct view that triggered the event handler, but on the setSelection I am getting an exception java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.AutoCompleteTextView
Below is the code I am using:
private void addRow()
{
TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.create_table_column_row, createTable, false);
txtColumnName = row.findViewById(R.id.txtColumnName);
txtDataType = row.findViewById(R.id.txtDataType);
txtDataType.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
txtDataType.showDropDown();
}
});
txtDataType.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
//Check if the string has a ( and if so, set the cursor to be just after it so the user can enter the size
int bracketPos = selectedItem.indexOf("(");
if (bracketPos > 0)
{
//Crashes on this line
((AutoCompleteTextView)view).setSelection(bracketPos+1);
}
}
});
List<String> datatypes = Arrays.asList(getResources().getStringArray(R.array.table_datatypes));
datatypeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, datatypes);
txtDataType.setAdapter(datatypeAdapter);
rowViews.add(row);
createTable.addView(row);
}
I tried casting the view to AppCompatTextView but then this doesn't have the setSelection() method.
The view in onItemClick() is the AppCompatTextView that is clicked in the drop down box for the AutoCompleteTextView. That is why you can't cast it.
Since you have multiple AutoCompleteTextViews, use a focus change listener to capture which AutoCompleteTextView is being addressed by the user. You can then use that value to set the position of the cursor.
private AutoCompleteTextView textView;
private AutoCompleteTextView mAuto;
textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
mAuto = (AutoCompleteTextView) v;
}
});
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("MainActivity", "<<<<onItemClicked");
int bracketPos = textView.getText().toString().indexOf("(");
if (bracketPos > 0) {
mAuto.setSelection(bracketPos + 1);
}
}
});
I believe that the AutoCompleteTextView is already populated when this method is called, so you could just search for ( within that field.
Here is a slightly different way. After
txtDataType = row.findViewById(R.id.txtDataType);
add
txtDataType.setOnClickListener(new View.OnClickListener() {
private AutoCompleteTextView mAutoView;
#Override
public void onClick(View v) {
mAutoView = (AutoCompleteTextView) v;
mAutoView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("MainActivity", "<<<<onItemClicked");
int bracketPos = mAutoView.getText().toString().indexOf("(");
if (bracketPos > 0) {
mAutoView.setSelection(bracketPos + 1);
}
}
});
}
});
You can delete your other listeners.
I'm trying to get an AutoCompleteTextView's ID after I clicked a value on the list. Tried looking up on google and stackoverflow, but the provided answers didn't work. Here's what I've got:
Created the view in my class declaration:
public class ActivityCadastrarCliente extends Activity implements OnClickListener, OnItemClickListener {
AutoCompleteTextView E_Nome_Cliente, E_CPF;
List<String> Nomes = new ArrayList<String>();
...
Associated the view to an XML element:
E_Nome_Cliente = (AutoCompleteTextView)findViewById(R.id.Nome_Cliente);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Nomes.toArray(new String[0]));
E_Nome_Cliente.setAdapter(adapter);
E_Nome_Cliente.setOnItemClickListener(this);
and my onItemClick method is called normally as below:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//switch (parent.getId()) {
//case R.id.Nome_Cliente:
...
//}
}
Does anybody know how I can access this view inside onItemClick? Tried several ways, but I only get exceptions:
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)view.getParent();
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)parent;
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)parent.getParent();
I need to identify which view was clicked, because I'm using 3 to 5 AutoCompleteTextView and based on the selected value I'll automatically fill in a bunch of other fields.
Have a look at the class AutoCompleteTextViewClickListener in this answer.
Change your setOnItemClickListener call in the following way:
E_Nome_Cliente.setOnItemClickListener(
new AutoCompleteTextViewClickListener(E_Nome_Cliente, this));
Now you can get the id by accessing the modified view parameter:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//view had been modified by AutoCompleteTextViewClickListener
//to contain the original AutoCompleteTextView
switch (view.getId()) {
case R.id.Nome_Cliente:
//...
}
}
An easier way:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adapter adapter = parent.getAdapter();
if (adapter == autoCompleteTextView1.getAdapter()) {
// Do something
} else if (adapter == autoCompleteTextView2.getAdapter()) {
// Do something else
}
}
i am not sure what do you mean by view id? do you want to get the selected value?
if yes, then the below code will do it, otherwise please clarify more what do you need and why you want to access the view itself.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//switch (parent.getId()) {
String selected = adapter.getItem(position);
//}
}
more on adapter methods are here
Use parent.findViewById(R.id.id_of_autocompleteTextView) on the parent of the AutoCompleteTextView.
I am using below code to setcolor of a selected item of a listview. The rule is only one should be colored. But with below code if I select 2 views both get colored. Can you please help me get all other views in the listview so that when I click on certain view all other views i set to different color and the selected view i set a different color(Green in this case).
Please let me know if any other solution?
lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(text![enter image description here][1]Adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
}
});
I resolved the problem using the below:
I put a loop where only the selected list item is set in RED whereas all others were set in Green, in this way only only one list item will be colored on selected.
lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(Adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < 3; i++)
{
if (position == i)
{
parent.getChildAt(i).setBackgroundColor(Color.RED);
}
else
{
parent.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.Dark_Green);
}
}
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
}
});
As you told you are not able to change adapter code, you can prefer solution 2.
Solution 1: Create one variable int selectedPosition and method setSelected in your adapter class
int selectedPosition = -1;
public void setSelected(int position)
{
selectedPosition = position;
notifyDatasetChanged();
}
Edit getView() of the adapter class and include following code
if(selectedPosition==position)
{
templateTextView.setBackgroundColor(Color.GREEN);
}
else templateTextView.setBackgroundColor(Color.BLUE);// default textView color
Solution 2: keep reference of previously selected textView as well each time change the color of currently selected textview to green andd previous one to blue
TextView previousSelected = null;
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(previousSelected!=null)
previousSelected.setBackgroundColor(Color.BLUE);
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
previousSelected = v;
}
});
I have list view with names and numbers and checkbox populated from contacts with SimpleAdapter, and on item click I listen event and change color of item, also I want to change checkbox to enabled and after user click one more time on the same item I want to deselect that item.
How can I do that?
Thanks Wolf.
This is my code, I can successfully change color, but I don't now how to change checkbox state for that specific item and than on second click to change that state again:
lImenik.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
// TODO Auto-generated method stub
Object obj = lImenik.getItemAtPosition(position);
String tmp = obj.toString();
view.setBackgroundColor(Color.GRAY);
for(int i = 0; i < adapter.getChildCount(); i++){
if(i == position){
if(position != prePos){
adapter.getChildAt(i).setBackgroundColor(Color.GRAY);
prePos = position;
} else {
adapter.getChildAt(i).setBackgroundColor(0x191919);
prePos = -1;
}
}
}
}
});
}
The method
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id)
provides you the view of row which you have clicked. All what you have to do is to mark or unmark the checkbox in that view. You can do that by
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
// TODO Auto-generated method stub
Object obj = lImenik.getItemAtPosition(position);
CheckBox checkbox = ( CheckBox ) view.findViewById ( R.id.your_checkedbox_id );
checkbox.setChecked ( !checkbox.isChecked() ) ;
But you should have array , or a boolean value for your objects so that at the end you can know that Which object are marked are check and which are not
hello frnds i want to change background color (white on selection) of list on selection of list in listview and if i select any other position then first selected row comes to its previous state and currently selected rows background become white.so how to do this
public void onListItemClick(ListView parent, View v, int position, long id) {
super.onListItemClick(parent, v, position, id);
//do some stuff here
Toast.makeText(getApplicationContext(), "You have selected"+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
I wouldn't do that in code, since you later on might want to change colors, and you shouldn't have "layout/styling" code hardcoded.
Do instead create a style, and apply that to the ListView in your xml. You can read about how you do that in this thread:
ListSelector applies to the entire list
Your list view click listener:
yourlistView.setOnItemClickListener(new OnItemClickListener() {
#Override public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
yourAdapter.toggleSelected(position);
yourAdapter.notifyDataSetChanged();
}
});
Then make an ArrayList in your adapter and initialize it to store all the positions of your list view items:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
int length = yourmainarraylist.size();
for(int i = 0; i < length; i++){
selectedIds.add(0);
}
then put a check in getView to toggle the background:
if (selectedIds.get(position)==1)
convertView.setBackgroundResource(R.drawable.list_row_selected);
else
convertView.setBackgroundResource(R.drawable.list_row);
and put this method in your adapter
public void toggleSelected(int position) {
selectedIds.set(position, (selectedIds.get(position) == 0));
}