Reputation: 5592
Trying to create a black line in my view to separate text blocks but its not showing up. The text shows up as it should but i don't see the line.
EDIT: Have tested to add both dynamically as suggested and also to modify my code but still no line? Am i missing something?
Also this is inside a Fragment, class extends Fragment {}
Xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/travelContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Java code:
public class Travel extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.travel_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
LinearLayout layout = (LinearLayout)view.findViewById(R.id.travelContainer);
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
text.setTypeface(null, Typeface.BOLD);
text.setText("TITLE");
text.setId(123456789);
layout.addView(text);
/*
View v = new View(getActivity());
LinearLayout.LayoutParams viewLp = new LayoutParams(LayoutParams.FILL_PARENT,1);
viewLp.setMargins(0, 5, 0, 5);
v.setLayoutParams(viewLp);
v.setBackgroundColor(0x000);
*/
View v = getActivity().getLayoutInflater().inflate(R.layout.line, (ViewGroup)getActivity().getCurrentFocus(), false);
layout.addView(v);
text = new TextView(getActivity());
padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
text.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
layout.addView(text);
}
}
Upvotes: 0
Views: 442
Reputation: 2150
I did something similar I think and an easy way is to overwrite the class of the component especially the onDraw() method you want to have the line separator.
I think your goal is too add a black line at the end of each textview, so you could do the following :
1) Create a png black line
2) declare a class extending TextView and overwrinting the onDraw method and doing the following ==>
private Bitmap line;
then in constructor :
line = BitmapFactory.decodeResource(context.getResources(), R.drawable.line_thin);
line = Bitmap.createBitmap(line, 0, 0, this.getWidth(), 1);
then on the onDraw() method :
canvas.drawBitmap(line, 0, this.getHeight(), null);
3) and finally you just don't forget to change the type of your component with the class your created in your XML.
I don't know if this solution fit to you but I think it's an okay one, then the line is dynamically created everytime you create this Textview.
Hope it helps.
Upvotes: 0
Reputation: 1459
In your Layout params, you must set the height to 1, and the width to fill parent. You are only setting 1 of the height/width parameters aswell. Try the following
LinearLayout.LayoutParams viewLp = new LayoutParams(LayoutParams.FILL_PARENT,1);
viewLp.setMargins(0, 5, 0, 5);
Also, you are using LayoutParams for a RelativeLayout yet you are using a LinearLayout. LinearLayouts dont support the following:
viewLp.addRule(RelativeLayout.BELOW, 123456789);
viewLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
Use LinearLayout.LayoutParams. LinearLayout will stack the Views, either horizontally or vertically, in the order they are added.
Upvotes: 0
Reputation: 6712
Do you need to create the View programatically?
An easy approach is creating a View with following attributes
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000" />
If you create a new XML file called line.xml you can use a LayoutInflater to get the view dynamically:
View line = MyActivity.this.getLayoutInflater().inflate(R.layout.line, (ViewGroup) getCurrentFocus(), false);
This version will result in a much cleaner code as the system does all the work for you.
Upvotes: 1