Utilizing Android's advanced feature of overlapping buttons to create multiple states

I'm currently developing a Piano app for Android. In my XML layout, I have created two rectangular buttons representing white and black keys. The challenge I'm facing is getting the black key to overlap the two white keys, similar to how a real piano looks.

Each button has two states - pressed and unpressed, with each state having its own drawable XML file. When pressing the black key, everything works as expected. However, when I press a white key, the new drawable shape seems to overlap the black key.

It seems that since I defined the black key last in the XML layout, it should naturally be on top of the white keys. Perhaps the issue arises from accessing a new drawable file as the source for the button when it's pressed, causing it to draw the view on top. Is there a solution to prevent this overlapping behavior? Below, I've included the code for each XML file. Thank you for any assistance!

Main XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#ffcccccc"
android:gravity="top|center"
tools:context=".MainActivity">
<Button
    android:layout_width="45dp"
    android:layout_height="150dp"
    android:layout_marginTop="100dp"
    android:background="@drawable/wselector"
    android:id="@+id/button1"


    />

<Button
    android:layout_width="45dp"
    android:layout_height="150dp"
    android:layout_marginTop="100dp"
    android:background="@drawable/wselector"
    android:id="@+id/button2"
    android:layout_toRightOf="@id/button1"

    />
<Button
    android:layout_width="40dp"
    android:layout_height="100dp"
    android:layout_marginTop="100dp"
    android:background="@drawable/selector"
    android:id="@+id/button3"
    android:layout_toRightOf="@id/button1"
    android:layout_marginLeft="-20dp"
    android:onClick="soundmmm"
    />

Selector XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/wpressed"
    android:paddingTop="10sp"></item>
<item android:drawable="@drawable/wnormal" android:layout_marginTop="50dp">
</item>
</selector>

Unpressed XML:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shadow" />

<item android:bottom="@dimen/layer_padding">
    <shape android:shape="rectangle">
        <corners android:radius="@dimen/corner_radius" />
        <solid android:color="#ffffffff"/>
    </shape>
</item>
</layer-list>

Pressed XML:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#00000000"/>
    </shape>
</item>
<item android:top="@dimen/layer_padding">
    <shape android:shape="rectangle">
        <corners android:radius="@dimen/corner_radius" />
        <solid android:color="#ffffffff" />
    </shape>
</item>
</layer-list>

Answer №1

To enhance the appearance of each Button, enclose it within a FrameLayout as shown below:

    <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/button1">

    <Button
        android:id="@+id/button2"
        android:layout_width="45dp"
        android:layout_height="150dp"
        android:layout_marginTop="100dp"
        android:background="@drawable/wselector"
        android:onClick="soundmmm" />
</FrameLayout>

(This example showcases only the right white key.) Android is expected to respect the layering of the FrameLayouts by keeping the white key image positioned behind the black key.

Although effective, implementing this method may complicate your layout. If you can identify an alternative approach that does not require additional setup, it would be preferable. However, if necessary, this solution should suffice for your needs.

Answer №2

android:stateListAnimator="@null"

Prevent button from coming to the forefront on click

This solution solved my issue. It appears that the problem was not with the button states, but rather with the default animations applied to Android buttons.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Failure occurred when attempting to redirect in HTML

Within my HTML code snippet, I have included a section that pertains to notifications. The main issue I am encountering is related to the redirection behavior when clicking on the "Home" link inside the designated notification container with the ID "notifi ...

Retrieve information from a MySQL database and transfer it to an Android application using PHP

I am currently in the process of creating an application that has the capability to exchange data with a mysql server, among other functions. The app utilizes a php script to establish a connection to the mysql server. I have successfully implemented the ...

Focusing on a specific image using Jquery

I am looking to specifically target the image within the "hero1project3" class, however, the image is currently set as a background. Is there a way in jQuery to apply effects like blur only to the image itself, for example, using ".hero1project3 img"? HTM ...

What's the reason for my badge being an oval shape?

I am attempting to design a badge that hovers above my cart icon and shows the total number of items currently in the cart. However, I have encountered an issue where the badge appears as an oval shape rather than a perfect circle. Can anyone assist with ...

Select the radio button by hovering the mouse over it

Is there a way to check my radio buttons when hovering over them without having to click? I want to display photos based on the selected radio button. I attempted this using JQuery, but I am still learning and consider myself a beginner. ...

Error encountered while attempting to insert new elements into an ArrayList simultaneously

When trying to incorporate MapOverlays into my MapView, I encountered the following error: java.util.ConcurrentModificationException at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569) at com.google.android.maps.OverlayBundle.draw(OverlayBun ...

The modal window pops up immediately upon the first click

Experience a dynamic modal element that springs to life with just the click of a button or an image. The magic lies in the combination of HTML, CSS, and jQuery code: <div id="modal-1" class="modal"> <div class="button modal-button" data-butto ...

Tips for customizing the appearance of a specific mat-button component in an Angular project

In my Angular application, I have set up a scenario where users are presented with multiple choices through buttons. When a user clicks on a button, a specific component is displayed based on their choice. I am currently working on enhancing the styling of ...

The jQuery dropdown menu smoothly expands to reveal all hidden submenu options

I'm currently encountering an issue with jQuery. I am trying to create a responsive drop-down menu with sub-menus. The behavior I want is that if the window width is less than 700px, the submenus will trigger onClick. And if the window is wider than 7 ...

What CSS property prevents a fixed header from overlapping a scrolled element?

After creating a fixed header for my HTML table, I noticed that as I scroll down the page, everything is being overlapped by the fixed header except for one slider (noUiSlider). I am curious to know which CSS property is preventing the header from overlayi ...

Is there a way to use CSS to make a div expand as much as it can?

I am attempting to accomplish the following; Do you think this is achievable? ...

Is it possible to trigger a reflow prior to initiating a lengthy JavaScript operation?

Ready to face the criticism, I understand that this question has been asked many times before, and I am aware that there are likely more efficient ways to achieve what I'm trying to do... In a JavaScript function, I have a process that can take up to ...

Assessing the performance of YUi and BackBone

As I embark on a new project, one of the crucial decisions to make is regarding the architecture. Currently, I am contemplating the selection of front-end components. I plan to utilize HTML5, CSS3, and Javascript for this purpose. When it comes to choos ...

The height of the container is not adjusted correctly after loading data via ajax which causes it to overlap with

My HTML code consists of a container with two columns (content and sidebar) and a footer. The content contains a horizontal list with li elements that are populated via ajax when the "Load More" button is pressed. I am using Bootstrap and testing on Chrome ...

Assistance with centering elements using pure CSS in responsive web design

Seeking assistance as I am facing a challenge with aligning the sponsors section on the website mentioned below. Despite implementing responsive web design, I am struggling to center the sponsors correctly. Your guidance is appreciated... ...

Exploring the Efficiency of XPath Child Navigation Techniques

Currently, I am utilizing lxml with Python 2.7. If we have a node called node and a child element named child_element, what distinguishes the following two operations: node.xpath('./child_element') node.xpath("*[local-name()='child_element ...

Two objects precisely located in the same spot yet not visible simultaneously

There are two elements here. One is a transparent button, and the other is an image positioned behind it. The background property didn't work for me, so I attempted different variations without success: .top-container > button { background-ima ...

What is the inability to place a div that is 30% wide alongside another div that is 70% wide, and keep them in a horizontal line?

If I make a div that is 30% wide, and another div that is 70% wide, they do not align horizontally. To fix this, I need to ensure that the combined widths of the two divs are less than 100%. Here's an example of how it can be done: <div id="wrapp ...

Preserving the video's aspect ratio by limiting the width and height to a maximum of 100%

I am trying to integrate a YouTube video using their embed code in a "pop-up". However, I am facing an issue where the video does not resize to fit within the height of its parent. I want it to be constrained by the div#pop-up that contains the video. Curr ...

What could be causing the hover effect to not work on other elements within the div?

I am working on creating a card that displays only the image when not hovered, but expands and reveals additional information in a div to the right of the image when hovered. Unfortunately, when I hover over the image and then move towards the adjacent div ...