How to padding bottom of the customize EditText character? - android

I have a customize EditText character with some simple character and image.
Look like this image :
But i want the image padding the bottom like this image :
This is my pwd_bullet.png :
This is my code :
custom_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b5555"
tools:context="com.example.user.myapplication.Custom_Edittext">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/editText"
android:inputType="text"
android:gravity="center_vertical"
android:background="#null"/>
</LinearLayout>
</RelativeLayout>
Custom_Edittext.java
public class Custom_Edittext extends Activity implements TextWatcher{
EditText editText;
Spannable.Factory spannableFactory;
int lastIndex = -1;
protected void onCreate(Bundle savedInstanceState) {
enter code here`super.onCreate(savedInstanceState);
setContentView(R.layout.custom_edittext);
editText = (EditText) findViewById(R.id.editText);
spannableFactory = Spannable.Factory
.getInstance();
editText.addTextChangedListener(this);
}
public Spannable getIconText(Context context, CharSequence text, int index) {
Spannable spannable = spannableFactory.newSpannable(text);
if (index>lastIndex) {
spannable.setSpan(new ImageSpan(context, R.drawable.pwd_bullet),
index, index + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
lastIndex=index;
return spannable;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (start>4) {
editText.removeTextChangedListener(this);
editText.setText(getIconText(getApplicationContext(), s, start));
editText.addTextChangedListener(this);
editText.setSelection(s.length());
}
}
public void afterTextChanged(Editable s) {}
}

There is no need to use padding in this case, Just use a Linear layout since your items are in a straight line, and make sure you set the Linear layout to an orientation and the gravity as below. This worked for me. Good luck
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="#+id/editText"
android:inputType="text"
android:gravity="center_vertical"
android:background="#null"
android:text="Simple Text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pwd_bullet.png"/>
</LinearLayout>

Create an xml to custom pwd_bullet in drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/layer_list"
android:drawable="#drawable/pwd_bullet"
android:width="10dp"
android:height="10dp"
android:left="3dp"
android:bottom="5dp"/>
</layer-list>

Related

Button stopped generating View

I'm on a working on a project on Android Studio. Then I would like to implement a button that generate a view above him. And store it's text data in an ArrayList.
This is the java code:
addClassBtn.setOnClickListener((v)->{
View cricketerView = getLayoutInflater().inflate(R.layout.check_box,categoryList,false);
EditText checkBox_title = (EditText)cricketerView.findViewById(R.id.checkBox_title);
switch (appPreference.getInt("lang",0)){
case 0:checkBox_title.setHint(getResources().getString(R.string.en_en_editBox_Text));break;
case 1:checkBox_title.setHint(getResources().getString(R.string.ch_tw_editBox_Text));break;
default:checkBox_title.setHint("Error");
}
temporaryClass.add(checkBox_title.getText().toString());
checkBox_title.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
temporaryClass.remove(checkBox_title.getText().toString());
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
temporaryClass.add(checkBox_title.getText().toString());
}
});
CheckBox checkBox_check = (CheckBox)cricketerView.findViewById(R.id.checkBox_check);
checkBox_check.setOnClickListener((w)->{
});
Button checkBox_delete = (Button)cricketerView.findViewById(R.id.checkBox_delete);
checkBox_delete.setOnClickListener((w)->{
categoryList.removeView(w);
});
categoryList.addView(cricketerView);
});
And this is the XML Layout code of the view that I would like to add above the button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/checkBox_title"
android:layout_width="295dp"
android:layout_height="40dp"
android:layout_marginStart="13dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:autofillHints="#string/ch_tw_editBox_Text"
android:background="#drawable/rounded_edittext"
android:hint="#string/ch_tw_editBox_Text"
android:inputType="text"
android:paddingStart="13dp"
android:paddingEnd="13dp"
android:selectAllOnFocus="false"
android:textSize="22sp" />
<CheckBox
android:id="#+id/checkBox_check"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:text=""
android:textColorLink="#000000" />
<Button
android:id="#+id/checkBox_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_delete"
android:layout_marginStart="5dp"
android:layout_marginEnd="13dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
Then when I try it, it only generate the view one time and I can't generate more view. I can't even delete the view that was generated.
TestCase:
I already used the code above on an other activity, and I work really good.
Thank you

How to add icon hint Edittext in center

How to add icon hint edittext in center
I have no idea to set that
and in xml file is not have android:drawbleCenter
How to create it
thanks for your help ;)
Unfortunately you can not set drawable at the center of the EditText like this, Alternatively, you can use background image which includes your hint + image what ever you display and inside,
Try below example.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#android:drawable/editbox_background"
android:gravity="center">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:drawableLeft="#android:drawable/ic_menu_search"
android:gravity="center"
android:hint="Search" />
</RelativeLayout>
You can achieve this type of view using above XML code
Add android:gravity="center_horizontal" to EditText
I have a solution
This is xml:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<EditText
android:id="#+id/edt_search"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/text_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableLeft="#android:drawable/ic_menu_search"
android:drawablePadding="8dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:text="Search"
android:textColor="#969696" />
</FrameLayout>
in onCreate()
edtSearch.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) {
textIcon.setVisibility(View.GONE);
}
#Override
public void afterTextChanged(Editable s) {
textIcon.setVisibility(s.length() == 0 ? View.VISIBLE : View.GONE);
}
});
Try it!

how to set text and image as a hint in edittext in center?

i want to set image and text as hint in Edit text in center like this
my XML code is
<EditText android:layout_width="300dp" android:layout_height="40dp"
android:id="#+id/ed_username" android:layout_marginBottom="152dp"
android:hint="#string/username"
android:drawableLeft="#drawable/username_image"
android:textColorHint="#color/skyblue"
android:background="#drawable/edittext_shape"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:ellipsize="start" android:gravity="center|center_horizontal"/>
please help me thanks in advance a
You can do via Two Way
1.Create EditText like this.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:hint="Search text"
android:id="#+id/editText"
android:background="#drawable/edit_text_bottom_border"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingLeft="15dp"
android:drawablePadding="10dp"
android:drawableLeft="#android:drawable/ic_search_category_default"/>
and in you layout create this file
edit_text_bottom_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#000" />
<solid android:color="#00ffffff" />
</shape>
</item>
</layer-list>
2. Make RelativeLayout like Below :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/editText" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="#android:drawable/btn_star_big_on"/>
<EditText
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="46dp"
android:hint="hint"
android:layout_alignParentRight="true" />
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="-7dp"
android:layout_below="#+id/text"
android:background="#000"/>
</RelativeLayout>
The Generated Output is :
You can add an image in edittext as drawableleft and remove it while getting focus.
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:drawableLeft="#drawable/yourimage" />
remove it when getting focus
final EditText et=(EditText) findViewById(R.id.editText1);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean gotfocus) {
// TODO Auto-generated method stub
if(gotfocus){
et.setCompoundDrawables(null, null, null, null);
}
else if(!gotfocus){
if(et.getText().length()==0)
et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.yourimage, 0, 0, 0);
}
}
});
You can change the hint color as depicted below inside your java files.
editText.setHintTextColor(getResources().getColor(R.color.Red));
you can set drawlable to the edit text using following property
android:drawableLeft="#drawable/ic_launcher"
android:drawableTop=""
android:drawableBottom=""
android:drawableStart=""
android:drawableEnd=""
android:drawableRight=""
but unfortunately you can not set drawlable at the center of the editText like this ,
Alternatively you can use background image which include your hint + image what ever you display and inside editText.addTextChangedListener you can remove that previously applied backgrund.
You can try to set your hint using spannable, and then you don't have to change your XML or layouts. This is an example with a textView. I never tried that with setHint(), but i think it will work the same way.
TextView tv = (TextView)findViewById(R.id.yourTextView);
SpannableStringBuilder span = new SpannableStringBuilder( " Username" );
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
span.setSpan(new ImageSpan(icon), 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv.setText(span, BufferType.SPANNABLE);
You can do it like this it is very Simple
et.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// loadData(et.getText().toString());
// et.setCompoundDrawablesWithIntrinsicBounds(, 0, 0, 0);
et.setCompoundDrawables(null, null, null, null);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (et.getText().length() == 0)
et.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_search_category_default, 0, 0, 0);
}
public void afterTextChanged(Editable s) {
if (et.getText().length() == 0)
et.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_search_category_default, 0, 0, 0);
}
});
and in Layout XML
<EditText
android:id="#+id/editTextSearch"
style="#android:style/Widget.Holo.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:selectAllOnFocus="false"
android:hint="Search"
android:drawableLeft="#android:drawable/ic_search_category_default"
/>
You can use the EditText itself and make hint with image on any side of the EditText. Refer following for sample:
EditText editText = (EditText) findViewById(R.id.edit_text);
Spannable spannable = new SpannableString(" Mobile Number");
Drawable drawable = getResources().getDrawable(R.drawable.one);
drawable.setBounds(50, 0, 0, 0);
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
spannable.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(spannable);
Note: you can adjust setBounds() over the image according to your preferred direction over the text.

Content from EditText does not appear in TextView.

I am completely new in android but I thought I at lease learnt how to write simple code to have contents from EditText appear in the TextView. But seems like not. Can anyone help? Thank you in advance. Here is my code.
detailsActivity.java
public class DetailsActivity extends ActionBarActivity {
EditText etTop;
TextView tvTop;
String cTop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
//imageUri = Uri.parse(extras.getString("imageUri"));
Bitmap bitmap = getIntent().getParcelableExtra("id");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
etTop = (EditText) findViewById(R.id.etTop);
tvTop = (TextView) findViewById(R.id.tvBottom);
cTop = etTop.getText().toString();
tvTop.setText(cTop);
}
}
details_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#b72828">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
<EditText
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/etTop"
android:hint="Enter Top Text"
android:textColor="#color/white"
android:clickable="true"
android:gravity="center"
android:inputType="textCapWords"
android:maxLines="2"
android:textColorHint="#fdfdfd"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/tvBottom"
android:layout_above="#+id/etBottom"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create"
android:id="#+id/cButton"
android:layout_gravity="center_horizontal|bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="false" />
<EditText
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/etBottom"
android:hint="Enter Bottom Text"
android:textColor="#color/white"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:gravity="center"
android:inputType="textCapWords"
android:maxLines="2"
android:textColorHint="#fdfdfd"
android:textSize="30dp"
android:textStyle="bold"
android:layout_above="#+id/cButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/tvTop"
android:layout_below="#+id/etTop"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Reason:
cTop = etTop.getText().toString();
tvTop.setText(cTop);
gets executed as soon as activity is created. By this time you don't get any chance to enter any text in EditText. When yo do, this code has been already executed. So you need to get the text from EditText and set it into TextView when you actually enter the text.
Solution:
Remove above mentioned two lines of code and add this
etTop.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
tvTop.setText(s.toString());
}
public void beforeTextChanged(CharSequence s, int start,int count, int after) {}
public void onTextChanged(CharSequence s, int start,int before, int count) {}
});
Now every time you will change the text in EditText, TextView will be update automatically.
try doing this:
etTop.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) {
tvTop.setText(s);
}
#Override
public void afterTextChanged(Editable s) {
//.. Or set it here if this is desired behavior
}
});
Your code doesn't listen for changes to the EditText instead your code is saying, go get me the text right now, instead of listening for text being added into the edit text.
So having said that drop these lines:
cTop = etTop.getText().toString();
tvTop.setText(cTop);
and add the above snippet of code in its place.
Good Luck and Happy Coding!

Need to make double hint in EditText with TextInputLayout

Here is the problem with double hint - TextInputLayout and EditText double hint issue
I need to do the same. I need two hints. One is a title of EditText, and second is a possible value if nothing wasn't input.
But unfortunately there is only one tag:
android:hint
Can I do something with it?
UPD1:
The answer was to set hint for EditText programatically and hint for TextInputLayout in xml.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:hint="#string/prompt_quantity"
android:layout_height="wrap_content">
<EditText
android:id="#+id/quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
And
quantityEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
quantityEditText.setHint(hasFocus ? "0" : "");
}
});
With the Material Components library you can use
android:hint="Label" to add a floating label
app:placeholderText="Placeholder Text" to add a placeholder in the EditText.
Use something like:
<com.google.android.material.textfield.TextInputLayout
android:hint="Label"
app:placeholderText="Placeholder Text"
..>
<com.google.android.material.textfield.TextInputEditText />
</com.google.android.material.textfield.TextInputLayout>
Note: it requires at least the version 1.2.0-alpha03.
Just check your input and change the hint depending on that:
EditText editText = (EditText)findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher()
{
#Override
public void afterTextChanged(Editable s)
{
if(editText.getText().toString().equals(""))
editText.setHint("");
else
editText.setHint("Your normal hint");
}
});
Try this way
xml layout
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/non_active_edit_text"
android:focusable="false"
android:gravity="right|center_vertical"
android:hint="Right"
android:paddingTop="4dp"
android:background="#null"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/active_edit_text"
android:hint="Left"
android:layout_gravity="center_horizontal"/>
</merge>
JAVA class
public class TwoHints extends RelativeLayout{
EditText activeField;
EditText nonActiveEditText;
final CharSequence hint;
public TwoHints(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.two_hints_edit_text, this);
activeField = (EditText)findViewById(R.id.active_edit_text);
nonActiveEditText = (EditText)findViewById(R.id.non_active_edit_text);
hint = nonActiveEditText.getHint();
activeField.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) {
nonActiveEditText.setHint(s.length() !=0 ? "" : hint);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
editetext for xml
<you.package.TwoHints
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>

Categories

Resources