Question

i am developing an android app. i need to display textview at right side in tablerow, below is the code i am using, what is wrong with this code???

ScrollView src;
src=(ScrollView)findViewById(R.id.scrollView1);

TableLayout t1=new TableLayout(context);

TableRow.LayoutParams tableRowParams=new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT);                        
tableRowParams.gravity=Gravity.RIGHT | Gravity.CENTER_VERTICAL;

for(int i=0;i<count;i++)
{
    TableRow tr=new TableRow(context);
    TextView txt_peron_name=new TextView(context);
    txt_peron_name.setTextColor(Color.BLACK);
    txt_peron_name.setTextSize(15);
    txt_peron_name.setText("Hello");
    //txt_peron_name.setLayoutParams(tableRowParams);
    tr.setBackgroundResource(R.drawable.cc1);
    //tr.setLayoutParams(tableRowParams);
    tr.addView(txt_peron_name);
    t1.addView(tr,tableRowParams);

}// End of For Loop
src.removeAllViews();   

src.addView(t1);
Was it helpful?

Solution 2

I implemented this code.

ScrollView sv = new ScrollView(StackDemosActivity.this);
TableLayout t1 = new TableLayout(StackDemosActivity.this);

TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT,
        TableRow.LayoutParams.MATCH_PARENT);
tableRowParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;

for (int i = 0; i < 25; i++) {
    TableRow tr = new TableRow(StackDemosActivity.this);
    TextView txt_peron_name = new TextView(StackDemosActivity.this);
    txt_peron_name.setTextColor(Color.BLACK);
    txt_peron_name.setTextSize(15);
    txt_peron_name.setText("Hello");
    tr.addView(txt_peron_name);
    tr.setGravity(Gravity.RIGHT);
    t1.addView(tr, tableRowParams);
}// End of For Loop
ll = (LinearLayout) findViewById(R.id.ll);
ll.removeAllViews();
sv.addView(t1);
ll.addView(sv);

I changed some code like taking LinearLayout then adding ScrollView inside. So this was minor changes I have made.

Output

enter image description here

OTHER TIPS

Assign gravity right to your text view in your XML.

OR Programmatically

txt_peron_name.setGravity(Gravity.RIGHT);

try this also

txt_peron_name.setLayoutParams(this.rowParams);//Layout params of your row

@Hitesh Kamani try this

TextView ta  = (TextView) findViewById(R.layout.text_view);
       LayoutParams lp = new LayoutParams();
       lp.gravity= Gravity.RIGHT; 
 ta.setLayoutParams(lp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top