Home Make "settings-like" buttons in layouts
Reply: 2

Make "settings-like" buttons in layouts

Imajou
1#
Imajou Published in 2018-02-14 09:38:13Z

I am new to Android dev and kindly asking for help.

I would like to make "settings-like" (full-width, flat, with images, with separators) button in my app. Here is the example: Example image from Samsung settings app

I know about "Preference" view - it is not the solution. Is there any native view for this type of button? If not, what is the most appropriate way to make this button possible?

Upd.: I have managed to make the buttons by myself using LinearLayout. You can find my implementation in answers below.

Vladislav Yundin
2#
Vladislav Yundin Reply to 2018-02-14 10:23:03Z

You can use RecyclerView. Here's the example of xml:

<LinearLayout

    ...>

    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:id="@+id/recycler"
            android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

In Java:

RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
            linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

recyclerView.setAdapter(new RecyclerAdapter(this, /*list of your items*/));

Than you have to create your own RecyclerAdapter class, ViewHolder and xml for one single item of the list.

Imajou
3#
Imajou Reply to 2018-02-14 10:46:00Z

Ok, seems there is no native view for this type of button in Android.

RecyclerView and ListView will not help, because they require adapters (I have no need in adapters, I need simple buttons only).

So, I decided to make these buttons using linear layout, here is the code:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_sim_card" />

    <Space
        android:layout_width="16dp"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="@string/action_edit" />

</LinearLayout>

Make LinearLayout android:clickable="true" and android:focusable="true" (later you can just bind onClickListener in code) for it will act like a button. And the main point is to make button-like animations (ink-effect on click) - specify background attribute: android:background="?android:attr/selectableItemBackground"

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.390606 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO