Appcompat v7 Android 5.0 ActionBarActivity TextView Not Visible - eclipse

I've attempting to upgrade my Android project to compile with Android 5.0. I was using the appcompat-v7 library successfully before this. Now using ActionBarActivity and Theme.AppCompat causes the views in my fragment layout not to render or not to be visible. If I change it back to Activity and remove the appcompat theme, the views are visible again. Can anyone point out the configuration that I'm doing wrong? My Eclipse project is compiling with Android 5.0, Private Libraries of android-support-v4.jar and android-support-v7-appcompat.jar. Under Android dependencies is android-support-v7-appcompat.jar.
And I have the following code:
in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE something>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.ctest"
android:versionCode="33"
android:versionName="1.4.0" >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18"/>
<application
android:icon="#drawable/ic_launcher"
android:label="My App"
android:allowBackup="true" >
<activity
android:name=".MyActivity"
android:theme="#style/Theme.AppCompat">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE something>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Nothing here"
android:textColor="#color/blue" >
</TextView>
</LinearLayout>
In MyActivity.java:
package com.mycompany.ctest;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MyActivity extends ActionBarActivity {
private FragmentManager mFM;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFM = getFragmentManager();
mFM.beginTransaction()
.add(android.R.id.content, new MyFragment(),"frag")
.commit();
}
}
And in MyFragment.java:
package com.mycompany.ctest;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment extends Fragment {
#Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
return inflater.inflate(R.layout.fragment_layout,group,false);
}
}
EDIT: LogCat shows
12-13 10:10:09.709: E/ResourceType(1013): Style contains key with bad entry: 0x01010479
Although I think that must be irrelevant because if I add an Activity layout with a static fragment loaded instead of using the Fragment Manager, my view becomes visible. And I still get the Style contains key with bad entry message.

I finally added an activity layout with a blank frame layout with id.
activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Then used that id when adding fragments to the activity with .add or .replace instead of using android.R.id.content.
setContentView(R.layout.activity_layout);
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container,new MyFragment(), "frag")
.commit();
I still don't know why that works and why android.R.id.content was not working.

Try importing from support library instead of just "import android.app.Fragment;" ...hope this might help
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

Related

How to call TabAcitivity from Activity

D
I need you help.
I have a activity ( the login Screen ), she extends of Activity!
My APP, have 3 tab, she extends of TabActivity.
I need write this code with tabactivity(deprecated). ;D
My problem is: When I click button in the Login Screen(Activity) , I want to call my activity(TabAcitivty), but when i make this, show up only a screen white =\
My ScreenLogin
public class TelaLogin extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);
Button btnGoogle = (Button) findViewById(R.id.btn_google);
btnGoogle.setOnClickListener(new View.OnClickListener() {
#Override
**public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), TabBar.class);
startService(myIntent);
setContentView(R.layout.act_frag_tab_bar);
}**
});
}
}
My TabActivity
public class TabBar extends TabActivity implements OnTabChangeListener {
... The code is "Maceta"(Large)
}
R.layout.act_frag_tab_bar have my 3 Tabs.
package com.example.androidtablayout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AndroidActivity extends Activity {
// button to show progress dialog
Button btnCallTabActivity;
Context con;
public static final int progress_bar_type = 0;
// File url to download
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mains);
con=this;
// show progress bar button
btnCallTabActivity = (Button) findViewById(R.id.btnTabActivity);
// Image view to show image after downloading
/**
* Show Progress bar click event
* */
btnCallTabActivity.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(con, AndroidTabLayoutActivity.class);
startActivity(i);
}
});
}
/**
* Showing Dialog
* */
}
And your Tab Activity should be like this
package com.example.androidtablayout;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
// setting Title and Icon for the Tab
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
}
I edit my manifest.xml and added that lines:
<activity android:name="tcc.sigme.TabBar" >
</activity>
<activity android:name="tcc.sigme.Tab01" >
</activity>
<activity android:name="tcc.sigme.Tab02" >
</activity>
<activity android:name="tcc.sigme.Tab03" >
</activity>
you see in : https://github.com/EdilsonGalvao/SigMe/blob/master/SigME/AndroidManifest.xml
Thanks a lot.
this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tcc.sigme"
android:versionCode="1"
android:versionName="1.0" >
<!-- Permission Uses Google Maps -->
<uses-permission android:name="permission_name" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="19" />
<!-- Unable OpenGL for run Maps -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<!-- Google API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCizmQT2lm48beRhFFa78kjEqoamFke9iM" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="tcc.sigme.TelaLogin"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="tcc.sigme.TabBar" >
</activity>
<activity android:name="tcc.sigme.Tab01" >
</activity>
<activity android:name="tcc.sigme.Tab02" >
</activity>
<activity android:name="tcc.sigme.Tab03" >
</activity>
</application>
</manifest>

Cannot start another activity

I followed the full instructions on http://developer.android.com/training/basics/firstapp/creating-project.html multiple times and have been unable to start an activity. The app basically has a text field and send button. When I enter text in the text field and hit the send button, another activity should be started. However, with my current code, hitting the send button does nothing.
MySecondApp/src/MainActivity.java
package com.example.mysecondapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.mysecondapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
MySecondApp/src/DisplayMessageActivity.java
package com.example.mysecondapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
MySecondApp/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
android:onClick="sendMessage" />
</LinearLayout>
MySecondApp/res/layout/activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
MySecondApp/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Safe\'s First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
MySecondApp/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysecondapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.mysecondapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.mysecondapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.mysecondapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.mysecondapp.MainActivity" />
</activity>
</application>
</manifest>
Your problem is in the xml for your first activity. You have two terminating characters in the Button. Remove the /> after your android:text line.
Change you xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
android:onClick="sendMessage" />
to:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
And also, I think you should reference your button in the onCreate method.
You can also try to change your code to:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
public final static String EXTRA_MESSAGE = "com.example.mysecondapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.yourid);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main.this, YourSecondActivity.class);
EditText editText = (EditText) findViewById(R.id.yourEditText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And Change your xml from:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
android:onClick="sendMessage" />
To:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
android:onClick="sendMessage" />
See how the last line is black? That's because you ended the button tag twice with />
So take the closing tag away on the second last line: android:text="#string/button_send"
and your code would be:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
When locating the onClick property and select sendMessage [MainActivity] from the drop-down list, at .xml file: android:onClick="sendMessage (MainActivity)".
Delete "(MainActivity)":
So, at .xml file: android:onClick="sendMessage"
And run the app again!

Custom Buttons - Error in Java - widget.Button;

Followed example to the letter. When I try to use a custom button via a custom.xml, emulator crashes with a Java error - inflating class android.widget.Button.
import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MasterActivity extends Activity {
Button uccBtn, tipBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
uccBtn = (Button) findViewById(R.id.button1);
tipBtn = (Button) findViewById(R.id.button2);
uccBtn.setOnClickListener (uccApp);
tipBtn.setOnClickListener (tipApp);
custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:state_pressed="true" android:drawable="#drawable/btn_ecolab_selected"></item>
<item android:state_focused="true"
android:drawable="#drawable/btn_ecolab_highlight"></item>
<item android:drawable="#drawable/btn_ecolab"></item>
</Selector>
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background ="#ffffcc"
tools:context=".MasterActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="#string/button1" android:typeface="serif"
android:background="#drawable/custom"
/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/version"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="56dp"
android:text="#string/button10" android:typeface="serif"
android:background="#drawable/custom" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button10"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="#string/button2" android:typeface="serif"
android:background="#drawable/custom"
/>
</RelativeLayout>
If I reference just the button by name (btn_ecolab), it works fine, just no animation of the buttons when pressed or selected. I've tried it with a LinearLayout to make sure that the Relitavelayout was not the problem. The custom buttons are in my Res/drawable-hpdi folder. Also tried copies of buttons and custom.xml in the -mdpi, -xhdpi and -xxhdpi. Tried without #string assignments.
try this one.
uccBtn = (Button) findViewById(R.id.button1);
tipBtn = (Button) findViewById(R.id.button2);
uccBtn.setOnClickListener (new OnClickListener() {
#Override
public void onClick(View v) {
//your code is here
}
});
make one folder drawable in res folder put your custom.xml
I am using this way and work great.

How to work with tabhost,tabwidget and framelayout?

I am using this code
package com.example.tabs;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Sími");
spec1.setContent(R.id.Sími);
spec1.setIndicator("Sími");
TabSpec spec2=tabHost.newTabSpec("Sms");
spec2.setIndicator("Sms");
spec2.setContent(R.id.Sms);
TabSpec spec3=tabHost.newTabSpec("Net");
spec3.setIndicator("Net");
spec3.setContent(R.id.Net);
TabSpec spec4=tabHost.newTabSpec("Greina");
spec4.setIndicator("Greina");
spec4.setContent(R.id.Greina);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.addTab(spec3);
tabHost.addTab(spec4);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
with this xml file
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/Sími"
android:layout_width="fill_parent"
android:layout_height="340dp"
android:orientation="vertical"
android:paddingTop="60px" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Sms"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Net"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Greina"
android:orientation="vertical"
android:paddingTop="60px"
>
</LinearLayout>
</FrameLayout>
</TabHost>
I am new to android eclipse. I made 4 tabs(linearlayout) inside this tabview. But it is difficult to set the location on a button in one tab. How can i work with this 4 types of tabs like i am working with 4 xml files, is that possible ? How can i make the tabwidget smaller without the text not going away? , i hope this makes sense.

import com.google.ads.*; doesn't have AdManager

I'm really stuck here, trying to set a banner with ads-admob to my android app. I have done all the layout settings in the main.xml, also worked in the manifest file with the permissions, tried both XML and JAVA methods to show the ads and trying to make it work but I always get "could not get currentAdManager" from logcat in Eclipse. The application also crashes here, but works just fine without the admob settings. My admob SDK is GoogleAdMobAdsSdk-6.0.1.jar and I'm developing using phonegap .
I noticed that the command "import com.google.ads.*;" doesn't have the "AdManager" because when I insert individually the "import com.google.ads.AdManager;", I receive the error message "The import com.google.ads.AdManager cannot be resolved". My files:
My JAVA:
> package what.car.notes;
>
> import android.os.Bundle;
> import org.apache.cordova.*;
> import com.google.ads.AdView;
> import com.google.ads.AdManager; **- ERROR APEARS HERE**
> import com.google.ads.*;
>
> public class Cargeous21forActivity extends DroidGap {
> /** Called when the activity is first created. */
> #Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> super.loadUrl("file:///android_asset/www/index.html");
> setContentView(R.layout.main);
>
> dView adView = (AdView)this.findViewById(R.id.AdView);
> adView.loadAd(new AdRequest());
> }
> }
My LAYOUT file (main.xml):
> <?xml version="1.0" encoding="utf-8"?>
>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
> xmlns:myapp="http://schemas.android.com/apk/res/what.car.notes"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> android:orientation="vertical">
>
> <com.admob.android.ads.AdView
> android:id="#+id/AdView"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> myapp:backgroundColor="#000000"
> myapp:primaryTextColor="#FFFFFF"
> myapp:secondaryTextColor="#CCCCCC"
> />
>
> </LinearLayout>
My MANIFEST:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="what.car.notes"
android:versionCode="4"
android:versionName="1.3" >
<uses-sdk android:minSdkVersion="7" />
<uses-library
android:name="com.google.ads.AdManager" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:value="i've put my correct id here" android:name="ADMOB_PUBLISHER_ID" />
<activity android:name="com.admob.android.ads.AdMobActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboard|keyboardHidden"
android:value="SQLite-NDK" />
<!-- Track Market installs -->
<receiver android:name="com.admob.android.ads.analytics.InstallReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
And finally, the attrs.xml:
> <?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>
Does anyone have already been through such a thing?
Please read the getting started guide for the Google AdMob SDK on Android. The example from which you got that code is from an old Google ads SDK before the Google AdMob Ads SDK rewrite.
Issues I can see on first pass:
You're manifest's activity definition is incorrect. See this page for the correct AdActivity activity definition.
You don't need attrs.xml. The SDK includes these attributes now.
I don't think the publisher id can be a meta-vale. You must specify the publisher id in your AdView XML definition.
Your AdView XML definition needs to have an adSize.
The new Google AdMob SDK doesn't have a com.google.ads.AdManager class.
Thanks Eric! Your answer helped me a lot to find the right code. The final code looks like this:
JAVA:
import android.os.Bundle;
import org.apache.cordova.*;
import android.app.Activity;
import android.os.Handler;
import com.google.ads.*;
import android.widget.*;
import android.widget.LinearLayout;
public class myactActivity extends DroidGap {
private static final String MY_AD_UNIT_ID = "a14fd7e04e46295";
private AdView adView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
//
// Create the adView
adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
LinearLayout layout = super.root; // this is the only change from the sample
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<com.google.ads.GoogleAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxxxxxxxxxxxxxxxx"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
</LinearLayout>
EVerthing worked since then... BUT! YEs, there is a but... 2 days later, with the incredible amount of $0,00 and 150 ads solicitacion, admob/google canceled my account!! I had heard their method are weird and remove developers for no reason at all and, with $0,00 and 150 views they cut me off of their program. Good part: I found some other ad services better than admob, but the minus side is that I'll have to figure out the code all over again...
Thanks anyway Eric!