How to open new Activity by clicking [card view] in activity. I am beginner and i have no idea of using [Card View] as a button. Help me i need example code.
XML.fragment
<androidx.cardview.widget.CardView
android:id="@+id/bankingcard"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#7c4dff"
android:padding="10dp"
android:src="@drawable/download_5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Banking"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="#ededed" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Check Your Bank Activities"
android:textColor="@color/black" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Fragment Class
intent = new Intent(getActivity(), MainActivity2.class);
final CardView button = (CardView) rootView.findViewById(R.id.bankingcard1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
Here I Add Two card and open two New Activity for open new view.
Step 1 - Create a new
project
Step 2 - Add two dependencies
Step 3 - Create two
new Activity which you want to open as new Activity.
Step 4 - Paste Xml
code in ActivityMain.xml which code
provide in post.
Step 5 - Paste Java
code in Main Activity which provide in post.
Step 6 - Your project
is ready to “open new activity by click on card view in android studio”
Step 7 – Now run your
project “I hope your project is successfully
created”.
Step 2
Step 2 - Add two dependencies
implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.recyclerview:recyclerview:1.1.0'
Step 4
Step 4 - Paste Xml code in ActivityMain.xml which code provide bellow.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="@+id/bankingcard"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#7c4dff"
android:padding="10dp"
android:src="@drawable/download_5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Banking"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="#ededed" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Check Your Bank Activities"
android:textColor="@color/black" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/ideascard"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#ff4081"
android:padding="10dp"
android:src="@drawable/download_5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Ideas"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="#ededed" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Check Your Bank "
android:textColor="@color/black" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Step 5
Step 5 - Paste Java code in Main Activity which provide in post.
package com.example.myapplicationjjjjj.ui.home;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.example.myapplicationjjjjj.MainActivity2;
import com.example.myapplicationjjjjj.MainActivity3;
import com.example.myapplicationjjjjj.R;
public class HomeFragment extends Fragment {
Intent intent ,cardview;
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
intent = new Intent(getActivity(), MainActivity2.class);
final CardView button = (CardView) rootView.findViewById(R.id.bankingcard1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
intent = new Intent(getActivity(), MainActivity3.class);
final CardView button1 = (CardView) rootView.findViewById(R.id.ideascard);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View c) {
startActivity(intent);
}
});
return rootView;
}
}
Step 7
Step 7 – Now run your project “I hope your project is successfully created” and output will like this.
Note – If you get any err comment in comment box.