1

I am new to Android programming.

My question is what is the best way to determine the width and height of a subview?

I am writing an application which includes a piano keyboard for input.

I have a custom view, PianoKeyboardView. I need to know the dimensions of my View in order to draw the keyboard. If I stuff the width and height from the following code into my PianoKeyboardView my piano keyboard draws OK.

Display display = ((WindowManager) this 
        .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    int width = display.getWidht();
    int height = display.getHeight();

Obviously I don’t want to do this.

I created a default android application in Eclipse and selected the FullScreen option. The default code for onCreate is:

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

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

When I call getWidth() and getHeight() on the default Views, I get 0’s.

    int width = controlsView.getWidth();
    int height = controlsView.getHeight();
    width = contentView.getWidth();
    height = contentView.getHeight();

My PianoKeyboardView width and height is also 0, and that is my problem.

My activity_fullscreen.xml sets the width and height for all the views as “match_parent”

<FrameLayout    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="?buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:orientation="horizontal"
        tools:ignore="UselessParent" >

        <com.application.piano.PianoKeyboardView
        android:id="@+id/keyboard_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             android:layout_weight="1"
        />

Thanks.

2 Answers2

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

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    ViewTreeObserver viewTreeObserver = controlsView.getViewTreeObserver();
    viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int width = controlsView.getWidth();
                int height = controlsView.getHeight();
                return true;
            }
    });

Hope this helps you.....

Sreejith Krishnan R
  • 1,244
  • 10
  • 12
  • Thank you so much! Yes, when my onDraw() method is invoke in my PianoKeyboardView, width and height have been initialized. So I just delay initializing my keyboard until the first time onDraw is invoked. – user1933620 Dec 28 '12 at 06:19
0

You could try something based on this... (copied from my answer to a related question).

I use the following technique - post a runnable from onCreate() that will be executed when the view has been created:

    contentView = findViewById(android.R.id.content);
    contentView.post(new Runnable()
    {
        public void run()
        {
            contentHeight = contentView.getHeight();
        }
    });

This code will run on the main UI thread, after onCreate() has finished. At that point the view has got a size.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255