Last active
September 12, 2019 09:17
-
-
Save mohamedagamy/16afa6de0c7fd6169cb7316443e2b2e0 to your computer and use it in GitHub Desktop.
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
<!--Here is our custom layout android--> | |
<declare-styleable name="CustomEditText"> | |
<attr name="text" format="string"/> | |
<attr name="hint" format="reference"/> | |
<attr name="drawableStart" format="reference" /> | |
<attr name="drawableEnd" format="reference" /> | |
<attr name="drawableStartVisibility" format="boolean" /> | |
<attr name="drawableEndVisibility" format="boolean" /> | |
<attr name="passwordToggleEnabled" format="boolean" /> | |
<attr name="android:inputType" /> | |
</declare-styleable> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.appcompat.widget.LinearLayoutCompat | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:background="@drawable/btn_transparent" | |
android:layoutDirection="locale"> | |
<androidx.appcompat.widget.AppCompatImageView | |
android:id="@+id/iv_start_icon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical" | |
android:src="@drawable/ico_said" /> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/et_user_input" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:hint="@string/e_mail" | |
android:inputType="textEmailAddress" | |
android:textSize="14sp" | |
android:gravity="center" | |
android:paddingStart="8dp" | |
android:paddingEnd="8dp" | |
android:layout_marginStart="8dp" | |
android:layout_marginEnd="8dp" | |
android:background="@android:color/transparent"/> | |
<CheckBox | |
android:id="@+id/iv_end_icon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical" | |
android:button="@drawable/password_icon_toggle" | |
android:checked="true" | |
android:src="@drawable/password_icon_toggle" /> | |
</androidx.appcompat.widget.LinearLayoutCompat> |
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
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.text.method.PasswordTransformationMethod; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.inputmethod.EditorInfo; | |
import android.widget.CheckBox; | |
import android.widget.CompoundButton; | |
import android.widget.LinearLayout; | |
import androidx.appcompat.widget.AppCompatEditText; | |
import androidx.appcompat.widget.AppCompatImageView; | |
import com.arabiait.konasha.R; | |
import com.arabiait.konasha.data.prefs.SharedPrefsData; | |
import com.arabiait.konasha.utils.FontsUtils; | |
import com.arabiait.konasha.utils.Utility; | |
import org.androidannotations.annotations.AfterViews; | |
import org.androidannotations.annotations.EViewGroup; | |
import org.androidannotations.annotations.ViewById; | |
@EViewGroup(R.layout.custom_edittext_layout) | |
public class CustomEditText extends LinearLayout { | |
@ViewById(R.id.iv_start_icon) AppCompatImageView startIcon; | |
@ViewById(R.id.iv_end_icon) CheckBox endIcon; | |
@ViewById(R.id.et_user_input) AppCompatEditText userInput; | |
Context context; | |
int startIconDrawable,endIconDrawable,inputType; | |
boolean startIconVisibility , endIconVisibility; | |
String userText,userHint; | |
boolean pwdToggleEnabled; | |
public CustomEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
this.context=context; | |
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CustomEditText, 0, 0); | |
startIconDrawable = a.getResourceId(R.styleable.CustomEditText_drawableStart,R.drawable.ico_said); | |
endIconDrawable = a.getResourceId(R.styleable.CustomEditText_drawableEnd,R.drawable.ico_view_password); | |
startIconVisibility = a.getBoolean(R.styleable.CustomEditText_drawableStartVisibility,true); | |
endIconVisibility = a.getBoolean(R.styleable.CustomEditText_drawableEndVisibility,true); | |
userText = a.getString(R.styleable.CustomEditText_text); | |
userHint = a.getString(R.styleable.CustomEditText_hint); | |
pwdToggleEnabled = a.getBoolean(R.styleable.CustomEditText_passwordToggleEnabled,false); | |
inputType = a.getInt(R.styleable.CustomEditText_android_inputType, EditorInfo.TYPE_TEXT_VARIATION_NORMAL); | |
a.recycle(); | |
} | |
@AfterViews | |
void initView(){ | |
//Here we resolve Images drawable again | |
String startIconName = getResources().getResourceEntryName(startIconDrawable); | |
int startIconRes = context.getResources().getIdentifier(startIconName, | |
"drawable", getContext().getPackageName()); | |
String endIconName = getResources().getResourceEntryName(endIconDrawable); | |
int endIconRes = context.getResources().getIdentifier(endIconName, | |
"drawable", getContext().getPackageName()); | |
startIcon.setImageResource(startIconRes); | |
endIcon.setBackgroundResource(endIconRes); | |
getUserInput().setInputType(inputType); | |
startIcon.setVisibility(startIconVisibility ? VISIBLE : GONE); | |
endIcon.setVisibility(endIconVisibility ? VISIBLE : GONE); | |
if(pwdToggleEnabled) { | |
getUserInput().setTransformationMethod(new PasswordTransformationMethod()); | |
endIcon.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked) -> { | |
if (!isChecked) { | |
getUserInput().setTransformationMethod(null); | |
} else { | |
getUserInput().setTransformationMethod(new PasswordTransformationMethod()); | |
} | |
}); | |
} | |
getUserInput().setText(userText); | |
getUserInput().setHint(userHint); | |
int gravity = startIconVisibility ? Gravity.START|Gravity.CENTER_VERTICAL : | |
Gravity.END|Gravity.CENTER_VERTICAL; | |
getUserInput().setGravity(gravity); | |
FontsUtils.setFont(context,new View[]{getUserInput()}); | |
} | |
//Here all setters and getters | |
public AppCompatImageView getStartIcon() { | |
return startIcon; | |
} | |
public void setStartIcon(AppCompatImageView startIcon) { | |
this.startIcon = startIcon; | |
} | |
public CheckBox getEndIcon() { | |
return endIcon; | |
} | |
public void setEndIcon(CheckBox endIcon) { | |
this.endIcon = endIcon; | |
} | |
public AppCompatEditText getUserInput() { | |
return userInput; | |
} | |
public void setUserInput(AppCompatEditText userInput) { | |
this.userInput = userInput; | |
} | |
public void setContext(Context context) { | |
this.context = context; | |
} | |
public int getStartIconDrawable() { | |
return startIconDrawable; | |
} | |
public void setStartIconDrawable(int startIconDrawable) { | |
this.startIconDrawable = startIconDrawable; | |
} | |
public int getEndIconDrawable() { | |
return endIconDrawable; | |
} | |
public void setEndIconDrawable(int endIconDrawable) { | |
this.endIconDrawable = endIconDrawable; | |
} | |
public String getUserText() { | |
return userText; | |
} | |
public void setUserText(String userText) { | |
this.userText = userText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment