I have one fragment with lsitview.
when i click on the button nothing happens expect animation.
I try fab.bringToFront();
but it's not working too.
I think problem with my OnclickListener but i dont know to fix that.
this my BlankFragment4.java
package com.example.vbg.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.Set;
public class BlankFragment4 extends ListFragment {
private static final String TAG = "Bluetooth Settings";
private ArrayAdapter<String> mNewDevicesArrayAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank_fragment4, container, false);
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.bringToFront();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "FAB was Pressed");
Toast.makeText(getActivity(), "FAB Pressed", Toast.LENGTH_LONG).show();
}
});
mNewDevicesArrayAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1);
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
getActivity().registerReceiver(receiver, filter);
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();
if (!bluetooth.isEnabled()) {
Toast.makeText(getActivity(), "bluetooth is off", Toast.LENGTH_LONG).show();
}
View rootView = inflater.inflate(R.layout.fragment_blank_fragment4, container,
false);
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
mNewDevicesArrayAdapter.add(deviceName + "\n" + deviceHardwareAddress);
}
}
setListAdapter(mNewDevicesArrayAdapter);
return rootView;
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress();
mNewDevicesArrayAdapter.add(device.getName()+"\n"+ device.getAddress());
mNewDevicesArrayAdapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "one new device find", Toast.LENGTH_SHORT).show();// MAC address
}
}
};
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "Connecting... ", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(receiver);
}
}
this is my xml file. fragment_blank_fragment4
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment4">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="@drawable/ic_bluetooth_searching_white_24dp"
app:fabSize="normal"
app:elevation="4dp"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"></ListView>
</RelativeLayout>
Remove this line
View rootView = inflater.inflate(R.layout.fragment_blank_fragment4, container,
false);
You are adding the fab onClickListener to view from the first inflate. In onCreateView of fragment you should inflate only once, and return the view at the end of the function.