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();
Related
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
}
});
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>
I have an Android app and I am trying to create the following layout in code:
This is the code I have so far:
public class MyLayout extends LinearLayout {
private Context mContext;
public MyLayout(Context context) {
super(context);
mContext = context;
this.build();
}
private void build() {
LinearLayout parent = new LinearLayout(mContext);
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
GridView gridView = new GridView(this.mContext);
gridView.setNumColumns(2);
ViewGroup.LayoutParams params = new ViewGroup.MarginLayoutParams(
MarginLayoutParams.FILL_PARENT,
MarginLayoutParams.WRAP_CONTENT);
gridView.setLayoutParams(params);
parent.addView(gridView);
addView(parent);
}
}
How can I add the ImageView so that it is docked at the bottom of the LinearLayout?
You could use an xml layout like the following, and then load it with setContentView(R.layout.your_layout_name) inside your onCreate method.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="10" >
<GridView
android:id="#+id/yourGridViewID"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="8" />
<ImageView
android:id="#+id/yourImageViewID"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2"/>
</LinearLayout>
Refer this code :
public class MyLayout extends LinearLayout {
private Context mContext;
private LinearLayout parent;
public MyLayout(Context context) {
super(context);
mContext = context;
parent = new LinearLayout(mContext);
this.build();
}
private void build() {
parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
GridView gridView = new GridView(this.mContext);
gridView.setNumColumns(2);
ViewGroup.LayoutParams params = new ViewGroup.MarginLayoutParams(
MarginLayoutParams.FILL_PARENT,
MarginLayoutParams.WRAP_CONTENT);
gridView.setLayoutParams(params);
ImageView imageview = new ImageView(this.mContext);
ViewGroup.LayoutParams params2 = new ViewGroup.MarginLayoutParams(
MarginLayoutParams.FILL_PARENT,
MarginLayoutParams.WRAP_CONTENT);
params2.gravity = Gravity.BOTTOM;
imageview.setLayout(params2);
parent.addView(gridView);
parent.addView(imageview);
addView(parent);
}
}
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.
i need add a new button and textedit to my LinearLayout.. if I create a new param, the buttons are created over each other..
my mainactivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyTouchEventView tv = new MyTouchEventView(this);
setContentView(tv);
addContentView(tv.btnReset, tv.params);
addContentView(tv.btnSave, tv.params2);
}
and MyTouchEventView:
public class MyTouchEventView extends View {
public Button btnReset;
public Button btnSave;
public LinearLayout.LayoutParams params;
public LinearLayout.LayoutParams params2;
public MyTouchEventView(Context context) {
super(context);
btnReset = new Button(context);
btnReset.setText("Vymazat platno");
btnSave = new Button(context);
btnSave.setText("Ulozit");
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
btnReset.setLayoutParams(params);
params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(40, 20, 40, 30);
btnSave.setLayoutParams(params2);
http://i.stack.imgur.com/kvY1T.png
Why don't you create a simple layout for your activity?
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
<Button
android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="myText" />
</LinearLayout>
Then in the onCreate method:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Also see http://developer.android.com/guide/topics/ui/layout/linear.html