0

The wanted scenario: My app has started and is visible. I press button A and want to display the width of button bMap. In short my not working method:

// Is called by button bQuestions' onClick
public void QuestionsButton(View v) {

    Context context = getApplicationContext();

    // getting button bMap
    Button bMap = (Button)findViewById(R.id.bMap);


    // getting the width of button bMap
    int width = bMap.getMeasuredWidth(); // getWidht() also returns 0
    CharSequence text = String.valueOf(width);

    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

I know this question has been asked at least a million times, but in all those scenario's the getWidth() is called too soon. In my case it isn't as I have to press another existing button to call this method. Why does bMap still have a width of 0, long after it already exists?

What i did find out was that if i first press bMap en save the view as a variable, that it does work. But of course it's not an option to first let a button be pressed before the function of another button works.

Edit Complete implementation: MainscreenActivity.java:

package com.jswebcom.jeroensak.zuydopendag;

import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.Toast;


public class MainscreenActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainscreen);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

}

public void ActivityButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Activiteiten";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

public void MapButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Plattegrond";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();


}

public void InfoButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Info";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

public void QuizButton(View v) {

    Context context = getApplicationContext();
    CharSequence text = "Quizvraag";
    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();

}

public void QuestionsButton(View v) {

    Context context = getApplicationContext();

    Button bMap = (Button)findViewById(R.id.bMap);

    int width = bMap.getMeasuredWidth();
    CharSequence text = String.valueOf(width);

    int duration = Toast.LENGTH_SHORT;

    Toast.makeText(context, text, duration).show();


    bMap.animate().translationX(v.getWidth());

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_mainscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_mainscreen, container, false);
        return rootView;
    }
}
}

activity_mainscreen.xml:

<LinearLayout 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"
tools:context=".MainscreenActivity$PlaceholderFragment"
android:weightSum="2"
android:padding="0dp"
android:id="@+id/container">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:backgroundTint="#ff005baa"
        android:background="#ff005baa"
        android:text="@string/activiteiten"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:id="@+id/bActivity"
        android:onClick="ActivityButton" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ff66beff"
        android:backgroundTint="#ff66beff"
        android:text="@string/info"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="InfoButton"
        android:id="@+id/bInfo" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:backgroundTint="#ff0077c0"
        android:background="#ff0077c0"
        android:text="@string/vragen"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="QuestionsButton"
        android:id="@+id/bQuestions" />


</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ff66beff"
        android:backgroundTint="#ff66beff"
        android:text="@string/plattegrond"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="MapButton"
        android:id="@+id/bMap" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ff0077c0"
        android:backgroundTint="#ff0077c0"
        android:text="@string/quizvraag"
        android:textColor="#ffffff"
        android:textSize="25sp"
        android:onClick="QuizButton"
        android:id="@+id/bQuiz" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#ffffffff"
        android:id="@+id/bLogo" />


</LinearLayout>

Jay Ess
  • 59
  • 9

1 Answers1

1

Before calling

bMap.getMeasuredWidth();

You have to call:

bMap.measure(0,0);

From API:

The actual measurement work of a view is performed in onMeasure(int, int), called by this method.

So the call measure() starts the actual measurement, only after this, getMeasuredWidth() and getMeasuredHeight() will work.

Ziem
  • 6,579
  • 8
  • 53
  • 86
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • Great now it works! But now that i know it's width, I want to move it to the right by using bMap.animate().translationX(v.getWidth()); Though i can animate the View v from the method parameter, i can't animate bMap.. Somehow it feels like i can't really access bMap.. Is this because of a similar problem? – Jay Ess May 06 '15 at 14:12
  • I don´t know....for me it looks like everything is ok in Your code. have You tried just to set a value inside like bMap.animate().translationX(250); , does this work? – Opiatefuchs May 07 '15 at 06:13