Natraj
Natraj

Reputation: 417

Android Custom component not working

I am trying to develop a custom table through coding in Android. I am trying to create following layout

<LinearLayout>
    <Table>
        <TableRow> Title </TableRow>
        <TableRow> Header </TableRow>
    </Table>
    <ScrollView>
        <Table>
            <TableRow> Row1 </TableRow>
            <TableRow> Row2 </TableRow>
        </Table>
    </ScrollView>
</LinearLayout>

I saw few post here and this is very near to what I want but not fully.

The class declaration is

public class TableView extends TableLayout implements OnClickListener

Constructor

    public TableView(Activity context, String title, String[] headers,
        List<String[]> data) {
    super(context);
    _data = data;
    _title = title;
    _context = context;
    _headers = headers;
}

init code

        LinearLayout linearLayout = new LinearLayout(_context);
    // Create a new Table Layout
    TableLayout table = new TableLayout(_context); 
    
    // Set stretch and shrink for all the columns in the table
    table.setStretchAllColumns(true);  
    table.setShrinkAllColumns(false); 
    
    // Add the title row to the table  
    TableRow rowTitle = new TableRow(_context);  
    rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);  
    
    TableRow rowHeader = new TableRow(_context);
    
    // title column/row  
    TextView title = new TextView(_context);  
    title.setText("User Details");  
    
    // Set properties of title.
    title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);  
    title.setGravity(Gravity.CENTER);  
    title.setTypeface(Typeface.SERIF, Typeface.BOLD);  
    
    // Create a table layout params and add the param to span 6 columns
    TableRow.LayoutParams params = new TableRow.LayoutParams();  
    params.span = 6;  
    
    rowTitle.addView(title, params);
      
    // labels column  
    TextView headerLabel1 = new TextView(_context);  
    headerLabel1.setText("Company Name");  
    headerLabel1.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel1);
    
    TextView headerLabel2 = new TextView(_context);  
    headerLabel2.setText("User Type");  
    headerLabel2.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel2);

    TextView headerLabel3 = new TextView(_context);  
    headerLabel3.setText("Active");  
    headerLabel3.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel3);

    TextView headerLabel4 = new TextView(_context);  
    headerLabel4.setText("Mobile #");  
    headerLabel4.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel4);

    TextView headerLabel5 = new TextView(_context);  
    headerLabel5.setText("Login Id");  
    headerLabel5.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel5);

    TextView headerLabel6 = new TextView(_context);  
    headerLabel6.setText("TimeZone");  
    headerLabel6.setTypeface(Typeface.DEFAULT_BOLD);  
    
    rowHeader.addView(headerLabel6);
    
    table.addView(rowTitle);  
    table.addView(rowHeader);
    
    linearLayout.addView(table);

    // Add Data to row
    ScrollView dataScroll = new ScrollView(_context);
    LayoutParams dataParams = LP_WC_WC;
    dataParams.height = LayoutParams.FILL_PARENT;
    dataParams.width = LayoutParams.FILL_PARENT;
    dataScroll.setLayoutParams(dataParams);
    TableLayout rowTable = new TableLayout(_context);
    rowTable.setLayoutParams(LP_FP_WC);
    
    TableRow row = new TableRow(_context);;
    
    TextView dataText1 = new TextView(_context);  
    dataText1.setText("IBM");  
    dataText1.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText1);
    
    TextView dataText2 = new TextView(_context);  
    dataText2.setText("Administrator");  
    dataText2.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText2);
    
    TextView dataText3 = new TextView(_context);  
    dataText3.setText("Yes");  
    dataText3.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText3);
    
    TextView dataText4 = new TextView(_context);  
    dataText4.setText("123-123-1234");  
    dataText4.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText4);
    
    TextView dataText5 = new TextView(_context);  
    dataText5.setText("testcompany");  
    dataText5.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText5);
    
    TextView dataText6 = new TextView(_context);  
    dataText6.setText("UTC+5:30");  
    dataText6.setTypeface(Typeface.DEFAULT_BOLD);  
    
    row.addView(dataText6);
    
    rowTable.addView(row);
    dataScroll.addView(rowTable);
    
    linearLayout.addView(dataScroll);
    
    _context.setContentView(linearLayout);  

On executing this code I am seeing only the title and header bars not the contents. What I am missing?

Any pointer or link would help me.

@Edit : For people struct with same problem here is the solution that worked for me.

XML file which I created:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="true"
    android:stretchColumns="true" >

    <TableRow
        android:id="@+id/headerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/dataRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <HorizontalScrollView
                    android:id="@+id/horizontalScrollView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:id="@+id/linearLayout2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal" >

                        <TableLayout
                            android:id="@+id/dataTableLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent" >

                            <TableRow
                                android:id="@+id/actualDataRow"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" >

                                <TextView
                                    android:id="@+id/textView1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Large Text"
                                    android:textAppearance="?android:attr/textAppearanceLarge" />
                            </TableRow>
                        </TableLayout>
                    </LinearLayout>
                </HorizontalScrollView>
            </LinearLayout>
        </ScrollView>
    </TableRow>

    <TableRow
        android:id="@+id/footerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>
</TableLayout>

</LinearLayout>

The main activity which I had used to call the sub activity. In the main activity window I have added only a button which calls my custom component. The code of my main activity is

import in.ns.views.CustomView;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout.LayoutParams;

public class CustomComponentActivity extends Activity implements OnClickListener {
Button viewButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    viewButton = (Button) findViewById(R.id.button1);
    viewButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    CustomView cv = new CustomView(this);
    LayoutParams LP_FP_FP = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    //this.addView(cv);
    //this.addContentView(cv.getView(), LP_FP_FP);
    setContentView(cv.getView());
    Log.d("custom", "added view");
}
}

The class which is creating my custom component.

import in.ns.custom.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CustomView extends LinearLayout {

private Context _context;
private LayoutInflater _inflater;
private View _view;

public CustomView(Context context) {
    super(context);
    _context = context;
    _inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    _view = _inflater.inflate(R.layout.custom, this);
    init();
}

public View getView() {
    return _view;
}

private void init() {
    TableLayout tableLayout = (TableLayout) _view.findViewById(R.id.dataTableLayout);
    TableRow tableRow ; //(TableRow) tableLayout.getChildAt(0);
    
    tableLayout.removeAllViews();
    
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    for(int i = 0; i < 32; i++) {
        tableRow = new TableRow(_context);
        
        tv1 = new TextView(_context);
        tv1.setText("Success1 ");
        
        tableRow.addView(tv1);
        
        tv2 = new TextView(_context);
        tv2.setText("Success2 ");

        tableRow.addView(tv2);
        
        tv3 = new TextView(_context);
        tv3.setText("Success1 ");
        
        tableRow.addView(tv3);
        
        tv4 = new TextView(_context);
        tv4.setText("Success2 ");

        tableRow.addView(tv4);
        
        tv5 = new TextView(_context);
        tv5.setText("Success1 ");
        
        tableRow.addView(tv5);
        
        tv6 = new TextView(_context);
        tv6.setText("Success2 ");

        tableRow.addView(tv6);
        
        tableLayout.addView(tableRow);          
    }
    
    tableRow = new TableRow(_context);
    
    TextView tv11 = new TextView(_context);
    tv11.setText("Success11 ");
    
    tableRow.addView(tv11);
    
    TextView tv21 = new TextView(_context);
    tv21.setText("Success21 ");
    tableRow.addView(tv21);
    
    tableLayout.addView(tableRow);
}
}

Upvotes: 1

Views: 595

Answers (1)

Macarse
Macarse

Reputation: 93163

Instead of creating the views from the code, you could have used an XML and inflate it after calling the constructor.

There are a few mistakes in your code:

  • _context.setContentView(linearLayout);

That's not the correct way of doing it. You shouldn't pass an Activity around. Construct the view from the Activity and set the content view from it.

  • You are never adding a child to the instance.

There is no this.addChild() anywhere. Everything is created is not added to TableLayout.

Upvotes: 1

Related Questions