I have this main.xml layout which has a fixed header and footer. The body content has to change based on the parameters received.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#FFFFFF"
android:id="#+id/main_view">
<com.HeaderView
android:id="#+id/HeaderView"
android:layout_alignParentTop="true"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<com.FooterView
android:id="#+id/FooterView"
android:layout_alignParentBottom="true" android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/BodyView"
android:layout_below="#id/HeaderView"
android:layout_above="#id/FooterView"/>
</RelativeLayout>
Based on the below conditions I need to load the BodyView in the main.xml.
If parameter == Apple
then load layout AppleView in the BodyView
if parameter == Bat
then load layout BatView in the BodyView
I have AppleView and BatView xml layout defined. Could somebody please guide me as to how I can load either of the views and set/get values into/from it.
Here is an example of inflating, hope this is what you need.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.temp, null);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.glavenfrejm);
RelativeLayout.LayoutParams parametri = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
parametri.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rl.addView(v, parametri);
Related
I am trying to add an empty view with height 30dp below my gridview but unfortunately, I am not able to see the expected result.
The only thing visible is gridview.
How to display the empty view after all contents of gridview are rendered?
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<GridView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:background="#color/colorAccent"
/>
<View
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/list"
android:background="#color/colorPrimaryDark"/>
</RelativeLayout>
This is my current output.
EDIT:
There are many workarounds you could use to achieve what you're looking for.
In my opinion, using a dedicated library could be the best options for lightness.
Have a look at this: GridViewWithHeaderAndFooter
From the docs:
GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) v.findViewById(R.id.ly_image_list_grid);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
View footerView = layoutInflater.inflate(R.layout.test_footer_view, null);
gridView.addHeaderView(headerView);
gridView.addFooterView(footerView);
I have a custom layout called "debug.xml" in folder "layout/" which is desired to add programmatically into a predefined Layout in activity_main.xml called centerLayout. The "debug.xml" is something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/debug_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/commandEditText"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:inputType="text" />
</LinearLayout>
</LinearLayout>
I want to add this "debug_layout" into a centerLayout:
LinearLayout centerLayout = (LinearLayout) findViewById (R.id.center_layout);
LinearLayout debugLayout = (LinearLayout) findViewById (R.id.debug_layout);
centerLayout.addView(debugLayout);
But it encounters NULL pointer excetion at "centerLayout.addView(debugLayout);". So it seems the debug_layout is not initialized or something. Does anyone can help me?
Are you sure that your centerLayout is not null? If not, did you tried?:
LinearLayout centerLayout = (LinearLayout) findViewById (R.id.center_layout);
LinearLayout debugLayout = getLayoutInflater().inflate(R.layout.debug, centerLayout, false);
centerLayout.addView(debugLayout);
you need to inflate the debugLayout with the LayoutInflater
I want to make XML template to reuse the code. The problem is that one of the template has several linearlayouts. However, when I run the app, it only shows one linearLayout. (I can only see id: manage_services. not manage_view neither manage_shows).
Here is my code. I hope anyone can figure out the problem.
drawable_manage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/manage_services"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_services"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<View
android:id="#+id/manage_view"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/menu_divider" />
<LinearLayout
android:id="#+id/manage_shows"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/slide_menu_header" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_shows"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
MainMenuActivity.java
private void addPanels(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
android.support.v4.widget.DrawerLayout parent = (android.support.v4.widget.DrawerLayout) inflater.inflate(R.layout.activity_main_menu,null);
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
leftLinearLayout.addView(layout_names);
leftLinearLayout.addView(layout_emails);
leftLinearLayout.addView(layout_manage);
setContentView(parent);
}
Change your parent LinearLayout's orientation from horizontal to vertical.
android:orientation="vertical"
Are you wanting to reuse your drawable_manage.xml linearlayouts in these (i.e. #+id/manage_services, #+id/manage_shows) or do you have separate layout files?
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
Try as others suggested and change the orientation, and make sure you are trying to inflate the correct layout. Make sure you are inflating drawable_manage in the correct location and calling to the child linearlayouts by the correct ids.
In my application I'm adding a view dynamically to the layout using addView.
The added view was inflated beforehand using
andorid:layout_width="match_parent"
however, after the adding the view using
parentView.addView(view, index);
The view is addded but dosen't fill the parent's width
I guess this is because the calculation to the "match_parent" size is done beforehand,
when the view is inflated, and therefore it has no reference of how to set the corect width
is this the case?
is there a way for me to tell the view to recalculate the layout params?
my Code:
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue_bg">
<ImageView android:id="#+id/myImg"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:src="#drawable/myImg"
android:scaleType="fitXY"/>
<LinearLayout android:id="#+id/addressItemContainer"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="#id/myImg" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<View android:id="#+id/placeHolder"
android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
inserted_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#drawable/blue_selector">
.... internal views
</LinearLayout>
java code:
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.inserted_view, null);
... fill inserted view fields ...
View aiv = findViewById(R.id.placeHolder);
ViewGroup parent = (ViewGroup) aiv.getParent();
int index = parent.indexOfChild(aiv);
parent.removeView(aiv);
parent.addView(view, index);
Tx for the help :)
Ok, just add this code
View aiv = findViewById(R.id.placeHolder);
aiv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Mark as resolved, please.
View view = findViewById(R.id.placeHolder);
view .setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//addView(view)
Thanks Dmytro Danylyk. Now its work prefectly.
I want to decompose my UI into several XML Layouts. The first one would be the main layout, and the other ones would be the content layouts.
I would like to be able to set which content_layout should be included dynamically at run-time, so I don't want to set a "layout="#+layout/content_layout" in my XML file.
Here are my layouts:
main_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="800dp" >
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<include /> <!-- I WANT TO INCLUDE MY CONTENT HERE -->
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Cancel" />
</RelativeLayout>
content_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/whatever"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
content_layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/whatever2"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
How can I do that?
Thanks!
<RelativeLayout android:id="#+id/rl" ...
In your code:
// get your outer relative layout
RelativeLayout rl = (RelativeLayout) findById(R.id.rl);
// inflate content layout and add it to the relative layout as second child
// add as second child, therefore pass index 1 (0,1,...)
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );
You could try using a ViewStub and just change which layout it is going to inflate programatically.
This answer discusses using one ViewStub.