Hide element from Activity

Asked

Viewed 496 times

3

I am using an Activity that has the following elements:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    tools:context=".MapsActivity" />

<Button
    android:id="@+id/evento_btn"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="66dp"
    android:layout_height="66dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="215dp"
    android:layout_marginBottom="25dp"
    android:drawableBottom="@drawable/button" />

<Button
    android:id="@+id/manu_btn"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="wrap_content"
    android:layout_height="90dp"
    android:layout_alignEnd="@+id/evento_btn"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="-86dp"
    android:layout_marginBottom="18dp"
    android:drawableBottom="@drawable/menu_btn" />

And I’m trying to hide the elements when the user clicks on the screen, to give more space for the user to interact with the map (same principle of google maps when you click on the map and the menu and the other buttons are hidden). For that I tried the following code:

    manu_btn = (Button) findViewById(R.id.evento_btn);
    evento_btn =(Button) findViewById(R.id.evento_btn);
    manu_btn.setText("+0");
    evento_btn.setText("+0");
     handler = new Handler();
     handler.postDelayed(runnable,7000);


}

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        evento_btn.setVisibility(View.INVISIBLE);
        evento_btn.setVisibility(View.INVISIBLE);

    }
};

It works, but it doesn’t fulfill what was proposed. Any idea how I can do this?

4 answers

2

Try to change the:

evento_btn.setVisibility(View.INVISIBLE);

for

evento_btn.setVisibility(View.GONE);

View.INVISIBLE will hide the item, but it will keep its space, while View.GONE will remove even the space of the screen object.

  • Yes, however I need the buttons to be hidden only when there is the touch on the screen and they return when the user touches again.

  • You can make an onClickListener that has a boolean to know if it was clicked or not, you have already solved?

1


"I need the buttons to be hidden only when there is the touch on the screen and they return when the user touches again."

Use the onUserInteraction of your Society, follow an example:

public class Main2Activity extends AppCompatActivity {
    private static final long DELAY_MS = 7000;
    private boolean hidden = false;
    private Runnable hideRunnable;
    private Handler hideHandler = new Handler();

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

        // ...

        hideRunnable = () -> { // Java 8 Lambda
            hidden = true;
            manu_btn.setVisibility(View.GONE);
            evento_btn.setVisibility(View.GONE);
        };

        // Quando a activity for criada, se o usuário não interagir com ela,
        // os botões serão automaticamente esondidos em 7 segundos
        hideHandler.postDelayed(hideRunnable, DELAY_MS);
    }

    @Override
    public void onUserInteraction() {
        super.onUserInteraction();

        // Cancelar ações pendentes
        hideHandler.removeCallbacks(hideRunnable);

        if (hidden) {
            manu_btn.setVisibility(View.VISIBLE);
            evento_btn.setVisibility(View.VISIBLE);
            hidden = false;
        } else {
            hideHandler.postDelayed(hideRunnable, DELAY_MS);
        }
    }

}

0

evento_btn.setVisibility(View.GONE); to "hide" the button and evento_btn.setVisibility(View.VISIBLE); to show the button again

You can use

if(evento_btn.getVisibility() == View.VISIBLE){ evento_btn.setVisibility(View.GONE); } else if (evento_btn.getVisibility() == View.GONE){ evento_btn.setVisibility(View.VISIBLE); }

0

INVISIBLE

This view is invisible, but still occupies space for layout purposes.

GONE

This view is invisible and does not take up space for layout purposes.

As you can read, INVISIBLE will hide the view, but it will take up your space, but GONE will hide the view and lose the space of the main view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.