So hear me out, I used a button to open an activity which generates an item which will then be returned to the original fragment and will be displayed in the recyclerview... but once I try to add a second item, the ImageButton simply won't let me access the item generating activity again, almost as if the ImageButton was deactivated (I tried using setEnabled and setClickable and it won't fix it), as soon as I use Button instead of ImageButton, it works just fine, but I can't customize it's apperance as I'd like to. Here are some Images and Code:
profile_fragment.java
public class profile_fragment extends Fragment implements ExerciseRecyclerAdapterFinal.OnItemListener, Serializable {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
public ArrayList<ExerciseItemFinal> listExercises = new ArrayList<>();
public RecyclerView rv;
private String mParam1;
private String mParam2;
public ExerciseItemFinal item;
public int contadorDias;
public TextView dateView;
public ImageView rightButton;
public ImageView leftButton;
public ImageButton addButtonCircle;
public String date;
public Calendar calendar;
ActivityResultLauncher<Intent> activityLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 111) {
Intent intent = result.getData();
if (intent != null) {
item = (ExerciseItemFinal) intent.getSerializableExtra("lol");
addItem(item);
updateRecyclerView();
addButtonCircle.setEnabled(true);
}
}
}
}
);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contadorDias = 0;
calendar = Calendar.getInstance();
date = DateFormat.getDateInstance(DateFormat.SHORT).format(calendar.getTime());
View view = inflater.inflate(R.layout.fragment_profile_fragment, container, false);
dateView = (TextView) view.findViewById(R.id.date_text_2);
rightButton = (ImageView) view.findViewById(R.id.right_button_2);
leftButton = (ImageView) view.findViewById(R.id.left_button_2);
addButtonCircle = (ImageButton) view.findViewById(R.id.add_button_circle);
rv = view.findViewById(R.id.recyclerViewExercises123);
dateView.setText("Today");
addButtonCircle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getActivity(), ExerciseCategoryChooser.class);
activityLauncher.launch(i);
}
});
rightButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.DATE, 1);
contadorDias ;
date = DateFormat.getDateInstance(DateFormat.SHORT).format(calendar.getTime());
dateView.setText(setDay(contadorDias, date));
}
});
leftButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.DATE, -1);
contadorDias--;
date = DateFormat.getDateInstance(DateFormat.SHORT).format(calendar.getTime());
dateView.setText(setDay(contadorDias, date));
}
});
return view;
}
private void updateRecyclerView() {
ExerciseRecyclerAdapterFinal adapter = new ExerciseRecyclerAdapterFinal(getContext(), listExercises, this);
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
}
private void addItem(ExerciseItemFinal item) {
listExercises.add(item);
}
private String setDay(int cont, String dateNew) {
if (cont == 0) {
return "Today";
} else if (cont == 1){
return "Tomorrow";
} else if (cont == -1){
return "Yesterday";
} else {
return dateNew;
}
}
@Override
public void onItemListener(int position) {
}
}
ExerciseCategoryChooser.java
public class ExerciseCategoryChooser extends AppCompatActivity implements Serializable, ExerciseRecyclerAdapter.onItemClick, ExerciseRecyclerAdapter1.OnItemListener{
public ImageView backArrow;
public ImageView addButton;
public int selection;
public RecyclerView rv;
public ExerciseItemFinal item;
public ExerciseItem item1;
public CategoryItem item2;
public ArrayList<CategoryItem> categories = new ArrayList<>(); String [] categoriesArray;
public ArrayList<ExerciseItem> absItems = new ArrayList<>(); String [] absArray;
public ArrayList<ExerciseItem> backItems = new ArrayList<>(); String [] backArray;
public ArrayList<ExerciseItem> bicepsItems = new ArrayList<>(); String [] bicepsArray;
public ArrayList<ExerciseItem> cardioItems = new ArrayList<>(); String [] cardioArray;
public ArrayList<ExerciseItem> chestItems = new ArrayList<>(); String [] chestArray;
public ArrayList<ExerciseItem> legsItems = new ArrayList<>(); String [] legsArray;
public ArrayList<ExerciseItem> shouldersItems = new ArrayList<>(); String [] shouldersArray;
public ArrayList<ExerciseItem> tricepsItems = new ArrayList<>(); String [] tricepsArray;
ActivityResultLauncher<Intent> activityLauncher2 = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 011) {
Intent intent = result.getData();
if (intent != null) {
item = (ExerciseItemFinal) intent.getSerializableExtra("code123");
finished();
}
}
}
}
);
private void finished() {
Intent i = new Intent();
i.putExtra("lol", item);
setResult(111, i);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise_category_chooser);
backArrow = findViewById(R.id.arrow_exercises);
addButton = findViewById(R.id.add_button_exercises);
rv = findViewById(R.id.recyclerViewExercise);
categoriesArray = getResources().getStringArray(R.array.categoriesExercises);
absArray = getResources().getStringArray(R.array.absExercises);
backArray = getResources().getStringArray(R.array.backExercises);
bicepsArray = getResources().getStringArray(R.array.bicepsExercises);
cardioArray = getResources().getStringArray(R.array.cardioExercises);
chestArray = getResources().getStringArray(R.array.chestExercises);
legsArray = getResources().getStringArray(R.array.legsExercises);
shouldersArray = getResources().getStringArray(R.array.shouldersExercises);
tricepsArray = getResources().getStringArray(R.array.tricepsExercises);
selection = 0;
Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();
insertAllItems(selection);
ActivityResultLauncher<Intent> activityLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 7171) {
Intent intent = result.getData();
if (intent != null) {
}
}
}
}
);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ExerciseCategoryChooser.this, AddExercise.class);
activityLauncher.launch(intent);
}
});
backArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(892, null);
finish();
}
});
}
public void insertAllItems(int s) {
switch(s) {
case 1:
for (int l = 0; l < absArray.length; l ) {
ExerciseItem i1 = new ExerciseItem(absArray[l], "Back", "Weight and Reps");
absItems.add(i1);
}
ExerciseRecyclerAdapter1 adapter1 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , absItems, this::onItemClick);
rv.setAdapter(adapter1);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
case 2:
for (int l = 0; l < backArray.length; l ) {
ExerciseItem i2 = new ExerciseItem(backArray[l], "Back", "Weight and Reps");
backItems.add(i2);
}
ExerciseRecyclerAdapter1 adapter2 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , backItems, this::onItemClick);
rv.setAdapter(adapter2);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
case 3:
for (int l = 0; l < bicepsArray.length; l ) {
ExerciseItem i3 = new ExerciseItem(bicepsArray[l], "Biceps", "Weight and Reps");
bicepsItems.add(i3);
}
ExerciseRecyclerAdapter1 adapter3 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , bicepsItems, this::onItemClick);
rv.setAdapter(adapter3);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
case 4:
for (int l = 0; l < cardioArray.length; l ) {
ExerciseItem i4 = new ExerciseItem(cardioArray[l], "Cardio", "Weight and Reps");
cardioItems.add(i4);
}
ExerciseRecyclerAdapter1 adapter4 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , cardioItems, this::onItemClick);
rv.setAdapter(adapter4);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
case 5:
for (int l = 0; l < chestArray.length; l ) {
ExerciseItem i5 = new ExerciseItem(chestArray[l], "Chest", "Weight and Reps");
chestItems.add(i5);
}
ExerciseRecyclerAdapter1 adapter5 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , chestItems, this::onItemClick);
rv.setAdapter(adapter5);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
case 6:
for (int l = 0; l < legsArray.length; l ) {
ExerciseItem i6 = new ExerciseItem(legsArray[l], "Legs", "Weight and Reps");
legsItems.add(i6);
}
ExerciseRecyclerAdapter1 adapter6 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , legsItems, this::onItemClick);
rv.setAdapter(adapter6);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
case 7:
for (int l = 0; l < shouldersArray.length; l ) {
ExerciseItem i7 = new ExerciseItem(shouldersArray[l], "Shoulders", "Weight and Reps");
shouldersItems.add(i7);
}
ExerciseRecyclerAdapter1 adapter7 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , shouldersItems, this::onItemClick);
rv.setAdapter(adapter7);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
case 8:
for (int l = 0; l < tricepsArray.length; l ) {
ExerciseItem i8 = new ExerciseItem(tricepsArray[l], "Triceps", "Weight and Reps");
tricepsItems.add(i8);
}
ExerciseRecyclerAdapter1 adapter8 = new ExerciseRecyclerAdapter1(getApplicationContext(), item1 , tricepsItems, this::onItemClick);
rv.setAdapter(adapter8);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
break;
default:
for (int l = 0; l < categoriesArray.length; l ) {
CategoryItem categoryItem = new CategoryItem(categoriesArray[l]);
categories.add(categoryItem);
}
ExerciseRecyclerAdapter adapter = new ExerciseRecyclerAdapter(getApplicationContext(), item2 , categories, this::onItemListener);
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
}
@Override
public void onItemListener(int position) {
switch (position) {
case 0:
selection = 1;
insertAllItems(selection);
break;
case 1:
selection = 2;
insertAllItems(selection);
break;
case 2:
selection = 3;
insertAllItems(selection);
break;
case 3:
selection = 4;
insertAllItems(selection);
break;
case 4:
selection = 5;
insertAllItems(selection);
break;
case 5:
selection = 6;
insertAllItems(selection);
break;
case 6:
selection = 7;
insertAllItems(selection);
break;
case 7:
selection = 8;
insertAllItems(selection);
break;
default:
}
}
@Override
public void onItemClick(int position) {
Intent intent = new Intent(ExerciseCategoryChooser.this, ConfirmAdditionExercise.class);
activityLauncher2.launch(intent);
}
}
fragment_profile_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".profile_fragment"
android:background="@drawable/background_gradient"
android:id="@ id/profile_frag">
<include
android:id="@ id/dateChangerProfile"
layout="@layout/date_changer_2"
android:layout_width="match_parent"
android:layout_height="50dp" />
<ImageButton
android:id="@ id/add_button_circle"
android:layout_width="57.5dp"
android:layout_height="57.5dp"
android:background="@drawable/circular_button_background"
android:src="@drawable/ic_baseline_add_24"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerViewExercises123"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
CodePudding user response:
Basically I switched positions between the recyclerView and the ImageButton, the problem was that when I added an item to the recyclerView, it sort of put the recycler view "in front" of the button, which made me unable to click the ImageButton.
Correct code:
fragment_profile_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".profile_fragment"
android:background="@drawable/background_gradient"
android:id="@ id/profile_frag">
<include
android:id="@ id/dateChangerProfile"
layout="@layout/date_changer_2"
android:layout_width="match_parent"
android:layout_height="50dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerViewExercises123"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp">
</androidx.recyclerview.widget.RecyclerView>
<ImageButton
android:id="@ id/add_button_circle"
android:layout_width="57.5dp"
android:layout_height="57.5dp"
android:background="@drawable/circular_button_background"
android:src="@drawable/ic_baseline_add_24"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
/>
</RelativeLayout>