3

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);

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
Oh thank you, you saved me. I was thinking about writing 500 lines of Java codes
4

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..

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.