0

I'm new to Android and recently created a dashboard for my app. I followed a tutorial but it only covered the front end. I created a layout called content_main.xml with three Cardviews. Each of these Cardviews contains the code

android:clickable="true"

Beginning the back end of things, I created a java receiver class called DashboardActivity (as seen below). I want to make my Cardviews clickable so that when one is clicked, I am taken to another screen within my app. The other screens in my app I am trying to get to are fragments. Each of these screens has its own java class in my "Fragments" folder. One of them, of which I try calling on in the code that follows is called SettingsViewFragment().

So far, I have tried the following. It does nothing.

public class DashboardActivity  extends AppCompatActivity {

    CardView cd1, cd2, cd3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        cd1 = (CardView) findViewById(R.id.dash_settings);
        cd2 = (CardView) findViewById(R.id.dash_profile);
        cd3 = (CardView) findViewById(R.id.dash_activity);

        cd1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment1 = new SettingsViewFragment();
            }

        });
    }
}

UPDATE: FULL CODE

public class DashboardActivity extends AppCompatActivity {

    CardView cd1, cd2, cd3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        cd1 = findViewById(R.id.dash_settings);
        cd2 = findViewById(R.id.dash_profile);
        cd3 = findViewById(R.id.dash_activity);

        cd1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.i("Testing 123 123 123");
            }

        });

    }
}

And my XML: (it continues for 3 cards this way)

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.newproject.DashboardActivity"
    tools:showIn="@layout/toolbar">


    <FrameLayout
        android:id="@+id/dashframe"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <LinearLayout
        android:clipToPadding="false"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.CardView
            android:id="@+id/dash_settings"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:layout_width="160dp"
            android:layout_height="190dp"
            android:layout_margin="10dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">
                <ImageView
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:background="@drawable/bg_circle1"
                    android:src="@drawable/ic_settingspicture"
                    android:padding="10dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="10dp"
                    android:text="View your settings"/>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/lightgray"
                    android:layout_margin="10dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Follow along with guided tutorials"
                    android:padding="5dp"
                    android:textColor="@android:color/darker_gray"/>

            </LinearLayout>
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
Jules Mena
  • 67
  • 8

3 Answers3

1

Replace

Fragment fragment1 = new SettingsViewFragment();

With this

Fragment fragment1 = new SettingsViewFragment();
FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.Main_Layout,fragment1);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
androidXP
  • 1,692
  • 3
  • 27
  • 58
  • This compiles and makes more sense to me, but still nothing is happening. I can't get onClick to work whatsoever. Do you think I need to move this code in my receiver class to an adapter class instead? – Jules Mena Dec 04 '17 at 22:40
  • You can move it anywhere and i personally handle all the on item click listener of recyclerview on adapter. But it should work here also. Try with printing log inside onClick – androidXP Dec 04 '17 at 23:32
  • I might try that. It's not working in the receiver class at all.. I tried testing with log output and nothing happened! – Jules Mena Dec 04 '17 at 23:51
  • Show your complete code then it will help us to figure out what exactly problem is – androidXP Dec 05 '17 at 00:22
  • okay I have edited my question to include my full code now. Onclick is still not working at all – Jules Mena Dec 05 '17 at 06:55
  • Where you filling data in cardview and where is adapter – androidXP Dec 05 '17 at 07:55
  • I don't have any sort of adapter. All I have is this XML class (content_main.xml) as a layout and my activity class (DashboardActivity.java) – Jules Mena Dec 05 '17 at 15:05
0

Change Fragment fragment1 = new SettingsViewFragment (); by Toast.makeText (mActivity, "onCLick", Toast.LENGTH_LONG) .show ();

//if show toast The problem is not the onClick, the problem is when loading the fragment

Gustavo Mora
  • 843
  • 1
  • 7
  • 18
  • Ok. The problem is onClick, not the fragments. I think I need to make an Adapter class and delete this Receiver class I'm currently working in. That's what I keep reading... – Jules Mena Dec 04 '17 at 21:56
0

Fragments should be instantiated with the newInstance() static method.

cd1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment settingsFragment = SettingsFragment.newInstance();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.contentView, settingsFragment);
        fragmentTransaction.commit();
    }

});

In the SettingsFragment

 public static SettingsFragment newInstance() {
            return new SettingsFragment();
 }

XML for content_main

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/dash_settings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        ...

     </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
        android:id="@+id/dash_profile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        ...

        </android.support.v7.widget.CardView>

      <android.support.v7.widget.CardView
        android:id="@+id/dash_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        ...

        </android.support.v7.widget.CardView>

    <FrameLayout
        android:id="@+id/contentView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    </FrameLayout>

</LinearLayout>

This will transact the fragment into the FrameLayout that is positioned below the CardViews. Now this is probably not ideal, you would most likely want to launch a new activity that will host the fragment but this should work. Unless this is already in some Activity that hosts the fragment. But the key code is in the onClick.

Azethoth
  • 2,276
  • 1
  • 13
  • 10
  • "Fragments should be instantiated with the newInstance() static method." Not true. You can do it for your own convenience. But it's not a rule – Revaz Shalikashvili Dec 04 '17 at 22:21
  • While technically true, it is good practice, and a Google recommended practice due to how Fragment constructors are used by the System. This is also the only way to reliably pass data to a Fragment. See this stack post explaining it: https://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – Azethoth Dec 04 '17 at 22:28
  • Read the post and I agree with you now. Gonna rewrite all nonstatic instantiations tomorrow:) – Revaz Shalikashvili Dec 04 '17 at 23:33