Home » Android » java – Android Studio: Button always appears at the front

java – Android Studio: Button always appears at the front

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a RelativeLayout to which I add Views.

I added a button to it, and the button always appears in front of all the other Views that are added to it, regardless of the order in which things were added. How come?

I’m coding purely in Java, no XML.

Here’s a simple example, the button will appear here in front of the text, even though the text was added last:

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

    RelativeLayout layout = new RelativeLayout(this);

    Button button = new Button(this);
    TextView text = new TextView(this);

    button.setText("Button");
    text.setText("Text");

    layout.addView(button);
    layout.addView(text);

    setContentView(layout);
}
How to&Answers:

Starting with Lollipop, a StateListAnimator controlling elevation was added to the default Button style. In my experience, this forces buttons to appear above everything else regardless of placement in XML (or programmatic addition in your case). That might be what you’re experiencing.

To resolve this you can add a custom StateListAnimator if you need it or simply set it to null if you don’t.

XML:

android:stateListAnimator="@null"

Java:

Button button = new Button(this);
button.setText("Button");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    button.setStateListAnimator(null);
}

More details:
Android 5.0 android:elevation Works for View, but not Button?

Answer:

In the Android 5.0 (API 21) and above, you must add android:elevation into the view.

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

    RelativeLayout layout = new RelativeLayout(this);

    Button button = new Button(this);
    TextView text = new TextView(this);

    button.setText("Button");
    text.setText("Text");
    button.setElevation(3.0f); // add this line, you could try with values > 3.0f

    layout.addView(button);
    layout.addView(text);

    setContentView(layout);
}

Answer:

From android developer docs :

By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams.

http://developer.android.com/guide/topics/ui/layout/relative.html

Try the following snippet :

RelativeLayout layout = new RelativeLayout(this);

        Button button = new Button(this);
        TextView text = new TextView(this);

        button.setId(View.generateViewId());
        text.setId(View.generateViewId());
        button.setText("Button");
        text.setText("Text");

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, text.getId());

        button.setLayoutParams(params);
        layout.addView(button);
        layout.addView(text);

Answer:

The button appears to float to the forefront of the RelativeLayout it’s in so…

Try this:

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

    Button button = new Button(this);
    button.setText("Button");

    RelativeLayout groupContainingButton = new RelativeLayout(this);
    groupContainingButton.addView(button);

    TextView text = new TextView(this);
    text.setText("Text");

    RelativeLayout activityLayout = new RelativeLayout(this);
    activityLayout.addView(groupContainingButton);
    activityLayout.addView(text);

    setContentView(activityLayout);
}

Answer:

Check the button’s state (enabled/disabled):

loginButton.setEnabled(false);