How to get button that is inside include

Asked

Viewed 18 times

0

I have a Fragment, which contains a Viewpager that inflates 2 views for now, but will have a button next in all layouts so I made a separate file with this button, and however I can’t catch it to make the click Systener, always returns me null

My Fragment that has Viewpager:

package doupenglish.com.br.doup.Fragments;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import doupenglish.com.br.doup.MainActivity;
import doupenglish.com.br.doup.R;
import mehdi.sakout.fancybuttons.FancyButton;

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

    private Button btnaudio;
    int posicaolayoutaudio;
    private int[] mlayouts;
    private int[] mlayoutslayout;
    int layoutaudioposicao;

    public DiaFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mlayouts = getArguments().getIntArray("layouts");
        mlayoutslayout = getArguments().getIntArray("layoutslayout");
        layoutaudioposicao = getArguments().getInt("layoutaudioposicao");
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.dia1f, container, false);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        ViewPager viewPager = view.findViewById(R.id.viewpagerdia1);
        viewPager.setAdapter(new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //ViewGroup layout1 = (ViewGroup) inflater.inflate(layouts[position], container, false);
                int resId;
                View view = null;
                if (position == layoutaudioposicao){
                    resId = mlayouts[position];
                    view = inflater.inflate(resId, null);
                    ConstraintLayout layout = view.findViewById(mlayoutslayout[position]);
                    /*btnaudio = layout.findViewById(R.id.btn_audio);
                    btnaudio.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getContext(), "aaa", Toast.LENGTH_SHORT).show();
                        }
                    });*/
                    container.addView(view, 0);
                }else{
                    resId = mlayouts[position];
                    view = inflater.inflate(resId, null);
                    View btn_proxlayout = inflater.inflate(R.layout.btnprox,null);
                    FancyButton btn_prox = btn_proxlayout.findViewById(R.id.btn_prox);
                    btn_prox.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getContext(), "aaa", Toast.LENGTH_SHORT).show();
                        }
                    });
                    container.addView(view, 0);
                }

                return view;
            }

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

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

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        });
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });






    }
}

My XML:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="64dp"
    android:fontFamily="@font/scada"
    android:text="Investigue a unidade"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView4"
    app:layout_constraintVertical_bias="0.0" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="64dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:id="@+id/textView14"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:text="Utilize as imagens para auxiliar no entendimento do contexto do diálogo"
    android:textAlignment="center"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView13" />

<include
    layout="@layout/btnprox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

If you need more code, I just asked.

1 answer

0

No need to inflate the layout btnprox to reference your Button, you can reference directly to your xml where the include this one inside.

Try using your main layout, this will work.

  • // View btn_proxlayout = inflater.inflate(R.layout.btnprox,null);&#xA;FancyButton btn_prox = view.findViewById(R.id.btn_prox);&#xA;Assim ? nao funcionou

Browser other questions tagged

You are not signed in. Login or sign up in order to post.