0

My issue is the Button in my activity needs to be clicked twice to make the code execute written in the onClick method. While I am sharing my Activity code and Layout file code. Please guide me to solve the issue.

Activity (Fragment)

public class SelfFamilyInfoActivity extends Fragment {

Button btnUpdateFamily;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.self_mem_details_family_info, 
  container, false);

    try {

        btnUpdateFamily = v.findViewById(R.id.btnUpdateFamily);

        btnUpdateFamily.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getActivity(), "Button Clicked", 
        Toast.LENGTH_SHORT).show();

            }
        });


    } catch (Exception e) {
        e.printStackTrace();
    }

    return v;

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}
}

Layout File

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">

<ScrollView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="vertical">

    <LinearLayout

        android:id="@+id/llLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="12dp"
        android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center">

                <Button
                    android:id="@+id/btnUpdateFamily"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/button_background"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="Update Family Info"
                    android:textColor="@color/buttonTextColorWhite"
                    android:textSize="18sp" />

            </LinearLayout>


        </LinearLayout>

 </ScrollView>
 </android.support.v4.widget.NestedScrollView>

While this fragment is being initialized in Another MainActivity.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
java bee
  • 115
  • 3
  • 15

1 Answers1

1

For fragment it is good to use your all view click in onViewCreated method. May be view not prepared but you start clicking . And you use try catch block did you see any exception in your log. I am not sure. But you can try your click listener in your onViewCreated method.

UPDATED try to use this code. this code working in my section

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

    return v;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    btnUpdateFamily = view.findViewById(R.id.btnUpdateFamily);

    btnUpdateFamily.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(getActivity(), "Button Clicked",
                    Toast.LENGTH_SHORT).show();

        }
    });


}
Tariqul Islam
  • 680
  • 6
  • 14