I have a BaseLayout, the BaseLayout is a ParentLayout for other layouts. In other words, the other Layouts inherit/include it. The BaseLayout has DrawerLayout (shared for all layouts) but each Activity has a different content.
My BaseLayout looks like this:
<?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="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/myDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Main Content-->
<LinearLayout
android:id="#+id/rootLinearLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<!--Content-->
<!--must add Other Layout elements there
must add Other Layout elements there
must add Other Layout elements there
must add Other Layout elements there
must add Other Layout elements there !-->
</LinearLayout>
<!-- Left Navigation Derawer -->
<LinearLayout
android:id="#+id/llLeftDrawer"
android:orientation="vertical"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="?attr/colorAccent"
android:padding="15dp"
android:layout_gravity="right">
<!-- Left Navigation Derawer Content-->
<TextView
android:text="Categories"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:gravity="center"
android:textColor="#FFFFFF"
fontPath="fonts/MobileFont.ttf" />
<ListView
android:id="#+id/lvLeftMenu"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
and included in Postlayout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="#layout/BaseLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
How can I add PostLayout elements into the BaseLayout LinearLayout id rootLinearLayout?
postLayout element is this:
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1">
<TextView
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPostTitle"
android:gravity="right" />
<TextView
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPostDate"
android:gravity="right" />
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/wbvPostContent" />
</LinearLayout>
First of all, provide id attribute to the included Baselayout in the Postlayout.
Then, you can get the View object from the included view by calling the following from the activity that has the Postlayout :-
View includedView = (View)findViewById(R.id.idThatYouGaveToIncludedLayout);
Then, you can access all Baselayout views from the includedView object and perform the operations that you require.
For example,
LinearLayout rootLinearLayout = (LinearLayout)includedView.findViewById(R.id.rootLinearLayout);
Similar to the above code, you can obtain objects of the other views of your Baselayout.
Bruno Bieri get solution is java and i converted to xamarin:
LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View v = inflater.Inflate(Resource.Layout.your_layout, null);
// fill in any details dynamically here
TextView textView = v.FindViewById<TextView>(Resource.Id.a_text_view);
textView.Text="your text";
// insert into main view
ViewGroup insertPoint = FindViewById<ViewGroup>(Resource.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
and Rohan Stark solution good for Access to elements of Included Layout
tanQ Bruno Bieri and Rohan Stark,
when you add view as include, can use all view like own main layout views. find the layout as a view and add view to it. like below:
LinearLayout linearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1);
linearLayout1.addView(new TextView(context));
Related
I have Custom Layout extending FrameLayout with custom XML. Parent children views have android:layout_gravity and android:gravity set to center. It is looking exactly as It should in layout preview, but in App, TextView and ProgressBar is not centered correctly. It is centered horizontally but not vertically.
Any suggestions?
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/buttonParent"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="#+id/buttonIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitCenter"/>
<TextView
android:id="#+id/buttonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="true"/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:indeterminate="true"
android:layout_gravity="center"
android:gravity="center"/>
</FrameLayout>
Output from LayoutInspector
UPDATE - SOLUTION:
I found out solution. I have to add this to my class extending FrameLayout
val rootParams = LayoutParams(buttonWidth.toInt(), buttonHeight.toInt())
rootParams.gravity = Gravity.CENTER
layoutParams = rootParams
It looks like that FrameLayout parent inside XML inflated layout is not considered as parent. Parent is not even visible in XML it is class where I inflated XML. I have to access parent parameter by this.parameter not in XML.
Set android:layout_gravity="center to your FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content">
Or set android:layout_height="match_parent" and android:layout_width="match_parent" to your FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
Try to set android:layout_height="match_parent" instead of android:layout_height="wrap_content" in your FrameLayout, like following.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="wrap_content"
android:layout_height="match_parent">
......
I am trying to achieve this layout
<RelativeLayout>
<Button> <!-- Always attached to the top of the parent -->
<RecyclerView>
<Button> <!-- Always attached to the bottom of the parent -->
<RelativeLayout>
Here is the 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:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
My problem is that although the views are placed correctly, whenever I scroll to the last item in the RecyclerView, it is partially hidden behind the bottom Button. How can I place RecyclerView exactly between the 2 buttons no matter how many items are there in the RecyclerView?
You need to tell the RecyclerView to be below and above your buttons.
<?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:orientation="vertical">
<Button
android:id="#+id/topButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewTemperatureLog"
android:layout_above="#+id/bottomButton"
android:layout_below="#+id/topButton"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/bottomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
You must use LinearLayout as a parent when you are specifying layout_weight attributes :
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</LinearLayout>
You can always specify the position of the recycler view inside the relative layout. Where you can put it on top, bottom or both of a view.
First of all you need to add ids to the buttons and then you can specify the position of the recycler view
android:layout_below="#+id/button_top" // place the view below the specified view id
android:layout_above="#+id/button_bottom" // place the view above the specified view id
Click here see the full list on how to position the view inside the RelativeLayout
I have this layout:
<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=".MainActivity" >
<TextView
android:id="#+id/campomodulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="21sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="#+id/listagiocatori"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/campomodulo" >
</ListView>
<ListView
android:id="#+id/panchina"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/listagiocatori" >
</ListView>
The first listview fills about half display, adding the second just below that, the total height of the two listviews exceeds the screen, so i have a scroll on the second listview.
I would like a general scrolling, not only for the second listview. How can i do that?
You can use a unique ScrollView and then create two different linearlayouts to "implement" your listview. Then you add your items programmatically like this:
LinearLayout layout = (LinearLayout)findViewById(R.id.MyListLayout);
for (int i=0; i<list.size(); i++) {
Item item = list.get(i);
View view = inflater.inflate(R.layout.MyRowLayout, null);
Textview myTextView = view.findViewById(R.id.MyTextView);
myTextView.setText(item.getString());
list.addView(vi);
The main layout should look similar to this:
<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=".MainActivity" >
<TextView
android:id="#+id/campomodulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="21sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/campomodulo">
<LinearLayout
android:id="#+id/listagiocatori"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<LinearLayout
android:id="#+id/panchina"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/listagiocatori" >
</LinearLayout>
</ScrollView>
Then you just need to set a simple layout for every row in order to add it to the appropriate layouts. It can be simply a TextView or a more elaborated layout
I am facing an issue where I need to generate dynamic views one below another inside a scrollview. Views should be generated after button click.
for the first button click, it is working fine but after second click it is failing with error "java.lang.IllegalStateException: ScrollView can host only one direct child"
can anyone help me to get this fixed.
My main activity layout where scrollview is activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/parentRL">
<Spinner android:id="#+id/spinState" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Spinner android:id="#+id/spinCity" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#id/spinState"/>
<TextView android:id="#+id/cityCount" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="#id/spinCity" android:layout_below="#id/spinState" android:layout_marginLeft="5dp"
android:textIsSelectable="false"/>
<Button android:id="#+id/showAddress" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="#id/spinState" android:onClick="populateAddress" android:text="Search"/>
<ScrollView android:id="#+id/scView" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="#id/spinCity">
</ScrollView>
</RelativeLayout>
And the view layout that need to be attached with scrollview is address_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#+id/center" android:layout_width="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:text="#string/hello_world" android:layout_margin="10dp"
android:textColor="#FF0000" android:textSize="30dp"
/>
In main activity, inside button click listener:
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.address_layout, null);
TextView _c_ = (TextView)(myView.findViewById(R.id.center));
_c_.setVisibility(View.VISIBLE);
_c_.setText(my_random_number);
View insertPoint = findViewById(R.id.scView);
((ViewGroup) insertPoint).addView(myView, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
the android scrollview supports only child so that it throws java.lang.IllegalStateException.
You should add one LinearLayout or Relative layout in scrollview and then add children into that layout.
<ScrollView android:id="#+id/scView" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="#id/spinCity">
<LinearLayout android:id="#+id/layout"
android:layout_width="match_parent" android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
I've got two layouts first and second, I want to insert second to first.
I want to insert second layout into layout that has id #+id/layout
when I press button Get Layout, it show the the second layout on the bottom of button
first layout
<LinearLayout 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"
android:orientation="vertical" >
<Button
android:id="#+id/btn_get_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Layout" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
second layout
<?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/card_base"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img_cover"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="#drawable/card_beauty" />
<ImageView
android:id="#+id/img_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:scaleType="fitXY"
android:src="#drawable/sample_0" />
</RelativeLayout>
</LinearLayout>
if I understood you right you should use the following code within your first layout
<include
layout="#layout/second_layout"
android:id="#+id/includedLayout"
android:visibility="gone"
android:layout_below="#id/buttonId" />
and then in your button's action you just use
((RelativeLayout)findViewById(R.id.includedLayout)).setVisibility(View.VISIBLE);
LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
getLayoutInflater().inflate(R.layout.second_layout, placeHolder);
Use include
Here is a somewhat fuller example. The square blue layout is inserted into the main layout using include.
activity_main.xml
This is the main layout. The custom layout is referenced using include. You can override any of the attributes here, too.
<?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:padding="16dp">
<!-- Here is the inserted layout -->
<include layout="#layout/my_layout"/>
</RelativeLayout>
my_layout.xml
This is the custom layout that will be inserted into the main layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="5dp"
android:text="My Layout"/>
</RelativeLayout>
You Should use this code snippet to add a Layout inside another one.
parentLayout=(LinearLayout)findViewById(R.id.parentLayout);
inflateView=View.inflate(this,R.layout.view_video,parentLayout);
Here the parentLayoutis the root View and R.layout.view_video is the layout that you need to insert.
Use LayoutInflator to inflate an external layout into existing layout. Checkout LayoutInflater on Android Dev Site for more information about it.