How to have a header in every layout page? - java

I want that this code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView android:id="#+id/logo" style="#style/logo"
android:layout_alignParentTop="true"
android:text="#string/logo" tools:context=".MainActivity" />
to start in every layout. How can I do that I wouldn't need to add this to every layout page? Like for example in PHP I would use <?=include("header.php");?> (just an example, actually it's bad practise, doesn't matter here). Thank you.

Save your header in XML file and then include this XML as child layout in other layouts:
<include layout="#layout/headerlayout" android:id="#+id/headerLayoutid" ... />
The headerlayout.xml is name of your above layout that should be defined in res/layout and you like that be shown as header in all layouts(layout="#layout/headerlayout"),also headerLayoutid is id of your headerlayout(in it's parent) and you can reference to it in your parent layouts or in your code.
You can override all the layout parameters. This means that any android:layout_* attribute can be used with the tag. Here is an example:
<include android:layout_width="fill_parent" layout="#layout/image_holder" />
You can see more details in about include in this page.
Edit:
If you have problems in about finding views in included layout,see this questions,I hope these help you:
findViewById not working for an include?
Finding an view by id?

The <include> tag is what you want. Read about it in Re-using Layouts with <include/>

You can use the <include .../> tag in your layout to reuse your header where is needed. See here for an example.

Related

Custom wrapping view in Android using xml layouts

I want to create a wrapper element that can be reusable throughout the whole app, because it's the base of each screen.
This wrapper should be possible to add to layout editor in Android Studio and needs to be able to hold any inner elements inserted in editor.
But these elements have to be inserted inside the layout under header with image and only inside a specified area defined in wrapper xml layout (innerContainer).
This is the layout of wrapper:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
...
android:background="#drawable/frame_container_layout"
tools:context=...>
<include
android:id="#+id/cv_header"
layout="#layout/cv_header"
...>
</include>
<android.support.constraint.ConstraintLayout
android:id="#+id/innerContainer"
.../>
</android.support.constraint.ConstraintLayout>
What I need is to use this layout in custom view in a way, that when it's used somewhere, the elements will be only inserted into innerContainer.
Child XML:
<...custom_views.WrapperFragmentView
...
app:exampleColor="#33b5e5"
app:exampleDimension="24sp"
app:exampleDrawable="#android:drawable/ic_menu_add"
app:exampleString="Hello, WrapperFragmentView">
<TextView
android:id="#+id/textView1"
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</...custom_views.WrapperFragmentView>
The child is now inserted over the wrapper layout, which is what I don't want. It's not inside innerContainer.
In kotlin file I defined
class WrapperFragmentView : android.support.constraint.ConstraintLayout {
...
private fun init(attrs: AttributeSet?, defStyle: Int) {
var inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.cv_wrapper_fragment, this, true)
findViewById<android.support.constraint.ConstraintLayout>(R.id.innerContainer)
The innerContainer is the view I want to have as a root element for the children that are going to use it. Not the whole layout cv_wrapper_fragment.
How to set this in the WrapperFragmentView?
Or, if the whole concept is wrong and if there is an easier approach how to work with reusable wrapping elements I am all for that.

Could Resource xml can be called from layout.xml?

i just come trough a doubt
We had rexource XML and Layout
we can call another layout.xml by this way
<include layout="#layout/content_main" />
in same way can we call #xml ?
Just a doubt
and I know we can so it from code java by (R.XML.ook);
Is there any other way from layout ?
Thank you
It's impossible. I'm wondering what is your purpose when you want to call #xml in layout file?
Layout file can only include View and ViewGroup. So only view tag (TextView, Button,...) or layout tag (include, merge tag) or ViewStub are available for using in layout file. xml file can have many invalid tags, so it can't be use in layout file.

How is +id valid inside a RelativeLayout?

In a relative layout I see a pattern as:
<TextView
android:id=“#+id/txt_id”
etc
android:layout_alignBottom="#+id/some_other_txt”
etc
/>
<TextView
android:id="#+id/some_other_txt"
etc
/>
I thought +id is used only when creating an id for a widget. Is this a kind of “trick” to layout a widget relative to another widget declared later in the file?
UPDATE:
This question is specifically about the RelativeLayout possitioning. Not about the difference in syntax in general as the linked question
Its just a reference point for placing the different elements in the layout builder.
From this SO answer
The first time you reference an ID, use the #+ prefix, which tells the resource builder to add the ID,
Here you're making a forward reference to a widget & id that's not already defined:
<TextView
android:id=“#+id/txt_id”
etc
android:layout_alignBottom="#+id/some_other_txt”
etc
/>
Here you are defining it:
<TextView
android:id="#+id/some_other_txt"
More context here

Android: Default layout for listitem containing title and subtitle

I have made a listitem, containing a title and a subtitle. Looking to the result, I think it doesn't look really professional. So that's why I'm asking.
The listitem I'm looking for is pretty common (it's used in a lot of apps i.e. Android's default settings menu and it's also shown when you add a listview in the graphical layout editor tab in eclipse).
So my question is: Where can I find a default layout for a listitem with a title and a subtitle?
Resource id is android.R.layout.simple_list_item_2
Upper text line has id android.R.id.text1 and lower one - android.R.id.text2
Layout is located in the <ANDROID_SDK_ROOT>/platforms/<any_api_level>/data/res/layout folder
OR
You can use TwoLineListItem from the default Android controls list(it is located under "Advanced" tab in Eclipse Layout Editor)
OR
You can build your own layout with anything you like(for example LinearLayout with orientation="vertical" and two TextEdits added
So The Best Way Is:- I took out the simple list item 2 and made an layout in my project and with little editing it took the same layout as you may give in android.R.simple_list_item_2
So the code is:-
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingLeft"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingEnd="?attr/listPreferredItemPaddingRight"
android:paddingRight="?attr/listPreferredItemPaddingRight">
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#+id/text1"
android:layout_alignLeft="#+id/text1"
android:textAppearance="?attr/textAppearanceListItemSmall" />

How can I define a fragment's layout from XML?

I'm trying to define a fragment's layout in XML in the same way that I defined the layout of my view.
Is this possible? I tried several things, but none of them seem to work.
My activity layout looks as follows (main.xml):
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="full.lommeregner.Lommeregnerrv2Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lommeregnerv2">
</fragment>
</ListView>
Now, since I'm not a big fan of generating layouts through raw Java code, I tried defining my fragment as follows (fragment_lommeregner.xml):
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="full.lommeregner.Lommeregnerrv2Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lommeregnerv2">
<!-- my content was located here (some textviews, buttons and so on). -->
</fragment>
Am I doing something wrong? How can I define the layout of a fragment through XML?
A Fragment works much like an activity, in that you need a Java class file to go with it. you cannot create a Fragment just by creating a fragment layout - you need a class for your fragment:
Create a layout XML and an Activity subclass for your activity
Create a layout XML and a Fragment subclass for your fragment
Tie the two together in your Activity layout XML (or using FragmentTransaction if you want to do it in Java code)
If you haven't already done so, read, re-read and digest everything on this page:
https://developer.android.com/guide/components/fragments.html
There's a lot there, but Fragments are an essential part of Android apps now so it's required reading. The good news is that the basics of fragments is pretty simple.
Simply add into your <fragment> tag as property:
tools:layout="#layout/your_layout_xml"
And in your main.xml (or <fragment> tag container) into your parent container (in this case ListView):
xmlns:tools="http://schemas.android.com/tools"

Categories

Resources