2

Hello guys I am basically doing a piano app. After much thought I think I've figured how most piano apps are done and I'm stuck here and this seems to be very crucial to get all the other functionality such as the slide,multitouch,adding keys,etc.

Is it possible to know the dimensions of my drawable button before Hand? Say I have basically two drawable key button, piano keys C and D:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom" >

<Button
    android:id="@+id/ckey"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/keybutton" />

<Button
    android:id="@+id/dkey"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/keybutton" />

For a piano app(white keys), they both use the same selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
    android:drawable="@drawable/key"
    android:state_pressed="true"/>

<item 
    android:drawable="@drawable/key_pressed"/>
</selector>

But I would like to know the dimensions OR the location BEFOREHAND of the Button created so that I can draw a region on Top of that button and use that region for onTouchListener. I need that region so that I can use onTouch.

@Override
public boolean onTouch(View v, MotionEvent event) {

int numberOfKeys = 2;
Region[] keyBoard = new Region[2]; //for 2 White Piano Keys
Integer pointerIndex = event.getActionIndex();
Float x = event.getX(pointerIndex);
Float y = event.getY(pointerIndex);

for(int j=0;j<1;j++){
    if(this.keyBoard[j].contains(x.intValue(),y.intValue())){
           //play corresponding sound

        }

}

So knowing the dimensions of my images: key.png and key_pressed.png and the screen width and height and maybe other parameters I don't know. is It possible to know beforehand the dimensions or COORDINATES or Location of my buttons before the app is launched?

Otherwise, how can I get the coordinates? it seems getTop() and getLeft() are not good options because they return 0 since the images take time to load therefore the code cannot retrieve it.

Thanks guys. I'm super noob by the way. I apologize if I missed something.

  • 1
    You don't need to know before hand. Search SO for `GlobalLayoutListener`. Some answers to the questions will also lead you to places in the life cycle other than onCreate() where you may be able to do this. In onCreate(), the layout is not yet built and dimensions will always be zero. – Simon Aug 20 '13 at 20:22
  • This doc may be helpful: http://developer.android.com/reference/android/view/ViewTreeObserver.html – kabuko Aug 20 '13 at 20:27
  • What's wrong with using `setOnTouchListener(OnTouchListener)` on the `Button` itself? Why draw a region above it? – Vikram Aug 20 '13 at 20:28
  • 1
    Alternative to `GlobalLayoutListener`: [post](http://developer.android.com/reference/android/view/View.html#post%28java.lang.Runnable%29) a `Runnable` to e.g. the `Button` and initiate your region from there. See:http://stackoverflow.com/questions/3602026/linearlayout-height-in-oncreate-is-0 – zapl Aug 20 '13 at 20:41
  • @zapl Oooh, I like that. Thanks! – Simon Aug 20 '13 at 20:56

1 Answers1

0

You can set the buttons' width/height in the layout - instead of just using wrap_content

ex: android:layout_width="100dp"

**EDIT: Don't do this it's deprecated ** You could also use an AbsoluteLayout(rather than your current LinearLayout if you want to set exactly where things will show up.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Dan Schnau
  • 1,505
  • 14
  • 17