-4

I have two activities, trying to navigate from the first one to the second one by pressing a button. First one is like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_first);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_message);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(FirstActivity.this, SecondActivity.class);
            startActivity(myIntent);
        }
    });
}

And the second one is like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_second);
   // other stuff
}

When the Floating Action Button is pressed then, absolutely nothing happens. What is wrong with my code?

Update: This is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="...">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".firstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="secondActivity"></activity>

    </application>

</manifest>

And this is the code for Floating Action Button:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    //Button button = (Button) findViewById(R.id.button);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast toast = Toast.makeText(getApplicationContext(), "Pressed!", Toast.LENGTH_LONG);
            toast.show();
                        }
    });

In XML:

 <android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:id="@+id/fab"
    android:tint="@android:color/white"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    app:fabSize="mini"
    android:layout_alignParentRight="true" />

The whole layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:id="@+id/fab"
        android:tint="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        app:fabSize="mini"
        android:layout_alignParentRight="true" />

    <ListView
        android:id="@+id/list_of_messages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:clickable="true"
        android:divider="@android:color/transparent"
        android:dividerHeight="16dp"
        android:focusable="auto"
        android:focusableInTouchMode="true" />
</RelativeLayout>
Mohsen
  • 43
  • 1
  • 9
  • 1
    Check in your manifest, if you added your `SecondActivity` – tahsinRupam Dec 07 '17 at 06:06
  • you don't have entry of SecondActivity in AndroidManifest.xml – Rahul Sharma Dec 07 '17 at 06:06
  • 1
    Did you added SecondActivity in Manifest? I should crash i think. But you are implying that nothing happen ? – ADM Dec 07 '17 at 06:06
  • share your manifest – yogesh lokhande Dec 07 '17 at 06:08
  • post your manifest file code here – Abdul Waheed Dec 07 '17 at 06:09
  • the manifest is now there – Mohsen Dec 07 '17 at 06:18
  • You have written secondActivity instead of SecondActivity in manifest file – Dhruv Dec 07 '17 at 06:18
  • 1
    hi i want to suggest you firstly print log in on click so you can notice that actually on click function is working or not – Mohammad irshad sheikh Dec 07 '17 at 06:24
  • Actually, the onClickListener is not working! What seems to be the problem? – Mohsen Dec 07 '17 at 06:51
  • I'd say either you're not clicking the button you think you are, or there's something wrong with the layout; e.g., something's covering the button. Is there visual feedback on the button when you click? – Mike M. Dec 07 '17 at 06:53
  • can you print log from stacktrace? – global_warming Dec 07 '17 at 06:53
  • I updated my question.Now you can see the FAB code. – Mohsen Dec 07 '17 at 06:59
  • Which is it? `R.id.fab_new_message` or `R.id.fab`? Seems like you're getting things mixed up. – Mike M. Dec 07 '17 at 06:59
  • It's R.id.fab. Both in Java code and in xml layout. Don't worry about that! – Mohsen Dec 07 '17 at 07:02
  • Don't worry about that? You've shown two completely different snippets. One for `R.id.fab_new_message` that tries to start an `Activity`, and one for `R.id.fab` that shows a `Toast`. Why did you post the second one, if it's the first of you're having a problem with? Are you _certain_ you're not mixing things up? – Mike M. Dec 07 '17 at 07:07
  • does original problem still exists? Are you able to move to `SecondActivity` without using `fab`? does toast gets popped while fab is pressed? – global_warming Dec 07 '17 at 07:08
  • I appreciate the time you are spending on this. Yes, I am certain nothing is getting mixed up. At this point, I have a Floating Action Button (fab), and this is the listener code: fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast toast = Toast.makeText(getApplicationContext(), "Pressed!", Toast.LENGTH_LONG); toast.show(); } }); The toast doesn't get popped. – Mohsen Dec 07 '17 at 07:24
  • OK, if you change your code, please don't leave the old code in the question. As I mentioned earlier, is something possibly covering the `FloatingActionButton`? Is there any visual feedback when you press the button; e.g., the ripple effect, or the button lowering? If that's the problem, we'd need to see the whole layout, not just the ``. – Mike M. Dec 07 '17 at 07:29
  • There is no visual feedback when I press it. The whole layout is now there. – Mohsen Dec 07 '17 at 07:33
  • The `FloatingActionButton` is underneath the `ListView`. Move it to after the `ListView` in the layout. – Mike M. Dec 07 '17 at 07:33
  • Problem Solved! As you said, the listview was covering it. Thank you very much. – Mohsen Dec 07 '17 at 07:35

2 Answers2

0

Try changing to this inside your Manifest.xml file:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".FirstActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity"></activity>

</application>

EDIT-
To bring FAB to front use:
fab.bringToFront(); before setting onClickListener to it.

Thanks for pointing out Mike M

global_warming
  • 823
  • 1
  • 7
  • 10
0

Use Below code to resolve your problem, working for me :

  FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />
global_warming
  • 823
  • 1
  • 7
  • 10