-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from invissvenska/develop
Develop
- Loading branch information
Showing
52 changed files
with
1,635 additions
and
684 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,87 @@ | ||
# NumberPickerPreference | ||
An AndroidX preference based NumberPicker | ||
[](https://android-arsenal.com/api?level=16) [](https://jitpack.io/#invissvenska/NumberPickerPreference) | ||
|
||
## Prerequisites | ||
|
||
Add this in your root `build.gradle` file (**not** your module `build.gradle` file): | ||
|
||
```gradle | ||
allprojects { | ||
repositories { | ||
... | ||
maven { url "https://jitpack.io" } | ||
} | ||
} | ||
``` | ||
|
||
## Dependency | ||
|
||
Add this to your module's `build.gradle` file (make sure the version matches the JitPack badge above): | ||
|
||
```gradle | ||
dependencies { | ||
... | ||
implementation 'com.github.invissvenska:NumberPickerPreference:VERSION' | ||
} | ||
``` | ||
|
||
## Configuration | ||
|
||
Add the NumberDialogPreference to the preferences.xml: | ||
|
||
```xml | ||
<nl.invissvenska.numberpickerpreference.NumberDialogPreference | ||
android:key="preference_key" | ||
android:title="Preference title" | ||
app:defaultValue="20" // optional, default is 0 | ||
app:numberPickerPreference_minValue="10" // optional, default is 0 | ||
app:numberPickerPreference_maxValue="60" // optional, default is 100 | ||
app:numberPickerPreference_unitText=" another quantity" /> // optional, default is "" | ||
``` | ||
|
||
Override the OnDisplayPreferenceDialog method in your fragment which extends the PreferenceFragmentCompat class: | ||
```java | ||
public class SettingsFragment extends PreferenceFragmentCompat { | ||
|
||
private static final String DIALOG_FRAGMENT_TAG = "CustomPreferenceDialog"; | ||
|
||
// some other code | ||
|
||
@Override | ||
public void onDisplayPreferenceDialog(Preference preference) { | ||
if (preference instanceof NumberDialogPreference) { | ||
NumberDialogPreference dialogPreference = (NumberDialogPreference) preference; | ||
DialogFragment dialogFragment = NumberPickerPreferenceDialogFragment | ||
.newInstance( | ||
dialogPreference.getKey(), | ||
dialogPreference.getMinValue(), | ||
dialogPreference.getMaxValue(), | ||
dialogPreference.getUnitText() | ||
); | ||
dialogFragment.setTargetFragment(this, 0); | ||
dialogFragment.show(getParentFragmentManager(), DIALOG_FRAGMENT_TAG); | ||
} else { | ||
super.onDisplayPreferenceDialog(preference); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
To create a NumberPickerPreference with default value of 20, min value of 10, max value of 60 and custom unit text: | ||
```xml | ||
<nl.invissvenska.numberpickerpreference.NumberDialogPreference | ||
android:key="preference_key" | ||
android:title="Preference title" | ||
app:defaultValue="20" | ||
app:numberPickerPreference_minValue="10" | ||
app:numberPickerPreference_maxValue="60" | ||
app:numberPickerPreference_unitText=" another quantity" /> | ||
``` | ||
|
||
## Screenshots | ||
|
||
**Please click the image below to enlarge.** | ||
|
||
<img src="https://raw.githubusercontent.com/invissvenska/NumberPickerPreference/master/media/collage.png"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion rootProject.ext.compileSdkVersion | ||
buildToolsVersion rootProject.ext.buildToolsVersion | ||
|
||
defaultConfig { | ||
applicationId "nl.invissvenska.numberpickerpreference" | ||
minSdkVersion rootProject.ext.minSdkVersion | ||
targetSdkVersion rootProject.ext.targetSdkVersion | ||
versionCode rootProject.ext.versionCode | ||
versionName rootProject.ext.versionName | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation "androidx.appcompat:appcompat:$androidXVersion" | ||
implementation "com.google.android.material:material:$androidXVersion" | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
testImplementation 'junit:junit:4.13' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
|
||
implementation project(':numberpickerpreference') | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
26 changes: 26 additions & 0 deletions
26
...droidTest/java/nl/invissvenska/numberpickerpreference/sample/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package nl.invissvenska.numberpickerpreference.sample; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); | ||
assertEquals("nl.invissvenska.modalbottomsheetdialog", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="nl.invissvenska.numberpickerpreference.sample"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:label="@string/app_name" | ||
android:theme="@style/AppTheme.NoActionBar"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/nl/invissvenska/numberpickerpreference/sample/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package nl.invissvenska.numberpickerpreference.sample; | ||
|
||
import android.os.Bundle; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.fragment.app.FragmentTransaction; | ||
|
||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
|
||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); | ||
fragmentTransaction.replace(R.id.nav_host_fragment, new MainFragment()); | ||
fragmentTransaction.addToBackStack(null); | ||
fragmentTransaction.commit(); | ||
} | ||
|
||
@Override | ||
public void onBackPressed() { | ||
super.onBackPressed(); | ||
if (getSupportFragmentManager().getBackStackEntryCount() == 0) { | ||
finish(); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
app/src/main/java/nl/invissvenska/numberpickerpreference/sample/MainFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package nl.invissvenska.numberpickerpreference.sample; | ||
|
||
import android.content.SharedPreferences; | ||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.fragment.app.FragmentTransaction; | ||
import androidx.preference.PreferenceManager; | ||
|
||
import com.google.android.material.floatingactionbutton.FloatingActionButton; | ||
|
||
public class MainFragment extends Fragment { | ||
|
||
private SharedPreferences preferences; | ||
private TextView demo1; | ||
private TextView demo2; | ||
private TextView demo3; | ||
private TextView demo4; | ||
private TextView demo5; | ||
private TextView demo6; | ||
|
||
public MainFragment() { | ||
//keep default constructor | ||
} | ||
|
||
@Override | ||
public void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
View view = inflater.inflate(R.layout.content_main, container, false); | ||
|
||
FloatingActionButton fb = view.findViewById(R.id.fab); | ||
fb.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction(); | ||
fragmentTransaction.replace(R.id.nav_host_fragment, new SettingsFragment()); | ||
fragmentTransaction.addToBackStack(null); | ||
fragmentTransaction.commit(); | ||
} | ||
}); | ||
|
||
demo1 = view.findViewById(R.id.demo1); | ||
demo2 = view.findViewById(R.id.demo2); | ||
demo3 = view.findViewById(R.id.demo3); | ||
demo4 = view.findViewById(R.id.demo4); | ||
demo5 = view.findViewById(R.id.demo5); | ||
demo6 = view.findViewById(R.id.demo6); | ||
|
||
preferences = PreferenceManager.getDefaultSharedPreferences(getContext()); | ||
return view; | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
demo1.setText("Demo_1 value is: " + preferences.getInt("demo_1", 20)); | ||
demo2.setText("Demo_2 value is: " + preferences.getInt("demo_2", 0)); | ||
demo3.setText("Demo_3 value is: " + preferences.getInt("demo_3", 0)); | ||
demo4.setText("Demo_4 value is: " + preferences.getInt("demo_4", 20)); | ||
demo5.setText("Demo_5 value is: " + preferences.getInt("demo_5", 0)); | ||
demo6.setText("Demo_6 value is: " + preferences.getInt("demo_6", 0)); | ||
} | ||
} |
Oops, something went wrong.