TechPro
TechPro

Reputation: 3

How to remove default title and summery from preference layout

I'm trying to create a custom preference settings (which will contain a title, summary and a progress bar which will indicate to user the syncing process). I have a problem to remove completely the original title and summery because it looks like there is indent in the layout (see attached picture for more details)

I have created a custom layout:

R.layout.manual_sync_layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:id="@+id/manual_sync_layout"
        android:background="?attr/selectableItemBackground">


    <ImageView
            android:id="@android:id/icon"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="gone"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@null"
            android:layout_gravity="center"/>

    <TextView android:id="@android:id/title"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              android:textSize="16sp"
              android:padding="10dp"
              android:text="@string/manual_sync"
              android:textColor="@android:color/black"/>
    <TextView
            app:layout_constraintTop_toBottomOf="@android:id/title"
            app:layout_constraintStart_toStartOf="parent"
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="@string/sync_manually_desc"
    />

    <ProgressBar
            android:id="@+id/manual_sync_progress"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:indeterminate="false"
            app:layout_constraintEnd_toEndOf="parent"
            style="?android:attr/progressBarStyleSmall"
            android:padding="10dp"
            android:visibility="gone"/>

</androidx.constraintlayout.widget.ConstraintLayout>

ManualSyncPreference

class ManualSyncPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int = 0) : Preference(context, attrs, defStyleAttr) {

init {
    widgetLayoutResource = R.layout.manual_sync_layout
}

override fun onBindViewHolder(holder: PreferenceViewHolder) {
    super.onBindViewHolder(holder)
    val manualSyncLayout = holder.findViewById(R.id.manual_sync_layout)
    val syncProgress = holder.findViewById(R.id.manual_sync_progress)
    manualSyncLayout.setOnClickListener {
        syncProgress.visibility = if(syncProgress.visibility== View.VISIBLE) View.GONE else View.VISIBLE
    }

}

}

preferences.xml

<PreferenceCategory
        app:title="@string/sync_header">

    <SwitchPreferenceCompat
            app:key="sync"
            app:title="@string/sync_title"/>

    <SwitchPreferenceCompat
            app:key="attachment"
            app:title="@string/attachment_title"
            app:summaryOn="@string/attachment_summary_on"
            app:summaryOff="@string/attachment_summary_off"
            app:dependency="sync"/>

    <com.example.myapplication.ManualSyncPreference
            app:key="manual_key"
            app:title=""
            app:summary=""/>

</PreferenceCategory>

there is a big margin in the last row

Upvotes: 0

Views: 449

Answers (1)

Louis
Louis

Reputation: 514

You need to set the layoutResource not the widgetLayoutResource - the widget layout resource is the part of the preference layout that contains a widget at the end, such as a switch or checkbox. In your case you want to replace the entire layout, so you need to change the layoutResource. I think just renaming widgetLayoutResource to layoutResource should work here.

Upvotes: 0

Related Questions