How to change color of Android ListView separator line?
You can set this value in a layout xml file using android:divider="#FF0000"
. If you are changing the colour/drawable, you have to set/reset the height of the divider too.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFCC00"
android:dividerHeight="4px"/>
</LinearLayout>
How to change the divider color in the listview?
I have tried it out with:
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/redBackground"
android:dividerHeight="1dip">
</ListView>
and color value is inside colors.xml:
<color name="redBackground">#C60202</color>
And its working fine and displaying Divider color as red with 1dip height.
Update:
Just check your listview layout, you have mentioned 1px for layout_width
and layout_height
and you are setting 4px for the dividerHeight
.
Change color of one line of a ListView
You're going to have override the getView
method of the ArrayAdapter
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(My_Activity.this, R.layout.row_lay, R.id.textViewList, myarray) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
//your condition logic
v.setBackgroundColor(yourColor);
return v;
}
};
how to change the color of the border line of listview in a list detail layout?
If you set black you will get black only. If you want grey color you need to set color as grey. Android can read what you have written in you code, can't read your mind. :D
Do this
<View android:background="#BDBDBD"
android:layout_width="1dp"
android:layout_height="wrap_content"/>
To change the list view separator color do this
android:divider="#BDBDBD"
android:dividerHeight="1dp"
in your list view
Unable to set ListView Divider color in Android
I set up a style for ListViews, in my styles.xml
:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
...
<style name="ListViews" parent="@android:style/Widget.ListView">
<item name="android:cacheColorHint">@color/transparent</item>
<item name="android:divider">@drawable/list_divider</item>
<item name="android:dividerHeight">1px</item>
</style>
...
</resources>
My ListView uses that style as such:
style="@style/ListViews"
Obviously, I have such drawable in my drawable folder.
My list_divider.xml
drawable is as such:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="@color/navy_semi" />
</shape>
[EDIT]
Obviously, instead of @color/transparent
and @color/navy_semi
, use your own colors.
These ones are defined only in my color resurces.
android listview divider line disappears when color set
Since the divider expects a drawable, setting the divider to a color will require that a dividerHeight be specified as well:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/white"
android:dividerHeight="1px"
android:id="@+id/lvSomeListView"/>
What is the default color of a ListView divider line?
The default divider is determined by the listDivider
item in the current theme. For the stock themes, the default divider is one of:
- android:drawable/divider_horizontal_dark (Theme)
- android:drawable/divider_horizontal_bright (Theme.Light)
- android:drawable/divider_horizontal_holo_dark (Theme.Holo)
- android:drawable/divider_horizontal_holo_light (Theme.Holo.Light)
Those drawables can be found in the platforms data folder of your sdk installation.
How to change color of listView item depending on position
Try the code below:
CustomAdapter.java:
public class CustomAdapter extends BaseAdapter {
private LayoutInflater lInflater;
private String[] valueList;
public CustomAdapter(Context context, String[] valueList) {
lInflater = LayoutInflater.from(context);
this.valueList = valueList;
}
@Override
public int getCount() {
return valueList.length;
}
@Override
public Object getItem(int position) {
return valueList[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.row, parent, false);
}
TextView text = view.findViewById(R.id.textRow);
text.setText(valueList[position]);
return view;
}
}
CustomView.java:
public class CustomView extends View {
private String[] hoursList;
private String[] minutesList;
private String[] dayOfTimeList;
private ListView hoursListView;
private ListView minutesListView;
private ListView dayOfTimeListView;
private CustomAdapter hoursAdapter;
private CustomAdapter minutesAdapter;
private CustomAdapter dayOfTimeAdapter;
AbsListView.OnScrollListener scrollListener = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
View firstView = view.getChildAt(0);
int offset = view.getTop() - firstView.getTop();
if (offset > (firstView.getHeight() / 2)) {
view.smoothScrollToPosition(view.getLastVisiblePosition());
} else {
view.smoothScrollToPosition(view.getFirstVisiblePosition());
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
for (int i = 0; i < view.getChildCount(); i++) {
View childView = view.getChildAt(i);
if (i == 1) childView.setBackgroundColor(Color.BLUE); else childView.setBackgroundColor(Color.WHITE);
}
}
};
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, ViewGroup viewGroup) {
super(context);
inflate(context, R.layout.custom_test, viewGroup);
hoursList = new String[26];
hoursList[0] = " ";
hoursList[25] = " ";
for (int i = 1; i < hoursList.length - 1; i++) hoursList[i] = String.valueOf(i);
minutesList = new String[62];
minutesList[0] = " ";
minutesList[61] = " ";
for (int i = 1; i < minutesList.length - 1; i++) minutesList[i] = String.valueOf(i);
dayOfTimeList = new String[4];
dayOfTimeList[0] = " ";
dayOfTimeList[1] = "AM";
dayOfTimeList[2] = "PM";
dayOfTimeList[3] = " ";
hoursListView = viewGroup.findViewById(R.id.hours_list);
hoursAdapter = new CustomAdapter(context, hoursList);
hoursListView.setAdapter(hoursAdapter);
minutesListView = viewGroup.findViewById(R.id.minutes_list);
minutesAdapter = new CustomAdapter(context, minutesList);
minutesListView.setAdapter(minutesAdapter);
dayOfTimeListView = viewGroup.findViewById(R.id.dayOfTime_list);
dayOfTimeAdapter = new CustomAdapter(context, dayOfTimeList);
dayOfTimeListView.setAdapter(dayOfTimeAdapter);
hoursListView.setOnScrollListener(scrollListener);
minutesListView.setOnScrollListener(scrollListener);
dayOfTimeListView.setOnScrollListener(scrollListener);
}
}
To color the 2nd element, the color of other elements need reset. Please note that, if 3 rows is displayed initially, then there may be 4 rows (2 partial displayed and 2 completely displayed) during/after scroll. The 2nd element may be off from the middle position.
Android ListView Change Row Color On Value (not on Select)
The reason for red color in more than one row is, there is no else block. Add an else block will fix this issue.
int urg = model.getUrgency();
if(urg == 1){
v.setBackgroundColor(Color.RED);
}else{
v.setBackgroundColor(Color.WHITE);
}
Related Topics
How to Open My Existing Eclipse Projects in Android Studio
Programmatically Add View One Below Other in Relative Layout
Pagination Not Work for the Recyclerview Within Nestedscrollview
Continuous Speech Recognition Android
How to Set Alarm in Android Programmatically
Suppress/Block Broadcastreceiver in Another App
Android: Fragments Overlapping Issue
Gradle Sync Failed Could Not Find Constraint-Layout:1.0.0-Alpha2
Firebase Ontokenrefresh() Is Not Called
Android: Setselection Having No Effect on Spinner
How to Hide Toolbar When I Scrolling Content Up
What Is a Maximum Size of SQLite Database on Android
Listen to Own Application Uninstall Event on Android
How to Disable Status Bar Click and Pull Down in Android
Using Camera in the Android Emulator
Run Task Before Compilation Using Android Gradle Plugin