Jedi Schmedi
Jedi Schmedi

Reputation: 818

ImageView pushed out from screen

I'm playing around in Android and what I'm trying to achieve is a 10x10tile boardgame. I want to read size and width of screen and then I want a square in the middle with a textViews above and below.

This is what I've done so far:

    public void init(){

        Point size = new Point();
        WindowManager w = getWindowManager();           

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
            w.getDefaultDisplay().getSize(size);
            screenWidth = size.x;
            screenHeight = size.y; 
        }else{
            Display d = w.getDefaultDisplay(); 
            screenWidth = d.getWidth(); 
            screenHeight = d.getHeight(); 
        }

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LinearLayout); 
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        TextView TVtop = new TextView(this.getApplicationContext());
        TVtop.setHeight((screenHeight-screenWidth)/2);
        TVtop.setWidth(screenWidth);
        TVtop.setText("TOP");

        TextView TVbot = new TextView(this.getApplicationContext());
        TVbot.setHeight((screenHeight-screenWidth)/2);
        TVbot.setWidth(screenWidth);
        TVbot.setText("BOT");

        TableLayout tableLayout = new TableLayout(this.getApplicationContext());

        //Make a cube
        LayoutParams tableParams = new LayoutParams(screenWidth, screenWidth);
        tableLayout.setLayoutParams(tableParams);

        //Populate tableLayout 
        for(int i = 0; i < nrOfTiles; i++){

            TableRow tableRow = new TableRow(this.getApplicationContext());

            for(int j = 0; j < nrOfTiles; j++){

                ImageView imgView = new ImageView(this.getApplicationContext());
                imgView.setImageResource(R.drawable.cell);
                tableRow.addView(imgView);
            }
            tableLayout.addView(tableRow);
        }

        linearLayout.addView(TVtop);
        linearLayout.addView(tableLayout);
        linearLayout.addView(TVbot);
    }
}

I've tried diffrent layoutparams but nothing seems to get the work done. :S

BR

Upvotes: 0

Views: 94

Answers (1)

Tigger
Tigger

Reputation: 9130

Personally, I would use a RelativeLayout and setMargins for the items.

While not a direct answer to your question, the following code will display 15 icons in three rows. This should be enough to explain and get you started.

The main activity.

public class MainActivity extends Activity {

    private int mWidth;
    private int mTile;
    private int mColMax = 5;
    private Context mContext;

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        // the screen width is need to work out the tile size
        WindowManager w = getWindowManager();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
            Point size = new Point();
            w.getDefaultDisplay().getSize(size);
            mWidth = size.x;
        }else{
            mWidth = w.getDefaultDisplay().getWidth(); 
        }

        // how wide (and high) each icon will be to fit the screen.
        mTile = (mWidth / mColMax);

        setContentView(R.layout.activity_main);
        // layout the icons
        initUI();
    }

    /**
     * Layout 15 icon images in three rows dynamically.
     */
    private void initUI() {
        // this is the layout from the XML
        ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout); 

        ImageView iv;
        RelativeLayout.LayoutParams params;

        int i = 0;
        int row = 0;
        int col = 0;
        do {
            params = new RelativeLayout.LayoutParams(mTile,mTile);
            params.setMargins((col * mTile), (row * mTile), 0, 0);
            iv = new ImageView(mContext);
            iv.setAdjustViewBounds(true);
            iv.setScaleType(ScaleType.FIT_CENTER);
            iv.setImageResource(R.drawable.ic_launcher);
            iv.setLayoutParams(params);
            layout.addView(iv);
            if (col == mColMax) {
                row++;
                col = 0;
            } else {
                col++;
            }
        } while (++i <= 16);
    }
}

And the layout XML.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</RelativeLayout>

Upvotes: 1

Related Questions