I'm developing a dynamic keyboard application using soft keyboard sample. My application changes layout. For example I have a keyboard(has one key) then I set up the app and I can use keyboard(has one key).
I want to create an XML file after compilation and use this file in the application (reading XML file with xmlpullparser or xmlresourceparser). However, keyboard class needs XML's id. How do I create an XML id?
It can be defined in ids.xml or any custom file like constants.xml inside your res\values folder.
Examples:
<resources>
<item type="id" name="keyboard" />
</resources>
and can be accessed in the code as following:
R.id.keyboard
You can define ids using an ids.xml file in your res/values directory. Here's an example:
<resources>
<item type="id" name="my_keyboard" />
</resources>
In code, you would set the id like so:
keyboardView.setId( R.id.my_keyboard );
XML files are compiled (binary XML) and thus you don't create them at runtime.
If you want to change your keyboard layout dynamically, you'll do that programmatically rather than in XML.
Related
I like to integrate ads with AppLovin into my Android App.
In their documentation they say:
Declare the base banner height of 50dp in res/values/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>
I also have a dimens.xml present with other <dimen... value's for my app. They work from there as well.
What is the difference between attrs.xml and dimens.xml? What to use in a situation like this?
attrs.xml is a file that allows you to define custom attributes for your views in XML layout files. These attributes can be used to customize the appearance and behavior of your views, and can be accessed programmatically in your Java code.
dimens.xml on the other hand, is a file that allows you to define dimension values for use in your app. These values can be used to set the size and layout of views in your XML layout files, and can also be accessed programmatically in your Java code.
In this situation, you should use dimens.xml to define the banner_height dimension, because you will use this value to set the height of the banner ad view in your layout.
You could also use attrs.xml to define the banner_height attribute, but since you are defining a dimension value, not an attribute, it would be better to use dimens.xml instead.
It doesn't actually matter - <attr> and <dimen> are both value resources, and you can put them in any file in the res/values folder. They'll all be combined into the same set of resources, so the actual file you use is up to you! So you can do this if you want:
# res/values/whatever.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="someDimension">48dp</dimen>
<attr name="someColour" format="color"></attr>
</resources>
and it'll work exactly the same as if you used attrs.xml and dimens.xml. It's just convention to put attrs and declare-styleable things in a file called attrs.xml, all your dimension resources in dimens.xml, all your strings in strings.xml...
But you don't have to do that, it's up to you! For example, you might want certain resource strings to be stored in a different file, for organisation (maybe they shouldn't be translated). Or maybe some values are common to all configs and you want those in a single file, and you want a separate file for the qualified stuff (that goes in folders based on API level, night theme etc)
How does you register a custom xml-resource-file with android studio so that you can reference it from other xml files?
I understand that its possible to call values from new resource values programmatically using R but I was wondering if there was a way to do that statically from another XML file.
For example, referencing that new file (in this case called keys.xml) in another xml file
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#keys/MAPS_API_KEY" />
as opposed to using strings.xml
Edit: It turns out that #string does NOT specifically reference strings.xml. I was originally under the impression that it did and knowing that know makes my question seem a tad silly.
Just create a resource file like:
keys.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="testKey">TEST_VALUE</string>
</resources>
In code, you can call:
getString(R.string.testKey);
From another xml:
<TextView
...
android:text="#string/testKey"
...
/>
The trick is you declare as a <resources> in your xml file.
As I asked in the title, is there any way to do so?
Now, when I put all styles into one file it looks a little crowded, I would like to separate styles.
For example:
res/values/styles_for_main_screen
res/values/styles_for_set_screen
And then in main_screen layout
<TextView
style="#styles_for_main_screen/text_view_custom_style">
</TextView>
This example obviously doesn't work, but it shows what I'd like to achieve.
I read in every tutorial that we need to put our custom styles into styles.xml file, but I wonder if there is a possibility to diversify styles in few xml files?
Every question I read was something like "how to do .... in styles.xml".
I can't find question similar to mine.
Example how it should be done, thanks #Frank N. Stein for the answer:
This how looks my custom xml res/values/styles_for_main_screen
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_back">
<item name="android:background">#E81C1C</item>
<item name="android:text">whatr</item>
</style>
</resources>
and then to retrieve this style I just write:
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="#style/custom_back"
/>
so the convention looks like:
style="#(what I want to retrieve)/(name of style)"
from the android developers site
In XML: #[package:]style/style_name
You can call your style files whatever you want (if you respect the naming conventions and you put them all in the values folder/s), as you do with strings and colors.
Therefore, YES! You can have multiple ones, if so you desire.
Obviously, you will NOT specify the path to each file.
Referencing the style/s by using R.style.your_new_style is enough.
Remember that, android scans the files found int the /values directory by reading their content. For styles, every <style name="styleName" > ... </style> will be parser and a style object reference will be created.
Then, as Frank said, Yes. You can use whatever file name to write your custom styles.
I am a fresh beginner in programming, when I am learning from Android developer website
https://developer.android.com/training/basics/actionbar/adding-buttons.html
I confused with the type of file, which file shall I create? XML layout file or value file? And what is the difference for both file?
There is no any differents. Both are a xml file. Only difference is inside file. In layout file proframmers expect see layouts and in values programmers expect see values. Both should be in correct folders layout or values. Android studio just created 2 buttons to create this xml file to help developers and provide for them some templates with xmlns. That all :)
You need to create a layout file, because you're creating a menu.
Simply create a xml file and add contents like this:
<menu 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"
tools:context="com.app.tweetdemo.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
There is no need to confuse as both layout and values files are xml files and the xml files need to be kept accordingly in their respective folders.
I want to define beans into xml file and want to inject it using RoboGuice in android app, is it possible with RoboGuice?
I believe, the answer is NO.
Though you can inject objects of simple types. A simple example:
Place this in your res/values folder:
<resources>
<item name="flag" type="bool">false</item>
</resources>
And this in your res/values-land folder:
<resources>
<item name="flag" type="bool">true</item>
</resources>
In your activity:
#InjectResource(R.bool.flag) private Boolean flag;
Now you can use flag to determine whether device is in landscape mode.