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 !
Related
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.
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"
I already found the question here , but it didn't really work for me. I have a currency converter with two edit texts which convert from INR to USD and vise versa. I need to use DataBinding to listen to the changes of each Edittext, and update the currency value of the next EdittText
Hers my code
public class BindingViewModel extends BaseObservable {
public ObservableField<Double> inr = new ObservableField<>();
public ObservableField<Double> dollar = new ObservableField<>();
public void onINRTextChanged(CharSequence s, int start, int before, int count) {
double aDouble = Double.parseDouble(s.toString());
dollar.set(aDouble * 0.014);
}
public void onDOLLARTextChanged(CharSequence s, int start, int before, int count) {
Log.d("tag", "onTextChanged " + s);
double aDouble = Double.parseDouble(s.toString());
inr.set(71.92 * aDouble);
}
}
And XML
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="vm"
type="android.com.tasks.BindinfViewModel" />
</data>
<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=".MainActivity">
<EditText
android:id="#+id/inr"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="number"
android:onTextChanged="#{vm.onDOLLARTextChanged}"
android:text="#{String.valueOf(vm.inr)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/dollar"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="number"
android:onTextChanged="#{vm.onINRTextChanged}"
android:text="#{String.valueOf(vm.dollar)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/inr"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="INR"
app:layout_constraintEnd_toEndOf="#+id/inr"
app:layout_constraintStart_toStartOf="#+id/inr"
app:layout_constraintTop_toBottomOf="#+id/inr" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="DOLLAR"
app:layout_constraintEnd_toEndOf="#+id/dollar"
app:layout_constraintStart_toStartOf="#+id/dollar"
app:layout_constraintTop_toBottomOf="#+id/dollar" />
</android.support.constraint.ConstraintLayout>
</layout>
Here, whenever I enter something in any of the EditTexts, both are going to an infinite loop since both text change listeners continuously triggering on each value update. I can solve this issue by manually adding a TextWatcher to each of the EditTexts and temporarily making textChangelistener as null just before setting the value to avoid infinite looping. Is there any other efficient way using databinding to do this?
I suggest you use two-way binding. you can write setter and getter for your fields like below and then use android:text="#={fieldName}" instead of android:text="#{fieldName}".
your Model class will maybe like this:
public class BindingViewModel extends BaseObservable {
public Double inr = 0.0;
public Double dollar = 0.0;
#Bindable
public String getInrString() {
String inrString = String.valueOf(inr);
if (inrString.endsWith(".0"))
return inrString.substring(0, inrString.length() - 2);
return inrString;
}
public void setInrString(String inrString) {
if (inrString != null && !inrString.isEmpty()) {
double newInr = Double.parseDouble(inrString);
if (this.inr != newInr) {
this.inr = newInr;
dollar = inr * 0.014;
}
} else {
inr = 0.0;
dollar = 0.0;
}
notifyPropertyChanged(BR.dollarString);
}
#Bindable
public String getDollarString() {
String dollarString = String.valueOf(dollar);
if (dollarString.endsWith(".0"))
return dollarString.substring(0, dollarString.length() - 2);
return dollarString;
}
public void setDollarString(String dollarString) {
if (dollarString != null && !dollarString.isEmpty()) {
double newDollar = Double.parseDouble(dollarString);
if (this.dollar != newDollar) {
this.dollar = newDollar;
inr = dollar * 71.92;
}
} else {
inr = 0.0;
dollar = 0.0;
}
notifyPropertyChanged(BR.inrString);
}
}
and you Layout XML:
<layout>
<data>
<variable
name="vm"
type="com.BindingViewModel" />
</data>
<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=".MainActivity">
<EditText
android:id="#+id/inr"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="number"
android:text="#={vm.inrString}"
android:selectAllOnFocus="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.17"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/dollar"
android:layout_width="108dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="number"
android:text="#={vm.dollarString}"
android:selectAllOnFocus="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/inr"
app:layout_constraintTop_toTopOf="parent" />
...
and ofcourse if your activity you need to set your viewModel:
BindingViewModel viewModel=new BindingViewModel();
binding.setVm(viewModel);
I want to make a ListView, when you long click on an item, the item will be added to the buttons: delete and update (only the clicked item):
So I have made a ListView in my MainLayout:
<ListView
android:id="#+id/neighborhood"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/allAddresses"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/AddresSearch" />
and a Custom ListView Item lay out, which contains 3 TextViews:
<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:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".passItemView">
<TextView
android:id="#+id/num"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="28dp"
android:text="cv"
app:layout_constraintEnd_toStartOf="#+id/house"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/house"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="cvc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pass"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="cv"
app:layout_constraintEnd_toStartOf="#+id/num"
app:layout_constraintTop_toTopOf="parent" />
and this layout is conneted to a class "passItemView", and there, what I did is that on longClick on the layout it will add 2 button (or even just will make a toast)
But it does nothing.
public class passItemView extends AppCompatActivity {
Button delete = new Button(this);
Button update = new Button(this);
ConstraintLayout btnLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass_item_view);
btnLayout = (ConstraintLayout) findViewById(R.id.button_layout);
btnLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(passItemView.this,"Toast it",Toast.LENGTH_LONG).show();
btnLayout.addView((delete));
btnLayout.addView((update));
return false;
}
});
Thank you for your Help.
use an boolean value (ex : showButtons) and initialize set it false for all items.
when you clicked someitem make items showButtons variable true and notifydatasetchange (reload lsitview) again.
I have an android application when clicked on an option from a side bar it goes to a fragment, and then into another fragment which has clickable radio buttons. When clicked on these it will create a popup window with some text fields in it.
Basically this is how the flow goes,
Activity --> Fragment 1 --> Fragment 2 --> PopupWindow
When i try to access the resources inside this popupWindow, in example:
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
in the line damageComponenetAutoCompleteTextview.requestFocus(); it gives me an error,
Attempt to invoke virtual method on a null object reference
I believe it's due to a wrong view i'm referring when calling on the Resources. Hope someone could point me to what i'm doing wrong. Thanks in Advance.
This is the method i'm using to create the popupWindow.
public void showDamagedItemEntryPopup(RadioButton radioButton, View view){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewG = ((ViewGroup)view.findViewById(R.id.damaged_comp_popup));
//I tried passing the root view to inflate() method instead of passing it null. Didn't work.
//View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.popupAnimation);
//I tried setting the content view manually but didnt work
//popupWindow.setContentView(view.findViewById(R.id.damaged_comp_popup));
Button buttonClose = (Button)popupView.findViewById(R.id.close_add_component_btn);
// Close button damaged item popop window
buttonClose.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
originalAmount = (EditText)this.findViewById(R.id.popup_add_component_original_amount);
customerContribution = (EditText)this.findViewById(R.id.popup_percentage);
quantity = (EditText)this.findViewById(R.id.popup_quantity);
finalAmount = (EditText)this.findViewById(R.id.popup_add_component_final_amount);
remarks = (EditText)this.findViewById(R.id.popup_add_component_remarks);
// Item Spinner
itemSpinnerArray = new ArrayList<String>();
itemSpinnerArray.add("Select Item");
// Status Spinner
ArrayList<String> statusSpinnerArray = new ArrayList<String>();
statusSpinnerArray.add("MR");
statusSpinnerArray.add("DR");
statusSpinnerArray.add("SP");
// Error comes at this point initially. When these Resource access line codes are commented, the popup works fine.
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
ArrayAdapter<String> itemSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itemSpinnerArray);
damageComponenetAutoCompleteTextview.setThreshold(1);
damageComponenetAutoCompleteTextview.setAdapter(itemSpinnerArrayAdapter);
damageComponenetAutoCompleteTextview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//int index = cityNames.indexOf(actvCity.getText().toString());
itemSpinnerValue = (String) parent.getItemAtPosition(position);
Log.d("SK-->", "----------------------------------------------------------");
Log.d("SK-->","itemSpinnerValue: " + itemSpinnerValue);
}
});
statusSpinner = (Spinner)this.findViewById(R.id.popup_status_spinner);
ArrayAdapter<String> statusSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, statusSpinnerArray);
statusSpinner.setAdapter(statusSpinnerArrayAdapter);
//Creating a text Watcher
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
//String answerString = originalAmount.getText().toString();
if (originalAmount.getText().toString().trim().equals("") || customerContribution.getText().toString().trim().equals("")
|| quantity.getText().toString().trim().equals("")) {
// Error , one or more editText are empty
}
else
{
calculateFinalAmount();
}
//and now we make a Toast
//modify "yourActivity.this" with your activity name .this
//Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();
}
};
// Adding Text Watcher to our text boxes
originalAmount.addTextChangedListener(textWatcher);
customerContribution.addTextChangedListener(textWatcher);
quantity.addTextChangedListener(textWatcher);
// Show the popup
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
This is the XML i'm using for the popup.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/damaged_comp_popup"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/popup_wire">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="2dp"
android:background="#color/popup_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/popup_damage_component_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:hint="#string/damaged_componenet_item_string"/>
<Spinner
android:id="#+id/popup_status_spinner"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_damage_component_item"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp" />
<EditText
android:id="#+id/popup_add_component_original_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_damage_component_item"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:layout_toRightOf="#+id/popup_status_spinner"
android:ems="10"
android:hint="#string/original_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_percentage"
android:layout_width="52dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_status_spinner"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/percentage_string"
android:inputType="number" />
<TextView
android:id="#+id/popup_percentageMark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_toRightOf="#+id/popup_percentage"
android:text="#string/percentage_string"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="1dp"
android:layout_marginRight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<EditText
android:id="#+id/popup_quantity"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_percentage"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_percentageMark"
android:ems="10"
android:hint="#string/quantity_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_final_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_quantity"
android:layout_alignBottom="#+id/popup_quantity"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_quantity"
android:ems="10"
android:hint="#string/final_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_remarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/popup_percentage"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/remarks_string"
android:inputType="text|textMultiLine" />
<Button
android:id="#+id/add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_below="#+id/popup_add_component_remarks"
android:background="#drawable/correct"
android:layout_marginBottom="15dp"
android:onClick="onSaveItem"/>
<Button
android:id="#+id/close_add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_add_component_remarks"
android:layout_toRightOf="#+id/add_component_btn"
android:layout_marginLeft="10dp"
android:background="#drawable/cancel"/>
</RelativeLayout>
</LinearLayout>
It seems your damageComponenetAutoCompleteTextview belongs to the popupWindow which you inflated at top.
So try changing
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
To
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) popupView.findViewById(R.id.popup_damage_component_item);
And other relevant elements as well.