Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

So, I'm trying to make a dynamic UI, and i want to add a seperator to it. unfortunately, i could only find out how do one in XML. is it possible to turn this

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/seperator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="2dp"
    android:scaleType="fitXY"
    android:src="@android:drawable/divider_horizontal_dark" />

into program code?

my best attempt was

ImageView seperator=new ImageView(this);
seperator.setImageDrawable(drawable.divider_horizontal_dark);
share|improve this question
up vote 4 down vote accepted

Put it in an extra layout file and inflate it when you need it in code - I think that what you want to do and should be the easiest way.

In your Activity:

View v = LayoutInflater.from(this).inflate(R.layout.seperator, null);

If you inflate a Layout:

LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_layout, null);
TextView tv = (TextView) ll.findViewById(R.id.tv);
share|improve this answer
    
oh really? i can do that? wow. i've just spent three hours re-writing some XML i'd already had into code, and was nowhere near done. i think you've just saved me a LOT of time and effort... i'll set your answer as correct if this works mate. cheers. – blucalculatr Jun 21 '12 at 17:44
    
okay, so, that works. slightly different question: if i use that layout inflater thing for a viewgroup, like, linearlayout, is there some special way i should go about editing the text in a child TextView? – blucalculatr Jun 21 '12 at 18:03
    
I have edited my post to cover that question too :) – s.krueger Jun 21 '12 at 20:49
    
Oh thank you, you saved me. I was thinking about writing 500 lines of Java codes – Bibaswann Bandyopadhyay May 10 at 7:47

There is a website can convert that for you. You can design the interface with eclipse then submit the generated xml to XMLtoJAVA online converter and it should do it for you..

share|improve this answer

You can also create a View, define a background and add it with a LayoutParams

    ViewGroup container = (ViewGroup) findViewById(R.id.container); 
    View separator = new View(context);
    separator.setBackgroundColor(Color.Black);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 2);

    container.addView(separator, layoutParams);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.