0

I'm creating a language app as homework for school in android.
Right now I'm setting up some clickable CardViews (which are in "LevelsHub") to take the user to another view (called "ProfileActivity") but for some reason INTENT is not working the way it should, it takes me back to the same view ("LevelsHub"),even though I've used it in some other parts of the app. What could be happening here?

I've followed this amazing dude tutorials on it.
https://www.youtube.com/watch?v=VUPM387qyrw and this one too https://www.youtube.com/watch?v=K2V6Y7zQ8NU

  • Tried clicking on the cards itself, didn't work
  • I've already tried adding buttons to the card and using Intent on it too, didn't work.
  • Cycling throughout all cards to see if any of them would take me somewhere
  • Using this guy's method of switch https://www.youtube.com/watch?v=d6CfaWW7G5Q
  • I've tried separating them one by one, the same result
  • package com.example.salo7.totonaku;
    
    import android.content.Intent;
    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.CardView;
    import android.view.View;
    import android.widget.Button;
    import android.widget.GridLayout;
    import android.widget.Toast;
    
    public class LevelsHub extends AppCompatActivity implements 
    View.OnClickListener {
    
     //GridLayout mainGrid;
     //CardView pronombresCard1;
     //CardView numeros1Card2;
     //CardView numeros2Card3;
     private Button  pronombresBtn1;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_levels_hub);
    
         //mainGrid = (GridLayout) findViewById(R.id.mainGrid);
         pronombresBtn1 = (Button) findViewById(R.id.pronombresBtn1);
         //numeros1Btn2 = (Button) findViewById(R.id.numeros1Btn2);
         //numeros2Btn3 = (Button) findViewById(R.id.numeros2Btn3);
    
         //Add click listener
         pronombresBtn1.setOnClickListener(this);
    
    
         //Set Event
         //setSingleEvent (mainGrid);
         //setToggleEvent (mainGrid);
     }
    
    @Override
    public void onClick(View v) {
        Intent i;
    
        switch (v.getId()){
            case R.id.pronombresBtn1 : i = new Intent(LevelsHub.this, 
    profileActivity.class);startActivity(i); break;
            default:break;
        }
    }
    
    /*private void setToggleEvent(GridLayout mainGrid) {
        //ciclar childs de mainGrid
        for (int i=0;i<mainGrid.getChildCount();i++){
            final CardView cardView = (CardView) mainGrid.getChildAt(i);
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cardView.getCardBackgroundColor().getDefaultColor() == 
    -1){
                        //Cambia el color
    
    cardView.setCardBackgroundColor(Color.parseColor("#FF6F00"));
                        Toast.makeText(LevelsHub.this,"State: 
    true",Toast.LENGTH_SHORT).show();
                    }else{
    
    cardView.setCardBackgroundColor(Color.parseColor("#FFFFFF"));
                        Toast.makeText(LevelsHub.this,"State: 
    false",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }*/
    
    /*private void setSingleEvent(GridLayout mainGrid) {
        //ciclar childs de mainGrid
        for (int i=0;i<mainGrid.getChildCount();i++){
            CardView cardView = (CardView) mainGrid.getChildAt(i);
            final int finalI = i;
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Toast.makeText(LevelsHub.this,"Cliqueado en index " + 
    finalI,Toast.LENGTH_SHORT).show();
                    if (finalI == 0){
                        Toast.makeText(LevelsHub.this,"Clicado 
    pronombres",Toast.LENGTH_SHORT).show();
                        Intent accountIntent = new Intent(LevelsHub.this, 
    profileActivity.class);
                        startActivity(accountIntent);
                        finish();
                    }
                }
            });
        }
     }*/
    }
    

    It should only take me to another view, can anyone help me out a bit?

    UPDATE Didn't know XML was necessary to explain my mistake, but here it is :).

    <?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"
    android:orientation="vertical"
    android:weightSum="10"
    tools:context=".LevelsHub">
    <!--android:background="@drawable/bg"-->
    <RelativeLayout
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    
        <TextView
            android:id="@+id/textGrid"
            android:text="NIVELES"
            android:textSize="34sp"
            android:textColor="@android:color/white"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    </RelativeLayout>
    
    <GridLayout
        android:id="@+id/mainGrid"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:alignmentMode="alignMargins"
        android:columnCount="2"
        android:columnOrderPreserved="false"
        android:padding="14dp"
        android:rowCount="3">
    
        <!--Row 1-->
        <!--Column 1-->
    
        <!--Column 2-->
        <android.support.v7.widget.CardView
            android:id="@+id/pronombresCard1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="107dp"
                    android:layout_height="102dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/logomakr_negro" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pronombres"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="18sp"
                    android:textStyle="bold" />
    
                <Button
                    android:id="@+id/pronombresBtn1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Empezar" />
    
    
            </LinearLayout>
    
        </android.support.v7.widget.CardView>
    
        <android.support.v7.widget.CardView
            android:id="@+id/numeros1Card2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="107dp"
                    android:layout_height="102dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/logomakr_negro" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Números 1"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="18sp"
                    android:textStyle="bold" />
    
                <Button
                    android:id="@+id/numeros1Btn2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Empezar" />
    
    
            </LinearLayout>
    
        </android.support.v7.widget.CardView>
    
        <!--Row 2-->
        <!--Column 1-->
        <android.support.v7.widget.CardView
            android:id="@+id/numeros2Card3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="107dp"
                    android:layout_height="102dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/logomakr_negro" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Números 2"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="18sp"
                    android:textStyle="bold" />
    
                <Button
                    android:id="@+id/numeros2Btn3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Empezar" />
    
    
            </LinearLayout>
    
        </android.support.v7.widget.CardView>
    
        <!--Column 2-->
        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="107dp"
                    android:layout_height="102dp"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/logomakr_negro" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="¡Más niveles en camino!"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="18sp"
                    android:textStyle="bold" />
    
    
            </LinearLayout>
    
        </android.support.v7.widget.CardView>
    
    </GridLayout>
    
    </LinearLayout>
    
    Phantômaxx
    • 37,901
    • 21
    • 84
    • 115
    Sarquamon
    • 49
    • 2
    • 8

    3 Answers3

    0

    You have code to start activity in same line, So the code after ; will be skipped. Format your code properly and move startActivity() to next line.

    case R.id.pronombresBtn1 : 
        i = new Intent(LevelsHub.this,profileActivity.class);
        startActivity(i); 
    break;
    
    karan
    • 8,637
    • 3
    • 41
    • 78
    • Thank you for listing a possible solution, sadly it didn't work even though I changed it the way you said :( – Sarquamon Jan 08 '19 at 07:34
    0

    Add two properties in your CardView

    android:clickable="true"
    android:focusable="true"
    

    It will work.

    Feel free to comment if you get any problem.

    Pratik Butani
    • 60,504
    • 58
    • 273
    • 437
    0

    So basically after some digging, debug and more I was able to find the answer to my problem. Which basically was that LevelsHub activity was correct but in my profileActivity activity I had a button which was not linking correctly to the OnClickListener I had for it, so all I did was correct that and now is working like a charm.
    Thanks to everybody who helped me out and gave some advices.

    Sarquamon
    • 49
    • 2
    • 8