Question

We're working on an Android application and we want to relayout our UI when the View is shown. As we we're unable to calculate the height of a button in the XML file we want to calculate it and show it throw our Java code. But our method fit() doesn't work when it's called from onCreate, while it works when we call it from our Action overflow. We tried this code on the Android Simulator and on a Samsung Galaxy SIII. Can anybody help us? Thanks Fotoschnitzel

Here's the code:

public class Game extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classic);

            fit();

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.settings:

            fit();

            default:
                return super.onOptionsItemSelected(item);
        }

    }


    public void fit() {

        Button Button1=(Button)findViewById(R.id.grid1);
        Button Button2=(Button)findViewById(R.id.grid2);
        Button Button3=(Button)findViewById(R.id.grid3);
        Button Button4=(Button)findViewById(R.id.grid4);
        Button Button5=(Button)findViewById(R.id.grid5);
        Button Button6=(Button)findViewById(R.id.grid6);
        Button Button7=(Button)findViewById(R.id.grid7);
        Button Button8=(Button)findViewById(R.id.grid8);
        Button Button9=(Button)findViewById(R.id.grid9);
        LinearLayout Lin1=(LinearLayout)findViewById(R.id.lin1);
        LinearLayout Lin2=(LinearLayout)findViewById(R.id.lin2);
        LinearLayout Lin3=(LinearLayout)findViewById(R.id.lin3);
        LinearLayout Global=(LinearLayout)findViewById(R.id.LinearLayout1);
        LinearLayout Up=(LinearLayout)findViewById(R.id.bestfiller);
        ProgressBar Bar=(ProgressBar)findViewById(R.id.progressBar1);

        int x = Button1.getWidth();
        int y = Global.getHeight();
        int z = Bar.getHeight();

        int Filler=y-3*x-z;
        if (Filler > 50){
        Up.setLayoutParams(new LinearLayout.LayoutParams(3*x, Filler));
        Lin1.setLayoutParams(new LinearLayout.LayoutParams(3*x, x));
        Lin2.setLayoutParams(new LinearLayout.LayoutParams(3*x, x));
        Lin3.setLayoutParams(new LinearLayout.LayoutParams(3*x, x));} 
        double size= x*0.4;
        Button1.setTextSize((float)size);
        Button2.setTextSize((float)size);
        Button3.setTextSize((float)size);
        Button4.setTextSize((float)size);
        Button5.setTextSize((float)size);
        Button6.setTextSize((float)size);
        Button7.setTextSize((float)size);
        Button8.setTextSize((float)size);
        Button9.setTextSize((float)size);

    }

}

No correct solution

OTHER TIPS

I can assure you that the method is called also when the application starts. Your issue is another. You are checking for the Filler condition, but just after setContentView

    int x = Button1.getWidth();
    int y = Global.getHeight();
    int z = Bar.getHeight();

x, y, and z are 0, making the if condition false

The view hierarchy has not yet been measured and laid out so getWidth() and getHeight() will return 0.

You can post a Runnable to the UI thread message queue that calls your fit() method after the measure/layout pass. See How to retrieve the dimensions of a view?

Use OnGlobalLayoutListener.

rootView.getViewTreeObserver().addOnGlovalLayoutListener( new OnGlobalLayoutListener() {

    public void onGlobalLayout() {
        //all views are measured now and getWidth() and getHeight() will return correct values

        //don't forget to unregister the listener for future layout events
        rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

    }

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top