0

I am looking to add a line below a textView programmatically in java and not in xml. I have the textView as follows:

textView.setText(DisplayName);

How do I go about the same? I have a textview supporting text and checkbox and I would like ot add a line below the same. Any clue?

Thanks! Justin

3 Answers3

2

You are probably looking for a separator. You could achieve this by 'faking' a line-seperator. add a normal View to your parent view with a height of 1dp.

Android - Dynamically Add Views into View

If you load a view from xml and want to add the line programmatically:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

ViewGroup layout = vi.inflate(R.layout.MyParentLayout, null);

View separator = new View(Context context);

set the view its layoutparams set its height: http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

layout.addView(separator);

If you create your layout programmatically:

new LinearLayout layout = new LinearLayout(context);` //or whatever kind of layout you want

I am writing this post on my mobile phone so it could contain some errors.

Community
  • 1
  • 1
Bram
  • 4,533
  • 6
  • 29
  • 41
  • I get the error for inser_point and addview related to inserpoint –  Jul 29 '13 at 15:47
  • Could you show me the exact error? I will update my answer with a code snippet in a few minutes. – Bram Jul 29 '13 at 17:34
1

Add line view to the parent layout after textview.

View ruler = new View(myContext); ruler.setBackgroundColor(0xFF00FF00);
parentLayout.addView(ruler, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 2));
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • It shows error on myContext and parentLayout(where it asks me to create the same fields). Can you please suggest a way to fix the same or what myContext is with respect to my code? Thanks –  Jul 29 '13 at 15:09
1

If you're trying to underline the text, try this (from https://stackoverflow.com/a/10947374/413254):

textview.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

If you're trying to create a divider of sorts, go with what RajaReddy suggested.

Community
  • 1
  • 1
loeschg
  • 29,961
  • 26
  • 97
  • 150
  • I am trying to create a divider below the textview, so far none of the answers are working –  Jul 29 '13 at 15:46