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.
Related
I need to create i number of photo_view and give it a special amount every time (loop). number of photo_view in every time will be different. What exactly should I do in the code ?
my xml file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/exit_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="20dp"
android:clickable="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_back_ltr" />
</android.support.design.widget.CoordinatorLayout>
Dynamically adding Views to a ViewGroup is what you are looking for. which is done by code not by XML.
In general, there are two ways to add a view dynamically,
either by inflating some layout file in a view then add this view to your parent viewgroup (LinearLayout for ex) by calling the add method on this parent view.
Or by defining a new object of that view and add all params needed to it then add it to the parent viewgroup.
See an example about way 1 here
See an example about way 2 here
in your case you could create a new layout.xml file contains only your custom view com.github.chrisbanes.photoview.PhotoView with all the needed params. then add an id for the parent linearlayout
<LinearLayout
android:"#+id/parentViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
And in your code, loop by the count and add the custom inflated view to parent. like this:
View inflatedView = View.inflate(context, yourViewXML, null);
LinearLayout parentViewGroup = findViewById(R.id.parentViewGroup);
for(int i = 0 ; i<count ; i++){
parentViewGroup.add(inflatedView);
}
I'm trying to dynamically add a layout into MainActivity (default Android Application that Android Studio creates), but the new layout isn't being displayed.
I've tried with a different layout, but that one was clipped after ~450px height.
I'm pretty new to Android UI programming and I have a feeling I'm doing something wrong in the adding function. Here's the adding code (which was inspired from this answer):
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
View v = vi.inflate(R.layout.test_layout, null);
// insert into main view
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
1000,//ViewGroup.LayoutParams.FILL_PARENT,
1000);//ViewGroup.LayoutParams.FILL_PARENT);
linearLayout.addView(v, 0, layoutParams);
This is the activity_main.xml:
<include
android:id="#+id/include"
layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
<LinearLayout
android:id="#+id/linlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!--<include layout="#layout/test_layout"/>-->
</LinearLayout>
And here's the layout I'm trying to add:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageView
android:id="#+id/imageView2"
android:layout_width="188dp"
android:layout_height="191dp"
app:srcCompat="#mipmap/ic_launcher" />
</RelativeLayout>
If I add the layout statically (adding <include layout="#layout/test_layout"/> in the xml), test_layout is shown properly.
What am I doing wrong here?
(Using Android 7.1.2 on Nexus 5X)
Use this method to add View Dynamically:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.linlayout);
View testlin= getLayoutInflater().inflate(R.layout.test_layout, myLayout, false);
myLayout.addView(testlin);
ll_fovorite = (LinearLayout) view.findViewById(R.id.ll_fovorite);
tv_no_fovorite = new TextView(getActivity());
tv_no_fovorite.setText("There are no fovorite words");
tv_no_fovorite.setTextColor(Color.parseColor("#8b8682"));
tv_no_fovorite.setTextSize(24);
tv_no_fovorite.setGravity(Gravity.CENTER);
tv_no_fovorite.setTypeface(Typeface.MONOSPACE);
ll_fovorite.addView(tv_no_fovorite, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
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 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);