androidonclicklistenercardview

OnclickListner On CardView - Android


I have The Following Code Which Display Search bar And Add Text Inside CardView..

       <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginStart="10dp"
                    android:layout_weight="0.7"
                    android:background="@drawable/round_rectangle"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="10dp"
                        android:src="@drawable/ic_search"
                        app:tint="@color/gray" />

                    <EditText
                        android:id="@+id/edt_search"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="10dp"
                        android:background="@android:color/transparent"
                        android:fontFamily="@font/spartan_medium"
                        android:gravity="center_vertical"
                        android:textSize="18sp"
                        android:textStyle="normal" />
                </LinearLayout>

                <androidx.cardview.widget.CardView
                    android:id="@+id/cardAddAccount"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="5dp"
                    android:layout_weight="0.3"
                    android:orientation="horizontal"
                    android:clickable="true"
                    app:cardBackgroundColor="@color/solitude"
                    app:cardCornerRadius="5dp"
                    app:cardElevation="0dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:fontFamily="@font/spartan_semibold"
                        android:gravity="center"
                        android:text="@string/home_btn_add"
                        android:textColor="@color/dodgerBlue"
                        android:textSize="14sp"
                        android:textStyle="italic" />

                </androidx.cardview.widget.CardView>

            </LinearLayout>

The Result Of the Above activity_home.xml is look like this.. activity_home.xml

HomeActivity.java

     CardView cardView = findViewById(R.id.cardAddAccount);
        if (cardView != null) {
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Launch AddAccountActivity when the CardView is clicked
                    Intent i = new Intent(HomeActivity.this, AddAccountActivity.class);
                    startActivity(i);
                }
            });
        }

I want to When User Click on Add Button Then AddAccountActivity.class Should Open..But With The Given Code the App is crashing While Clicking On Add..


Solution

  • I found a solution that I forgot to create onCreate() method Inside AddAccountActivity.class File..

    public class AddAccountActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_account);
        }
    
    }