Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the default divider?

Tags:

android

I want to make a form and put a divider between each form element, and I want the divider to have to same style as what is default for the ListView on the platform.

Can I somehow access information about the default divider for ListView and use it for my form?

like image 362
totoro Avatar asked Jan 01 '11 04:01

totoro


4 Answers

This is how it's done in some Android sources

<View
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="?android:attr/listDivider" />
like image 109
Michael Avatar answered Oct 20 '22 16:10

Michael


This will get the default list divider that matches your applications theme:

int[] attrs = { android.R.attr.listDivider };
TypedArray ta = getApplicationContext().obtainStyledAttributes(attrs);
//Get Drawable and use as needed
Drawable divider = ta.getDrawable(0);
//Clean Up
ta.recycle();
like image 33
SubHobo Avatar answered Oct 20 '22 16:10

SubHobo


This is how I do it

<ImageView 
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:scaleType="fitXY"
  android:src="?android:attr/listDivider" />
like image 7
Daren Robbins Avatar answered Oct 20 '22 15:10

Daren Robbins


To get default horizontal divider from code you could use:

    final TypedArray array = getContext().getTheme().obtainStyledAttributes(
            R.style.<some_theme>, new int[] {
                android.R.attr.dividerHorizontal
            });
    final int defaultDivider = array.getResourceId(0, 0);
    final Bitmap dividerBitmap = BitmapFactory.decodeResource(r, defaultDivider);
    final BitmapDrawable divider = new BitmapDrawable(r, dividerBitmap);

Then, to also draw it yourself on a Canvas in onDraw:

divider.setBounds(X, Y, X + width, Y + height);
divider.draw(canvas);
like image 2
jayeffkay Avatar answered Oct 20 '22 15:10

jayeffkay