skip to Main Content

I have created a app where on the first screen there will be a image, if the user click on that image a suggestion screen will open to change that image.
I want that the suggestion image screen should be a Recyclerview screen, which will open on the same screen. This is just a very basic work to pratice recycler view.
I have not implement the change till now I have stuck here. Actually I can’t figure out that how should I connect those class on the main_activity to preform the desire task. I am sharing all the code I have done for this , please help me for the next step.

main_activity.xml

<RelativeLayout 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">
    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/ic_img1"
        android:layout_margin="11dp"
        android:background="@color/black"
        android:layout_centerInParent="true"
        android:id="@+id/imgicon1">
    </ImageView>

</RelativeLayout>

1.structure xml file which will use for the recycler view to set other image(lay_for_select_img.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="450dp"

    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="7dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:id="@+id/layLinearforimg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:padding="10dp"
            >
            <ImageView
                android:id="@+id/imgicon2"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:src="@drawable/ic_img1"
                android:background="@color/black">

            </ImageView>
</LinearLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>

structure class to set the image(structure.java)

public class structureclass {
    int img;
    public structureclass(int img){
        this.img=img;
    }
}

main_activity.java

public class MainActivity extends AppCompatActivity {
    RecyclerView layrecycle = findViewById(id.layRecycle);
    ImageView imgicon1;
    ArrayList<structureclass> strucimg=new ArrayList<>();
    RecyclerViewAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);
        layrecycle = findViewById(id.layRecycle);

        imgicon1=findViewById(id.imgicon1);
        layrecycle.setLayoutManager(new GridLayoutManager(this,3));
        strucimg.add(new structureclass(drawable.ic_img1));
        strucimg.add(new structureclass(drawable.ic_img2));
        strucimg.add(new structureclass(drawable.ic_img3));
        strucimg.add(new structureclass(drawable.ic_img4));
        strucimg.add(new structureclass(drawable.ic_img5));
        strucimg.add(new structureclass(drawable.ic_img6));
        strucimg.add(new structureclass(drawable.ic_img7));
        strucimg.add(new structureclass(drawable.ic_img8));
        strucimg.add(new structureclass(drawable.ic_img9));
        strucimg.add(new structureclass(drawable.ic_img10));
        strucimg.add(new structureclass(drawable.ic_img11));
        strucimg.add(new structureclass(drawable.ic_img12));
        strucimg.add(new structureclass(drawable.ic_img13));
        strucimg.add(new structureclass(drawable.ic_img14));
        strucimg.add(new structureclass(drawable.ic_img15));
        adapter=new RecyclerViewAdapter(this,strucimg);
        layrecycle.setAdapter(adapter);
        imgicon1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layrecycle.setVisibility(View.VISIBLE);
            }
        });




    }
}

activityrecyclerclass.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layRecycle">

    </androidx.recyclerview.widget.RecyclerView>

</RelativeLayout>

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.viewHolder> {
    Context context;
    ArrayList<structureclass> strucimg=new ArrayList<>();

    public RecyclerViewAdapter(Context context, ArrayList<structureclass> strucimg){
        this.context=context;
        this.strucimg=strucimg;
    }
    public class viewHolder extends RecyclerView.ViewHolder{
        ImageView imgicon2;


        public viewHolder(@NonNull View itemView) {
            super(itemView);
            imgicon2=itemView.findViewById(R.id.imgicon2);
        }
    }


    @NonNull
    @Override
    public RecyclerViewAdapter.viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=LayoutInflater.from(context).inflate(R.layout.lay_for_select_img,parent,false);
        //inflate return view
        viewHolder viewholder=new viewHolder(v);
        return viewholder;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.viewHolder holder, int position) {
        holder.imgicon2.setImageResource(strucimg.get(position).img);



    }

    @Override
    public int getItemCount() {
        return strucimg.size();
    }
}

error on logcat

---------------------------- PROCESS STARTED (5451) for package com.sumita.changeimgwithcardview ----------------------------
2023-01-31 13:00:34.807  5451-5451  AndroidRuntime          com.sumita.changeimgwithcardview     D  Shutting down VM
2023-01-31 13:00:34.817  5451-5451  AndroidRuntime          com.sumita.changeimgwithcardview     E  FATAL EXCEPTION: main
                                                                                                    Process: com.sumita.changeimgwithcardview, PID: 5451
                                                                                                    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sumita.changeimgwithcardview/com.sumita.changeimgwithcardview.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2878)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3072)
                                                                                                        at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1766)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loop(Looper.java:214)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6977)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)
                                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                                                                                                        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
                                                                                                        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
                                                                                                        at android.content.Context.obtainStyledAttributes(Context.java:661)
                                                                                                        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:922)
                                                                                                        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:889)
                                                                                                        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:691)
                                                                                                        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:264)
                                                                                                        at com.sumita.changeimgwithcardview.MainActivity.<init>(MainActivity.java:18)
                                                                                                        at java.lang.Class.newInstance(Native Method)
                                                                                                        at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2868)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3072) 
                                                                                                        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1766) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                                        at android.os.Looper.loop(Looper.java:214) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6977) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910) 
2023-01-31 13:00:34.842  5451-5451  Process                 com.sumita.changeimgwithcardview     I  Sending signal. PID: 5451 SIG: 9
---------------------------- PROCESS ENDED (5451) for package com.sumita.changeimgwithcardview ----------------------------

2

Answers


  1. Next step is to create the adapter class which will handle the data binding between the structureclass and the lay_for_select_img layout. Here’s how you can create the adapter class:

    Adapter class (RecyclerAdapter.java)

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
    private ArrayList<structureclass> strucimg;
    
    public RecyclerAdapter(ArrayList<structureclass> strucimg) {
        this.strucimg = strucimg;
    }
    
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lay_for_select_img, parent, false);
        return new MyViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.imgicon2.setImageResource(strucimg.get(position).img);
    }
    
    @Override
    public int getItemCount() {
        return strucimg.size();
    }
    
    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imgicon2;
    
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imgicon2 = itemView.findViewById(R.id.imgicon2);
        }
    }
    

    }

    Finally, set the adapter to your RecyclerView in the MainActivity class:

    layrecycle.setAdapter(new RecyclerAdapter(strucimg));
    

    You also need to handle the click event on the ImageView in the MainActivity class. To open the RecyclerView when the ImageView is clicked:

    imgicon1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        layrecycle.setVisibility(View.VISIBLE);
    }
    

    });

    And to hide the RecyclerView when a suggestion image is selected:

    //TODO: implement the code to hide the RecyclerView and update the selected image on the main screen
    
    Login or Signup to reply.
  2. Step 1

    Change this

    RecyclerView layrecycle = findViewById(id.layRecycle);
    

    to

    RecyclerView layrecycle;
    

    Step 2

    Add this line layrecycle.setAdapter(adapter); below initialization of adapter as follow:

    adapter=new RecyclerViewAdapter(this,strucimg);
    layrecycle.setAdapter(adapter);
    

    Step # 3 (Optional)

    Moreover, you change height to wrap_content of your item_layout.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="450dp"
        ...
        />
    

    to following

    android:layout_height="wrap_content"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search