skip to Main Content

I am trying to learn android studio and I have made an app and in that app there is a screen which shows an image with buttons to move to previous/next images. Now the image is shown but the buttons don’t work and my image doesn’t change.

Following is the code for my java file

package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class PhotoGalleryFragment extends Fragment {

    private int[] imageIds = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5 };
    private int currentIndex = 0;
    private ImageView imageView;
    private Button previousButton;
    private Button nextButton;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo_gallery, container, false);

        imageView = view.findViewById(R.id.imageView);
        previousButton = view.findViewById(R.id.previousButton);
        nextButton = view.findViewById(R.id.nextButton);

        imageView.setImageResource(imageIds[currentIndex]);

        previousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex--;
                if (currentIndex < 0) {
                    currentIndex = imageIds.length - 1;
                }
                imageView.setImageResource(imageIds[currentIndex]);
            }
        });

        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex++;
                if (currentIndex >= imageIds.length) {
                    currentIndex = 0;
                }
                imageView.setImageResource(imageIds[currentIndex]);
            }
        });

        return view;
    }
}

and following is the xml for that file


<?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="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/previousButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:text="Previous" />

        <Button
            android:id="@+id/nextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Next" />

    </LinearLayout>

</LinearLayout>

Following is the screen that I get and image doesn’t change when clicking the buttons

enter image description here

2

Answers


  1. Hi you should remove android:clickable="false" or set android:clickable="true"

    <Button
        android:id="@+id/previousButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:text="Previous" />
    
    Login or Signup to reply.
  2. In your Previous button attributes, you have set android:clickable="false”. Hence button is inactive. Set it to true and you are good to go.
    Hope this should solve your problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search