Button stopped generating View - android

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

Related

Edittext Move Next to Item of Recyclerview

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.

How to get input multiple time from a text in android?

How can we embed an input text with a TextView in Android?
For example, I want to implement this:
"Your Name is ___ , This is the Level ____"
The "___" is the blank space which the user must fill with a specified input and I'll check it for validation.
Please note that it is different from android:hint.
How can I implement this?
You could make a fill in the blanks type format using constant text inside EditText. Do this by creating an EditText in XML.
<EditText
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
In your Java class add the following code:
final EditText edittext = (EditText) findViewById(R.id.editText);
edittext.setText("Something ");
Selection.setSelection(edittext.getText(), edittext.getText().length());
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 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 afterTextChanged(Editable s) { if(!s.toString().contains("Something ")){
edittext.setText("Something ");
Selection.setSelection(edittext.getText(), edittext.getText().length());
}
}
});
This is output of the code. Here the string Something is static and cannot be removed.
You could hide the underline in Edittext using android:background="#android:color/transparent". I would also recommend you to disable enter in the EditText by using the code:
<EditText
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:maxLines="1" <----- add this
android:inputType="text"/> <------ and this to disable enter in edittext
You can try like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textSize="16sp"
android:textColor="#000000"
android:text="Your Name is"
android:id="#+id/textview1" />
<EditText
android:layout_width="60dp"
android:textSize="16sp"
android:layout_height="40dp"
android:textColor="#000000"
android:id="#+id/edittext1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textSize="16sp"
android:textColor="#000000"
android:text=", This is the Level"
android:id="#+id/textview2" />
<EditText
android:layout_width="60dp"
android:textSize="16sp"
android:layout_height="40dp"
android:textColor="#000000"
android:id="#+id/edittext2"/>
</LinearLayout>

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.

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!

Categories

Resources