Android Studio sample programm Error - android

I have this error when working in Android Studio. Please help me. I am new to android studio.

I suppose you copied all these lines from another project that you found online.
In the project where these lines originally come from there is a file dimens.xml in res folder which contains all these #dimen/... values. These are numeric values and they are used to set the padding of your Layout
You must copy this file in your project too or just delete the first 4 red underlined lines.
As for the 5th line you may leave it like this:
tools.context=".MainActivity"

1) create dimens file It's located in res/values/dimens , here's what a sample of the file look like:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
2) Make activity context like this:
tools:context=".MainActivity"

Related

Alias for mipmap launcher icon

Is it possible to somehow make alias for mipmap icon? For example we have icon named icon_app but in our lib we use ic_launcher (as it should be). So for drawables you are able to create reference with values/drawables.xml
I tried with following approach:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="ic_launcher" type="mipmap">#mipmap/icon_app</item>
</resources>
but it seems its not working i tried naming it both drawables.xml and mipmaps.xml, it seems it always tries to reference it from drawable instead of mipmap folder?
Further attempts show that the code originally posted works fine when compiled onto device, but is just not recognised by Android Studio.
I was hoping to alias the system default launcher icon, and this seems to work:
<resources>
<item name="test_alias" type="mipmap">#android:mipmap/sym_def_app_icon</item>
</resources>
...as do these:
<resources>
<mipmap name="test_alias">#android:mipmap/sym_def_app_icon</mipmap>
<mipmap name="test_alias2">#mipmap/ic_test</mipmap>
</resources>
...where ic_test is a "normal" mipmap in my res/mipmap resources folder.
To explain the use case in my situation, this above code is in a library project. The test_alias mipmap is declared in the library so that I can use it to put up a Notification.
But the intention is that the developer using the library would be able to override this icon with their own launcher, or other icon of their choosing by adding something like this into their own code, overriding the library definition:
<resources>
<mipmap name="test_alias">#mipmap/ic_launcher</mipmap>
</resources>

Cannot resolve symbols for dimen, mipmap, string although the corresponding xml files exist in res folder

android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.datafrominternet.MainActivity">
I am new to android development.
Above is part of my XML file.The system says cannot resolve symbol..... for all these. I do have all the dimen, string, written in the res folder already. I am not able to access them (both anything from dimen.xml or string.xml) in my XML file.
Similarly in the AndroidManifest.XML I am not able to access #mipmap/ic_launcher. Although I do have it in my res folder.
I have tried hard and not able to remove the error.
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
this is how my dimen.xml looks like. similarly string.xml is well defined too but still it says cannot resolve symbols
Goto res folder in your project structure, go to values directory and check whether dimen.xml file is exist or not. If exists then initialize your values like below
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen> // 16dp is customizable as per your requirement

Padding value in android

What is the meaning of this line of code?
android padding Right = "# dimen /activity_horizontal_margin".
What is #dimen referring to?
you use the dimens file like this so that you can easily set many things to the same value (like a constant in code), or so you can override the value easily for different screen sizes.
dimen is located in / res / values / in studio project like below
dimen.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="action_button_min_width">56dp</dimen>
<dimen name="indeterminate_progress_size">32dp</dimen>
</resources>
Learn more about project structure in android studio
https://developer.android.com/studio/projects/index.html
dimen.xml is the file which you can set constant values for dimensions.
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
It is usually created inside /res/values folder. If you don't find it there you can create and add values.
You may refer the following link for more details.
https://developer.android.com/samples/BasicSyncAdapter/res/values/dimen.html

Style / themes not working

whenever I try to apply a VERY simple style in eclipse I get errors:
the style is as below (filename styles.xml in res/layout)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="commonStyle">
<item name="android:background">#EEEEEE</item>
</style>
</resources>
Regardless of whether I try to apply this to a LinearLayout (using style="#style/commonStyle") or to an activity in my manifest file (using android:theme="#style/commonStyle") I ALWAYS get the error "No resource found that matches the given name...".
If I try to apply an Android theme to an activity (say Theme.Black) it works just fine.
If I remove any usage of my style, my R.java file is generated as normal with the following contents:
public static final int styles=0x7f030002; (in the generated layout class)
I have NO idea what is going on. It seems that NOONE is having the same troubles as me (Ive spent about 4 hours searching using google, and came up with not even a remotely close answer).
Ive tried restarting eclipse, clean building etc. etc. and nothing works what so ever...
So what am I missing?
Try moving your styles.xml file to res/values.
http://developer.android.com/guide/topics/resources/style-resource.html
Styles need to be stored in the res/values folder :)
http://developer.android.com/guide/topics/ui/themes.html (See Defining Styles)
The name of the XML file is arbitrary,
but it must use the .xml extension and
be saved in the res/values/ folder.
I haven't used styles in my code yet, however generally you need to use
android:
Does it work when you use this?:
<style android:name="commonStyle">
Please verify the you call the style in the correct manner :
for example this might compile but will not work :
style="mainButtonText"
this is the correct way to call style:
style="#style/mainButtonText"

Storing dimensions in xml file in Android

I am trying to consistently use the same dimension in all the views in my android app (e.g., a left margin of 20dp). If I were using HTML, I would simply use a CSS file, but I am at a loss of how to do this on Android.
Is there a way I can store this value in a xml file inside res/values and then use it in layouts?
e.g., I thought of storing them in strings.xml like
<string name="app_wide_left_padding">20dp</string>
and then using the following text in layout.xml
android:paddingLeft="#string/app_wide_left_padding"
but I am not sure it will work. Am I on the right track?
Thanks in advance for your answers.
Create a file called res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="icon_width">55dip</dimen>
<dimen name="icon_height">55dip</dimen>
<dimen name="photo_width">170dip</dimen>
<dimen name="photo_height">155dip</dimen>
</resources>
Then reference them in your other xml:
android:padding="#dimen/icon_width">
You can use styles (res/values). This article will be a good start.

Categories

Resources