javaandroidalignmentandroid-imageviewtablelayout

Alignment of imageview in tablelayout


This should be easy, how can I align the star in the middle. I'm adding the top header textviews and stars dynamically in code. I tried setting gravity on the imageview did not work.

UPDATE: I added iv.setScaleType(ImageView.ScaleType.FIT_START) now star aligns to left

enter image description here enter image description here

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
   <TableLayout  
    android:id="@+id/TableAch"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="@color/white"
    android:layout_alignParentTop="true"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    >

        <TableRow  
        android:id="@+id/RowAchTitle"  
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"  
        android:gravity="center_horizontal"
        >  

            <TextView  
                android:id="@+id/ViewAchTitle"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:textStyle="bold"  
                android:text="Achievements"  
                android:gravity="center"  
                android:layout_span="6">
            </TextView>  

        </TableRow>  
       <TableRow  
        android:id="@+id/RowAchHeader"  
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"
        android:weightSum="6"
        >   
        </TableRow>

    </TableLayout>



    </RelativeLayout>

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

            tbLayout = (TableLayout) this.findViewById(R.id.TableAch);
            rowHeader = (TableRow) this.findViewById(R.id.RowAchHeader);

            FlurryAgent.logEvent("Achievements"); 

            TableRow.LayoutParams tableRowParams=
                      new TableRow.LayoutParams
                      (TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);

            tableRowParams.setMargins(0, 0, 0, 5);


            TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);

            params.weight = 1;
            params.gravity= Gravity.CENTER;

            int fontSize = 12;

            TextView tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("     ");
            tv.setTextSize(fontSize);
            rowHeader.addView(tv); //This is blank top left corner

            for (Level level : Level.values()) { //Then we add all the headers for games
                tv = new TextView(this);

                tv.setText("EASY");
                tv.setTextSize(fontSize);
                tv.setLayoutParams(params);
                rowHeader.addView(tv);
            }

            //Next we add the levels for each game, putting a pass image where passed



            for (Games  game : Games.values()) {
                TableRow newRow = new TableRow(this);
                newRow.setLayoutParams(tableRowParams);

                String[] words = game.name().split("(?=[A-Z])");
                String name = "";
                for (String word : words) {
                    if(word.length() < 1) continue;
                    name += word + "\n";
                }

                tv = new TextView(this);
                tv.setTextSize(fontSize);
                tv.setText(name);
                tv.setLayoutParams(params);
                newRow.addView(tv);
                newRow.setWeightSum(6);

                for (Level level : Level.values()) {
                    ImageView iv = new ImageView(this);

                        iv.setScaleType(ImageView.ScaleType.FIT_START);
                    iv.setLayoutParams(params);

                    tv = new TextView(this);
                    tv.setLayoutParams(params);
                    tv.setText("EASY");
                    tv.setTextSize(fontSize);

                    if( acheivements.getPassed(level, game) )//has achievement
                    {
                        iv.setImageResource(R.drawable.star_gold_full_small);
                    }
                    else {
                        iv.setImageResource(R.drawable.star_gold_empty_small);
                    }
                    newRow.addView(tv);
                }
                tbLayout.addView(newRow);
            }

            setupAds(true);
     }

Solution

  • You should do this:

    TableRow.LayoutParams params = new TableRow.LayoutParams(0,WRAP_CONTENT);
    

    you should have layout params of tablerow for textviews.

    then in this params you should add weight.

    params.weight = 1;
    params.gravity = GRAVITY.CENTER;
    

    and your table row should have gravity "Center" for its content (not layout gravity).

    Try do this:

    for (Games  game : Games.values()) {
                    TableRow newRow = new TableRow(this);
                    newRow.setLayoutParams(tableRowParams);
    
    TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);
    
                    params2.weight = .8f;
                    params2.gravity= Gravity.CENTER;
    
                    String[] words = game.name().split("(?=[A-Z])");
                    String name = "";
                    for (String word : words) {
                        if(word.length() < 1) continue;
                        name += word + "\n";
                    }
    
                    tv = new TextView(this);
                    tv.setTextSize(fontSize);
                    tv.setText(name);
                    tv.setLayoutParams(params2);
                    newRow.addView(tv);
                    newRow.setWeightSum(6);
    
                    for (Level level : Level.values()) {
                        ImageView iv = new ImageView(this);
    
                        iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        iv.setAdjustViewBounds(true);
                        iv.setLayoutParams(params);
    
                        tv = new TextView(this);
                        tv.setLayoutParams(params);
                        tv.setText("EASY");
                        tv.setTextSize(fontSize);
    
                        if( acheivements.getPassed(level, game) )//has achievement
                        {
                            iv.setImageResource(R.drawable.star_gold_full_small);
                        }
                        else {
                            iv.setImageResource(R.drawable.star_gold_empty_small);
                        }
                        newRow.addView(tv);
                    }
                    tbLayout.addView(newRow);
                }
    

    In this code i have created new layout params as params2 because the textview was using weight of 1 so that your images were little right sided. i had used .8f weight for the names so now its working ok. so do as per your req. Hope it Helps!!