The library extends Android edit text widgets by adding the ability to treat some compound drawables as buttons.
The complete list of extended classes:
- ClickableEditText
- ClickableTextInputEditText
- ClickableAutoCompleteTextView
- ClickableMultiAutoCompleteTextView
You can get the latest APK from this repo or download it from Google Play:
Add this in your root build.gradle
file (not your module build.gradle
file):
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
dependencies {
// other dependencies
compile('com.github.vnidens:ClickableEditText:0.5.0.5'){
transitive = true
}
}
Drawables for buttons can be set either from code either in XML.
From code:
ClickableEditText cet = ...;
Drawable drawable = ...;
cet.setStartButtonDrawable(R.drawable.ic_icon);
cet.setEndButtonDrawable(drawable);
In XML:
<com.vnidens.clickableedittext.ClickableEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ClickableEditText"
app:cet_drawableStart="@drawable/ic_icon"
app:cet_drawableEnd="@drawable/ic_icon_0"/>
You can add tinting for each button separately. The tint can be either a simple color either a color recource or a color state list.
From code:
ClickableEditText cet = ...;
ColorStateList colorList = ...;
cet.setStartButtonTintRes(R.color.blue_500);
cet.setEndButtonTintList(colorList);
ClickableEditText cet1 = ...;
int color = ...;
cet1.setStartButtonTint(color);
From XML:
<com.vnidens.clickableedittext.ClickableEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ClickableEditText"
app:cet_drawableStart="@drawable/ic_icon"
app:cet_drawableStartTint="@color/state_list_blue"
app:cet_drawableEnd="@drawable/ic_icon_0"
app:cet_drawableEndTint="@color/blue_500"/>
To get the button click event you should implement OnCompoundButtonClickListner
interface and register it for start and/or end button click event.
ClickableEditText cet = ...;
OnCompoundButtonClickListner listener = new OnCompoundButtonClickListener(){
@Override
public void onCompoundButtonClicked(@NonNull EditText view, @IdRes int which){
...
}
};
cet.setOnStartButtonClickListener(listner);
cet.setOnEndButtonClickListener(listener);
The view
parameter will tell you from which view the event came from. And the which
parameter will tell you the button's id
which has been clicked.