I want to use switch button in my app. As its API is 11 and switch needs API higher than 14, I decided to use merge and include method but I get this error in values-v14/compound_switch.xml file : Element merge must be declerd. here are my files, what is the problem?
Main layout:
<include layout="#layout/compound_switch" />
layout/compound_switch.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/night_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView20"
android:layout_toLeftOf="#+id/textView20"
android:layout_toStartOf="#+id/textView20"
android:layout_marginRight="5dp"
android:checked="false" />
</merge>
values-v14/compound_switch.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="#+id/night_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView20"
android:layout_toLeftOf="#+id/textView20"
android:layout_toStartOf="#+id/textView20"
android:layout_marginRight="5dp" />
</merge>
Move your layout that has the switch from the folder values-v14 to a folder called layout-v14. Layouts should be in the layout folder. The values folder is for colors, dimensions, attributes, etc.
Related
I have some drawable resources that are used only in a single xml layout. I would like to insert them into the specific xml layout (inline) so i could have a better organization of my files and a less overloaded drawable folder
i found this topic:
https://developer.android.com/guide/topics/resources/complex-xml-resources
but this using an inline drawable as resource for another one... i would like to use it in a layout xml
EXAMPLE:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner"
android:orientation="horizontal"
android:padding="#dimen/smallMargin">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:drawableTop="#drawable/code"
android:text="#string/delete"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
where code.xml is:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="#dimen/popupButtonSize"
android:height="#dimen/popupButtonSize"
android:drawable="#drawable/ic_population" />
</layer-list>
code is only used in that specific layout, is there any way to make it inline?
What seems to be the issue? If you follow the guide it works without a problem:
Add xmlns:aapt="http://schemas.android.com/aapt" to root node
Replace android:drawableTop="#drawable/code" attribute with aapt:attr child
Final XML will look like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner"
android:orientation="horizontal"
android:padding="#dimen/smallMargin">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/delete">
<aapt:attr name="android:drawableTop">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="#dimen/popupButtonSize"
android:height="#dimen/popupButtonSize"
android:drawable="#drawable/ic_population" />
</layer-list>
</aapt:attr>
</androidx.appcompat.widget.AppCompatButton>
</androidx.appcompat.widget.LinearLayoutCompat>
This is the .xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:seekarc="http://schemas.android.com/apk/res/com.pelkinsoft.enpower"
android:layout_width="match_parent"
android:layout_height="match_parent" >
The errors occur at:
<com.triggertrap.seekarc.SeekArc
android:id="#+id/seekArc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="30dp"
seekarc:progressColor="#color/arc_color"
seekarc:rotation="180"
seekarc:startAngle="30"
seekarc:sweepAngle="300"
seekarc:touchInside="true" />
I am new to Android and this is keeping me from being able to effectively work on this app. Any help would be much appreciated.
You must add the Seek Arc library (https://github.com/neild001/SeekArc#adding-to-your-project) to your project
Inside res foder, create following file:
res/values/color.xml
Then, define the color used by SeekArc view:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="arc_color">#0000FF</color>
</resources>
OR
Manually set the color in the SeekArk view:
<com.triggertrap.seekarc.SeekArc
android:id="#+id/seekArc"
android:layout_width="match_parent"
....
seekarc:progressColor="#0000FF"
....
/>
ListFragment by defaults shows a progressbar while the data get loaded. I plan on putting the listfragment in xml file and have it show progressbar till I get the data from the server.
The reason I m trying to put listfragment inside xml is because in my layout I have a linearlayout above the place where I plan to put listfragment.
Here is my xml file.
<?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"
>
<LinearLayout
android:id="#+id/filterHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#color/white"
>
<ToggleButton
android:id="#+id/filterToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:textColor="#color/black"
android:textOff="#string/filterToggleButtonText"
android:textOn="#string/filterToggleButtonText"
android:drawableLeft="#drawable/filter_small"
/>
</LinearLayout>
<fragment
andorid:name="in.coupondunia.androidapp.testListFragment"
android:id="#+id/couponsByCategoryFragment"
android:layout_below="#id/filterHolder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Have you tried using http://developer.android.com/reference/android/app/ListFragment.html#setListShown(boolean) with parameter true? From the docs it might work.
When creating a custom element with attributes in Android I need to put the namespace of the application in the layout.
For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:whatever="http://schemas.android.com/apk/res/org.example.mypackage"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.example.mypackage.MyCustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>
Does this also requires that the structure of my project in Eclipse be the same as the name of the Android package as definied in the Manifest - as per the example?
Would this work too:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:whatever="http://schemas.android.com/apk/res/org.mycompany.myproduct"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.example.mypackage.MyCustomView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
whatever:my_custom_attribute="Hello, world!" />
</LinearLayout>
Its the package name of your application that should be reflected. So its correct to use it as you are doing.
For example:
In xmlns:whatever="http://schemas.android.com/apk/res/org.mycompany.myproduct the last part org.mycompany.myproduct should be the same as your package name. And you may change xmlns:whatever to anything like xmlns:theblitz but then make sure you do use theblitz as a prefix for your attributes in the xml.
For more info, read this
How to include a xml data into an other xml data?
I have a header xml-file for my application which I want to use in my special other content xmls. Is there a ways to include the header into my content?
use include tag
<?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"
>
<!-- Header -->
<include
android:id="#+id/container_header_lyt"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_above=...
android:layout_toLeftOf=...
layout="#layout/header_logo_lyt" //Name of the xml layout file you want to include
/>
...
</RelativeLayout>
You have to declare your "body" xml layout with a < merge > tag to use < include > tag on your principal layout.
<?xml version="1.0" encoding="utf-8"?>
<merge>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/image" / >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Some Text" / >
</merge>
This is the content of your < include > tag
You should use < include > tag: Re-using Layouts with