0

Sorry for my bad english. I've a table row and i've set the first view with record's code. I've set this view with 0px height and 0px width. But it takes me half screen! Why? Where is the mistake?

This is the code:

        try{

        txtId= new TextView(this);
        imgCategory= new ImageView(this);
        txtDescrizione= new TextView(this);
        txtDataFine= new TextView(this);
        imgPriorita= new ImageView(this);

        txtId.setText(id);
        txtId.setWidth(0);
        txtId.setHeight(0);
        txtId.setTextColor(getResources().getColor(R.color.white));
        txtId.setTextSize(1);


        if(priorita.equalsIgnoreCase("3")){
            imgPriorita.setImageResource(R.drawable.todo_priority_3);}
        else if(priorita.equalsIgnoreCase("2")){
            imgPriorita.setImageResource(R.drawable.todo_priority_2);}
        else if(priorita.equalsIgnoreCase("1")){
            imgPriorita.setImageResource(R.drawable.todo_priority_1);}

        imgPriorita.setPadding(0, 0, 0, 0);
        imgPriorita.setMaxWidth(widthDisplay/10);
        imgPriorita.setMaxHeight(40);
        imgPriorita.setScaleType(ScaleType.CENTER_INSIDE);


        txtDescrizione.setPadding(0, 0, 0, 0);
        //txtDescrizione.setText(Html.fromHtml("<i>"+descrizione+"</i>"));
        txtDescrizione.setText(descrizione);
        txtDescrizione.setTextColor(getResources().getColor(R.color.black));
        txtDescrizione.setWidth(widthDisplay/5);
        txtDescrizione.setHeight(40);
        txtDescrizione.setTextSize(12);
        //setto la descrizione su una sola riga e con true fino a quando ci stanno le lettere
        txtDescrizione.setSingleLine(true);
        txtDescrizione.setLines(1);


        txtDataFine.setPadding(0, 0, 0, 0);
        txtDataFine.setText(data.getDateTimeTodo(dataFine));
        txtDataFine.setWidth(widthDisplay/5);
        txtDataFine.setHeight(40);
        txtDataFine.setTextColor(getResources().getColor(R.color.black));
        txtDataFine.setGravity(Gravity.CENTER);
        txtDataFine.setTextSize(12);

        if(category.equalsIgnoreCase("2")){
            imgCategory.setImageResource(R.drawable.todo_category_2);}
        else if(category.equalsIgnoreCase("1")){
            imgCategory.setImageResource(R.drawable.todo_category_1);}
        else if(category.equalsIgnoreCase("3")){
            imgCategory.setImageResource(R.drawable.todo_category_3);}
        else if(category.equalsIgnoreCase("5")){
            imgCategory.setImageResource(R.drawable.todo_category_5);}
        else if(category.equalsIgnoreCase("4")){
            imgCategory.setImageResource(R.drawable.todo_category_4);}
        else if(category.equalsIgnoreCase("6")){
            imgCategory.setImageResource(R.drawable.todo_category_6);}



        imgCategory.setPadding(0,0,0,0);
        imgCategory.setMaxWidth(widthDisplay/5);
        imgCategory.setMaxHeight(40);
        imgCategory.setScaleType(ScaleType.CENTER_INSIDE);

        rowTodo[counterBackground]= new TableRow(this); 
        if((counterBackground%2)==0){
            rowTodo[counterBackground].setBackgroundColor(getResources().getColor(R.color.greyListCp));}
        else{
            rowTodo[counterBackground].setBackgroundColor(getResources().getColor(R.color.white));}

        rowTodo[counterBackground].setMinimumHeight(heightRowTodo);
        idRow_gl=Integer.parseInt(id);
        rowTodo[counterBackground].setId(idRow_gl);

        rowTodo[counterBackground].setOnClickListener(new OnClickListener() {
            public void onClick(View view_cliccata) {
                    onclickRow(view_cliccata);
            }   
        });


        imgAnagraficaTodo[counterBackground]=new ImageView(this);

        if(id_anagTodo.equals("")){}
        else{   
            imgAnagraficaTodo[counterBackground].setImageResource(R.drawable.toanag);
            imgAnagraficaTodo[counterBackground].setPadding(20, 3, 0, 0);
            imgAnagraficaTodo[counterBackground].setMinimumWidth(widthDisplay/5);
            imgAnagraficaTodo[counterBackground].setMaxHeight(40);
            imgAnagraficaTodo[counterBackground].setId(idRow_gl);
            imgAnagraficaTodo[counterBackground].setScaleType(ScaleType.CENTER_INSIDE);

            imgAnagraficaTodo[counterBackground].setOnClickListener(new OnClickListener() {
                public void onClick(View view_cliccata) {
                        Data.myHashMapVar.put("ID_TODO", view_cliccata.getId());
                        onclickImgAnagraficaTodo(view_cliccata,id_anagTodo);
                }   
            });
            arrList_AnagTodo.add(id_anagTodo);
        }

        rowTodo[counterBackground].addView(txtId);
        rowTodo[counterBackground].addView(imgPriorita);
        rowTodo[counterBackground].addView(txtDescrizione);
        rowTodo[counterBackground].addView(txtDataFine);
        rowTodo[counterBackground].addView(imgCategory);
        rowTodo[counterBackground].addView(imgAnagraficaTodo[counterBackground]);


        List_TodoList.addView(rowTodo[counterBackground]);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

And heightDisplay and widthDisplay are set in this way:

display=getWindowManager().getDefaultDisplay();
             heightDisplay=display.getHeight();
             widthDisplay=display.getWidth();

Thanks in advance!

Tabita Tucci
  • 63
  • 10

2 Answers2

1

You should really really really consider working with layout xml instead of code for your screen designs, check this tutorial for more info: http://developer.android.com/guide/topics/ui/declaring-layout.html

Regarding your problem, I'd suggest using visibility instead of changing width/height:

txtId.setVisibility(View.GONE);
marmor
  • 27,641
  • 11
  • 107
  • 150
1

If you wan't it gone. why not just use

txtId.setVisibility(View.GONE);

?

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • But as @marmor wrote, you should really declare your layouts in the xml. And only when creating stuff genericly you could be forced to do it in code. – Anders Metnik Sep 11 '12 at 08:49
  • I've already declared it @_@. I've declared tablelayout in my xml, I only add row by code... :) – Tabita Tucci Sep 11 '12 at 08:52
  • I can't create my table row by row in xml because i grab record dinamically from db – Tabita Tucci Sep 11 '12 at 08:59
  • you should create the row layout in another xml, and in the code you'll inflate it, put the data in it, and add it to the table. See this: http://stackoverflow.com/a/2685130/819355 – marmor Sep 11 '12 at 09:07
  • And maybe consider using a listview/listactivity and use adapter. Makes code much more consistently and readable – Anders Metnik Sep 11 '12 at 11:17
  • can you explain it anders please? – Tabita Tucci Sep 11 '12 at 12:48
  • Sounds to me like you wan't to display a decent amount of data... Like a list of countries, or fruits in this example: http://www.mkyong.com/android/android-listview-example/ – Anders Metnik Sep 11 '12 at 14:17