Reputation: 103
What type of list do I have to use to set the background of ImageViews to variable drawables?
For example:
There are 6 drawables: image1
, image2
, image3
, image4
, image5
, image6
If you choose image1
and image4
I need a list of those two images.
From that list I need to randomly set an ImageView to one of those pictures.
How is that possible?
Upvotes: 0
Views: 124
Reputation: 1334
You will first need to create a selection list,then a button that when clicked ,picks the list of only the selected items then set a random image from the list of selected items.
so for the layout : main layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/selection_list"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/get_selection"
android:text="Show randomly selected"/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/random_img"/>
</LinearLayout>
Then the adapter for the selection list :
public class test_adapter extends RecyclerView.Adapter<test_adapter.view> {
Context cntxt;
public ArrayList<selection_items> selection_items_list;
AdapterView.OnItemClickListener listener;
public test_adapter(Context cntxt, ArrayList<selection_items> selection_items_list)
{
this.cntxt=cntxt;
this.selection_items_list = selection_items_list;
}
public ArrayList<selection_items> selected_items()
{
ArrayList<selection_items> selected_=new ArrayList<>();
for(int i = 0; i< selection_items_list.size(); i++)
{
if(selection_items_list.get(i).selected){
selected_.add(selection_items_list.get(i));
}
}
return selected_;
}
@NonNull
@Override
public view onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(cntxt).inflate(R.layout.img_in_list_test, parent, false);
return new view(view);
}
@Override
public void onBindViewHolder(@NonNull view holder, int position) {
selection_items obj= selection_items_list.get(position);
holder.name.setText(obj.name);
holder.img.setImageDrawable(cntxt.getDrawable(obj.img));
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
obj.selected=b;
}
});
}
@Override
public int getItemCount() {
return selection_items_list.size();
}
public class view extends RecyclerView.ViewHolder implements View.OnClickListener {
public CheckBox name;
public ImageView img;
public void set_printed()
{
itemView.setBackgroundColor(cntxt.getColor(R.color.primary_semi_transparent));
}
view(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.check);
img = itemView.findViewById(R.id.img);
}
@Override
public void onClick(View view) {
}
}
}
Your selected item layout :
<?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="52dp">
<ImageView
android:layout_width="50dp"
android:id="@+id/img"
android:layout_height="50dp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check"/>
</LinearLayout>
Finally your activity
public class TestActivity extends AppCompatActivity {
RecyclerView selection_view;
Button get_selection;
ImageView random_selected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
selection_view = findViewById(R.id.selection_list);
get_selection = findViewById(R.id.get_selection);
random_selected = findViewById(R.id.random_img);
selection_view.setLayoutManager(new LinearLayoutManager(this));
ArrayList<selection_items> selection_items=new ArrayList<>();
selection_items.add(new selection_items("Image 1",R.drawable.backup_icon));
selection_items.add(new selection_items("Image 2",R.drawable.ic_add_photo));
selection_items.add(new selection_items("Image 3",R.drawable.ic_barcode_scan));
selection_view.setAdapter(new test_adapter(this,selection_items));
get_selection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<selection_items> selected_items=((test_adapter)selection_view.getAdapter()).selected_items();
if(selected_items.size()>0){
random_selected.setImageDrawable(getDrawable(selected_items.get(new Random().nextInt(selected_items.size())).img));
}else{
random_selected.setImageDrawable(null);
}
}
});
}
}
Upvotes: 1
Reputation: 135
here maybe help
layouts = new int[]{
R.drawable.welcome_slide1,
R.drawable.welcome_slide2,
R.drawable.welcome_slide3,
R.drawable.welcome_slide4};
more accurate
ArrayList<Integer> myImageList = new ArrayList<>();
myImageList.add(R.drawable.thingOne);
myImageList.add(R.drawable.thingtwo);
myImageList.add(R.drawable.thingthree);
myImageView.setImageResource(myImageList.get(i));
Upvotes: 0