Skip to content

Commit

Permalink
Merge pull request #1 from invissvenska/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
invissvenska authored Jul 13, 2020
2 parents 1fe85e7 + 50736a3 commit 5b79623
Show file tree
Hide file tree
Showing 52 changed files with 1,635 additions and 684 deletions.
32 changes: 17 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Built application files
*.apk
*.aar
*.ap_
*.aab
*.aar

# Files for the ART/Dalvik VM
*.dex
Expand All @@ -14,15 +14,17 @@
bin/
gen/
out/
# Uncomment the following line in case you need and you don't have the release build type files in your app
# release/
release/

# Gradle files
.gradle
.gradle/
build/
/build

# Local configuration file (sdk path, etc)
local.properties
/local.properties

# Proguard folder generated by Eclipse
proguard/
Expand All @@ -35,20 +37,12 @@ proguard/

# Android Studio captures folder
captures/
/captures

# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml
.idea/**
/.idea/*

# Keystore files
# Uncomment the following lines if you do not want to check your keystore files in.
Expand All @@ -57,7 +51,6 @@ captures/

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
.cxx/

# Google Services (e.g. APIs or Firebase)
# google-services.json
Expand All @@ -83,3 +76,12 @@ lint/generated/
lint/outputs/
lint/tmp/
# lint/reports/
/app/debug/output.json

.DS_Store
.cxx
.cxx/




827 changes: 159 additions & 668 deletions LICENSE

Large diffs are not rendered by default.

87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,87 @@
# NumberPickerPreference
An AndroidX preference based NumberPicker
[![API](https://img.shields.io/badge/API-16%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=16) [![](https://jitpack.io/v/invissvenska/NumberPickerPreference.svg)](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">
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions app/build.gradle
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')

}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
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
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());
}
}
24 changes: 24 additions & 0 deletions app/src/main/AndroidManifest.xml
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>
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();
}
}
}
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));
}
}
Loading

0 comments on commit 5b79623

Please sign in to comment.