I am trying to let users select their own icon for a task. It will basically like a notes and task application with some RPG elements like EXP and Gold. For the create task activity I made the first screenshot as a layout:enter image description here
When clicking on the choose Icon button one is taken to another activity with a dynamically populated grid view which looks like this: enter image description here
I have 2 issues at the moment, the first being that when I click on an Item the scene immediately changes back to the create activity scene without me starting that activity or finishing the other activity at any point in my on item listener.
The second one is that the icon of the first image button remains the same eventhough I change it's source attribute in my code. It should be changed to the selected Icon and be displayed instead of the generic icon search when returning to the create task activity. I am very confused as to why this happens so i really need help what follows is the code I use:
This is the code for adding the grid items and changing the image buttons src
package com.example.rpg_app;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.example.rpg_app.databinding.ActivityMainBinding;
public class ChooseIconActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_icon);
int[] iconIds = {
R.drawable.image_0, R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4,
R.drawable.image_5, R.drawable.image_6, R.drawable.image_7, R.drawable.image_8, R.drawable.image_9,
R.drawable.image_10, R.drawable.image_11, R.drawable.image_12, R.drawable.image_13, R.drawable.image_14,
R.drawable.image_15, R.drawable.image_16, R.drawable.image_17, R.drawable.image_18, R.drawable.image_19,
R.drawable.image_20, R.drawable.image_21, R.drawable.image_22, R.drawable.image_23, R.drawable.image_24,
R.drawable.image_25, R.drawable.image_26, R.drawable.image_27, R.drawable.image_28, R.drawable.image_29,
R.drawable.image_30, R.drawable.image_31, R.drawable.image_32, R.drawable.image_33, R.drawable.image_34,
R.drawable.image_35, R.drawable.image_36, R.drawable.image_37, R.drawable.image_38, R.drawable.image_39,
R.drawable.image_40, R.drawable.image_41, R.drawable.image_42, R.drawable.image_43, R.drawable.image_44,
R.drawable.image_45, R.drawable.image_46, R.drawable.image_47, R.drawable.image_48, R.drawable.image_49,
R.drawable.image_50, R.drawable.image_51, R.drawable.image_52, R.drawable.image_53, R.drawable.image_54,
R.drawable.image_55, R.drawable.image_56, R.drawable.image_57, R.drawable.image_58, R.drawable.image_59,
R.drawable.image_60, R.drawable.image_61, R.drawable.image_62, R.drawable.image_63, R.drawable.image_64,
R.drawable.image_65, R.drawable.image_66, R.drawable.image_67, R.drawable.image_68, R.drawable.image_69,
R.drawable.image_70, R.drawable.image_71, R.drawable.image_72, R.drawable.image_73, R.drawable.image_74,
R.drawable.image_75, R.drawable.image_76, R.drawable.image_77, R.drawable.image_78, R.drawable.image_79,
R.drawable.image_80, R.drawable.image_81
};
GridView gridView = findViewById(R.id.gridView);
GridAdapter gridAdapter = new GridAdapter(ChooseIconActivity.this, iconIds);
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ImageButton imageButton = findViewById(R.id.iconBtn);
imageButton.setImageResource(iconIds[i]);
}
});
}
This is the code from create task activity:
package com.example.rpg_app;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class CreateTaskActivity extends AppCompatActivity {
ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_task_2);
imageButton = findViewById(R.id.iconBtn);
imageButton.setOnClickListener(v-> chooseIcon());
}
void chooseIcon(){
startActivity(new Intent(CreateTaskActivity.this, ChooseIconActivity.class));
}
}
I had first implemented the logic to change the activity. After deleting it it still changed activities for some reason. I also tried to use the input to change the background color of the selected item which of course didnt work since the activity keeps chaging