Android : How to get the top-level parent layout of an activity - android

I would like to get the top-level layout of this sample android xml layout. The top-level I'm referring to is the ViewGroup that is holding the whole activity together.
I am aware of
getWindow().getDecorView().findViewById(android.R.id.content);
But I don't need the DecorView.
Please refer to the xml below.
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout">
<WebView
android:id="#+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
I need to get the RelativeLayout (which is the top-level layout I'm referring to, please correct me if I'm wrong with my terms). I can get it by using the code below.
WebView webView = (WebView) findViewById(R.id.my_webview);
webview.getParent().getParent()
However, what if the Layout is nested deeper like
<Layout>
<Layout>
<Layout>
<Webview/>
<Layout>
<Layout>
<Layout>
How will be able to get the top-level Layout without using getParent(). Thanks!

In 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:id="#+id/parentLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout">
<WebView
android:id="#+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
In your activity
RelativeLayout parentLayout=(RelativeLayout)findViewById(R.id.parentLayout);
or
getWindow().getDecorView().getRootView();
or
getWindow().getDecorView().findViewById(android.R.id.content);
or
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);

Related

Many layouts in Scroll View in android

I want to have 3 layouts in my ScrollView but when I add them it actually doesn't scroll. I tried to put all layouts in different files, then include them, tried to put them in ListView and it doesn't work too. In this option, when I put in ScrollView the LinearLayout and there include the rest of the layouts the application show nothing. That's probably becouse I don't know how to refer to that nested layouts... This is my XML:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include layout="#layout/first"/>
<include layout="#layout/second"/>
<include layout="#layout/third"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
#EDIT views included (all are the same so I put just one):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
And in the main activity I put the objects into layouts like this:
setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);
...some code creating views...
first.addView(myView1);
second.addView(myView2);
third.addView(myView3);
Need to put fix size in included layout, for example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
I don`t understand why do you need to add view manually when you already added it to the layout.
You should also eliminate top Linear Layout since there is no use of it.

Why does my fragment fill the screen qua height? - Android

I've got a layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<fragment
android:id="#+id/fragment_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="<headerClass>"/>
<fragment
android:id="#+id/fragment_body"
android:layout_below="#id/fragment_header"
android:layout_above="#+id/fragment_banner"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="<bodyClass>"/>
<fragment
android:id="#id/fragment_banner"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="<BannerClass>"/>
</RelativeLayout>
Notice that the first fragment has "wrap_content" as param for the layout_height.
This fragment is defined qua layout as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/headerWrapper"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/header_background">
// stuff here...
</RelativeLayout>
As you can see the height is set to 40dp.
Yet, the header takes the full height of the screen. I am not sure why. I was looking at the layoutInflater class, but since I am unfamiliar with it I cannot see any problems with that. I use this line to inflate my fragment:
View view = inflater.inflate(R.layout.fragment_header, container, false);
Why is it showing this behavior and how can I fix it?
I've managed to fix it by putting every fragment in its own LinearLayout and giving the first a height of 40dp. I don't think this is the best solution, but it's all I could come up with.

alignParentRight in xml not working

I have a generic layout, to which I attach fragments:
<?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:background="#drawable/back"
android:orientation="vertical" >
<include
android:id = "#+id/login_header"
layout="#layout/header_layout"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I have a fragment which I want to display on the right side of the screen.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="#dimen/two_third_width"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="#dimen/small_margin"
android:background="#drawable/stock_price_bg"
tools:context=".FoundItemFragment" >
</RelativeLayout>
The layout is displayed left-justified rather than right justified. When I color the fragment_container FrameLayout, I see that it takes the entire width of the screen. I attach the fragment with the following code in my "FoundItemFragment extends Fragment implements OnClickListener" class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragView = null;
try {
fragView = inflater.inflate(layoutID, container, false);
//LoadData(fragView);
} catch (Exception ex) {
ex.printStackTrace();
}
return fragView;
}
The container parameter passed to this routine has the id of the fragment_container layout, so I am not attaching it to some other object which might have settings that override the alignParentRight setting. Why is the fragment displayed left-justified rather than right-justified?
The layout parent should be Relative Layout so that alignParentRight will work .
You are using the alignParentRight for a root element RelativeLayout
Just edit your container xml to
<?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="#drawable/back" >
<include
android:id = "#+id/login_header"
layout="#layout/header_layout"
android:layout_alignParentTop = "true"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:below = "#id/login_header"
android:layout_alignParentRight="true"/>
</LinearLayout>
<?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="#drawable/back"
android:orientation="vertical" >
<include
android:id = "#+id/login_header"
layout="#layout/header_layout"/>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Since generic layout hold the container of the fragment, you should align/place the fragment there.
Placement of the views of the fragment should be done in fragment specific xml file

view viewpager in another layout android

My main xml layout file....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayoutPlayerActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundimage"
android:orientation="vertical" >
</LinearLayout>
I created the viewpager.....
that layout file......
<?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.support.v4.view.ViewPager
android:id="#+id/carousel_layout_home"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:overScrollMode="never" />
</LinearLayout>
I load the images into this view pager by creating three classes & another xml file......
now I want to add this viewpager into linearlayout of main xml file....
It means I want dynamically view the viewpager in mainxml file. how to that? pls give me answer
thankyou
If you have two Layouts such as LinearLayout you can add one onto the other with addView(). In your Activity it would look something along the lines of,
LinearLayout layout = (LinearLayout) findViewById(R.id.relLayoutPlayerActivity);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout.addView(inflater.inflate(R.layout.idOfLayoutHoldingYourViewPager, null));

Layout XML inside an empty RelativeLayout

I have a "Tools.java" that shows "tools.xml" correct? Within this "tools.xml" RelativeLayout have a layout id = #+/contentLayout that is empty.
And I have several other activits "tool1.java", "tools2.java" ... that shows "tool1.xml", "tool2.xml" ...
How to call the class "tool1.java" for example and have it show the "tool1.xml" within the RelativeLayout that is in "tools.xml"
Thanks
<?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"
>
<include
layout="#layout/yourxmlfilename" />
</RelativeLayout>

Categories

Resources