This is my very first attempt at including ads in my app. I have read the online documentation and read it word for word. Now, the only part I don't get is how to actually request ads and to add it into my app.
AdView adView = (AdView)findViewById(R.id.ad);
That seems to work so far so good.
Anything else I do will just force close.
For example adding this line:
adView.setAdListener(this);
I have implemented AdListener to the Activity.
By the way, when I hover over the imported classes:
import com.admob.android.ads.AdView;
or other similar classes, it says: Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found.
Is that supposed to be correct?
It's tough to say for sure without seeing your code and your layout, but here are some things you might want to check:
1) The AdMobActivity is declared in your AndroidManifest:
<activity android:name="com.admob.android.ads.AdMobActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboard|keyboardHidden" />
2) You request the INTERNET permission in your AndroidManifest:
<uses-permission android:name="android.permission.INTERNET" />
3) Your attrs.xml file contains the necessary styles:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.admob.android.ads.AdView">
<attr name="backgroundColor" format="color" />
<attr name="primaryTextColor" format="color" />
<attr name="secondaryTextColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
</declare-styleable>
</resources>
4) Your AdView is included in your layout:
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
5) Your AdView is being found properly:
AdView adView = (AdView)findViewById(R.id.ad);
if (adView == null) {
Log.e(TAG, "AdView not found!");
}
Related
First of all, i know that there plenty of post about the issue and i think i read them all. If someone ask i will attach them for approve :)
I have an issue with attrs. The issue is that sometimes i see programmers using it when they create a custom view but sometimes i see that they are used also when building a theme.
For example:
<declare-styleable name="Main_Theme">
<attr name="background" format="reference" />
<attr name="backgroundCard" format="reference" />
<attr name="secondaryTextColor" format="reference" />
<attr name="primaryTextColor" format="reference" />
<attr name="primaryColor" format="reference" />
<attr name="secondaryColor1" format="reference" />
<attr name="secondaryColor2" format="reference" />
<attr name="secondaryColor3" format="reference" />
<attr name="dividerColor" format="reference" />
and then they creates a theme using it.
For example:
<item name="background">#color/dark_theme_background</item>
<item name="backgroundCard">#color/dark_theme_card_background</item>
<item name="secondaryTextColor">#color/dark_theme_scores_new</item>
<item name="primaryTextColor">#color/dark_theme_primary_text_color</item>
<item name="primaryColor">#color/dark_theme_primary_color</item>
<item name="secondaryColor1">#color/dark_theme_secondary_1_color</item>
<item name="secondaryColor2">#color/dark_theme_secondary_2_color</item>
<item name="secondaryColor3">#color/dark_theme_secondary_3_color</item>
<item name="dividerColor">#color/dark_theme_divider_color</item>
Then i saw them using those items as values for attributes in layout activity xml file
So i really can't understand the variety use of attrs.
I hope someone can help me to understand cause i feeling very confused
attr is used when make custom view with custom attribute.
If you define attr, you can use it in xml file.
Normal View use default attribute such as width, height, background, text etc.
So you can use it in xml file.
<TextView
android:width="match_parent"
android:height="wrap_content"
android:background="#color/white"
android:text="#string/app_name" />
But there is no attribute for your custom view.
If your custom view needs dividerColor attribute, you can't use it android:dividerColor as following.
<YourCustomView
android:width="match_parent"
android:height="wrap_content"
android:background="#color/white"
android:dividerColor="#color/black" /> // it caused compile error.
So you need your attribute for use in xml file.
For it, you need declare attrs in attrs.xml file. (xml file name can change.)
<declare-styleable name="Main_Theme">
<attr name="dividerColor" format="reference" />
</declare-styleable>
And then you can use new attribute in view xml file.
<YourCustomView
xmlns:app="http://schemas.android.com/apk/res-auto" // need define app
android:width="match_parent"
android:height="wrap_content"
android:background="#color/white"
app:dividerColor="#color/black" /> // it's works
PS: You need to additional code for use custom attr in YourCustomView class file.
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Main_Theme);
dividerColor = ta.getColor(R.styleable.MainTheme_divider_color, Color.WHITE);
Attr represents an attribute in an element. Simply put attrs are allowable quantities that are defined in a schema associated with a document.
Whenever we create a custom view we don't want the view to accept every possible value under the sky, so we define a few values that can be accepted by the view using the attr interface. these attributes are defined using declare-stylable that enables you to define attributes for custom views.for example:
<resources>
<declare-styleable name="PieChart">
<attr name="showText" format="boolean" />
<attr name="labelPosition" format="enum">
<enum name="left" value="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
this custom view accepts showText and label attributes. the showText accepts a boolean value and the labelPosition accepts values: left and right. like the example below
<PieChart
showText="false"
labelPosition="left"/>
similarly in themes we need to define certain attributes that can be used inside the theme for example if we create 2 themes and want to define a different primary color that will be used by the app when using either theme. we first declare the color attr like so in the attrs.xml:
<declare-styleable name="Main_Theme">
<attr name="color_primary" format="color" />
</declare-stylable>
and then inside the styles.xml we define the values for the color_primary attr for the 2 themes
<style name="theme_one">
<item name="color_primary">#ff0000</item> // red
</style>
<style name="theme_two">
<item name="color_primary">#00ff00</item> // green
</style>
this value could then be used in layout files like so: ?attr/color_primary
similarly attr could be used to define a wide range of things. an attr element itself has 2 attributes the name with which you refer it in other parts of the code and the format which could be color, boolean, dimension, float etc. for example enums could be defined as:
<attr name="some_enum_attr">
<enum name="value_one" value="1" />
<enum name="value_two" value="2" />
</attr>
I am using custom font style for that created custom xml attributes.already used enum in declared style attributes but still no success. Its seems some messed up and facing the below-mentioned issue.
Facing this issue:
Error:(555, 5) error: expected enum but got (raw string) bold.
attrs.xml file is
<declare-styleable name="CustomTextView">
<attr name="font_name" format="string" />
<attr name="font_style" format="enum">
<enum name="normal" value="0" />
<enum name="bold" value="1" />
<enum name="italic" value="2" />
</attr>
</declare-styleable>
I created styles for Textview.
<style name="STextView">
<item name="android:textColor">#color/colorBlack</item>
<item name="font_style">bold</item>
</style>
It was working earlier recently updated compile sdk version in build.gradle to 27 and stated facing this issue.
Change
<item name="font_style">bold</item>
to
<item name="font_style">#bold</item>
I've been looking to the Google iosched 2014 app to ensure im following correct design patterns for material design and specifically for pre-L devices with the support v7 library and appcompat v21.
In order to use the material design theme you must inherit from "Theme.AppCompat", but i can't seem to understand or find any documentation of the styles attributes in the link below. They aren't under the "android:" namespace and i dont seem to be able to use them myself.
https://github.com/google/iosched/blob/master/android/src/main/res/values/styles.xml#L32-L38
<item name="actionBarIconColor">#fff</item>
<item name="actionBarInsetStart">#dimen/keyline_2</item>
<item name="homeAsUpIndicator">#drawable/ic_up</item>
<item name="spinnerBarInsetStart">#dimen/keyline_2_minus_16dp</item>
<item name="popupItemBackground">?android:selectableItemBackground</item>
<item name="photoItemForeground">?android:selectableItemBackground</item>
<item name="photoItemForegroundBorderless">?android:selectableItemBackground</item
I'm barely familiar with custom attributes in /attr folder, but how can i find where they are used.
<declare-styleable name="BaseTheme">
<attr name="actionBarIconColor" format="color" />
<attr name="actionBarInsetStart" format="dimension" />
<attr name="spinnerBarInsetStart" format="dimension" />
<attr name="popupItemBackground" format="reference" />
<attr name="photoItemForeground" format="reference" />
<attr name="photoItemForegroundBorderless" format="reference" />
</declare-styleable>
I don't understand how this custom set of attributes is being used and its bugging me. Can anyone explain to me whats going on here and how they attributes are being used?
Here's a search of the repository that shows some instances of where they are used:
https://github.com/google/iosched/search?utf8=%E2%9C%93&q=photoItemForeground
I am having problem with invoking a custom component in my Android projects in Eclipse. It seem that I do not understand how the namespace declarations belong together. I have checked several other threads here at SO, which at first seem to be related, but I cannot solve my problem with these:
android-custom-control-namespace-issue
how-to-pass-custom-component-parameters-in-java-and-xml
android-custom-widget-styles-how-to-put-them-into-a-namespace
I have the following set-up (code is anonymized):
/values/extra_attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="extraComponent">
<attr name="count" format="integer" />
</declare-styleable>
</resources>
/layout/extra_main.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/extra_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/extra_main"
/>
</merge>
/com.site.package.extra/extra.java
package com.site.package.extra;
... misc imports...
public class Extra extends FrameLayout
{
... misc code...
}
/com.site.package/main.java (start-up class)
package com.site.package;
... misc imports...
public class Main extends Activity
{
... misc code...
}
/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/main_style"
xmlns:extra="http://schemas.android.com/apk/res/com.site.package" >
<com.site.package.Extra
android:layout_width="wrap_content"
android:layout_height="wrap_content"
extra:count="3">
</com.site.package.Extra>
</RelativeLayout>
The problem I face is; whatever I do, I cannot manage to invoke my custom component. The errors occur in may layout and I have tried to change the following items:
name space declaration
xmlns:extra="http://schemas.android.com/apk/res/com.site.package"
xmlns:extra="http://schemas.android.com/apk/res/com.site.package.extra"
xmlns:extra="http://schemas.android.com/apk/res/extra"
component invokation
<com.site.package.extraComponent />
<com.site.package.Extra.extraComponent />
<extraComponent />
<Extra.extraComponent />
<android.view.ext.extraComponent />
attributes
extra:count="3"
com.site.package.extra:count="3"
In neither case I manage to get any help from intellisense so I am completely lost. I really do not understand how the namespaces are working here and how I should put the code to work.
EDIT :
I forgot to include my AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.site.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_logo"
android:label="MyApp"
android:name="MyApp" >
<activity
android:label="MyApp"
android:name=".Main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</manifest>
Your custom component is situated in the package com.site.package.extra(from the code you posted) so you could use it in the xml layout with:
<com.site.package.extra.Extra // ... other attributes
or with:
<view class="com.site.package.extra.Extra"
// ... other attributes />
The namespace for the custom attributes:
xmlns:extra="http://schemas.android.com/apk/res/com.site.package"
and to use them:
extra:count="3"
I've been trying to set a holo theme in Android, but I haven't been able to get it to recognize it. Any ideas?
Posted is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" android:theme="#android:style/Theme.Holo">
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
It gives me the red line under #android:style/Theme.Holo even if I change the minSdkVersion to 11. Any ideas?
Update:
I changed the line <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15"/> to <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15"/> and I am still getting the same error.
Update 2:
This ended up being that my target api was specified correctly in the manifest, but not in project properties. Weird, but now as is well.
Eclipse is giving you an error because SDK versions 7-10 won't know what Theme.Holo is. You need to provide separate styles for both platforms to ensure that the correct style will be found at runtime.
In your res/values/styles.xml file, create the following style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyTheme" parent="#android:style/Theme.Black" />
</resources>
In your res/values-v11/styles.xml file, create the following style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyTheme" parent="#android:style/Theme.Holo" />
</resources>
In your AndroidManifest.xml file, use the following line for your application's theme:
android:theme="#style/Theme.MyTheme"
you are trying to use the Holo theme for API Low than Android 4.0 that's why you get the red Line you can:
Use the HoloEverywhere project
Pick the necessary resources for Holo Theme you most want from the
ICS source and create your own custom theme/styles
https://github.com/android/platform_frameworks_base/tree/master/core/res
You ca also try to make use a custom theme for Android Api 7 to 10
and the holo theme for Api 11 and higher, see #Alex Lockwood's
answer.
The Holo theme needs to be updated in all of the style folders. Check under the values folder, values-sw600dp, values-sw720dp-land, values-v11 and values-v14. The theme on values-v14 overrides all other themes on the higher API's.