0

In My application i want to create the layout as like below image:

enter image description here

So How to make it possible ?

I have done Something like below code:

public void showResult()
{

    List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
    for(int i = 0; i<=tempEmployerList.size()-1; i++)
    {    
        LinearLayout innerLayout = new LinearLayout(this);
        innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        innerLayout.setBackgroundColor(0x00000000);

        // set the Multiple TextView
        TextView mHeading = new TextView(getApplicationContext());
        TextView middleValue = new TextView(getApplicationContext());
        TextView aImageView = new TextView(getApplicationContext());

        mHeading.setText("\n"+"1");
        //mHeading.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        mHeading.setTextColor(0xFFFF0000);
        mHeading.setPadding(3, 0, 0, 0);


        middleValue.setText("\n"+"2");
        //middleValue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        middleValue.setTextColor(0xFFFF0000);
        middleValue.setGravity(Gravity.RIGHT);
        middleValue.setPadding(2, 0, 9, 0);

        aImageView.setText("\n"+"3");
        //aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        aImageView.setGravity(Gravity.RIGHT);
        aImageView.setTextColor(0xFF000000);
        aImageView.setPadding(0, 0, 9, 0);

        View line = new View(getApplicationContext());
        //line.setOrientation(1);
        line.setLayoutParams(new LayoutParams(2, android.view.ViewGroup.LayoutParams.FILL_PARENT)); 
        line.setBackgroundColor(0xFF000000); 

        /**** Any other text view setup code ****/    

        innerLayout.addView(mHeading);
        innerLayout.addView(middleValue);
        innerLayout.addView(aImageView);
        innerLayout.addView(line);
        myLinearLayout.addView(innerLayout);  

        textListWord.add(mHeading); 
        textListAnswer.add(middleValue);
        imageListAnswer.add(aImageView);

    } 
}

So please guide me that what more i have to do to create such view ? Thanks.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188

1 Answers1

1

try TableLayout and TableRow instead of LinearLayout.

You can create TableLayout in xml as well, as it, I think would be static and add TableRows. To Create a TableLayout in Java use,

TableLayout tblLayout=new TableLayout(this); 

Set LayoutParams and other properties of table layout in java, like I am setting LayoutParams:

LayoutParams params=new LayoutParams(LayoutParams.Fill_Parent, LayoutParams.Wrap_Content); 
tblLayout.setLayoutParams(params);

Create a loop for TableRows creation and insertion:

for(int i=0;i<arrList1.size();i++)
{
   TableRow row=new TableRow(this);
   row.setLayoutParams(params);

   TextView lbl1=new TextView(this);
   lbl1.setText(arrList1.get(i));
   row.addView(lbl1);

   TextView lbl2=new TextView(this);
   lbl2.setText(arrList2.get(i));
   row.addView(lbl2);

   TextView lbl3=new TextView(this);
   lbl3.setText(arrList3.get(i));
   row.addView(lbl3);

   tblLayout.addView(row);
}
jeet
  • 29,001
  • 6
  • 52
  • 53
  • You can create TableLayout in xml as well, as it, I think would be static and add TableRows after creating those. – jeet Jan 10 '12 at 05:28
  • @ji tendra: i got the view but what about the gravity and the heading any other that i have done in the xml ? please help me to make it possible by java code. – Shreyash Mahajan Jan 10 '12 at 07:31
  • i want to create whole layout dynamicaly. And based on the for loop the whole layout will created as many time as for loop will run. – Shreyash Mahajan Jan 10 '12 at 07:35
  • you can set gravity of any view by using view.setGravity(Gravity.CENTER) etc, but if you are having some other issue, let me know your exact problem and if possible code. – jeet Jan 10 '12 at 07:35
  • I dont want for loop for single row. – Shreyash Mahajan Jan 10 '12 at 07:35
  • If i am using LinearLayout then i am not able to use gravity or i am not able to see any effect. Please see my Code and try it. And i am not able to do add by your code instead of it addView works. – Shreyash Mahajan Jan 11 '12 at 04:51
  • Loop is used to eliminate need of writing some common code, and I dont see any problem in using for loop for each row, please explain your issue more precisely. Yes, there is no add(View view) method in table row, so use addView() method. To use Gravity there are some basics you need to understand before apply Gravity, like if you are using TextView and Set its width to Fill_PARENT, then you need to set Gravity of TextView to see effect, but if you are using WRAP_CONTENT and setting Gravity to TextView it wont reflect the Gravity, so you need to set Layout Gravity of its Parent in this case. – jeet Jan 11 '12 at 05:03
  • Yes yo are right. but As i have given image, I want to create such layout in Java code and that whole should be repeated as per the for loop condition. I dont want whole layout to be createsd but want just small pease of layout code that saw like above image. thanks. – Shreyash Mahajan Jan 11 '12 at 06:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6605/discussion-between-jitendra-sharma-and-idroid-explorer) – jeet Jan 11 '12 at 10:06