Place Imageview Over Button Android

Place ImageView over Button android

The reason is actually very simple. :) We are so caught up thinking in 2D that we overlook the elevation - in Z.

There is nothing wrong with your first layout. The Button simply has a higher elevation than the ImageView - exactly 1dp higher. Therefore, no matter how you arrange the two views, the Button rises above.

A bit of proof:

A Button, by default gets the Widget.Material.Button style:

<!-- Bordered ink button -->
<style name="Widget.Material.Button">
<item name="background">@drawable/btn_default_material</item>
<item name="textAppearance">?attr/textAppearanceButton</item>
<item name="minHeight">48dip</item>
<item name="minWidth">88dip</item>
<item name="stateListAnimator">@anim/button_state_list_anim_material</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>

The attribute that introduces this elevation is android:stateListAnimator. StateListAnimator is similar to StateListDrawable, and provides state change animations. The complete xml is here: Link. But here's the base state of the button:

<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="@integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>

As you can see, the elevation value for the button is set to @dimen/button_elevation_material:

<dimen name="button_elevation_material">1dp</dimen>

And that's how the ImageView ends up being behind/below the Button.

So, what can we do?

A straight-forward solution would be to set the ImageView's elevation to the same amount - 1dp.

Another solution, which will require a bit of work, is to remove the Button's elevation rather than change ImageView's. Based on the default StateListAnimator, we can create our own - and remove the elevation. Then, in your res/values-v21/styles.xml, define a style that inherits from Widget.Material.Button:

<style name="MyDepressedButtonStyle" parent="android:Widget.Material.Button">
<item name="android:stateListAnimator">@anim/customized_state_animator</item>
</style>

Now, set this style on your Button:

<Button
style="@style/MyDepressedButtonStyle"
....
.... />

Edit:

Actually, we can apply the customized StateListAnimator directly:

<Button
android:stateListAnimator="@anim/customized_state_animator"
....
.... />

No need to take the scenic route!

I want to put image over button

u can use this:

android:drawableTop="@drawable/SomeIcon"

so then you get this:

<Button
android:id="@+id/damage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="about"
android:drawableTop="@android:drawable/ic_menu_info_details"/>

Sample Image

it works for me :)

Put a button over an ImageView

Don't put the image as a background, you don't have any control on how the image is scaled. Instead, create a RelativeLayout, and put an ImageView as one of the child, and you can place anything (buttons etc) as other RelativeLayout children.

<RelativeLayout ...>
<ImageView (your image) ...>
<Button (the button you want) ... />
</RelativeLayout>

Android ImageView disappears under Buttons in RelativeLayout

The problem was the default style of the Buttons.

So I endet up using TextView (a Button without a style) and everything worked fine.

Android - How do I add an ImageView over a button contained within a Grid Layout

You could stack an ImageView with the "X" vector as its image on top of the button and change its visibility once the button is clicked using an OnClickListener. Here is a simple example:

Layout:

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<ImageView
android:elevation="10dp"
android:id="@+id/iv_cross"
android:src="@drawable/ic_clear_24px"
android:visibility="invisible"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintTop_toTopOf="@id/btn"
app:layout_constraintBottom_toBottomOf="@id/btn"
app:layout_constraintLeft_toLeftOf="@id/btn"
app:layout_constraintRight_toRightOf="@id/btn"/>

Where "@drawable/ic_clear_24px" is the "X" vector.

Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = findViewById(R.id.btn);
final ImageView iv_cross = findViewById(R.id.iv_cross);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
iv_cross.setVisibility(View.VISIBLE);
}
});
}

Note that the visibility of the ImageView is set to invisible by default.

How to place a button below Imageview

Try this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="horizontal">

<TextView
android:id="@+id/cup_edit_image"
android:layout_width="40dp"
android:layout_height="38dp"
android:layout_gravity="center"
android:layout_marginLeft="50dp"
android:layout_marginRight="-70dp"
android:background="@drawable/btn_round"
android:gravity="center"
android:padding="@dimen/_5sdp"
android:src="@drawable/btn_round"
android:text="3+"
android:textColor="#fff" />

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/cup_user_profile"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/ic_userprofile_placeholder" />
</LinearLayout>


Related Topics



Leave a reply



Submit