Unable to add RelativeLayout programmatically - android

I am unable to add a RelativeLayout programmatically to existing xml layout. only xml layout is displaying. Sharing my code
fragment_home.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.timetable.act.MainActivity$PlaceholderFragment"
android:id="#+id/homeLayout"
android:background="#color/white">
<ImageButton
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="380dp"
android:layout_marginLeft="240dp"
android:background="#drawable/circle"
android:id="#+id/addEvent"
android:src="#drawable/ic_mode_edit_white_24dp"
android:layout_gravity="right|bottom"
android:contentDescription="#string/hello" />
</RelativeLayout>
PlaceholderFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = null;
switch (Integer.valueOf(getArguments().get(ARG_SECTION_NUMBER).toString())) {
case 1:
rootView = inflater.inflate(R.layout.fragment_home, container, false);
displyAllEvent(rootView);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_setting, container, false);
break;
default:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
}
return rootView;
}
private void displyAllEvent(View mainView){
Drawable drawableShadow = ContextCompat.getDrawable(getActivity(),R.drawable.tile_shadow);
RelativeLayout tileLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tileLayout.setBackground(drawableShadow);
tileLayout.setPadding(R.dimen.tilePadding,R.dimen.tilePadding,R.dimen.tilePadding,R.dimen.tilePadding);
tileLayout.setLayoutParams(rlp);
tileLayout.setId(999);
RelativeLayout.LayoutParams titleLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView titleView = new TextView(getActivity());
titleView.setText("Testing title with test");
titleView.setId(111);
titleView.setTextColor(R.color.white);
titleView.setLayoutParams(titleLayout);
tileLayout.addView(titleView);
RelativeLayout.LayoutParams textLeftLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textLeftLayout.addRule(RelativeLayout.BELOW, titleView.getId());
TextView startHView = new TextView(getActivity());
startHView.setText("left Text");
startHView.setId(222);
startHView.setTextColor(R.color.white);
startHView.setLayoutParams(textLeftLayout);
tileLayout.addView(startHView);
RelativeLayout.LayoutParams textRightLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textRightLayout.addRule(RelativeLayout.BELOW, titleView.getId());
textRightLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textLeftLayout.addRule(RelativeLayout.ALIGN_RIGHT, startHView.getId());
TextView startMView = new TextView(getActivity());
startMView.setText("Right Text");
startMView.setId(333);
startMView.setTextColor(R.color.white);
startMView.setLayoutParams(textRightLayout);
tileLayout.addView(startMView);
RelativeLayout parentView = (RelativeLayout)mainView.findViewById(R.id.homeLayout);
parentView.addView(tileLayout);
}
output:

Instead of creating a relative layout during runtime, you could simply make a pre-existing one, with all its children, visible... E.g:
if (myCondition) {
myLayout = (RelativeLayout) myContext.findViewById(R.id.myLayoutID);
myLayout.setVisibilty(View.visible);
myLayoutTextView = (TextView) myContext.findViewById(R.id.myTextView);
myLayoutTextView.setVisibility(View.VISIBLE);
}
In case you also have a button in the layout that you want to become click-able when the layout is active, you can do:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (myCondition) {
//Do Stuff Here
}
});

Related

Specific child already has a parent

I want to create a dynamic layout but I had error
The specified child already has a parent. You must call removeView() on the child's parent first.
I found something on Stackoverlow but most of all suggest to use this line that I already use:
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
Any solution??
ItemFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_items, container, false);
layout = (TableLayout) rootView.findViewById(R.id.itmes_fragment_layout);
item_folder_button = new Button(getActivity());
item_folder_button = (Button) rootView.findViewById(R.id.items_button_layout);
return rootView;
}
private void createItemsButtons() {
layout.removeAllViews();
if (itemscontainer.length == 0) {
TableRow row = new TableRow(myContext);
TextView no_items = new TextView(myContext);
no_items.setText(getResources().getString(R.string.no_items));
no_items.setTextSize(35);
row.addView(no_items);
layout.addView(row);
} else {
for (int i = 0; i <= itemscontainer.length; ) {
TableRow tableRow = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
tableRow.setLayoutParams(param);
tableRow.setGravity(Gravity.CENTER_VERTICAL);
for (int y = 0; (y < 3) && (i <= itemscontainer.length); y++) {
item_folder_button.setText(("Text " + i));
item_folder_button.setVisibility(View.VISIBLE);
item_folder_button.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
item_folder_button.setLayoutParams(par);
int id = i;
item_folder_button.setId(id);
item_folder_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = item_folder_button.getId();
Fragment fragment = new ItemFragment();
if (fragment != null) {
FragmentManager fragmentManager = myContext.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment).addToBackStack(null);
fragmentTransaction.commit();
}
}
});
tableRow.addView(item_folder_button);
i++;
}
layout.removeView(layout.getRootView());
layout.addView(tableRow);
}
}
}
return rootView;
}
fragment_items.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/items_fragment_scrollview_layout"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:id="#+id/items_fragment_linearlayout"
>
<TableLayout
android:id="#+id/items_fragment_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
<Button
android:id="#+id/items_button_layout"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="40"
android:drawableLeft="#drawable/folder"
android:paddingLeft="10dp"
android:text="#string/text"
android:background="#drawable/item_button"
android:drawablePadding="-75dp"
android:visibility="gone"/>
</LinearLayout>

add layout programmatically in android button listener

i have some layout file and i try to add new relativelayout with programmatically
this is a my xml layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
and this is java code
public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new listener());
staticlayout.setId(10);
staticlayout.addView(add_btn);
parms.addRule(RelativeLayout.BELOW, R.id.rot);
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
}
});
}
class listener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout relativelayout = new RelativeLayout(
getApplicationContext());
relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setOnClickListener(new listener());
add_btn.setLayoutParams(imagelayoutparam);
relativelayout.addView(add_btn);
costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
relativelayout.setLayoutParams(costomparam);
mainlayout.addView(relativelayout);
}
}
}
i try to explain my problem. i can add new layout and imageview inside new layout(which i created
programmatically) and also i can add new layout in button click with a button i also created programmatically.
and now i want to write code to create new layout each time.i want recursion function.how i can solve my problem ?
if anyone knows solution problem please help me
Check if this solution fulfil your needs:
Activity code:
package com.example.abc;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private RelativeLayout myImage, mainlayout;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
img.setOnClickListener(new RecursionOnClickListener());
}
private int layoutId = 1;
class RecursionOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setId(layoutId++);
if(layoutId % 2 == 0) {
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
}
else {
staticlayout.setBackgroundColor(Color.parseColor("#FF0000"));
}
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new RecursionOnClickListener());
staticlayout.addView(add_btn);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms.addRule(RelativeLayout.BELOW, mainlayout.getChildAt(mainlayout.getChildCount()-1).getId());
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
}
}
}
Layout XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>

add layout programmatically in android button click

I have a XML layout file. This is the source
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/rot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#ff0000" >
<ImageView
android:id="#+id/btn_categorry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher" />
</RelativeLayout>
I wrote some code which can add new layout programmatically and I also added(created) image view in layout programmatically. I want to wrote code witch would can add new layout programmatically. For more information. I want to create new layout each image view click witch I created programmatically.
This is a my source
public class MainActivity extends Activity {
RelativeLayout myImage, mainlayout;
private ImageView img;
RelativeLayout staticlayout;
RelativeLayout.LayoutParams parms;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (RelativeLayout) findViewById(R.id.rot);
mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
img = (ImageView) findViewById(R.id.btn_categorry);
parms = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
staticlayout = new RelativeLayout(getApplicationContext());
staticlayout.setBackgroundColor(Color.parseColor("#000000"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parms2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
parms2.addRule(RelativeLayout.CENTER_HORIZONTAL);
//
//
add_btn.setLayoutParams(parms2);
add_btn.setOnClickListener(new listener());
staticlayout.setId(10);
staticlayout.addView(add_btn);
parms.addRule(RelativeLayout.BELOW, R.id.rot);
staticlayout.setLayoutParams(parms);
mainlayout.addView(staticlayout);
// parms.addRule(RelativeLayout.BELOW, R.id.rot);
}
});
}
class listener implements OnClickListener {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams costomparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout relativelayout = new RelativeLayout(
getApplicationContext());
relativelayout.setBackgroundColor(Color.parseColor("#AB3232"));
ImageView add_btn = new ImageView(getApplicationContext());
add_btn.setBackgroundResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams imagelayoutparam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imagelayoutparam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imagelayoutparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
add_btn.setOnClickListener(new listener());
//
//
add_btn.setLayoutParams(imagelayoutparam);
relativelayout.addView(add_btn);
costomparam.addRule(RelativeLayout.BELOW, staticlayout.getId());
relativelayout.setLayoutParams(costomparam);
mainlayout.addView(relativelayout);
}
}
}
My code can create new layout and also can create new layout in image view click. How can I write code to can create new layout automatically each time when I click image view?
If anyone knows solution please help me.

Programatically adding views without xml element in android

Description::
On click of button I want to add a textview dynamically on run-time
I don't want to use a textview widget(from xml)
I want to generate textviews programatically
Is it possible ?
If so how ?
MainActivity.java
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Container" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBox1"
android:layout_centerHorizontal="true"
android:layout_marginTop="67dp"
android:text="Button" />
</RelativeLayout>
Just
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) findViewById(R.id.parent_layout);
TextView textView = new TextView(MainActivity.this);
layout .addView(textView);
}
});
You can create a textView using the context of your activity.
example:
RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
TextView tv = new TextView(MainActivity.this);
relativeLayout.addView(tv,lp);
}
});
Simple example, including layout params (width, height, etc):
// instantiate the new view - int his example, a textview
TextView label=new TextView(context);
label.setText(labelText);
// new LayoutParams, specify the height as WRAP_CONTENT
// width is 0 in this example as we are using the layout weight (2)
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT,2
);
// margins
lp.rightMargin=5;
lp.leftMargin=5;
// apply the layout params to this view
label.setLayoutParams(lp);
// finally, add the view to the container view
content.addView(label);
Something like this:
// Create the TextView
TextView textView = new TextView(this);
// Create some parameters
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
// Get your relative layout by its id
RelativeLayout relativeLayout = (RelativeLayout )findViewById(R.id.Container);
// Add the newly created TextView to the layout
relativeLayout.addView(textView, p);
TextView in a LinearLayout:
LinearLayout layout = new LinearLayout(this);
TextView textview = new TextView(this);
textview.setText("Hellow, I'm a TextView!");
layout.addView(textview);
this.setContentView(layout);

Programatically create List Fragment and Fragment

How can we convert the below xml progrmatically
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/list_fragment"
android:name="com.aaa.bbb.activity.FragmentedListActivity$MyListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#27577f" />
<fragment
android:id="#+id/detail_fragment"
android:name="com.aaa.bbb.activity.FragmentedListActivity$MyDetailFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
this may help you...
private static final int ID_MY_FRAGMENT = 0xDEAF1;
private static final int ID_MY_DETAIL_FRAGMENT = 0xDEAF2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = getView();
setContentView(contentView);
getSupportFragmentManager().beginTransaction()
.replace(ID_MY_FRAGMENT, new MyListFragment())
.replace(ID_MY_DETAIL_FRAGMENT, new MyDetailFragment()).commit();
}
private View getView() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
FrameLayout frameLayout = new FrameLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
frameLayout.setLayoutParams(layoutParams);
frameLayout.setId(ID_MY_FRAGMENT);
layout.addView(frameLayout);
View view = new View(this);
layoutParams = new LinearLayout.LayoutParams((int) (1 * getResources()
.getDisplayMetrics().density),
LinearLayout.LayoutParams.MATCH_PARENT);
view.setLayoutParams(layoutParams);
view.setBackgroundColor(27555);
layout.addView(view);
frameLayout = new FrameLayout(this);
layoutParams = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT, 2);
frameLayout.setLayoutParams(layoutParams);
frameLayout.setId(ID_MY_DETAIL_FRAGMENT);
layout.addView(frameLayout);
return layout;
}
You can change your fragment tags to FrameLayout ones, remove the attribute name in each tag, and then add your fragments programmatically like:
MyListFragment fragment = new MyListFragment();
MyDetailFragment fragment2 = new MyDetailFragment();
fragmentTransaction.add(R.id.list_fragment, fragment);
fragmentTransaction.add(R.id.detail_fragment, fragment2);
fragmentTransaction.commit();

Categories

Resources