Question

I'm trying to set the weight of a textview and checkbox in a tablerow programmatically. After some research I found that the 3rd parameter of LayoutParams is used to do this. However, it does not seem to be working. Could someone take a look at this code and see what the problem is? Thanks. (Edited with LinearLayout suggestion)

public LayerSelectorView(Context context) 
{
    super(context);
    this.context = context;
    float density = context.getResources().getDisplayMetrics().density;

    linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    final LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    linearLayout.setLayoutParams(mainParams);

    titleText = new TextView(context);
    titleText.setTextAppearance(context, android.R.style.TextAppearance_Large);
    titleText.setText("Select Layers");
    titleText.setTextColor(Color.WHITE);
    titleText.setId(id);

    final LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    titleParams.setMargins(20, 20, 0, 0);
    titleText.setLayoutParams(titleParams);

    linearLayout.addView(titleText);
}
public void updateView()
{
    overlays = ((MapActivity)context).getMapView().getOverlays();

    loadLayers();

    this.addView(linearLayout);

    linearLayout.setBackgroundColor(0x88000000);
    linearLayout.invalidate();
    this.invalidate();
}
public void loadLayers()
{
    TableLayout table = new TableLayout(context);
    final LinearLayout.LayoutParams tableParams = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    table.setLayoutParams(tableParams);
    int i = id+1;
    for(final MapOverlay mo: overlays)
    {       
        TableRow tblRow = new TableRow(context);

        TextView layer = new TextView(context);
        TableRow.LayoutParams trParams = new TableRow.LayoutParams(0,
                             LayoutParams.FILL_PARENT, 1f);
        layer.setTextAppearance(context, android.R.style.TextAppearance_Large);
        layer.setText(mo.getName());
        layer.setTextColor(Color.WHITE);
        layer.setId(i);
        i++;
        layer.setLayoutParams(trParams);

        final CheckBox chk = new CheckBox(context);
        TableRow.LayoutParams trParams1 = new TableRow.LayoutParams(0, 
                             LayoutParams.FILL_PARENT, .2f);
        chk.setLayoutParams(trParams1);
        chk.setFocusable(false);
        chk.setClickable(false);
        chk.setChecked(true);

        tblRow.addView(layer);  
        tblRow.addView(chk);
        table.addView(tblRow);

        tblRow.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                if(mo.isVisible())
                {
                    mo.setVisible(false);
                    chk.setChecked(false);
                }
                else
                {
                    mo.setVisible(true);
                    chk.setChecked(true);
                }
            }
        });
    }
    linearLayout.addView(table);
}
Was it helpful?

Solution

You can't use weights with RelativeLayout. You need to use LinearLayout. I don't know if that will work for you, but I hope it helps none the less.

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