Reputation: 340
I have a list which contains hardcoded items which bydefault comes in white colour which is almost invisible with my whitebackground. public class Message extends ListFragment {
/** An array of items to display in ArrayList */
String android_versions[] = new String[]{
"Jelly Bean",
"IceCream Sandwich",
"HoneyComb",
"Ginger Bread",
"Froyo"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);
/** Setting the array adapter to the listview */
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
Upvotes: 0
Views: 109
Reputation: 91
You can use the baseadapter with custom view, It is easy way to change the color in custom view
Refer Example : http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
Upvotes: 0
Reputation: 3372
you have to put below code in styles.xml
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:itemBackground">@android:color/holo_green_dark</item>
</style>
Upvotes: 0
Reputation: 1135
for your requirement you should change custom adapter instead of default: here you may find the some help:
public class CustomUsersAdapter extends ArrayAdapter<User> {
public CustomUsersAdapter(Context context, ArrayList<User> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvName.setTextColor(Color.parseColor("#FFFFFF"));
// Return the completed view to render on screen
return convertView;
}
}
you need a model that use as a data type in your array
public class User {
public String name;
public User(String name) {
this.name = name;
}
public static ArrayList<User> getUsers() {
ArrayList<User> users = new ArrayList<User>();
users.add(new User("Jelly Bean"));
users.add(new User("IceCream Sandwich"));
users.add(new User("HoneyComb"));
users.add(new User("Ginger Bread"));
return users;
}
}
in your fragment class:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayList<User> arrayOfUsers = User.getUsers();
// Create the adapter to convert the array to views
CustomUsersAdapter adapter = new CustomUsersAdapter(getActivity(), arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) getActivity().findViewById(R.id.lvUsers);
listView.setAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
Now you are able to change the color of background or textColor dynamically in Java or in XML "item_user"
Upvotes: 0
Reputation: 7354
A simple way would be copying the code in android.R.layout.simple_list_item_multiple_choice into your own layout resource and customizing it.
In Android-v22 this code is:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />
And then you can pass your own customized resource to the ArrayAdapter constructor:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), R.layout.my_simple_list_item_multiple_choice, android_versions);
Upvotes: 0
Reputation: 855
Make a Custom Layout for ListviewAdapter instead of using Default, Refer the Below Link for more Understanding [Link] (http://www.vogella.com/tutorials/AndroidListView/article.html)
Upvotes: 2