How do I get a floating button like uber












-2















I want to create a floating button like Uber. Uber shows a floating button when online and hides it when offline. Also "DU recorder" app has the floating button.



I want the floating button remain on top of all apps and be movable on any place on screen.



I have Samsung Galaxy S7 Edge with Android 8 (Oreo)










share|improve this question


















  • 1





    take a look here androidhive.info/2016/11/…

    – Skenia
    Nov 13 '18 at 18:29
















-2















I want to create a floating button like Uber. Uber shows a floating button when online and hides it when offline. Also "DU recorder" app has the floating button.



I want the floating button remain on top of all apps and be movable on any place on screen.



I have Samsung Galaxy S7 Edge with Android 8 (Oreo)










share|improve this question


















  • 1





    take a look here androidhive.info/2016/11/…

    – Skenia
    Nov 13 '18 at 18:29














-2












-2








-2








I want to create a floating button like Uber. Uber shows a floating button when online and hides it when offline. Also "DU recorder" app has the floating button.



I want the floating button remain on top of all apps and be movable on any place on screen.



I have Samsung Galaxy S7 Edge with Android 8 (Oreo)










share|improve this question














I want to create a floating button like Uber. Uber shows a floating button when online and hides it when offline. Also "DU recorder" app has the floating button.



I want the floating button remain on top of all apps and be movable on any place on screen.



I have Samsung Galaxy S7 Edge with Android 8 (Oreo)







android floating-action-button floating






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:25









MckMck

2410




2410








  • 1





    take a look here androidhive.info/2016/11/…

    – Skenia
    Nov 13 '18 at 18:29














  • 1





    take a look here androidhive.info/2016/11/…

    – Skenia
    Nov 13 '18 at 18:29








1




1





take a look here androidhive.info/2016/11/…

– Skenia
Nov 13 '18 at 18:29





take a look here androidhive.info/2016/11/…

– Skenia
Nov 13 '18 at 18:29












2 Answers
2






active

oldest

votes


















0














use this code 

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

private float downRawX, downRawY;
private float dX, dY;

public MovableFloatingActionButton(Context context) {
super(context);
init();
}

public MovableFloatingActionButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
setOnTouchListener(this);
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent){

int action = motionEvent.getAction();
if (action == MotionEvent.ACTION_DOWN) {

downRawX = motionEvent.getRawX();
downRawY = motionEvent.getRawY();
dX = view.getX() - downRawX;
dY = view.getY() - downRawY;

return true; // Consumed

}
else if (action == MotionEvent.ACTION_MOVE) {

int viewWidth = view.getWidth();
int viewHeight = view.getHeight();

View viewParent = (View)view.getParent();
int parentWidth = viewParent.getWidth();
int parentHeight = viewParent.getHeight();

float newX = motionEvent.getRawX() + dX;
newX = Math.max(0, newX); // Don't allow the FAB past the left hand side of the parent
newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the FAB past the right hand side of the parent

float newY = motionEvent.getRawY() + dY;
newY = Math.max(0, newY); // Don't allow the FAB past the top of the parent
newY = Math.min(parentHeight - viewHeight, newY); // Don't allow the FAB past the bottom of the parent

view.animate()
.x(newX)
.y(newY)
.setDuration(0)
.start();

return true; // Consumed

}
else if (action == MotionEvent.ACTION_UP) {

float upRawX = motionEvent.getRawX();
float upRawY = motionEvent.getRawY();

float upDX = upRawX - downRawX;
float upDY = upRawY - downRawY;

if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
return performClick();
}
else { // A drag
return true; // Consumed
}

}
else {
return super.onTouchEvent(motionEvent);
}

}

}

//-------------------this code inside the xml file -----------
<com.example.MovableFloatingActionButton
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"
android:src="@drawable/ic_navigate_next_white_24dp"/>





share|improve this answer
























  • Though This gives me a floating button it is tied to my MainActivity Layout. I want this to be not tied to my MainActivity layout. I want it always available whether my app is in the foreground or background.

    – Mck
    Nov 15 '18 at 2:46



















0














Your question contains multiple questions so let us break it down and take it to step by step.



For Creating Floating Button:



I would say use @Vishal Sharma answer about this question in this page



And about showing and hiding it when the user is online or offline:



public class NetworkStateReceiver extends BroadcastReceiver {

protected Set<NetworkStateReceiverListener> listeners;
protected Boolean connected;

public NetworkStateReceiver() {
listeners = new HashSet<NetworkStateReceiverListener>();
connected = null;
}

public void onReceive(Context context, Intent intent) {
if(intent == null || intent.getExtras() == null)
return;

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();

if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
connected = true;
} else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
connected = false;
}

notifyStateToAll();
}

private void notifyStateToAll() {
for(NetworkStateReceiverListener listener : listeners)
notifyState(listener);
}

private void notifyState(NetworkStateReceiverListener listener) {
if(connected == null || listener == null)
return;

if(connected == true)
listener.networkAvailable();
else
listener.networkUnavailable();
}

public void addListener(NetworkStateReceiverListener l) {
listeners.add(l);
notifyState(l);
}

public void removeListener(NetworkStateReceiverListener l) {
listeners.remove(l);
}

public interface NetworkStateReceiverListener {
public void networkAvailable();
public void networkUnavailable();
}
}


YOUR ACTIVITY:



public class MyActivity implements NetworkStateReceiverListener {
private NetworkStateReceiver networkStateReceiver;
}


IN YOUR ACTIVITY: INSTANTIATE THE RECEIVER



public void onCreate(Bundle savedInstanceState) {
networkStateReceiver = new NetworkStateReceiver();
networkStateReceiver.addListener(this);
this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}

public void onDestroy() {
super.onDestroy();
networkStateReceiver.removeListener(this);
this.unregisterReceiver(networkStateReceiver);
}


IN YOUR ACTIVITY: IMPLEMENTS THE REQUIRED METHODS



@Override
public void networkAvailable() {
/* TODO: show your button here */
}

@Override
public void networkUnavailable() {
/* TODO: hide your button here */
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287341%2fhow-do-i-get-a-floating-button-like-uber%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    use this code 

    import android.content.Context;
    import android.support.design.widget.FloatingActionButton;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;

    public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

    private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

    private float downRawX, downRawY;
    private float dX, dY;

    public MovableFloatingActionButton(Context context) {
    super(context);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
    }

    private void init() {
    setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent){

    int action = motionEvent.getAction();
    if (action == MotionEvent.ACTION_DOWN) {

    downRawX = motionEvent.getRawX();
    downRawY = motionEvent.getRawY();
    dX = view.getX() - downRawX;
    dY = view.getY() - downRawY;

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_MOVE) {

    int viewWidth = view.getWidth();
    int viewHeight = view.getHeight();

    View viewParent = (View)view.getParent();
    int parentWidth = viewParent.getWidth();
    int parentHeight = viewParent.getHeight();

    float newX = motionEvent.getRawX() + dX;
    newX = Math.max(0, newX); // Don't allow the FAB past the left hand side of the parent
    newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the FAB past the right hand side of the parent

    float newY = motionEvent.getRawY() + dY;
    newY = Math.max(0, newY); // Don't allow the FAB past the top of the parent
    newY = Math.min(parentHeight - viewHeight, newY); // Don't allow the FAB past the bottom of the parent

    view.animate()
    .x(newX)
    .y(newY)
    .setDuration(0)
    .start();

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_UP) {

    float upRawX = motionEvent.getRawX();
    float upRawY = motionEvent.getRawY();

    float upDX = upRawX - downRawX;
    float upDY = upRawY - downRawY;

    if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
    return performClick();
    }
    else { // A drag
    return true; // Consumed
    }

    }
    else {
    return super.onTouchEvent(motionEvent);
    }

    }

    }

    //-------------------this code inside the xml file -----------
    <com.example.MovableFloatingActionButton
    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"
    android:src="@drawable/ic_navigate_next_white_24dp"/>





    share|improve this answer
























    • Though This gives me a floating button it is tied to my MainActivity Layout. I want this to be not tied to my MainActivity layout. I want it always available whether my app is in the foreground or background.

      – Mck
      Nov 15 '18 at 2:46
















    0














    use this code 

    import android.content.Context;
    import android.support.design.widget.FloatingActionButton;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;

    public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

    private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

    private float downRawX, downRawY;
    private float dX, dY;

    public MovableFloatingActionButton(Context context) {
    super(context);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
    }

    private void init() {
    setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent){

    int action = motionEvent.getAction();
    if (action == MotionEvent.ACTION_DOWN) {

    downRawX = motionEvent.getRawX();
    downRawY = motionEvent.getRawY();
    dX = view.getX() - downRawX;
    dY = view.getY() - downRawY;

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_MOVE) {

    int viewWidth = view.getWidth();
    int viewHeight = view.getHeight();

    View viewParent = (View)view.getParent();
    int parentWidth = viewParent.getWidth();
    int parentHeight = viewParent.getHeight();

    float newX = motionEvent.getRawX() + dX;
    newX = Math.max(0, newX); // Don't allow the FAB past the left hand side of the parent
    newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the FAB past the right hand side of the parent

    float newY = motionEvent.getRawY() + dY;
    newY = Math.max(0, newY); // Don't allow the FAB past the top of the parent
    newY = Math.min(parentHeight - viewHeight, newY); // Don't allow the FAB past the bottom of the parent

    view.animate()
    .x(newX)
    .y(newY)
    .setDuration(0)
    .start();

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_UP) {

    float upRawX = motionEvent.getRawX();
    float upRawY = motionEvent.getRawY();

    float upDX = upRawX - downRawX;
    float upDY = upRawY - downRawY;

    if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
    return performClick();
    }
    else { // A drag
    return true; // Consumed
    }

    }
    else {
    return super.onTouchEvent(motionEvent);
    }

    }

    }

    //-------------------this code inside the xml file -----------
    <com.example.MovableFloatingActionButton
    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"
    android:src="@drawable/ic_navigate_next_white_24dp"/>





    share|improve this answer
























    • Though This gives me a floating button it is tied to my MainActivity Layout. I want this to be not tied to my MainActivity layout. I want it always available whether my app is in the foreground or background.

      – Mck
      Nov 15 '18 at 2:46














    0












    0








    0







    use this code 

    import android.content.Context;
    import android.support.design.widget.FloatingActionButton;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;

    public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

    private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

    private float downRawX, downRawY;
    private float dX, dY;

    public MovableFloatingActionButton(Context context) {
    super(context);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
    }

    private void init() {
    setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent){

    int action = motionEvent.getAction();
    if (action == MotionEvent.ACTION_DOWN) {

    downRawX = motionEvent.getRawX();
    downRawY = motionEvent.getRawY();
    dX = view.getX() - downRawX;
    dY = view.getY() - downRawY;

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_MOVE) {

    int viewWidth = view.getWidth();
    int viewHeight = view.getHeight();

    View viewParent = (View)view.getParent();
    int parentWidth = viewParent.getWidth();
    int parentHeight = viewParent.getHeight();

    float newX = motionEvent.getRawX() + dX;
    newX = Math.max(0, newX); // Don't allow the FAB past the left hand side of the parent
    newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the FAB past the right hand side of the parent

    float newY = motionEvent.getRawY() + dY;
    newY = Math.max(0, newY); // Don't allow the FAB past the top of the parent
    newY = Math.min(parentHeight - viewHeight, newY); // Don't allow the FAB past the bottom of the parent

    view.animate()
    .x(newX)
    .y(newY)
    .setDuration(0)
    .start();

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_UP) {

    float upRawX = motionEvent.getRawX();
    float upRawY = motionEvent.getRawY();

    float upDX = upRawX - downRawX;
    float upDY = upRawY - downRawY;

    if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
    return performClick();
    }
    else { // A drag
    return true; // Consumed
    }

    }
    else {
    return super.onTouchEvent(motionEvent);
    }

    }

    }

    //-------------------this code inside the xml file -----------
    <com.example.MovableFloatingActionButton
    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"
    android:src="@drawable/ic_navigate_next_white_24dp"/>





    share|improve this answer













    use this code 

    import android.content.Context;
    import android.support.design.widget.FloatingActionButton;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;

    public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

    private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

    private float downRawX, downRawY;
    private float dX, dY;

    public MovableFloatingActionButton(Context context) {
    super(context);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
    }

    private void init() {
    setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent){

    int action = motionEvent.getAction();
    if (action == MotionEvent.ACTION_DOWN) {

    downRawX = motionEvent.getRawX();
    downRawY = motionEvent.getRawY();
    dX = view.getX() - downRawX;
    dY = view.getY() - downRawY;

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_MOVE) {

    int viewWidth = view.getWidth();
    int viewHeight = view.getHeight();

    View viewParent = (View)view.getParent();
    int parentWidth = viewParent.getWidth();
    int parentHeight = viewParent.getHeight();

    float newX = motionEvent.getRawX() + dX;
    newX = Math.max(0, newX); // Don't allow the FAB past the left hand side of the parent
    newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the FAB past the right hand side of the parent

    float newY = motionEvent.getRawY() + dY;
    newY = Math.max(0, newY); // Don't allow the FAB past the top of the parent
    newY = Math.min(parentHeight - viewHeight, newY); // Don't allow the FAB past the bottom of the parent

    view.animate()
    .x(newX)
    .y(newY)
    .setDuration(0)
    .start();

    return true; // Consumed

    }
    else if (action == MotionEvent.ACTION_UP) {

    float upRawX = motionEvent.getRawX();
    float upRawY = motionEvent.getRawY();

    float upDX = upRawX - downRawX;
    float upDY = upRawY - downRawY;

    if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
    return performClick();
    }
    else { // A drag
    return true; // Consumed
    }

    }
    else {
    return super.onTouchEvent(motionEvent);
    }

    }

    }

    //-------------------this code inside the xml file -----------
    <com.example.MovableFloatingActionButton
    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"
    android:src="@drawable/ic_navigate_next_white_24dp"/>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 18:36









    Vishal SharmaVishal Sharma

    1




    1













    • Though This gives me a floating button it is tied to my MainActivity Layout. I want this to be not tied to my MainActivity layout. I want it always available whether my app is in the foreground or background.

      – Mck
      Nov 15 '18 at 2:46



















    • Though This gives me a floating button it is tied to my MainActivity Layout. I want this to be not tied to my MainActivity layout. I want it always available whether my app is in the foreground or background.

      – Mck
      Nov 15 '18 at 2:46

















    Though This gives me a floating button it is tied to my MainActivity Layout. I want this to be not tied to my MainActivity layout. I want it always available whether my app is in the foreground or background.

    – Mck
    Nov 15 '18 at 2:46





    Though This gives me a floating button it is tied to my MainActivity Layout. I want this to be not tied to my MainActivity layout. I want it always available whether my app is in the foreground or background.

    – Mck
    Nov 15 '18 at 2:46













    0














    Your question contains multiple questions so let us break it down and take it to step by step.



    For Creating Floating Button:



    I would say use @Vishal Sharma answer about this question in this page



    And about showing and hiding it when the user is online or offline:



    public class NetworkStateReceiver extends BroadcastReceiver {

    protected Set<NetworkStateReceiverListener> listeners;
    protected Boolean connected;

    public NetworkStateReceiver() {
    listeners = new HashSet<NetworkStateReceiverListener>();
    connected = null;
    }

    public void onReceive(Context context, Intent intent) {
    if(intent == null || intent.getExtras() == null)
    return;

    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = manager.getActiveNetworkInfo();

    if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
    connected = true;
    } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
    connected = false;
    }

    notifyStateToAll();
    }

    private void notifyStateToAll() {
    for(NetworkStateReceiverListener listener : listeners)
    notifyState(listener);
    }

    private void notifyState(NetworkStateReceiverListener listener) {
    if(connected == null || listener == null)
    return;

    if(connected == true)
    listener.networkAvailable();
    else
    listener.networkUnavailable();
    }

    public void addListener(NetworkStateReceiverListener l) {
    listeners.add(l);
    notifyState(l);
    }

    public void removeListener(NetworkStateReceiverListener l) {
    listeners.remove(l);
    }

    public interface NetworkStateReceiverListener {
    public void networkAvailable();
    public void networkUnavailable();
    }
    }


    YOUR ACTIVITY:



    public class MyActivity implements NetworkStateReceiverListener {
    private NetworkStateReceiver networkStateReceiver;
    }


    IN YOUR ACTIVITY: INSTANTIATE THE RECEIVER



    public void onCreate(Bundle savedInstanceState) {
    networkStateReceiver = new NetworkStateReceiver();
    networkStateReceiver.addListener(this);
    this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
    }

    public void onDestroy() {
    super.onDestroy();
    networkStateReceiver.removeListener(this);
    this.unregisterReceiver(networkStateReceiver);
    }


    IN YOUR ACTIVITY: IMPLEMENTS THE REQUIRED METHODS



    @Override
    public void networkAvailable() {
    /* TODO: show your button here */
    }

    @Override
    public void networkUnavailable() {
    /* TODO: hide your button here */
    }





    share|improve this answer




























      0














      Your question contains multiple questions so let us break it down and take it to step by step.



      For Creating Floating Button:



      I would say use @Vishal Sharma answer about this question in this page



      And about showing and hiding it when the user is online or offline:



      public class NetworkStateReceiver extends BroadcastReceiver {

      protected Set<NetworkStateReceiverListener> listeners;
      protected Boolean connected;

      public NetworkStateReceiver() {
      listeners = new HashSet<NetworkStateReceiverListener>();
      connected = null;
      }

      public void onReceive(Context context, Intent intent) {
      if(intent == null || intent.getExtras() == null)
      return;

      ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo ni = manager.getActiveNetworkInfo();

      if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
      connected = true;
      } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
      connected = false;
      }

      notifyStateToAll();
      }

      private void notifyStateToAll() {
      for(NetworkStateReceiverListener listener : listeners)
      notifyState(listener);
      }

      private void notifyState(NetworkStateReceiverListener listener) {
      if(connected == null || listener == null)
      return;

      if(connected == true)
      listener.networkAvailable();
      else
      listener.networkUnavailable();
      }

      public void addListener(NetworkStateReceiverListener l) {
      listeners.add(l);
      notifyState(l);
      }

      public void removeListener(NetworkStateReceiverListener l) {
      listeners.remove(l);
      }

      public interface NetworkStateReceiverListener {
      public void networkAvailable();
      public void networkUnavailable();
      }
      }


      YOUR ACTIVITY:



      public class MyActivity implements NetworkStateReceiverListener {
      private NetworkStateReceiver networkStateReceiver;
      }


      IN YOUR ACTIVITY: INSTANTIATE THE RECEIVER



      public void onCreate(Bundle savedInstanceState) {
      networkStateReceiver = new NetworkStateReceiver();
      networkStateReceiver.addListener(this);
      this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
      }

      public void onDestroy() {
      super.onDestroy();
      networkStateReceiver.removeListener(this);
      this.unregisterReceiver(networkStateReceiver);
      }


      IN YOUR ACTIVITY: IMPLEMENTS THE REQUIRED METHODS



      @Override
      public void networkAvailable() {
      /* TODO: show your button here */
      }

      @Override
      public void networkUnavailable() {
      /* TODO: hide your button here */
      }





      share|improve this answer


























        0












        0








        0







        Your question contains multiple questions so let us break it down and take it to step by step.



        For Creating Floating Button:



        I would say use @Vishal Sharma answer about this question in this page



        And about showing and hiding it when the user is online or offline:



        public class NetworkStateReceiver extends BroadcastReceiver {

        protected Set<NetworkStateReceiverListener> listeners;
        protected Boolean connected;

        public NetworkStateReceiver() {
        listeners = new HashSet<NetworkStateReceiverListener>();
        connected = null;
        }

        public void onReceive(Context context, Intent intent) {
        if(intent == null || intent.getExtras() == null)
        return;

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = manager.getActiveNetworkInfo();

        if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
        connected = true;
        } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
        connected = false;
        }

        notifyStateToAll();
        }

        private void notifyStateToAll() {
        for(NetworkStateReceiverListener listener : listeners)
        notifyState(listener);
        }

        private void notifyState(NetworkStateReceiverListener listener) {
        if(connected == null || listener == null)
        return;

        if(connected == true)
        listener.networkAvailable();
        else
        listener.networkUnavailable();
        }

        public void addListener(NetworkStateReceiverListener l) {
        listeners.add(l);
        notifyState(l);
        }

        public void removeListener(NetworkStateReceiverListener l) {
        listeners.remove(l);
        }

        public interface NetworkStateReceiverListener {
        public void networkAvailable();
        public void networkUnavailable();
        }
        }


        YOUR ACTIVITY:



        public class MyActivity implements NetworkStateReceiverListener {
        private NetworkStateReceiver networkStateReceiver;
        }


        IN YOUR ACTIVITY: INSTANTIATE THE RECEIVER



        public void onCreate(Bundle savedInstanceState) {
        networkStateReceiver = new NetworkStateReceiver();
        networkStateReceiver.addListener(this);
        this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
        }

        public void onDestroy() {
        super.onDestroy();
        networkStateReceiver.removeListener(this);
        this.unregisterReceiver(networkStateReceiver);
        }


        IN YOUR ACTIVITY: IMPLEMENTS THE REQUIRED METHODS



        @Override
        public void networkAvailable() {
        /* TODO: show your button here */
        }

        @Override
        public void networkUnavailable() {
        /* TODO: hide your button here */
        }





        share|improve this answer













        Your question contains multiple questions so let us break it down and take it to step by step.



        For Creating Floating Button:



        I would say use @Vishal Sharma answer about this question in this page



        And about showing and hiding it when the user is online or offline:



        public class NetworkStateReceiver extends BroadcastReceiver {

        protected Set<NetworkStateReceiverListener> listeners;
        protected Boolean connected;

        public NetworkStateReceiver() {
        listeners = new HashSet<NetworkStateReceiverListener>();
        connected = null;
        }

        public void onReceive(Context context, Intent intent) {
        if(intent == null || intent.getExtras() == null)
        return;

        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = manager.getActiveNetworkInfo();

        if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
        connected = true;
        } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
        connected = false;
        }

        notifyStateToAll();
        }

        private void notifyStateToAll() {
        for(NetworkStateReceiverListener listener : listeners)
        notifyState(listener);
        }

        private void notifyState(NetworkStateReceiverListener listener) {
        if(connected == null || listener == null)
        return;

        if(connected == true)
        listener.networkAvailable();
        else
        listener.networkUnavailable();
        }

        public void addListener(NetworkStateReceiverListener l) {
        listeners.add(l);
        notifyState(l);
        }

        public void removeListener(NetworkStateReceiverListener l) {
        listeners.remove(l);
        }

        public interface NetworkStateReceiverListener {
        public void networkAvailable();
        public void networkUnavailable();
        }
        }


        YOUR ACTIVITY:



        public class MyActivity implements NetworkStateReceiverListener {
        private NetworkStateReceiver networkStateReceiver;
        }


        IN YOUR ACTIVITY: INSTANTIATE THE RECEIVER



        public void onCreate(Bundle savedInstanceState) {
        networkStateReceiver = new NetworkStateReceiver();
        networkStateReceiver.addListener(this);
        this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
        }

        public void onDestroy() {
        super.onDestroy();
        networkStateReceiver.removeListener(this);
        this.unregisterReceiver(networkStateReceiver);
        }


        IN YOUR ACTIVITY: IMPLEMENTS THE REQUIRED METHODS



        @Override
        public void networkAvailable() {
        /* TODO: show your button here */
        }

        @Override
        public void networkUnavailable() {
        /* TODO: hide your button here */
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 18:50









        hossam scotthossam scott

        1




        1






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287341%2fhow-do-i-get-a-floating-button-like-uber%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python