user3584935
user3584935

Reputation: 51

Android Imagebutton fragment

Hey guys so i'm trying to get my imagebutton to work within my fragment. The code works fine with a Activity but I cannot get it to work within the fragment. What do I need to change? Errors keep occurring within the configureImage method, Thanks guys.

 import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * 
 */
public class FragmentA extends Fragment {

    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_a);

        configureImageButton();


    }






private void configureImageButton() {
    // TODO Auto-generated method stub
    ImageButton btn = (ImageButton) findViewById(R.id.imageButton1);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(FragmentA.this, "You Clicked the button!", Toast.LENGTH_LONG).show();

        }
    });


}

Upvotes: 1

Views: 4949

Answers (3)

M S Gadag
M S Gadag

Reputation: 2057

use this this will work for you, and follow this for fragment.

public class FragmentA extends Fragment {
ViewGroup rootViewA;
    ImageButton btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootViewA = (ViewGroup) inflater.inflate(
                R.layout.fragment_a, container, false);
     btn = (ImageButton ) rootViewA
            .findViewById(R.id.imageButton1);
     btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();

        }
    });
    return rootViewA;

}
}

Upvotes: 0

Kishan Dhamat
Kishan Dhamat

Reputation: 3784

    public class FragmentA extends Fragment {

        public FragmentA() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater
                    .inflate(R.layout.fragment_a, container, false);
            configureImageButton(view);
            return view;
        }
       private void configureImageButton(View view) {
        // TODO Auto-generated method stub
        ImageButton btn = (ImageButton) view.findViewById(R.id.imageButton1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();

            }
        });


    }
}

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Your fragment implementations is wrong. Do this way.

public class FragmentA extends Fragment {


    private View v;

    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_a,container, false);
        configureImageButton();
        return v;
    }

    private void configureImageButton() {
        // TODO Auto-generated method stub
        ImageButton btn = (ImageButton) v.findViewById(R.id.imageButton1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();

            }
        });


    }
}

Upvotes: 2

Related Questions