-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drop down with special chars for Characters
Custom DialogPreference with special character presets for character preference settings in custom format. Closes #37 Signed-off-by: Aron Heinecke <[email protected]>
- Loading branch information
Showing
12 changed files
with
279 additions
and
75 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
34 changes: 34 additions & 0 deletions
34
...main/java/vocabletrainer/heinecke/aron/vocabletrainer/lib/Widget/CharacterPreference.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,34 @@ | ||
package vocabletrainer.heinecke.aron.vocabletrainer.lib.Widget; | ||
|
||
import android.content.Context; | ||
import android.support.v7.preference.EditTextPreference; | ||
import android.util.AttributeSet; | ||
|
||
import vocabletrainer.heinecke.aron.vocabletrainer.R; | ||
|
||
/** | ||
* Custom EditTextPreference for single character settings | ||
* Created by Aron Heinecke | ||
*/ | ||
public class CharacterPreference extends EditTextPreference { | ||
public CharacterPreference(Context context, AttributeSet attrs, int defStyleAttr, | ||
int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
} | ||
public CharacterPreference(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
public CharacterPreference(Context context) { | ||
super(context); | ||
} | ||
|
||
public CharacterPreference(Context context, AttributeSet attrs) { | ||
this(context, attrs, R.attr.dialogPreferenceStyle); | ||
} | ||
|
||
@Override | ||
public int getDialogLayoutResource() { | ||
return R.layout.dialog_character_preference; | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
...ava/vocabletrainer/heinecke/aron/vocabletrainer/lib/Widget/CharacterPreferenceDialog.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,164 @@ | ||
package vocabletrainer.heinecke.aron.vocabletrainer.lib.Widget; | ||
|
||
import android.app.AlertDialog; | ||
import android.app.Dialog; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.design.widget.TextInputEditText; | ||
import android.support.design.widget.TextInputLayout; | ||
import android.support.v7.preference.DialogPreference; | ||
import android.support.v7.preference.EditTextPreference; | ||
import android.support.v7.preference.Preference; | ||
import android.support.v7.preference.PreferenceDialogFragmentCompat; | ||
import android.text.Editable; | ||
import android.text.TextWatcher; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.Button; | ||
import android.widget.Spinner; | ||
|
||
import vocabletrainer.heinecke.aron.vocabletrainer.R; | ||
import vocabletrainer.heinecke.aron.vocabletrainer.lib.Storage.GenericSpinnerEntry; | ||
|
||
/** | ||
* Character Preference Dialog | ||
*/ | ||
public class CharacterPreferenceDialog extends PreferenceDialogFragmentCompat { | ||
private static final int MAX_LENGTH = 1; | ||
private static final String ARG_TITLE = "title"; | ||
private static String PLACEHOLDER; | ||
TextInputEditText charInput; | ||
TextInputLayout charInputLayout; | ||
Spinner spPreset; | ||
ArrayAdapter<GenericSpinnerEntry<Character>> adapter; | ||
private CustomItemSelectedListener listener; | ||
private Button okButton; | ||
|
||
public static CharacterPreferenceDialog newInstance(Preference preference) { | ||
final CharacterPreferenceDialog | ||
fragment = new CharacterPreferenceDialog(); | ||
final Bundle b = new Bundle(1); | ||
b.putString(ARG_KEY, preference.getKey()); | ||
b.putString(ARG_TITLE, preference.getTitle().toString()); | ||
fragment.setArguments(b); | ||
return fragment; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Dialog onCreateDialog(Bundle savedInstanceState) { | ||
Dialog dialog = super.onCreateDialog(savedInstanceState); | ||
okButton = dialog.findViewById(AlertDialog.BUTTON_POSITIVE); | ||
return dialog; | ||
} | ||
|
||
@Override | ||
protected void onBindDialogView(View view) { | ||
super.onBindDialogView(view); | ||
PLACEHOLDER = getString(R.string.Placeholder); | ||
DialogPreference preference = getPreference(); | ||
|
||
charInput = view.findViewById(R.id.tCharInput); | ||
charInputLayout = view.findViewById(R.id.tCharInputLayout); | ||
charInputLayout.setCounterEnabled(true); | ||
charInputLayout.setCounterMaxLength(MAX_LENGTH); | ||
String hint = getArguments().getString(ARG_TITLE,PLACEHOLDER); | ||
charInputLayout.setHint(hint); | ||
charInput.setSingleLine(); | ||
charInput.setHint(hint); | ||
charInput.setText(((EditTextPreference) preference).getText()); | ||
spPreset = view.findViewById(R.id.spPreset); | ||
|
||
adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item); | ||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | ||
adapter.add(new GenericSpinnerEntry<>(null,getString(R.string.Character_Custom))); | ||
adapter.add(new GenericSpinnerEntry<>('\t',getString(R.string.Character_Tab))); | ||
adapter.add(new GenericSpinnerEntry<>('\r',getString(R.string.Character_Line_Feed))); | ||
adapter.add(new GenericSpinnerEntry<>(',',getString(R.string.Character_Comma))); | ||
adapter.add(new GenericSpinnerEntry<>(';',getString(R.string.Character_Semicolon))); | ||
adapter.add(new GenericSpinnerEntry<>('"',getString(R.string.Character_Quotations_Mark))); | ||
adapter.add(new GenericSpinnerEntry<>('\\',getString(R.string.Character_Backslash))); | ||
spPreset.setAdapter(adapter); | ||
|
||
listener = new CustomItemSelectedListener() { | ||
@Override | ||
public void itemSelected(AdapterView<?> parent, View view, int position, long id) { | ||
Character selected = adapter.getItem(position).getObject(); | ||
if(selected != null){ | ||
charInput.setText(String.valueOf(selected)); | ||
} | ||
} | ||
}; | ||
|
||
spPreset.setOnItemSelectedListener(listener); | ||
|
||
charInput.addTextChangedListener(new TextWatcher() { | ||
@Override | ||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | ||
|
||
} | ||
|
||
@Override | ||
public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
|
||
} | ||
|
||
@Override | ||
public void afterTextChanged(Editable s) { | ||
//listener.disableNextEvent(); | ||
updateSpinner(); | ||
int length = s.toString().toCharArray().length; | ||
if(length == MAX_LENGTH){ | ||
charInputLayout.setError(null); | ||
}else{ | ||
charInputLayout.setError(getString(R.string.Character_Incorrect_Amount)); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void updateSpinner(){ | ||
String input = charInput.getText().toString(); | ||
switch(input){ | ||
case "\t": | ||
spPreset.setSelection(1); | ||
break; | ||
case "\r": | ||
spPreset.setSelection(2); | ||
break; | ||
case ",": | ||
spPreset.setSelection(3); | ||
break; | ||
case ";": | ||
spPreset.setSelection(4); | ||
break; | ||
case "\"": | ||
spPreset.setSelection(5); | ||
break; | ||
case "\\": | ||
spPreset.setSelection(6); | ||
break; | ||
default: | ||
spPreset.setSelection(0); | ||
if(input.length() == MAX_LENGTH) { | ||
adapter.getItem(0).updateObject(input.charAt(0)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onDialogClosed(boolean positiveResult) { | ||
if(positiveResult && charInputLayout.getError() == null){ | ||
DialogPreference preference = getPreference(); | ||
if (preference instanceof EditTextPreference) { | ||
EditTextPreference textPreference = | ||
((EditTextPreference) preference); | ||
// This allows the client to ignore the user value. | ||
if(textPreference.callChangeListener(charInput.getText().toString())){ | ||
textPreference.setText(charInput.getText().toString()); | ||
} | ||
} | ||
} | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
...java/vocabletrainer/heinecke/aron/vocabletrainer/lib/Widget/CustomEditTextPreference.java
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:orientation="vertical" | ||
android:padding="16dp" | ||
tools:context="vocabletrainer.heinecke.aron.vocabletrainer.lib.Widget.CharacterPreference"> | ||
|
||
<android.support.design.widget.TextInputLayout | ||
android:id="@+id/tCharInputLayout" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<android.support.design.widget.TextInputEditText | ||
android:id="@+id/tCharInput" | ||
android:hint="@string/Placeholder" | ||
android:layout_width="match_parent" | ||
android:maxLength="1" | ||
android:layout_height="wrap_content" /> | ||
</android.support.design.widget.TextInputLayout> | ||
|
||
<TextView | ||
style="@style/InputLabel" | ||
android:paddingTop="8dp" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="@string/Character_Preference_Preset_Label" /> | ||
|
||
<Spinner | ||
android:id="@+id/spPreset" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
|
||
</LinearLayout> |
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
Oops, something went wrong.