0

I have hit a problem when trying to align my text to the top of a text view dynamically. I have a method that creates textviews dynamically. I have set the gravity of the text view to Gravity.TOP. Which a believe should place the text at the very top of the textView object. However when I run the project there is a gap of about 2 lines of text from the top of the object. I can work around this by changing the height of the textView but I would rather it loaded in an set the size depending on how much text there is too load into the textView.

Here is my function, it works well and does what it is supposed to do:

public void addItems(LinearLayout page,int id,String text,int row){

        LinearLayout item = new LinearLayout(this);
        item.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

        TextView itemNum = new TextView(this);
        //itemNum.setLayoutParams(new ViewGroup.LayoutParams(50,50));
        itemNum.setWidth(50);
        itemNum.setTextSize(40);
        itemNum.setTextAppearance(getApplicationContext(), R.style.large_text);
        itemNum.setLeft(0);
        itemNum.setText(row+":");
        LayoutParams numParams = new LinearLayout.LayoutParams(70, 70);
        numParams.setMargins(25, 0, 0, 0);

        TextView itemDetails = new TextView(this);
        itemDetails.setLayoutParams(new ViewGroup.LayoutParams(150,LayoutParams.WRAP_CONTENT));
        itemDetails.setText(text);
        itemDetails.setWidth(100);
        itemDetails.setLeft(5);
        itemDetails.setRight(5);
        itemDetails.setTop(0);
        itemDetails.setVisibility(View.VISIBLE);
        itemDetails.setGravity(itemDetails.getGravity() | Gravity.TOP);
        itemDetails.setPadding(0,0,0,0);

        Button delete = new Button(this);
        //delete.setLayoutParams(new ViewGroup.LayoutParams(50,50));
        delete.setText("Delete");

        LayoutParams btnParams = new LinearLayout.LayoutParams(70, 70);
        btnParams.setMargins(0, 0, 15, 0);


        item.addView(itemNum,numParams);            
        item.addView(itemDetails);
        item.addView(delete,btnParams);

        page.addView(item);
        Log.d("Loop","Loop is adding function in");                     
    }

If anybody can see where I have gone wrong I would really appreciate it. The method above may seem very long winded but I am new to Java.

Not sure if it will help but here is my string to show in the textView:

String formatText = lat + ", " + lng + " /n" + result;
Paul Ledger
  • 1,076
  • 3
  • 18
  • 44
  • http://stackoverflow.com/a/6573535/1276374 – Boris Mocialov Aug 03 '13 at 17:06
  • doesn't really help there is still a gap above the text when I added: setIncludeFontPadding(false); I will try creating the class mentioned in the topic to see if that helps – Paul Ledger Aug 03 '13 at 17:18
  • I got working, the problem occurred after I changed the theme of the app. I also changed the gravity of the main LinearLayout being generated from top to fill – Paul Ledger Aug 04 '13 at 14:12

0 Answers0