Home » Android » android – How can I get the default divider?

android – How can I get the default divider?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&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" />

Answer:

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();

Answer:

This is how I do it

<ImageView 
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:scaleType="fitXY"
  android:src="?android:attr/listDivider" />

Answer:

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);