vlio20
vlio20

Reputation: 9295

extend tablerow view

I am trying to programmatically create this xml view by extending tablerow view:

 <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:weightSum="1.0" >

        <Button
            android:id="@+id/tester_button"
            style="?android:attr/buttonStyleSmall"
            android:layout_weight=".2"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="A" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:layout_weight=".7"
            android:text="Column 1"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_weight=".1"
            android:layout_width="0dip"
            android:layout_height="wrap_content" />

    </TableRow>

This is my class of the extended row:

public class CategoryRow extends TableRow {
    LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
    LayoutParams innerParams = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
    CheckBox check;
    Button arrowButton;
    TextView categoryTitle;

    public CategoryRow(Context context) {
        super(context);
        this.setLayoutParams(rowParams);
        this.setGravity(Gravity.CENTER);
        this.setWeightSum(1);

        check = new CheckBox(context);
        innerParams.weight = (float) 0.1;
        check.setLayoutParams(innerParams);

        arrowButton = new Button(context);
        innerParams.weight = (float) 0.2;
        arrowButton.setLayoutParams(innerParams);
        arrowButton.setText("A");

        categoryTitle = new TextView(context);
        innerParams.weight = (float) 0.7;
        categoryTitle.setLayoutParams(innerParams);
        categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);

        //add views to row
        this.addView(arrowButton);
        this.addView(categoryTitle);
        this.addView(check);
    }

    public void setCategoryTitle(String title){
        categoryTitle.setText(title);
    }

    public void checkCategory(){
        check.setChecked(true);
    }

    public void unCheckCategory(){
        check.setChecked(false);
    }
}

in my activity I am adding the CategoryRow view to the TableLayout by addView function.

public class NotificationCategorise extends Activity {
    TableLayout categoryConteiner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_categorise);

        categoryConteiner = (TableLayout) findViewById(R.id.categories_conteiner);
        CategoryRow categoryRow = new CategoryRow(this);
        categoryRow.setCategoryTitle("bla");
        categoryConteiner.addView(categoryRow);


    }

My problem is that it the CategoryRow not added to the activity sceen.

Upvotes: 0

Views: 1351

Answers (2)

vlio20
vlio20

Reputation: 9295

I have changed the extended class to this code, and it worked fine.

import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableRow;
import android.widget.TextView;

public class CategoryRow extends TableRow {
    LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    LayoutParams innerParams1 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
    LayoutParams innerParams2 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
    LayoutParams innerParams3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
    CheckBox check;
    Button arrowButton;
    TextView categoryTitle;

    public CategoryRow(Context context) {
        super(context);
        this.setLayoutParams(rowParams);
        this.setWeightSum(1f);

        check = new CheckBox(context);
        innerParams1.weight = 0.1f;
        check.setLayoutParams(innerParams1);

        arrowButton = new Button(context);
        innerParams2.weight = 0.2f;
        arrowButton.setLayoutParams(innerParams2);
        arrowButton.setText("A");

        categoryTitle = new TextView(context);
        categoryTitle.setGravity(Gravity.CENTER);
        innerParams3.weight = 0.7f;
        categoryTitle.setLayoutParams(innerParams3);
        categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);

        //add views to row
        this.addView(arrowButton);
        this.addView(categoryTitle);
        this.addView(check);
    }

    public void setCategoryTitle(String title){
        categoryTitle.setText(title);
    }

    public void checkCategory(){
        check.setChecked(true);
    }

    public void unCheckCategory(){
        check.setChecked(false);
    }
}

Upvotes: 1

confused_at_times
confused_at_times

Reputation: 545

Ok, I don't think you can pass in the GRAVITY.CENTER as an argument for the LayoutParams constructor for the row. You change it for each individual View added to the row, but not for the row itself. Change:

LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);

to

LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

See if that works.

Upvotes: 0

Related Questions