Search code examples
c#androidandroid-cardview

Use card views as buttons


On the home screen of my app i have a list of card views that i want to use as buttons to switch to their related fragment.

i want to do it similar to this instead of writing a onclick method for every card.

private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
            {
                Fragment fragment = null;
                
    
                switch (e.Item.ItemId)
                {
                    case Resource.Id.menu_home:
                        fragment = homeFragment;
                        break;
    
                    case Resource.Id.menu_favourites:
                        fragment = favFragment;
                        break;
    
                    case Resource.Id.menu_more:
                        fragment = moreFragment;
                        break;
                }
    
                FragmentTransaction fragmentTx = SupportFragmentManager.BeginTransaction();
                fragmentTx.Replace(Resource.Id.container, fragment);
                fragmentTx.Commit();
    
    
            }

I am doing this inside the home fragment and my cardviews are the paceCard, wattsCard and weightadjustCard. So how can i implement this for my cards?


Solution

  • You can try this:

    public void onClickCardView(View v){
        switch(v.getId()){
            case R.id.YOUR_ID:
            //statement
            break;
        switch(v.getId()){
            case R.id.YOUR_ID:
            //statement
            break;
        switch(v.getId()){
            case R.id.YOUR_ID:
            //statement
            break;
        }
    }
    

    and in your xml give all the card onClick: like this

    android:onClick="onClickCardView";