Edittext Move Next to Item of Recyclerview - android

I created a recycler view in which one is text view and other is edit text where data is added at runtime so I want to do that when I click of a keyboard next button then it moves to next edit text item issue is when I do it move to text view instead of edit text and so on... as shown in below image. what can I do that this is going through as my requirement? I tried all answers of s.o.f but failed.
here is my adapter code where the item is shown.
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
StudentMarks subject = subjectList.get(position);
holder.subjectName.setText(subject.getStudentsName());
holder.subjectId.setText(Collections.singletonList(subject.getStudentsId()).toString());
holder.subjectMarks.setText(subject.getStudentMarks());
if (!idStudent.contains(subject.getStudentsId())) {
idStudent.add(subject.getStudentsId());}
holder.subjectMarks.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) {
subjectList.get(position).setStudentMarks(holder.subjectMarks.getText().toString());
try {
String st_id = subjectList.get(position).getStudentsId();
for (int i = 0; i < id.size(); i++) {
if (id.get(i).equals(st_id)) {
id.remove(subjectList.get(position).getStudentsId());
value.remove(i);
}}
id.add(subjectList.get(position).getStudentsId());
value.add(holder.subjectMarks.getText().toString().trim());
} catch (Exception e) {
e.printStackTrace();
}
if (idStudent.toArray().length != id.toArray().length) {
listListener.onArrayListListener(id, value, position, "Please Enter Student Marks First");
} else {
listListener.onArrayListListener(id, value, position, "Student Marks Submitted");
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
holder.setIsRecyclable(false);
}
here is xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/add_student_category"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="#dimen/_180sdp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_8sdp"
android:layout_marginBottom="#dimen/_8sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/add_marks_category"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/add_student_List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:hint="#string/student_name"
android:inputType="none"
android:textColor="#color/greenish_shade"
android:textSize="#dimen/_12sdp"
tools:ignore="Deprecated,LabelFor" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/add_marks_category"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="#dimen/_80sdp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#id/add_student_category"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/add_student_category"
app:layout_constraintTop_toTopOf="#id/add_student_category">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/add_exam_marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/obtain_marks"
android:inputType="number"
android:imeOptions="actionNext"
android:focusable="true"
android:focusableInTouchMode="true"
android:textAlignment="center"
android:textColor="#color/greenish_shade"
android:textColorHint="#color/greenish_shade"
android:textSize="#dimen/_12sdp"
tools:ignore="LabelFor" />
</com.google.android.material.textfield.TextInputLayout>
<View
android:id="#+id/view"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_marginStart="#dimen/_8sdp"
android:layout_marginEnd="#dimen/_8sdp"
android:background="#color/light_black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/add_student_category" />
<TextView
android:id="#+id/student_idss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

use android:inputType="text" in editText in xml file
if you are adding dynamic edittexts then you can do it like this
editText.setInputType(InputType.TYPE_CLASS_TEXT);
refere this site where you can found various input types according to your need like for password(this will hide the typed text), numbers(numbered keyboard),etc

you can add android:maxLines="1" & android:inputType="text" to your EditText. It will surely work.

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

Return cursor to starting position

I want to be able to scan a tag and then populate this data on the listview. I have managed to do this successfully.
However I do not want to keep tapping the editText in order to return the cursor back. The scanning is setup in emulator mode, so when you scan the tag the tag's info gets populated where ever the cursor is.
The problem I am having is once I scan the tag, the cursor does not return back to the editText automatically which means when I want to read another tag I have to keep presses the editText in order for the cursor to return to this place.
The listview gets the data from the editText. The code is shown below with pics of what I have now. From the pics you see that the cursor fails to go back to the editText automatically.
public class MainActivity extends AppCompatActivity {
EditText etRfidNo;
TextView textView;
private Set<String> epc = new HashSet<>();
ArrayAdapter<String> contactAdapter;
String single_epc;
Button scan;
ListView listView;
boolean set = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
etRfidNo = (EditText) findViewById(R.id.etRfidNo);
listView = (ListView) findViewById(R.id.listviewID);
TextView textV = (TextView)findViewById(R.id.textView);
etRfidNo.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) {
single_epc = String.valueOf(s);
if(s.length() == 10)
{
Date currentDate = new Date();
epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));
display();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
public void display() {
contactAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<>(epc));
listView.setAdapter(contactAdapter);
}
}
Starting the application
Data populated on listview
Xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.name.myapplication.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="201dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:cursorVisible="true"
android:text="TextView"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.83" />
<EditText
android:id="#+id/etRfidNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contextClickable="false"
android:ems="10"
android:inputType="text"
android:selectAllOnFocus="false"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.017" />
<Button
android:id="#+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:onClick="scan"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/listviewID"
android:layout_width="368dp"
android:layout_height="282dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276"
tools:layout_editor_absoluteX="8dp" />
</android.support.constraint.ConstraintLayout>
You need to put focus on your editText View after scanning of each tag, so cursor will be shown without user input
editText.requestFocus();
Code as follows -
public void onTextChanged(CharSequence s, int start, int before, int count) {
single_epc = String.valueOf(s);
if(s.length() == 10)
{
Date currentDate = new Date();
epc.add("\n" + etRfidNo.getText().toString() + ", " + DateFormat.getInstance().format(currentDate));
display();
etRfidNo.requestFocus(); //Putting focus back
}
}
Edit (Fix Issue with XML)
There are issues with your xml file. TextView is getting overlapped by EditText which is further overlapped by ListView and there is no horizontal constraint set on your ListView as well. I will suggest you to go through Constraint Layout Guide to get more info.
So in simple word, when you are attaching adapter to your ListView, it is getting focus atomically because then ListView become the first child of View Hierarchy in your xml. I have updated your layout to use simple linear layout, code as follows -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="201dp"
android:layout_height="53dp"
android:layout_margin="8dp"
android:text="TextView"
android:textAlignment="center"/>
<EditText
android:id="#+id/etRfidNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:contextClickable="false"
android:ems="10"
android:inputType="text"
android:selectAllOnFocus="false"
android:textAlignment="center"/>
<Button
android:id="#+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="scan"
android:text="Button"/>
<ListView
android:id="#+id/listviewID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
</LinearLayout>
Above xml result as follows -
Happy Coding !

android recyclerview going through my edit text search filter

I have a RecyclerView inside a fragment with a search filter edit text above it, and when I scroll the recyclerview go through the edit text, but I want the RecyclerView always to be under the edit text and leave the edit text on top like a header or something like that no matter how much I scroll. How can I achieve that?
code:
the layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
tools:context=".fragments.FoodListFragment">
<com.steelkiwi.library.view.BadgeHolderLayout
android:id="#+id/badge"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:elevation="100dp"
app:bhl_badge_radius="10dp"
app:bhl_default_badge_background="#color/five"
app:bhl_text_color="#android:color/white"
app:bhl_text_size="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/capsule_shopping_cart"
android:elevation="10dp"
app:srcCompat="#drawable/white_shopping_cart" />
</com.steelkiwi.library.view.BadgeHolderLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview_foodlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input" />
<EditText
android:id="#+id/food_list_search_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/search_input_style"
android:drawableStart="#drawable/search_icon"
android:drawablePadding="8dp"
android:ems="10"
android:hint="search"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
the class
public class FoodListFragment extends Fragment {
EditText etSearchInput;
private RecyclerView recyclerView;
private FoodListAdapter foodListAdapter;
private BadgeHolderLayout badgeHolderLayout;
private List<Food> foods;
public interface ListenerFoodListFragment {
void sendToCart(int value, int adapterPosition);
void cartClickListener();
}
private ListenerFoodListFragment listenerFoodListFragment;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listenerFoodListFragment = (ListenerFoodListFragment) context;
} catch (ClassCastException e) {
Log.d("FoodListFragment", "onAttach: " + e.getMessage());
}
}
public FoodListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_food_list, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.recyclerview_foodlist);
badgeHolderLayout = view.findViewById(R.id.badge);
etSearchInput = view.findViewById(R.id.food_list_search_input);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
foods = new ArrayList<>();
foods.add(new Food("Chicken", "99999$", R.drawable.chicken));
badgeHolderLayout.setOnClickListener(v -> listenerFoodListFragment.cartClickListener());
foodListAdapter = new FoodListAdapter(getActivity(), foods, listenerFoodListFragment);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(foodListAdapter);
etSearchInput.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) {
foodListAdapter.getFilter().filter(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
public void updateCartValue(int value) {
badgeHolderLayout.setCountWithAnimation(value);
}
}
Remove this android:layout_marginTop="?attr/actionBarSize" from your Recyclerview
and then Set android:layout_height of your RecyclerView to 0dp will works.
Further more add app:layout_constraintBottom_toTopOf="#id/recyclerview_foodlist" in search EditText or it may be due to background drawable file you added in search EditText
You are using a ConstraintLayout.
match_parent will ignore your constraints.
You need to replace the layout_height="match_parent" of the RecyclerView with layout_height="0dp". Same for its width.
Then it will be underneath the EditText as you expect.
i fixed it by changing my layout to this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview_foodlist"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input" />
<EditText
android:id="#+id/food_list_search_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/search_input_style"
android:drawableStart="#drawable/search_icon"
android:drawablePadding="8dp"
android:ems="10"
android:hint="search"
android:inputType="textPersonName"
android:textColor="#fff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.steelkiwi.library.view.BadgeHolderLayout
android:id="#+id/badge"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:elevation="100dp"
app:bhl_badge_radius="10dp"
app:bhl_default_badge_background="#color/five"
app:bhl_text_color="#android:color/white"
app:bhl_text_size="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/capsule_shopping_cart"
android:elevation="10dp"
app:srcCompat="#drawable/white_shopping_cart" />
</com.steelkiwi.library.view.BadgeHolderLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Change your recyclerview height to wrap_content
android:layout_height="wrap_content"
And remove all this code in recyclerview
android:layout_marginTop="?attr/actionBarSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input"
At last paste this two line in recylerview
app:layout_constraintEnd_toEndOf="#id/food_list_search_input"
app:layout_constraintTop_toBottomOf="#+id/food_list_search_input"

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!

Make character count visible during input

I have an EditText used for a description.
Below it, I have a TextView showing the number of characters inputted in the EditText.
Example:
The user should be able to see the live character count during the input, but at this moment the characted counter is hidden by the keyboard:
What can I do to correct that ?
Here is my xml code:
<EditText
android:id="#+id/MyDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:maxLength="500"
android:hint="Description" />
<TextView
android:id="#+id/MyDescriptionCharCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0/500"
android:layout_below="#id/MyDescription"
android:layout_marginTop="5dp"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
The parent is a RelativeLayout.
You have to extend the class with TextWatcher and override afterTextChanged(),beforeTextChanged(), onTextChanged().
EditText MyDescription = (EditText)findViewById(R.id. MyDescription);
MyDescription.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s.toString().trim().length();
}
});
You can try the following layout.
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:background="#android:color/transparent"
android:inputType="textNoSuggestions|textMultiLine"
android:maxLength="200"
android:paddingBottom="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:textColor="#202020"
android:textColorHint="#979797"
android:textSize="14sp"/>
<TextView
android:id="#+id/charecter_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/add_comment_edit_text"
android:layout_gravity="bottom"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:gravity="bottom"
android:text="0/200"
android:textColor="#979797"
android:textSize="14sp"/>
</RelativeLayout>
To see Textview of character counter, add below line in activity tag of manifest file.
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
This will automatically move your activity layout up to fix in available height after adding soft input keyboard.For more detail about this check here.

Categories

Resources