2

I need to overlap an image view with textview. And this combined view will be repeated 100 times in a LinearLayout. I was thinking of using FrameLayout in LinearLayout and Repeating the FrameLayout in LinearLayout 100 times when FrameLayout holds the imageview and textview overlapped. Need to do this programatically not from xml file.

I added the image and textview to framelayout first then tried to add the framelayout to linearlayout. But it says : the specified child has already a parent.. so not working. Can you please show me in code? Thanks for your help.

it is going to be like this, but need to be done programmaticaly

---linear layout--------------
------------------------------
|frame layout----------------|
||txt view on top of img view|
------------------------------
frame layout will be repeated|

---/end of linear layout------

Also here is the separated code:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        LinearLayout dynamicview = (LinearLayout) findViewById(R.id.main_layout);

        FrameLayout barFrameLayout = new FrameLayout(this);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
                Gravity.CENTER);
        barFrameLayout.setLayoutParams(params);


        LinearLayout.LayoutParams slparams1 = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        for (int i = 65; i <= 75; i++) {
            TextView catTV = new TextView(this);
            catTV.setLayoutParams(slparams1);
            catTV.setText("===" + Character.toString((char) i) + "===");
            catTV.setTextSize(32);

            ImageView iv = new ImageView(this);
            iv.setImageResource(R.drawable.ic_launcher);
            iv.setLayoutParams(slparams1);

            barFrameLayout.addView(catTV);
            barFrameLayout.addView(iv);

            dynamicview.addView(barFrameLayout);
        }
    }
4
  • I'm not sure I understand what you're trying to do… if you could provide more information that would probably help. Commented May 2, 2013 at 1:41
  • Which point you don't understand? Commented May 2, 2013 at 1:57
  • I see now what you're trying to do, but show us some code :) You said you tried and got an error. Why not use a relative layout? Commented May 2, 2013 at 2:07
  • the code is messy that's why and it is easy to understand na?. What difference will it make if i use relative layout? Commented May 2, 2013 at 2:10

2 Answers 2

4

Here is the code to demonstrate what you are trying to achieve. I have used RelativeLayout, which is very flexible, you can position the elements easily relative to others.( if you need to change to FrameLayout you can change ).

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ExampleLayout extends LinearLayout{

public ExampleLayout(Context context,AttributeSet attrs){
    super(context,attrs);

    for(int i =0; i< 100; i++){

        RelativeLayout childLayout = new RelativeLayout(context);

        ImageView img  = new ImageView(context);
        TextView text = new TextView(context);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        childLayout.addView(img, params);

        params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        childLayout.addView(text, params);

        LinearLayout.LayoutParams parentParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);           
        this.addView(childLayout,parentParams);
    }
}
}

You can then use the ExampleLayout class to add it to any of the layout.xml file.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks i am looking into it. in the meantime can you see what's wrong in my code?
@durgomgiri , You are creating only 1 instance of framelayout for child and adding it to linearlayout which is giving you problem that ' child is already added'. FrameLayout barFrameLayout = new FrameLayout(this); You need to do this inside the for loop...!!
1

FrameLayout is designed to block out an area on the screen to display a single item

(source: http://developer.android.com/reference/android/widget/FrameLayout.html).

Anyway, you have to create new FrameLayouts not use the same.

When you're doing:

barFrameLayout.addView(catTV);
barFrameLayout.addView(iv);
dynamicview.addView(barFrameLayout);

you're always adding these new objects (catTV and iv) to the same instance of a FrameLayout (barFrameLayout).

I don't think that's what you wanted to do.

2 Comments

you look wierd, why don't you let us see your original face :?
I am weird, that's why I'm in stackoverflow. ;)

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.