Hola buenas tardes tengo un problema como mi DialogFragment (FragmentProduct) en el cual declaro mis elementos e inicializo de mi layout (fragment_product.xml), tengo varios botones pero uno en específico el que tiene el id btnAgregar el cual puse un OnClickListener y dentro hago con Lg.d para saber si entra. Al presionar el botón no me imprime mi mensaje, pero tampoco me manda ningún error.
Espero que me puedan apoyar a saber porque no me está imprimiendo el mensaje.
Gracias.
fragment_product.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentProduct">
<!-- TODO: Update blank fragment layout -->
<LinearLayout android:id="@+id/activity_item_details"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView android:id="@+id/scrollbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9.5"
android:scrollbars="none"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/productimage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/ic_cart_plus_solid"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:id="@+id/txtVCod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Codigo"
android:textSize="16dp"
android:textColor="@color/gen_black"/>
<TextView
android:id="@+id/txtVDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Producto"
android:textSize="20dp"
android:textColor="@color/gen_black"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="Cantidad: "
android:textSize="16sp"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/txtVMenos"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:backgroundTint="#eeeeee"
android:text="-"/>
<EditText
android:id="@+id/txtVMas"
android:layout_width="40dp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textColor="@android:color/black"
android:textSize="16sp"
android:inputType="number"
android:text=""/>
<Button
android:id="@+id/btnAgregar"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:backgroundTint="#eeeeee"
android:text="+"
android:textColor="@android:color/black" />
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="@dimen/view_width_small"
android:background="@color/grey_light"
android:layout_marginTop="8dp"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="2"
android:elevation="30dp"
android:background="@color/gen_black">
<TextView
android:id="@+id/txtVCancelar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:text="CANCELAR"
android:textSize="14dp"
android:textColor="@color/gen_white"
android:textStyle="bold"
android:gravity="center"/>
<TextView
android:id="@+id/add_to_cart"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/gen_white"
android:text="AGREGAR"
android:textSize="14dp"
android:textColor="@color/gen_black"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
FragmentProduct.java
package com.example.pegasso;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link FragmentProduct.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link FragmentProduct#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentProduct extends DialogFragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
// Creo los elementos
public TextView txtVMas, txtVCancelar;
public Button btnAgregar;
public FragmentProduct() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentProduct.
*/
// TODO: Rename and change types and number of parameters
public static FragmentProduct newInstance(String param1, String param2) {
FragmentProduct fragment = new FragmentProduct();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_product, null);
txtVMas = view.findViewById(R.id.txtVMas);
txtVCancelar = view.findViewById(R.id.txtVCancelar);
btnAgregar = view.findViewById(R.id.btnAgregar);
txtVCancelar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentProduct.this.dismiss();
Log.d("Holisssssss------","hhhhhhhhhh");
}
});
btnAgregar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int cantidad = 1;
if (cantidad < 500) {
cantidad++;
txtVMas.setText(Integer.toString(cantidad));
Log.d("Contador-----------------------", ""+cantidad);
} else {
}
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_product, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}