Skip to content

Commit

Permalink
TextView converted to EditText for display
Browse files Browse the repository at this point in the history
  • Loading branch information
travijuu committed Apr 14, 2017
1 parent 380f5dc commit 8823d27
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 20 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Download

Download [the latest JAR][2] or grab via Gradle:
```groovy
compile 'com.travijuu.numberpicker:numberpicker:1.0.5'
compile 'com.travijuu.numberpicker:numberpicker:1.0.6'
```
or Maven:
```xml
<dependency>
<groupId>com.travijuu.numberpicker</groupId>
<artifactId>numberpicker</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
</dependency>
```

Expand Down Expand Up @@ -47,7 +47,8 @@ Add NumberPicker component in your XML layout
numberpicker:max="10"
numberpicker:value="-5"
numberpicker:unit="1"
numberpicker:custom_layout="@layout/number_picker_custom_layout" />
numberpicker:custom_layout="@layout/number_picker_custom_layout"
numberpicker:focusable="false" />

</LinearLayout>

Expand Down Expand Up @@ -88,8 +89,7 @@ Example XML layout:
android:layout_height="match_parent"
android:text="1"
android:textColor="@android:color/black"
android:focusable="false"
android:inputType="none"
android:inputType="number"
android:id="@+id/display"
android:gravity="center"
/>
Expand Down Expand Up @@ -141,6 +141,8 @@ Getters/Setters
- getUnit()
- setValue(int value)
- getValue()
- setActionEnabled(ActionEnum action, boolean enabled)
- setDisplayFocusable(boolean focusable)

Useful functions
--------
Expand Down Expand Up @@ -182,4 +184,10 @@ public class DefaultValueChangedListener implements ValueChangedListener {
Log.v(this.getClass().getSimpleName(), message);
}
}
```
```

- setOnEditorActionListener(OnEditorActionListener onEditorActionListener)

**OnEditorActionListener** is triggered when you click "**done**" button on keyboard after you edit current value.

*Note:* "**done**" button can be changed on xml so this listener should be overrided according to new IME option.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# org.gradle.parallel=true

#VERSION_NAME=1.0.3-SNAPSHOT
VERSION_NAME=1.0.5
VERSION_CODE=4
VERSION_NAME=1.0.6
VERSION_CODE=5
GROUP=com.github.travijuu

POM_DESCRIPTION=A simple customizable NumberPicker plugin for Android
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.travijuu.numberpicker.library.Listener;

import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.travijuu.numberpicker.library.NumberPicker;

/**
* Created by travijuu on 13/04/17.
*/

public class DefaultOnEditorActionListener implements TextView.OnEditorActionListener {

NumberPicker numberPicker;

public DefaultOnEditorActionListener(NumberPicker numberPicker) {
this.numberPicker = numberPicker;
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
int value = numberPicker.getMin();

try {
value = Integer.parseInt(v.getText().toString());
} catch (Exception e) {
// pass
}

numberPicker.setValue(value);

if (numberPicker.getValue() == value) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

Expand All @@ -14,6 +15,7 @@
import com.travijuu.numberpicker.library.Listener.ActionListener;
import com.travijuu.numberpicker.library.Listener.DefaultLimitExceededListener;
import com.travijuu.numberpicker.library.Listener.DefaultValueChangedListener;
import com.travijuu.numberpicker.library.Listener.DefaultOnEditorActionListener;

/**
* Created by travijuu on 26/05/16.
Expand All @@ -26,19 +28,21 @@ public class NumberPicker extends LinearLayout {
private final int DEFAULT_VALUE = 1;
private final int DEFAULT_UNIT = 1;
private final int DEFAULT_LAYOUT = R.layout.number_picker_layout;
private final boolean DEFAULT_FOCUSABLE = false;

// required variables
private int minValue;
private int maxValue;
private int unit;
private int currentValue;
private int layout;
private boolean focusable;

// ui components
private Context mContext;
private Button decrementButton;
private Button incrementButton;
private TextView displayTextView;
private EditText displayEditText;

// listeners
private LimitExceededListener limitExceededListener;
Expand Down Expand Up @@ -67,6 +71,7 @@ private void initialize(Context context, AttributeSet attrs) {
this.currentValue = attributes.getInteger(R.styleable.NumberPicker_value, this.DEFAULT_VALUE);
this.unit = attributes.getInteger(R.styleable.NumberPicker_unit, this.DEFAULT_UNIT);
this.layout = attributes.getResourceId(R.styleable.NumberPicker_custom_layout, this.DEFAULT_LAYOUT);
this.focusable = attributes.getBoolean(R.styleable.NumberPicker_focusable, this.DEFAULT_FOCUSABLE);
this.mContext = context;

// if current value is greater than the max. value, decrement it to the max. value
Expand All @@ -81,23 +86,27 @@ private void initialize(Context context, AttributeSet attrs) {
// init ui components
this.decrementButton = (Button) findViewById(R.id.decrement);
this.incrementButton = (Button) findViewById(R.id.increment);
this.displayTextView = (TextView) findViewById(R.id.display);
this.displayEditText = (EditText) findViewById(R.id.display);

// register button click listeners
// register button click and action listeners
this.incrementButton.setOnClickListener(new ActionListener(this, ActionEnum.INCREMENT));

This comment has been minimized.

Copy link
@Prakash9612

Prakash9612 Feb 14, 2019

gcvgtvt

this.decrementButton.setOnClickListener(new ActionListener(this, ActionEnum.DECREMENT));
this.setOnEditorActionListener(new DefaultOnEditorActionListener(this));

// init listener for exceeding upper and lower limits
this.setLimitExceededListener(new DefaultLimitExceededListener());
// init listener for increment&decrement
this.setValueChangedListener(new DefaultValueChangedListener());

// set default display mode
this.setDisplayFocusable(this.focusable);

// update ui view
this.updateView();
}

private void updateView() {
this.displayTextView.setText(Integer.toString(this.currentValue));
this.displayEditText.setText(Integer.toString(this.currentValue));
}

public void setMin(int value) {
Expand Down Expand Up @@ -146,6 +155,10 @@ public void setValueChangedListener(ValueChangedListener valueChangedListener) {
this.valueChangedListener = valueChangedListener;
}

public void setOnEditorActionListener(TextView.OnEditorActionListener onEditorActionListener) {
this.displayEditText.setOnEditorActionListener(onEditorActionListener);
}

public void setActionEnabled(ActionEnum action, boolean enabled) {
if (action == ActionEnum.INCREMENT) {
this.incrementButton.setEnabled(enabled);
Expand All @@ -154,6 +167,15 @@ public void setActionEnabled(ActionEnum action, boolean enabled) {
}
}

public void setDisplayFocusable(boolean focusable) {
this.displayEditText.setFocusable(focusable);

// required for making EditText focusable
if (focusable) {
this.displayEditText.setFocusableInTouchMode(true);
}
}

public void increment() {
this.changeValueBy(this.unit);
}
Expand Down
11 changes: 6 additions & 5 deletions library/src/main/res/layout/number_picker_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,34 @@
android:background="@drawable/background_white">

<Button
android:id="@+id/decrement"
android:layout_width="30dp"
android:layout_height="match_parent"
android:padding="0dp"
android:textColor="@color/colorPrimary"
android:background="@null"
android:id="@+id/decrement"
android:textStyle="bold"
android:text=""/>

<TextView
<EditText
android:id="@+id/display"
android:layout_width="70dp"
android:background="@drawable/border_gray"
android:layout_height="match_parent"
android:text="1"
android:textColor="@android:color/black"
android:focusable="false"
android:inputType="none"
android:id="@+id/display"
android:inputType="number"
android:gravity="center"
android:imeOptions="actionDone"
/>
<Button
android:id="@+id/increment"
android:layout_width="30dp"
android:layout_height="match_parent"
android:padding="0dp"
android:textSize="25sp"
android:textColor="@color/colorPrimary"
android:background="@null"
android:id="@+id/increment"
android:text="+"/>
</LinearLayout>
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<attr name="value" format="integer"/>
<attr name="unit" format="integer" />
<attr name="custom_layout" format="reference" />
<attr name="focusable" format="boolean" />
</declare-styleable>

<declare-styleable name="Themes">
Expand Down
5 changes: 2 additions & 3 deletions sample/src/main/res/layout/number_picker_custom_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
android:layout_height="40dp"
android:orientation="horizontal">

<TextView
<EditText
android:layout_width="70dp"
android:layout_height="match_parent"
android:text="@string/display"
android:textColor="@android:color/black"
android:focusable="false"
android:inputType="none"
android:inputType="number"
android:id="@+id/display"
android:gravity="center"
/>
Expand Down

0 comments on commit 8823d27

Please sign in to comment.