I have two spinner. They look like that. I don't know why dropdown menu icon is far away from texts.
(Chagned image after gravity attribute is set to "end")
In my activity_main.xml it is written:
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/map_root_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#mipmap/map_bg"
android:orientation="vertical"
app:layoutDescription="#xml/map_scroll">
<FrameLayout
android:id="#+id/map_top_panel_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#mipmap/home_top"
android:orientation="vertical"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Spinner
android:id="#+id/map_spinner_sector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:gravity="end"/>
<Spinner
android:id="#+id/map_spinner_sorting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:gravity="end"/>
</FrameLayout>
...
</androidx.constraintlayout.motion.widget.MotionLayout>
SpinnerAdapter.java class:
public class SpinnerAdapter extends ArrayAdapter<String> {
public SpinnerAdapter(#NonNull Context context, ArrayList<String> items) {
super(context,0, items);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}
private View initView(int position, View convertView, ViewGroup parent){
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.spinner_item, parent, false
);
}
TextView spinnerTextView = (TextView) convertView;
spinnerTextView.setText(getItem(position));
spinnerTextView.setGravity(Gravity.END); //This line is added later
return convertView;
}
}
and spinner_item.xml file, which is the layout of spinner items:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="#+id/my_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am not experienced with Spinners. I am learning. I wonder why i encountered such a bug.
Not: I have EDITED the spinner_item.xml file :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:background="#drawable/suggestions_bg"
android:textColor="#color/blue_popular" />
I just had a similar problem, to solve this I had to set the attribute gravity in spinner.
android:gravity="end" - this helps to display items next to the dropdown icon.
Next, I had to override getDropDownView in my adapter class in this way:
public View getDropDownView(int position, View convertView,ViewGroup parent)
{
View v = super.getDropDownView(position, convertView,parent);
((TextView) v).setGravity(Gravity.END);
return v;
}
This helps to display items next to the dropdown icon when Spinner is expanded. Your spinner_item is also TextView so I think You can just copy and paste this function.
Your spinner_item.xml file shouldn't again contain Spinner, just a TextView would be enough but if you want a fancy layout, you can other widgets such ImageView. The following is the example of the simplest spinner_item.xml file.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_text_1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit" />
Now, get rid of implementation of SpinnerAdapter class.
We can use ArrayAdapter as adapter of Spinner like this :
// bind your spinner layout from file to a spinner object
Spinner spinner = findViewById(R.id.map_spinner_sector);
// string array which is the content of the spinner
// if you already have array or list object, then this
// is not needed.
ArrayList<String> spinnerStringArray = ...; // populate the array backing your adapter
// if you have string array resources in file,
// use ArrayAdapter.createFromResource() method to get your array adapter
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_item, spinnerStringArray);
spinner.setAdapter(spinnerAdapter);
// set listener to handle item selected event
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("Item Selected : ", spinnerStringArray[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
Hi everyone I'm trying to show with a GridView some items that show the contents of an array of strings. This is the CardView I want to set as item:
So when I click on a button, the layout of the choice of the type of film becomes visible and I set an adapter:
cTBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postaCardVIew.setVisibility(View.GONE);
cT.setVisibility(View.VISIBLE);
GridView simpleGridView = findViewById(R.id.simpleGridView);
String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza","Fantasy",
"Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
};
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (posta.this,android.R.layout.simple_list_item_1, genrelist);
simpleGridView.setAdapter(adapter);
}
But this is what I am getting:
I want to inflate this layout though:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="150dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="30dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type"
android:layout_centerInParent="true"
android:textSize="20dp"
android:textStyle="bold"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
To get such a thing:
Can anyone help me figure out how I could inflate the desired layout?
In order to use a custom item layout, you need to create a custom adapter that extends from the BaseAdapter or ArrayAdapter
Then inflate and build your item layout in the getView() method:
public class CustomAdapter extends BaseAdapter {
String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza", "Fantasy",
"Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
};
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title = convertView.findViewById(R.id.type);
holder.title.setText(genrelist[position]);
return convertView;
}
static class ViewHolder {
TextView title;
}
public int getCount() {
return genrelist.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
And use it as the GridView adapter:
cTBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postaCardVIew.setVisibility(View.GONE);
cT.setVisibility(View.VISIBLE);
GridView simpleGridView = findViewById(R.id.simpleGridView);
simpleGridView.setAdapter(new CustomAdapter());
}
});
I had to manipulate the margin and cared width and wrap the CardView with a FrameLayout in order to force the margin among cards:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="4dp"
android:layout_marginVertical="4dp">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="120dp"
android:layout_height="120dp"
app:cardCornerRadius="30dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Type: 1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
Result:
I have an custom array adaptor which is handling a list view.
Each row has two checkboxes. There are five rows in total.
I want to be able to count the number of checkboxes ticked and then display this number in Stand1
So basically the project Layout is like this
Inside stand1 there is a list view and a text view.
The stand1.java calls an array adapter Called CustomArrayAdaptor.
I want to be able to count the number of checkboxes clicked and send that data back to the stand1
I am quite new to android development so if somebody can be able to push me in the right direction, it would be great.
Here is my code
Stand1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="446dp"
android:id="#+id/Stand1list"
android:scrollIndicators="right"
android:smoothScrollbar="true">
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Score To Databse"
android:id="#+id/sendtodb"
android:textSize="12dp"
android:clickable="true"
android:layout_below="#+id/Stand1list"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8/10"
android:id="#+id/Score"
android:layout_marginLeft="33dp"
android:layout_marginStart="33dp"
android:layout_alignBottom="#+id/sendtodb"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp" />
Row_Layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textelement"
xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="30dp"
android:textStyle="bold"
android:longClickable="false" />
<CheckBox
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/Checkbox1"
android:button="#drawable/checkboxcustom"
android:layout_marginLeft="50dp" />
<CheckBox
android:button="#drawable/checkboxcustom"
android:id="#+id/Checkbox2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp" />
</LinearLayout>
Stand1.java
public class Stand1 extends Fragment {
ListView mList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair 1","Pair 2","Pair 3","Pair 4","Pair 5"};
//build adapter
ArrayAdapter<String> adapter = new CustomArrayAdaptor(getActivity(),pair);
mList.setAdapter(adapter);
}
}
CustomArrayAdaptor.java
public class CustomArrayAdaptor extends ArrayAdapter<String>{
public CustomArrayAdaptor(Context context, String [] Pairs) {
super(context, R.layout.row_layout1, Pairs);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View CustomView = inflater.inflate(R.layout.row_layout1, parent, false);
String stringelement = getItem(position);
TextView Text= (TextView)CustomView.findViewById(R.id.textelement);
Text.setText(stringelement);
return CustomView;
}
}
Thanks Folks
I added a few things to your CustomArrayAdaptor and tested it, see below:
public class CustomArrayAdapter extends ArrayAdapter<String> {
int checkAccumulator;
public CustomArrayAdapter(Context context, String [] Pairs) {
super(context, R.layout.row_layout1, Pairs);
checkAccumulator = 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customView = inflater.inflate(R.layout.row_layout1, parent, false);
String stringelement = getItem(position);
TextView Text = (TextView) customView.findViewById(R.id.textelement);
CheckBox checkBox1 = (CheckBox) customView.findViewById(R.id.Checkbox1);
CheckBox checkBox2 = (CheckBox) customView.findViewById(R.id.Checkbox2);
CompoundButton.OnCheckedChangeListener checkListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
countCheck(isChecked);
Log.i("MAIN", checkAccumulator + "");
}
};
checkBox1.setOnCheckedChangeListener(checkListener);
checkBox2.setOnCheckedChangeListener(checkListener);
Text.setText(stringelement);
return customView;
}
private void countCheck(boolean isChecked) {
checkAccumulator += isChecked ? 1 : -1 ;
}
}
Added int checkAccumulator to keep track of how many items have been clicked.
Added two CheckBox Views for the purpose of tracking checks.
Added CompoundButton.OnCheckedChangeListener that calls a new countCheck method everytime any checkBox is checked / unchecked. We pass the isChecked boolean to track whether we need to add or subtract from our checkAccumulator.
Added countCheck function that adds or subtracts 1 dependent on the boolean. Sorry for the obfuscated code, for all those who aren't used to the ? conditional it is basically saying if true add 1, else add -1.
Not sure what you wanted to do with the check count, so I added a Log.i("MAIN", checkAccumulator + ""); just so you can watch the Android Monitor to ensure that it is actually working.
Should work for you.
First of all, you should recycle your views in the adapter:
LayoutInflater inflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_layout1, parent, false);
}
After that, I think you should save the state of each checkbox in your data source or just keep a list, the same size of your pair array, and save it there using the position you get from getView.
I created a ListFragment by using a custom adapter. OnListItemClick is not work after I add a button in the list row. If I click on the list row it should be intent to other class, but now when I click on the list row, it did not have any action. No error and no action. So I think it might because there has two clickable items in the list row, so the onListItemClick is not work. But I do not know how can I solve this problem.
Fragment1.java
public class Fragment1 extends ListFragment implements AdapterInterface{
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_show_queue, container, false);
// initialize the items list
mItems = new ArrayList<ListViewItem>();
// initialize and set the list adapter
adapter = new ListViewAdapter2(getActivity(), mItems, this);
setListAdapter(adapter);
return rootView;
}
#Override
public void buttonPressed(int position) {
System.out.println("Fragment1: " + position);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(getActivity(), QueueDetail.class);
i.putExtra("qid", qid.get(position).toString());
startActivity (i);
}
}
ListViewAdapter2.java
public class ListViewAdapter2 extends ArrayAdapter<ListViewItem> {
AdapterInterface buttonListener;
private int position;
public ListViewAdapter2(Context context, List<ListViewItem> items, AdapterInterface buttonListener) {
super(context, R.layout.listview_layout2, items);
this.buttonListener = buttonListener;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
this.position = position;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_layout2, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
viewHolder.txt = (TextView) convertView.findViewById(R.id.txt);
viewHolder.smallTxt = (TextView) convertView.findViewById(R.id.smallTxt);
viewHolder.datetime = (TextView) convertView.findViewById(R.id.datetime);
viewHolder.viewBtn = (Button) convertView.findViewById(R.id.viewBtn);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.viewBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("ListViewAdapter: " + position);
buttonListener.buttonPressed(position);
}
});
...
return convertView;
}
private int getViewBtnPosition(){
return position;
}
}
fragment_show_queue.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</FrameLayout>
listview_layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_toRightOf="#+id/img"
android:layout_toLeftOf="#+id/datetime"
android:layout_toStartOf="#+id/datetime">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp" />
<TextView
android:id="#+id/smallTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/datetime"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text=""
android:textSize="10dp" />
<Button
android:layout_width="35dp"
android:layout_height="20dp"
android:id="#+id/viewBtn"
android:text="JOIN"
android:textSize="10dp"
android:background="#b5e61d"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
add android:focusable="false" and android:focusableInTouchMode="false" to your Button in the row's layout file.
i'm trying to catch the click event for a list of Switches
ListView list = (ListView) findViewById(R.id.main_view);
ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this, R.layout.list_main_view_item, R.id.switch1, values);
list.setAdapter(LTRadapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
Log.w("MiniTasker","w00t " + Integer.toString(pos));
}
});
i've added android:descendantFocusability="blocksDescendants" to the ListView but to no avail.
Here's activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/main_view"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
and here's list_main_view_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:minHeight="?android:attr/listPreferredItemHeight"
android:focusable="false"
android:focusableInTouchMode="false">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="#+id/switch1"
android:layout_gravity="center_horizontal|top"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:checked="true"
android:tag="0"/>
</RelativeLayout>
Ok you should add an id to your list view in your XML.
Then you can apply that ID to your ListView in your method:
ListView list = (ListView) findViewById(R.id.my_list_view);
Right now your list reference is pointing at your Linear layout (white space outside your actual ListView). That's why it detects clicks outside the ListView
I think if you set the clickable attribute to false for switch tag in XML like below, it would work.
android:clickable="false"
But, I don't see a point in adding a switch in a list view where switch wouldn't be reacting the way we need it to react to click events. If you replace switch with imageView or TextView it will work as those won't react to click events unless specified.
The only solution I found was to extend ArrayAdapter I used in the code and to override getView
public class MainListAdapter extends ArrayAdapter<String> {
...
Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) mycontext).getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_main_view_item, null);
assert convertView!=null;
Switch s = (Switch)convertView.findViewById(R.id.switch1);
assert s!=null;
s.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.w("MiniTasker", "adapter w00t " + Integer.toString(view.getId()));
}
});
}
return super.getView(position, convertView, parent);
}
}
android:focusable="false"
android:focusableInTouchMode="false"
Add this in the XML inside the Switch element.
I am doing project in Android. I want to change background color as well as textcolor of selected item from ListView. Here is my code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listView1"
android:layout_width="265dp"
android:layout_height="366dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_weight="0.00"
android:drawSelectorOnTop="true" >
</ListView>
</LinearLayout>
</LinearLayout>
So,I have ListView with some student names and with facility of multiple choice by using checkbox.
ListView stud_lst=(ListView) findViewById(R.id.listView1);
stud_lst.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
I want to change the background and text color of selected student.
I already saw some answers but I am not getting it.
Please help me.
Use a custom adapter and in your activity class do the following:
// mListview is ur listview object.
mListview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
view.setBackgroundColor("your bg's color id");
}
}
You have to create a Custom Adapter to change the item's Background Color. Here is the example of Custom adapter:
public class PaListAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<String> platevalue = new ArrayList<String>();
ViewHolder holder;
public PaListAdapter(Context context,ArrayList<String> value)
{
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
//mycontext = context;
platevalue.clear();
platevalue =value;
}
public int getCount()
{
return platevalue.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.select_dialog, null);
holder = new ViewHolder();
holder.hTransID =(TextView) convertView.findViewById(R.id.txtChoice);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.hTransID.setText(platevalue.get(position));
return convertView;
}
static class ViewHolder
{
TextView hTransID;
}
}
select_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:background="#000000"
>
<TextView
android:id="#+id/txtChoice"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</LinearLayout>
In Activity Class.Define it like:
simpleefficientadapter efficientadapter;
efficientadapter=new simpleefficientadapter(CLASSNAME.this, VALUES);
listView.setAdapter(efficientadapter);