I have a problem.
I slip my Screen in two areas with 2 ViewStubs.
But if i inflated the ViewStubs they are no more accessible and i can't inflate an other View within this ViewStub.
So what is an alternative to use these kind containers.
Here some Code. You all love Code:
ViewStub contentSpace = (ViewStub) findViewById(R.id.ContentSpace);
contentSpace.setLayoutResource(R.layout.view1);
contentSpace.inflate();
contentSpace.setInflatedId(R.id.Content);
RelativeLayout content = (RelativeLayout) findViewById(R.id.Content);
contentSpace.setLayoutResource(R.layout.view1); //crash
contentSpace.inflate();
It seems like ViewStub can't be re-inflated. It is designed to be inflated just once, then it is removed from the View hierarchy. More details here: https://stackoverflow.com/a/11579271/752781
Here my Solution
rootLayout is a linear Layout where may ViewStubs were located.
rootLayout.removeView(contentLayout);
inflater.inflate(R.id.view1, rootLayout);
contentLayout = findViewById(R.id.contentLayout);
view1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/contentLayout">
[...]
Related
I have a layout as indicated below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<LinearLayout
android:layout_height="match_parent"
...
The associated activity is like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);// here is the layout
}
I'm trying to make it visible in the following way:
LinearLayout layoutActLogin = (LinearLayout) findViewById(R.layout.act_login);
layoutActLogin.setVisibility(View.VISIBLE);
But Android Studio told me that there is an error about R.layout.act_login
findViewById is for views, not layouts.
You should put an ID in your view like:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<LinearLayout
android:layout_height="match_parent"
...
Then get the view reference and make it visible
View viewActLogin = findViewById(R.id.my_view);
viewActLogin.setVisibility(View.VISIBLE);
You can not change visibility of Layout. you can only change visibility of Views inside.
You can assign id to you view by using android:id tag in you layout. You can read more about this here.
In your case. Just give some Id to your View/ViewGroup, and reference that View from your Activity and using findViewById method and change its visibility.
You're misunderstanding how layouts and views work. Layouts define what is shown on-screen to a user during an activity, set by calling setContentView() within the Activity's onCreate() method. Views are individual elements within the layout, which are accessed with the R.id prefix using findViewById().
In your example you'll need to apply an ID to the root ConstraintLayout (using android:id) to be able to access it:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
Then you can access it with:
ConstraintLayout layoutActLogin = (ConstraintLayout) findViewById(R.id.parent_layout);
layoutActLogin.setVisibility(View.VISIBLE);
EDIT Looking at your code I now realise you want to control the root ConstraintLayout, which makes my answer almost identical to Eduardo Herzer's. Leaving my answer up due to the added explanation at the beginning.
Is it possible to inflate a ViewStub with a native android widget?
For example:
<LinearLayout...>
<TextView...>
<TextView...>
<ViewStub...>
</LinearLayout>
I want to replace/inflate the ViewStub with a Switch or a Checkbox. Is that possible?
To set the layout to inflate into the ViewStub either:
Set the layout via XML:
<ViewStub
android:id="#+id/stub"
android:inflatedId="#+id/myLayout"
android:layout="#layout/myLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Or, set the layout at runtime:
viewStub.setLayoutResource(R.layout.myLayout);
Then, you can inflate the ViewStub.
View inflatedView = viewStub.inflate();
You could call this inflate() method from where you want, such as a Switch or Checkbox being clicked (as you asked).
Currently there is no way to replace the StubView with a View object. You must create a layout representing the view you want to replace. So for example, if you want to switch it with a single CheckBox, you need to create a layout like this
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_checkbox"
android:text="Click me"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then calling
viewStub.setLayoutResource(R.layout.my_checkbox);
viewStub.inflate();
If you need a reference to the inflated view you can use this instead
CheckBox checkInflated = (CheckBox)viewStub.inflate();
I want to create a custom layout to reduce redundancy in the code. Currently every layoutfile has about 30 Lines of code which are identical.
My goal was to create a custom layout/view which can hold in itself children.
<BaseLayout xmlns:...>
<!-- Normal Content -->
<Button />
<Label />
</BaseLayout>
While the above xml holds most of the content, the BaseLayout is in itself an xml containing other views and functionality:
<FrameLayout xmlns:...>
<LinearLayout><!-- contains the Header--></LinearLayout>
<LinearLayout><!-- INDIVIDUAL CONTENT HERE--></LinearLayout>
<FrameLayout><!-- contains the loading screen overlay --></FrameLayout>
</FrameLayout>
So all children from the above xml should be inserted into second linear-layout. I have already succeeded into doing so. But am confronted with layout problems (match parents does not match parents and only wraps)
My approach was extending the LinearLayout with following logic:
/**
* extracting all children and adding them to the inflated base-layout
*/
#Override
protected void onFinishInflate() {
super.onFinishInflate();
View view = LayoutInflater.from(getContext()).inflate(R.layout.base_layout, null);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.base_layout_children);
while(0 < getChildCount())
{
View child = getChildAt(0);
LinearLayout.MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
removeViewAt(0);
linearLayout.addView(child, layoutParams);
}
this.addView(view);
}
Is there a better, cleaner approach to capsule the xml and reuse a basis layout? How do I fix the match_parent issue?
While writing this post and thinking hard how to explain best, the solution for the match_parent issue became clear. Though the question remains if there is a better approach for the whole problem.
//Solution:
this.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//wrong:
this.addView(view);
Suppose you have two layout files. common_views.xml and layout_main.xml. You can include content of one layout file into another like this.
<?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"
>
<include
android:id="#+id/common"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/common_views" />
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/common"
>
</WebView>
</RelativeLayout>
If I had two layouts, layout1.xml and layout2.xml, that are both just empty LinearLayouts, and I set layout1 as an activity's content view:
setContentView(R.layout.layout1);
Is it possible to add the second layout right under the first?
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout2 = (LinearLayout) inflater.inflate(R.layout.layout2);
View parent = // parent of current layout root
parent.addView(layout2);
So the hierarchy would look like:
<parent>
<layout1 />
<layout2 />
</parent>
Basically I'm wondering how to get that parent view if it exists.
I think the easiest way to accomplish what you want is to create a third layout (parent.xml) that includes the other two layouts using include tags. Then setContentView(R.layout.parent);
Have a look here:
http://developer.android.com/training/improving-layouts/reusing-layouts.html
As an example, consider the following parent layout that pulls in your other two layouts:
<?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">
<include android:id="#+id/layoutOne" layout="#layout/layout1" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<include android:id="#+id/layoutTwo" layout="#layout/layout2" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>
View parent= getWindow().getDecorView().findViewById(android.R.id.content)
I think you want something like this :
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutFirst = (RelativeLayout) inflate.inflate(R.layout.abc, null);
RelativeLayout layoutSecond = (RelativeLayout) inflate.inflate(R.layout.xyz, null);
layoutMain.addView(layoutFirst, 100, 100); // width=100, height=100
layoutMain.addView(layoutSecond, 100, 100);
You can read the documetation here.
I have a UI, I build it dynamically. I should want to put some component in a xml resource file. So I do :
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
... in a file res/layout/titreitem.xml as I see anywhere. But I don't understand how get it to put inside my UI. So, inside activity.onCreate, I want to do something like :
RelativeLayout myBigOne = new RelativeLayout(this);
TextView thingFromXML = [what here ? ];
myBigOne.addView(thingFromXML);
setContentView(myBigOne);
Use a LayoutInflater....The Entire Layout can be inflated, without dynamically creating it....
LayoutInflater li = LayoutInflater.from(context);
View theview = li.inflate(R.layout.whatever, null);
Approach seems to be little incorrect. You should put RelativeLayout to the xml as your TextView made, and inflate the whole xml. Afterwards, you will be free to add views to your layout. So, do this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+androi:id/relLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/titreItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
In your activity:
setContentView(R.layout.titreitem);
RelativeLayout layout = (RelativeLayout)findViewByid(R.id.relLayout);
layout.addView(...);