There 2 ways to handle input events from user input
- Immediate: using listener
- At process: at the end to process data, usually user fire "process" button
Button
@Override protected void onCreate(Bundle savedInstanceState) {... // to process AppCompatButton buttonTest = findViewById(R.id.button_test); // change to your button id from layout buttonTest.setOnClickListener(myTest); } View.OnClickListener myButton = new View.OnClickListener() { @Override public void onClick(View v) { // todo } };
TimePicker
1. Immediate
...
//on view created
// date picker
TimePicker myTimePicker = view.findViewById(R.id.my_time_picker);
myTimePicker.setOnTimeChangedListener(timeChangeListener);
...
// TimePicker Listeneer
private final TimePicker.OnTimeChangedListener timeChangeListener =
new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO
}
};
2. At process
// Pull the values exactly as they are right now
int hour = timePicker.getHour();
int minute = timePicker.getMinute();
RadioGroup
1. Immediate
...
//on view created
// date picker
RadioGroup radioSelectModeBackup = view.findViewById(R.id.radio_select_mode_backup);
radioSelectModeBackup.setOnCheckedChangeListener(radioListener);
...
// RadioGroup Listener
RadioGroup.OnCheckedChangeListener radioListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(@NonNull RadioGroup radioGroup, int idSelected) {
if (idSelected==R.id.radio_weekly) {
// weekly
} else if (idSelected==R.id.radio_moonthly) {
// monthly
}
}
};
2. At process
// Inside your "Save" or "Submit" button click listener
int selectedId = radioSelectModeBackup.getCheckedRadioButtonId();
if (selectedId == R.id.radio_weekly) {
// Logic for weekly backup
} else if (selectedId == R.id.radio_moonthly) {
// Logic for monthly backup
} else {
// Nothing is selected (returns -1 if no default is set in XML)
}
RecyclerView
RecyclerView layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:background="#FFFFFF"
android:gravity="center_vertical">
<HorizontalScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scrollbars="none"
android:fillViewport="true">
<TextView
android:id="@+id/my_item_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textSize="20sp"
android:maxLines="1"
android:textColor="#333333" />
</HorizontalScrollView>
<ImageButton
android:id="@+id/btn_add_phrase"
android:layout_width="64dp"
android:layout_height="match_parent"
android:src="@android:drawable/ic_input_add"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="Add phrase"
android:scaleType="centerInside"
app:tint="#4CAF50" />
</LinearLayout>
RecyclerView Adapter and Holder
public class MyRecyclerView extends RecyclerView.Adapter<MyRecyclerView.ViewHolder> {
String[] stringsTmp = {"Hello", "Test", "How are you?", "Help me write", "The quick brown fox jumps over the lazy dog\n The quick brown fox jumps over the lazy dog"};
OnMyItemClickListener onMyItemClickListener;
public MyRecyclerView(OnMyItemClickListener listener) {
// Use the parameter 'listener', not the uninitialized global variable
this.onMyItemClickListener = listener;
}
@NonNull
@Override
public MyRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_view_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerView.ViewHolder holder, int position) {
// Get the string from your array and put it in the TextView
String text = stringsTmp[position];
holder.textView.setText(text);
// Add a click listener for the item NOT WORKINGN HERE
// Set the click here. It can see 'onMyItemClickListener' and 'text'
holder.addButton.setOnClickListener(v -> {
Log.e("dedetok", "onBindViewHolder Clicked : " + text);
if (onMyItemClickListener != null) {
onMyItemClickListener.getString(text);
}
});
}
@Override
public int getItemCount() {
return stringsTmp.length;
}
/*
* View Holder
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ImageButton addButton; // Changed from Button
public ViewHolder(View view) {
super(view);
// Matches the ID in your item_phrase.xml
textView = view.findViewById(R.id.my_item_view);
addButton = view.findViewById(R.id.btn_add_phrase);
}
}
public interface OnMyItemClickListener {
void getString(String stringSelect);
}
}
If you use action listener on item in holder, DO NOT add onItemSelected event into adapter or any else touch event. They will conflict with other event inside the items.
You need to implement interface MyRecyclerView.OnMyItemClickListener on your activity or fragment.
You can remove all event on holder if you prefered to use onItemSelected event.