Adding multiple framelayout in a linearlayout programmatically

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

Answers


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.


Need Your Help

How do I search for all my git repositories on my Mac?

git osx apple

I'm curious where I've scattered my git repositories across my mac. I'm trying to figure out how I could do a search to find them all so I can organize my life a bit. How can I do this?

Handling versions

mysql database innodb

Currently I'm working on a website for a client, who deals in software plugins. The client needs to be able to upload Products, and for these products different Versions, Updates and Patches. For