Add custom DeviceListPreference

This commit is contained in:
Denis-Cosmin Nutiu 2020-12-05 19:59:20 +02:00
parent 8b94e77ca7
commit 9b10461725
8 changed files with 122 additions and 52 deletions

View file

@ -1,11 +1,12 @@
package dev.nuculabs.nucuhub;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceFragmentCompat;
import dev.nuculabs.nucuhub.ui.settings.SettingsFragment;
public class SettingsActivity extends AppCompatActivity {
@ -28,8 +29,16 @@ public class SettingsActivity extends AppCompatActivity {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO: Add settings menu
// getMenuInflater().inflate(R.menu.settings_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// back button click
if (item.getItemId()==android.R.id.home) {
finish();
return true;
@ -37,11 +46,4 @@ public class SettingsActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}

View file

@ -0,0 +1,24 @@
package dev.nuculabs.nucuhub.ui.settings;
import android.content.Context;
import android.util.AttributeSet;
import androidx.preference.ListPreference;
public class DeviceListPreference extends ListPreference {
public DeviceListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public DeviceListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DeviceListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DeviceListPreference(Context context) {
super(context);
}
// TODO: Create custom dialog, override onClick.
}

View file

@ -0,0 +1,21 @@
package dev.nuculabs.nucuhub.ui.settings;
import android.os.Bundle;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;
import dev.nuculabs.nucuhub.R;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// To get preferences use this in main activity:
// SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
setPreferencesFromResource(R.xml.root_preferences, rootKey);
ListPreference devicesList = findPreference("device_list");
// TODO: Override dialog in order to add a search devices button.
//
// set default value
}
}

View file

@ -211,7 +211,7 @@
tools:text="100.11"
/>
<TextView
android:text="@string/gasvoc_sensor_unit_descriptio"
android:text="@string/gasvoc_sensor_unit_description"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView5"
android:gravity="center"/>

View file

@ -7,25 +7,24 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" tools:visibility="visible"/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"

View file

@ -1,4 +1,7 @@
<resources>
<string-array name="device_list_array">
</string-array>
<!-- Reply Preference -->
<string-array name="reply_entries">
<item>Reply</item>

View file

@ -1,17 +1,22 @@
<resources>
<string name="app_name">NucuHub</string>
<string name="environment_status_description">Temperature sensor status</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="nav_header_title">NucuHub</string>
<string name="nav_header_subtitle">denis@nuculabs.dev</string>
<string name="nav_header_desc">Navigation header</string>
<string name="action_settings">Settings</string>
<string name="menu_gallery">Gallery</string>
<string name="menu_slideshow">Slideshow</string>
<!-- Main Menu-->
<string name="action_settings">Settings</string>
<!-- Activity Titles-->
<string name="title_activity_settings">Settings Activity</string>
<!-- Environment Sensor Data -->
<string name="menu_environment_sensor">Environment Sensor</string>
<string name="environment_status_description">Temperature sensor status</string>
<string name="humidity_icon_desc">humidity image</string>
<string name="temperature_icon_desc">temperature icon</string>
<string name="barometer_icon_desc">barometer icon</string>
@ -19,12 +24,16 @@
<string name="humidity_sensor_unit">Rh%</string>
<string name="temp_sensor_unit">Celsius</string>
<string name="pressure_unit_desc">hPa</string>
<string name="gasvoc_sensor_unit_descriptio">VOC kΩ</string>
<string name="title_activity_settings">SettingsActivity</string>
<string name="gasvoc_sensor_unit_description">VOC kΩ</string>
<!-- Preference Titles -->
<string name="messages_header">Messages</string>
<string name="sync_header">Sync</string>
<string name="devices_header">Devices</string>
<!-- Device Preferences -->
<string name="device_selection_title">Your current device</string>
<!-- Messages Preferences -->
<string name="signature_title">Your signature</string>

View file

@ -1,37 +1,49 @@
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:title="@string/messages_header">
<!-- <PreferenceCategory-->
<!-- app:title="@string/messages_header">-->
<EditTextPreference
app:key="signature"
app:title="@string/signature_title"
app:useSimpleSummaryProvider="true"/>
<!-- <EditTextPreference-->
<!-- app:key="signature"-->
<!-- app:title="@string/signature_title"-->
<!-- app:useSimpleSummaryProvider="true"/>-->
<ListPreference
app:key="reply"
app:title="@string/reply_title"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:defaultValue="reply"
app:useSimpleSummaryProvider="true"/>
<!-- <ListPreference-->
<!-- app:key="reply"-->
<!-- app:title="@string/reply_title"-->
<!-- app:entries="@array/reply_entries"-->
<!-- app:entryValues="@array/reply_values"-->
<!-- app:defaultValue="reply"-->
<!-- app:useSimpleSummaryProvider="true"/>-->
</PreferenceCategory>
<!-- </PreferenceCategory>-->
<!-- <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"/>-->
<!-- </PreferenceCategory>-->
<PreferenceCategory
app:title="@string/sync_header">
app:title="@string/devices_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"/>
<dev.nuculabs.nucuhub.ui.settings.DeviceListPreference
app:key="device_list"
app:title="@string/device_selection_title"
app:entries="@array/device_list_array"
app:entryValues="@array/device_list_array"
app:useSimpleSummaryProvider="true"/>
</PreferenceCategory>