Aldrin B.
Aldrin B.

Reputation: 1

Change set of Image resource on Button click in fragment

I have there sets of drawable in int array. Is it possible to change the image resource of the image view in the fragment on button click? for example i have there a method onclicklistener for button benefit_babies... i want to set the image resource into the int array benefitBaby on click of the button.

InformationTab fragment

package com.example.aldrinjohn.sample;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Aldrin John on 3/20/2017.
 */

public class InformationTab extends Fragment{
    ViewPager viewPager;
    CustomSwipeAdapter adapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.informationtab, container, false);
        viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
        adapter = new CustomSwipeAdapter(this.getActivity());
        viewPager.setAdapter(adapter);


        return rootView;

    }

}

XML File:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/trivia"
        android:text="BreastFeeding Trivia"/>

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/benefit_babies"
        android:text="Benefits to Babies"/>

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/storing"
        android:text="Storing Breastmilk"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/pump"
            android:text="How to Pump Breastmilk"/>

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/benefit_mother"
            android:text="Benefits to Mothers"/>

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/tips"
            android:text="Tips and Information"/>

    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image_view"/>

</LinearLayout>

JAVA Code:

package com.example.aldrinjohn.sample;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.android.gms.tagmanager.Container;

/**
 * Created by Aldrin John on 3/26/2017.
 */

public class CustomSwipeAdapter extends PagerAdapter implements View.OnClickListener{
    private int[] trivia = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4, R.drawable.c5,R.drawable.c6};
    private int[] benefitBaby = {R.drawable.c7,R.drawable.c8,R.drawable.c9};
    private int[] benefitMother = {R.drawable.c91,R.drawable.c92};
    private int[] info = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,
                            R.drawable.a8,R.drawable.a9,R.drawable.a91,R.drawable.a92};
    private int[] storing = {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4};
    private Context ctx;
    private LayoutInflater layoutInflater;
    Button btnbenefitB;

    public CustomSwipeAdapter(Context ctx)
    {
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return trivia.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view==(LinearLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
        btnbenefitB = (Button)item_view.findViewById(R.id.benefit_babies);
        btnbenefitB.setOnClickListener(this);
        ImageView imageView = (ImageView)item_view.findViewById(R.id.image_view);
        imageView.setImageResource(trivia[position]);
        container.addView(item_view);

        return item_view;
    }
@Override
    public void destroyItem(ViewGroup container, int position, Object object){
        container.removeView((LinearLayout)object);
    }


    public int getCount(int[] x) {
        return x.length - 1;
    }



    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.benefit_babies:

                break;
        }
    }
}

Upvotes: 0

Views: 1346

Answers (2)

Aldrin B.
Aldrin B.

Reputation: 1

Hi everyone thanks for the help. I solved it by using view page adapter. Every onclick button I will change the adapter. Thank you!!

Upvotes: 0

Cheticamp
Cheticamp

Reputation: 62831

imageview.setImageResource(id) where id is in your int array. See the documentation for ImageView for details. It is the same as you have in your code just in the onClick() handler.

setImageResource

void setImageResource (int resId)

Sets a drawable as the content of this ImageView.

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

Upvotes: 1

Related Questions