How to add Home button using Card View Button in Android Studio

593 views Asked by At

This is my layout and i want to move to home page with a single click.

[Added a Photo]eKv.png

My home button card view code is here

    <androidx.cardview.widget.CardView
        android:id="@+id/cv_home"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        app:cardCornerRadius="50dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iv_home"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/home" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>
1

There are 1 answers

3
M Ashhal On BEST ANSWER

I can't understand what you mean by go to home page?

If u want to go HomeActivity/MainActivity, Just set a listener on the imageButton with an intent to start your home activity

Kotlin

class MainActivity : AppCompatActivity() {
    private lateinit var imageButton : ImageButton
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        imageButton = findViewById(R.id.iv_home)

        imageButton.setOnClickListener {
            val intent = Intent(this, HomeActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
}

Java

public class MainActivity extends AppCompatActivity {
    private ImageButton imageButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imageButton = findViewById(R.id.iv_home);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}