Search code examples
androidlayoutimageviewgravityandroid-framelayout

Layout API-dependent?


The idea I'm trying to perform is printing a guitar fretboard and on top of it some squares to point some notes. Well, a picture is worth a thousand words, so this is it running in Android 2.3.3

"Why do you need our help, so?!" Because I tried running it in Android 4.0.3 and this other one was the result. I realized (leaving only one square) that the squares were trying to be as big as they could, so I added dot[counter].setScaleType(ScaleType.CENTER) for every dot and got this(http://i.imgur.com/4yOJD.png Sorry, I can't post another hyperlink), while 2.3.3 remained the same as the first capture. This makes me unable to make setPadding() to every dot universally, as it is different in the two versions. Have to remark it is COMPLETELY the same code, just running in different AVDs (I also tried physical devices with the same result).

I also tried changing the layout's gravity in code and in xml, but made no effect.

I'm copying what I believe is relevant to the situation, but feel free to ask for anything else.

This is my simple layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/add"
    android:textAppearance="?android:attr/textAppearanceLarge" />


<FrameLayout
    android:id="@+id/layoutFrets"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" >

    <ImageView
        android:id="@+id/imageView1"
        android:scaleType="fitXY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fretboard" />

</FrameLayout>

And here is the code of the activity that generates and prints the dots:

To create the ImageViews:

for (int i = 0; i<notesN; i++)
        dot[i] = new ImageView(this);

And to print them I had to do this, as I need the size of the layout to print the dots OK

fl.post(new Runnable(){
        public void run() {
            int counter = 0;

            //Pointless constants go here to print the dots in their place              

            for (int strings = 0; strings < fretboard.NSTRINGS; strings++){
                for (int frets = 0; frets < fretboard.NFRETS; frets++){

                    if (fretboard.isOccupied(strings, frets)){
                        dot[counter].setScaleType(ScaleType.CENTER);
                        dot[counter].setImageResource(R.drawable.dot);  

                        //Quite a messy code... calculates the positions based
                        //on the constants above
                        dot[counter].setPadding(
                                (int) (-fl.getWidth() + 2 * fl.getWidth()
                                        * fretDistance[frets]),
                                (int) (fl.getHeight() * (stringDistance[strings])),
                                0, 0);

                        fl.addView(dot[counter]);
                        counter++;
                    }
                }
            }

        }

    });

That would address the last capture I gave you.

I also tried to init the ImageViews outside the post() method, with the same result.

Any idea what am I doing wrong? Thank you VERY much in advance :)


Solution

  • I woke up with the idea of changing the type of the layout... and it worked.

    Changed the FrameLayout for a relative layout and everything worked as expected.

    I'll still keep an eye on this thread in case someone can give an explanaition to why that happened.