Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to add tablelayout dynamically
    primarykey
    data
    text
    <p>I am creating an application that needs <code>TableLayout</code> . I have done my coding with <code>LinearLayout</code> and it is working but i am not able to use <code>TableLayout</code> in my project .i am getting an error of <code>NullPoinerException</code> in <code>logcat</code> . So please give me some solution how to add <code>TableLayout</code> dynamically. thank u</p> <pre><code>public class player_name extends Activity { LinearLayout player_name; TableLayout ply_name = new TableLayout(this); Bundle b,b1; List&lt;TextView&gt; allEds = new ArrayList&lt;TextView&gt;(); List&lt;Button&gt; allplus = new ArrayList&lt;Button&gt;(); List&lt;Button&gt; allminus = new ArrayList&lt;Button&gt;(); List&lt;EditText&gt; alledit = new ArrayList&lt;EditText&gt;(); List&lt;TextView&gt; alltotal = new ArrayList&lt;TextView&gt;(); //List&lt;TableRow&gt; allrow = new ArrayList&lt;TableRow&gt;(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.player_name); //b = getIntent().getExtras(); //String resStr = b.getString("name"); b1 = getIntent().getExtras(); String[] result = b1.getStringArray("playerName"); player_name = (LinearLayout) findViewById(R.id.player_name); TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f); TextView[] ed1 = new TextView[result.length+1]; Button[] plus = new Button[result.length+1]; Button[] minus = new Button[result.length+1]; EditText[] point = new EditText[result.length+1]; TextView[] total = new TextView[result.length+1]; TableRow[] TR= new TableRow[result.length+1]; Button btnResult = new Button(player_name.this); btnResult.setText(" click here to get RESULT"); for(int i=0;i&lt;=(result.length-1);i++) { ed1[i] = new TextView(player_name.this); plus[i] = new Button(player_name.this); minus[i] = new Button(player_name.this); point[i] = new EditText(player_name.this); total[i] = new TextView(player_name.this); TR[i] = new TableRow(player_name.this); allEds.add(ed1[i]); alltotal.add(total[i]); alledit.add(point[i]); allplus.add(plus[i]); allminus.add(minus[i]); //allrow.add(TR[i]); TR[i].addView(ed1[i]); TR[i].addView(plus[i]); TR[i].addView(minus[i]); TR[i].addView(point[i]); TR[i].addView(total[i]); ply_name.addView(TR[i]); TR[i].setLayoutParams(tableRowParams); player_name.addView(ply_name); ed1[i].setId(i); ed1[i].setHeight(50); ed1[i].setWidth(300); ed1[i].setText(result[i]); ed1[i].setTextColor(Color.CYAN); total[i].setId(i); total[i].setHeight(50); total[i].setWidth(300); total[i].setText(""+0); total[i].setTextColor(Color.CYAN); point[i].setId(i); point[i].setHeight(50); point[i].setWidth(300); //point[i].setText(Integer.parseInt("0")); point[i].setHint("enter\t"+result[i]+"\'s\tpoint"); point[i].setInputType(InputType.TYPE_CLASS_NUMBER); point[i].setTextColor(Color.BLACK); plus[i].setId(i); plus[i].setHeight(50); plus[i].setWidth(300); plus[i].setText("+"); plus[i].setTextColor(Color.BLACK); minus[i].setId(i); minus[i].setHeight(50); minus[i].setWidth(300); minus[i].setText("-"); minus[i].setTextColor(Color.BLACK); } LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); player_name.addView(btnResult, lp); btnResult.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent1 = new Intent(player_name.this,result.class); startActivity(intent1); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload