Skip to content

Commit

Permalink
Merge pull request #9 from sadra/issue-390794136
Browse files Browse the repository at this point in the history
Issue 390794136
  • Loading branch information
sadra authored Dec 14, 2018
2 parents 18b38f9 + 40bfaf0 commit 10f4b21
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
4 changes: 2 additions & 2 deletions AwesomeSpinner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.1.0"
versionCode 12
versionName "1.2.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.isapanah.awesomespinner;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatSpinner;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
* Created by sadra on 5/26/17.
Expand All @@ -33,6 +32,11 @@ public class AwesomeSpinner extends RelativeLayout {
private boolean _isItemResourceDeclared = false;
private int _spinnerType = 0;
private boolean _isSelected;
private final int HINT_BUTTON_NOT_SELECTED_COLOR = Color.parseColor("#aaaaaa");
private final int HINT_BUTTON_DISABLED_COLOR = Color.parseColor("#BDBDBD");
private int HINT_BUTTON_COLOR = Color.BLACK;
private final int DOWN_ARROW_DEFAULT_TINT_COLOR = Color.parseColor("#797979");
private int DOWN_ARROW_TINT_COLOR = Color.parseColor("#797979");

public AwesomeSpinner (Context context) {
super(context);
Expand Down Expand Up @@ -63,7 +67,7 @@ private void init(AttributeSet attrs) {

private void setSpinnerStyle(TypedArray typedArray){

_hintButton.setText(typedArray.getString(R.styleable.AwesomeSpinnerStyle_spinnerHint));
setHintButtonText(typedArray.getString(R.styleable.AwesomeSpinnerStyle_spinnerHint));

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10,40,10,10);
Expand Down Expand Up @@ -118,8 +122,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
_isSelected = true;
Object item = AwesomeSpinner.this._spinner.getItemAtPosition(position);
AwesomeSpinner.this._callback.onItemSelected(position, (String) item);
_hintButton.setText(_spinner.getSelectedItem().toString());
setHitButtonStyle();
setHintButtonText(_spinner.getSelectedItem().toString());
}
_allowToSelect = true;

Expand Down Expand Up @@ -152,8 +155,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
_isSelected = true;
Object item = AwesomeSpinner.this._spinner.getItemAtPosition(position);
AwesomeSpinner.this._callback.onItemSelected(position, (String) item);
_hintButton.setText(_spinner.getSelectedItem().toString());
setHitButtonStyle();
setHintButtonText(_spinner.getSelectedItem().toString());
}
_allowToSelect = true;

Expand Down Expand Up @@ -239,7 +241,7 @@ public interface onSpinnerItemClickListener<T> {
public void setSpinnerEnable(boolean enable){
this._spinner.setEnabled(enable);
this._hintButton.setEnabled(enable);
this._downArrow.setAlpha(enable ? 1.0f : 0.6f);
setDownArrowStyle();
setHitButtonStyle();
}

Expand All @@ -248,7 +250,34 @@ public boolean isSpinnerEnable(){
}

private void setHitButtonStyle(){
this._hintButton.setTextColor(this._hintButton.isEnabled() ? Color.BLACK : Color.parseColor("#BDBDBD"));
this._hintButton.setTextColor(
this._hintButton.isEnabled() ?
(isSelected() ? HINT_BUTTON_COLOR : HINT_BUTTON_NOT_SELECTED_COLOR)
:
HINT_BUTTON_DISABLED_COLOR
);
}

public void setSelectedItemHintColor(int color){
this.HINT_BUTTON_COLOR = color;
this._hintButton.setTextColor(isSelected() ? this.HINT_BUTTON_COLOR : this.HINT_BUTTON_NOT_SELECTED_COLOR);
}

private void setHintButtonText(String label){
_hintButton.setText(label);
setHitButtonStyle();
}

private void setDownArrowStyle(){
this._downArrow.setColorFilter(
this._hintButton.isEnabled() ? this.DOWN_ARROW_TINT_COLOR : this.DOWN_ARROW_DEFAULT_TINT_COLOR,
PorterDuff.Mode.SRC_ATOP);
this._downArrow.setAlpha(this._hintButton.isEnabled() ? 1.0f : 0.6f);
}

public void setDownArrowTintColor(int color){
this.DOWN_ARROW_TINT_COLOR = color;
setDownArrowStyle();
}

}
16 changes: 16 additions & 0 deletions AwesomeSpinner/src/main/res/drawable/ic_arrow_drop_down.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="30dp"
android:width="30dp"
android:viewportHeight="16.0"
android:viewportWidth="16.0">
<group
android:pivotX="8"
android:pivotY="7"
android:rotation="180"
android:name="triableGroup">
<path
android:name="arrowPath"
android:fillColor="#797979"
android:pathData="M11.844,9.04L8.345,5.163c-0.195,-0.217 -0.511,-0.217 -0.705,0l-0.012,0.02L4.136,9.04c-0.195,0.217 -0.195,0.569 0,0.786l0.006,0.005C4.233,9.936 4.358,10 4.499,10h6.983c0.143,0 0.27,-0.068 0.359,-0.176l0.002,0.002C12.039,9.609 12.039,9.257 11.844,9.04z"/>
</group>
</vector>
2 changes: 1 addition & 1 deletion AwesomeSpinner/src/main/res/layout/spinner_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<ImageView
android:id="@+id/awesomeSpinner_downArrow"
android:src="@drawable/drop_down_arrow"
android:src="@drawable/ic_arrow_drop_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ allprojects {

**Step 2.** Add the dependency:
```groovy
implementation 'com.github.sadra:AwesomeSpinner:1.1.0'
implementation 'com.github.sadra:AwesomeSpinner:1.3.0'
```

## How To Use
Expand Down Expand Up @@ -93,6 +93,8 @@ there is some controls for your spinner:
| setSelection(TEXT) | `String` value | You should pass the **item value** to select the item programatically |
| setSpinnerEnable(BOOLEAN); | `Boolean` value | If you want enable or disable the spinner, you should call this method. |
| isSpinnerEnable(); | - | Result = `boolean`: Returns **true** if the spinner is enabled, and vice versa |
| setSelectedItemHintColor(); | `int` Color | If you need to change the **Selected Item Hint** color, just call the method and pass you color, ex: `Color.BLUE` |
| setDownArrowTintColor(); | `int` Color | If you need to change the **Down Arrow** hint color, just call the method and pass you color, ex: `Color.BLUE` |

You can see the [EXAMPLE PROJECT](https://github.com/amlashi-sadra/AwesomeSpinner/tree/master/app/src/main) for more infromation.

Expand Down
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "com.isapanah.awsomespinnerexample"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.1.0"
versionCode 12
versionName "1.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -31,5 +31,6 @@ android {

dependencies {
compile 'com.android.support:design:25.4.0'
implementation 'com.github.sadra:AwesomeSpinner:1.1.0'
implementation project(":AwesomeSpinner")
// implementation 'com.github.sadra:AwesomeSpinner:1.2.0'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.isapanah.awsomespinnerexample;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
Expand All @@ -33,6 +33,9 @@ protected void onCreate(Bundle savedInstanceState) {
spinnerRTL = (AwesomeSpinner) findViewById(R.id.awesomeSpinner_example_rtl);
spinnerLTR = (AwesomeSpinner) findViewById(R.id.awesomeSpinner_example_ltr);

spinnerLTR.setSelectedItemHintColor(Color.BLUE);
spinnerLTR.setDownArrowTintColor(Color.RED);

//Initialize the Adapter and spinnerRTL listener
ArrayAdapter<CharSequence> provincesAdapter = ArrayAdapter.createFromResource(this, R.array.iran_provinces, android.R.layout.simple_spinner_item);
spinnerRTL.setAdapter(provincesAdapter, 0);
Expand Down

0 comments on commit 10f4b21

Please sign in to comment.