How to close activity without destroying it? - android

Ok so first, I have menu with 3 buttons leading to 3 different layouts.
What I want, I want to show interstitial ad when I close my application.
What happens, In order to save memory I had to add finish() on every button so it doesn't take all memory. And when I click on any button interstitial ad shows.
Whats the problem, I can't set my code to show interstitial only when I completely close app not when I switch activities
My code:
package puske.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.ads.*;
import com.google.ads.AdRequest.ErrorCode;
public class Menu extends Activity implements AdListener {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
interstitial = new InterstitialAd(this, "ID");
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
}
#Override
public void onReceiveAd(Ad ad) {
{
Button button1=(Button)findViewById(R.id.menu1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Menu.this, Rifles.class));
finish();
}
});
Button button2=(Button)findViewById(R.id.menu2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Menu.this, Pistols.class));
finish();
}
});
Button button4=(Button)findViewById(R.id.menu3);
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Menu.this, Snipers.class));
finish();
}
});
}
}
#Override
protected void onDestroy() {
interstitial.show();
super.onDestroy();
// explicitly release media player
unbindDrawables(findViewById(R.id.menuzor));
System.gc();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
try
{
((ViewGroup) view).removeAllViews();
}
catch(UnsupportedOperationException ignore)
{
}
}
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
}

Look at this picture
onPause or onStop();
Just try

Related

how to place ads in admob in app

how to use ad-mob in app and what are the steps how i post ads in ad-mob how can i test in my device before uploading to Google play store.i am new to ad-mob. please explain me with sample example of it by step-wise.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.interstitialAds = new InterstitialAd(this, "ca-app-pub-****");
this.interstitialAds.setAdListener(this);
Button loadButton = (Button) this.findViewById(R.id.loadButton);
loadButton.setOnClickListener(loadButtonOnClick);
this.textView = (TextView) this.findViewById(R.id.stateTextView);
}
private OnClickListener loadButtonOnClick = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Loading Intertitial Ads");
AdRequest adr = new AdRequest();
// add your test device here
adr.addTestDevice(AdRequest.TEST_EMULATOR);
interstitialAds.loadAd(adr);
}
};
#Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
String message = "Load Ads Failed: (" + error + ")";
textView.setText(message);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
/**
* Called when an Activity is created in front of the app (e.g. an
* interstitial is shown, or an ad is clicked and launches a new Activity).
*/
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
if (interstitialAds.isReady()) {
interstitialAds.show();
} else {
textView.setText("Interstitial ad was not ready to be shown.");
}
}
}
Get rid of your AdListener.
Call intersitial.loadAd in your constructor
Call interstitial.show at a natural break point in your app
Call interstitial.loadAd
Go to 3.
See https://developers.google.com/mobile-ads-sdk/docs/admob/android/interstitial

Get an error while i try to put Insterstitial Ads

i ask here because i can't put instertital ads in my app. I get like 20 errors when i put the code below the saved Instance.
I had other app and i never had problems, but now i just can't.
In the section of Toast (below ad code) i get this error:
Multiple markers at this line
- Syntax error on token "(", invalid VariableDeclaratorId
- Syntax error on tokens, delete these tokens
- Syntax error on token "makeText", Identifier expected after
this token
- Syntax error on token ")", delete this token
Below toast, on btnSwitch i get this errors:
Multiple markers at this line
- Return type for the method is missing
- Syntax error on token ".", ... expected
- Syntax error on token ")", { expected after this token
- Syntax error on token "btnSwitch", VariableDeclaratorId expected after
this token
- Syntax error, insert ";" to complete FieldDeclaration
package lol.flashlight;
Then here:
ImageView Facebook = (ImageView) findViewById(R.id.imageView3);
Facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
i get this errors:
Multiple markers at this line
- Syntax error, insert "}" to complete
MethodBody
- Syntax error, insert ")" to complete
Expression
- Syntax error, insert ";" to complete
Statement
- Syntax error, insert "}" to complete
ClassBody
...I get MORE errors
I tried to put it at the final but anyway i get these errors, what can i do?
I let my full code if u wanna check:
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements AdListener {
ImageView btnSwitch;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
MediaPlayer mp;
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the interstitial
interstitial = new InterstitialAd(this, "fgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfgfg");
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
}
#Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
#Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
Toast.makeText(MainActivity.this, "Touch amumu's body to play a sound!", Toast.LENGTH_SHORT).show();
// flash switch button
btnSwitch = (ImageView) findViewById(R.id.imageView1);
// First check if device is supporting flashlight or not
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
ImageView Facebook = (ImageView) findViewById(R.id.imageView3);
Facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mp = MediaPlayer.create(MainActivity.this, R.raw.draven);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
Intent facebookIntent = new Intent(Intent.ACTION_SEND);
facebookIntent.setType("text/plain");
facebookIntent.setPackage("com.facebook.katana");
facebookIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=lol.flashlight");
startActivity(facebookIntent);
}});
ImageView Twitter = (ImageView) findViewById(R.id.imageView4);
Twitter.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this, R.raw.draven);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
//al apretar click en el boton Ir a Web abre el browser con la pág: google.com
Intent browserIntent =
new Intent(Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/intent/tweet?text=https://play.google.com/store/apps/details?id=lol.flashlight%20-%20This%20app%20is%20amazing,%20i%20love%20it!"));
startActivity(browserIntent);
}});
//OnClick del View :D
ImageView Ez = (ImageView) findViewById(R.id.imageView2);
Ez.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(MainActivity.this, R.raw.amumu);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}});
//OnClick del View :D
// ImageView Mundo = (ImageView) findViewById(R.id.imageView6);
// Mundo.setOnClickListener(new OnClickListener(){
// public void onClick(View v) {
// TODO Auto-generated method stub
//
// mp = MediaPlayer.create(MainActivity.this, R.raw.mundo);
//mp.setOnCompletionListener(new OnCompletionListener() {
// #Override
// public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
// mp.release();
// }
// });
// mp.start();
//}});
//OnClick del View :D
// ImageView Orianna = (ImageView) findViewById(R.id.imageView7);
// Orianna.setOnClickListener(new OnClickListener(){
//public void onClick(View v) {
// TODO Auto-generated method stub
// mp = MediaPlayer.create(MainActivity.this, R.raw.orianna);
// mp.setOnCompletionListener(new OnCompletionListener() {
// #Override
// public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
// mp.release();
// }
// });
// mp.start();
//}});
// get the camera
getCamera();
// displaying button image
toggleButtonImage();
// Switch button click event to toggle flash on/off
btnSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
// Get the camera
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
// Turning On flash
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
// Playing sound
// will play button toggle sound on flash on / off
private void playSound(){
if(isFlashOn){
mp = MediaPlayer.create(MainActivity.this, R.raw.playbutton);
}else{
mp = MediaPlayer.create(MainActivity.this, R.raw.rayo);
}
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage(){
if(isFlashOn){
btnSwitch.setImageResource(R.drawable.on);
}else{
btnSwitch.setImageResource(R.drawable.off);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
// on pause turn off the flash
turnOffFlash();
}
#Override
protected void onRestart() {
super.onRestart();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onStart() {
super.onStart();
// on starting the app get the camera params
getCamera();
}
#Override
protected void onStop() {
super.onStop();
// on stop release the camera
if (camera != null) {
camera.release();
camera = null;
}
}
}
your imports here are wrong, because they import from legacy admob:
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;
You have to import from .gms Google Play Services Admob:
import com.google.android.gms.ads.*
Also note that all the code below Toast.makeText(MainActivity.this, "Touch amumu's body to play a sound!", Toast.LENGTH_SHORT).show(); in your example is outside of any method (!) ..this is causing the dozens of compile errors. ==> move that code into some method

how to call ad mob ads show in some time intervals or different Activity in android apps

I am use ads of ad mob for advertise in my application its always display on on start up of my application but I want display this ads on when user exit application and back to the main activity call so how it implement in my app
my code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial = new InterstitialAd(this, "a152eb628a493d8");
adRequest = new AdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
web=(WebView)findViewById(R.id.webview);
web.getSettings().setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient());
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://dcs-dof.gujarat.gov.in/live-info.htm");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,Menu.NONE,"About");
menu.add(0,2,Menu.NONE,"Feedback");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id == 1)
{
Toast.makeText(MainActivity2.this,"About",Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity2.this,about.class);
startActivity(i);
}
else {
Toast.makeText(MainActivity2.this,"Feedback",Toast.LENGTH_LONG).show();
Intent i2 =new Intent(MainActivity2.this,feedback.class);
startActivity(i2);
}
return super.onOptionsItemSelected(item);
}
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
//interstitial.loadAd(adRequest);
super.onResume();
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this,"Press Again to Exit", Toast.LENGTH_SHORT).show();
}
#Override
public void onDismissScreen(Ad ad) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onRestart();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onStart();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onDestroy();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onStop();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onPause();
}
You really don't want to attempt to display an ad to a user on exit from your Actvity, it is a poor user experience.
Even if you did orchestrate a way to get the ad and present prior to your Activity's destruction you don't know how long it will take to retrieve the ad so your Activity might hang for several seconds on exit. User's hate that kind of thing.
I strongly suggest you find another spot within your app in which to display ads.

My app is not working in emulator..please help me out

This is the code I would like you to go through.
Please spot my mistakes.
This is a simple code for a calculator app.
I am not sure but the mistake might be in button section of the code.
Would be really grateful if you helped me out in this..
Thanks in advance.
package com.iitg.sau_calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Cal extends Activity {
Button one, two, thr, fou, fiv, six, sev, eig, nin, zer, add, sub, mul, div, clr, equ;
TextView scr;
int ans=0,fans=0,ca=0,cs=0,cm=0,cd=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cal);
one=(Button)findViewById(R.id.one);
two=(Button)findViewById(R.id.two);
thr=(Button)findViewById(R.id.thr);
fou=(Button)findViewById(R.id.fou);
fiv=(Button)findViewById(R.id.fiv);
six=(Button)findViewById(R.id.six);
sev=(Button)findViewById(R.id.sev);
eig=(Button)findViewById(R.id.eig);
nin=(Button)findViewById(R.id.nin);
zer=(Button)findViewById(R.id.zer);
add=(Button)findViewById(R.id.add);
sub=(Button)findViewById(R.id.sub);
mul=(Button)findViewById(R.id.mul);
div=(Button)findViewById(R.id.div);
clr=(Button)findViewById(R.id.clr);
equ=(Button)findViewById(R.id.equ);
scr=(TextView)findViewById(R.id.Screen);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+1;
scr.setText(ans);
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+2;
scr.setText(ans);
}
});
thr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+3;
scr.setText(ans);
}
});
fou.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+4;
scr.setText(ans);
}
});
fiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+5;
scr.setText(ans);
}
});
six.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+6;
scr.setText(ans);
}
});
sev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+7;
scr.setText(ans);
}
});
eig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+8;
scr.setText(ans);
}
});
nin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10+9;
scr.setText(ans);
}
});
zer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=ans*10;
scr.setText(ans);
}
});
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
ca+=1;
scr.setText("+");
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cs+=1;
scr.setText("-");
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cm+=1;
scr.setText("*");
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
cd+=1;
scr.setText("/");
}
});
equ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Ans();
scr.setText(fans);
}
});
clr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ans=0;
fans=0;
ca=0;cm=0;cd=0;cs=0;
scr.setText("0");
}
});
}
public void Ans(){
if(ca>0)
{
fans=fans+ans;
ca=0;
}
else if(cs>0)
{
fans=fans-ans;
cs=0;
}
else if(cm>0)
{
fans=fans*ans;
cm=0;
}
else if(cd>0)
{
fans=fans/ans;
cd=0;
}
else
{
fans=ans;
ans=0;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cal, menu);
return true;
}
}
scr is textview and ans is a int. Your app probably crashes.
Replace this
scr.setText(ans);
By
scr.setText(String.ValueOf(ans));
public static String valueOf(int i)
Returns the string representation of the int argument.
Same for all
public final void setText (int resid)
resid is a resource which is an int value.
textView.setText(resid) looks for a resid with that value. If it's not found you will get ResourceNotFoundException.
You should use the below
public final void setText (CharSequence text)
takes a CharSequence as a param
http://developer.android.com/reference/android/widget/TextView.html#setText(int)
There might me other mistakes also (which may lead to crash). If its a crash its better you post the stacktrace.

how to load activity layout in android while the video is playing in background

how to load activity layout in android while the video is playing in background in android emulator???
the video demo you can see here
sample video
till now i have only animated the layout but i am unable to do like the one in video and when i am trying to play only video in emulator it gives error saying "can't play video"
actually i don't have an android device so i have to test my application in emulator.
any help is highly appreciated.
here is a code of animation :
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.VideoView;
public class CorporateProfileActivity extends Activity implements OnPreparedListener, OnCompletionListener{
public ImageView bbhome, bbmap, bbprojects, bbcontact, bbprofile;
private VideoView video;
private ImageView ivcp1, ivcp2, ivcp3, ivcp4, ivcp5, ivcp6;
Animation forcp1, forcp2, forcp3, forcp4, forcp5, forcp6, animation;
View view, view1, view2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
//below bar variables
bbhome = (ImageView) findViewById(R.id.bbhome);
bbmap = (ImageView) findViewById(R.id.bbmap);
bbprojects = (ImageView) findViewById(R.id.bbprojects);
bbcontact = (ImageView) findViewById(R.id.bbcontact);
bbprofile = (ImageView) findViewById(R.id.bbprofile);
animation = AnimationUtils.loadAnimation(this, R.anim.intro_scale);
//animation.setFillAfter(true);
video=(VideoView) findViewById(R.id.VideoView1);
//images in order from left to right
ivcp1 = (ImageView)findViewById(R.id.cp1);
ivcp2 = (ImageView)findViewById(R.id.cp2);
ivcp3 = (ImageView)findViewById(R.id.cp3);
ivcp4 = (ImageView)findViewById(R.id.cp4);
ivcp5 = (ImageView)findViewById(R.id.cp5);
ivcp6 = (ImageView)findViewById(R.id.cp6);
forcp1 = new TranslateAnimation(-1500, 0, 0, 0);
forcp1.setDuration(700);
forcp2 = new TranslateAnimation(-1200, 0, 0, 0);
forcp2.setDuration(800);
forcp2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp1.setVisibility(1);
ivcp1.startAnimation(forcp1);
}
});
forcp3 = new TranslateAnimation(-1000, 0, 0, 0);
forcp3.setDuration(800);
forcp3.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp2.setVisibility(1);
ivcp2.startAnimation(forcp2);
}
});
forcp4 = new TranslateAnimation(-500, 0, 0, 0);
forcp4.setDuration(800);
forcp4.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp3.setVisibility(1);
ivcp3.startAnimation(forcp3);
}
});
forcp5 = new TranslateAnimation(-500, 0, 0, 0);
forcp5.setDuration(800);
forcp5.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp4.setVisibility(1);
ivcp4.startAnimation(forcp4);
}
});
forcp6 = new TranslateAnimation(-500, 0, 0, 0);
forcp6.setDuration(800);
ivcp6.setVisibility(1);
ivcp6.startAnimation(forcp6);
forcp6.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
ivcp5.setVisibility(1);
ivcp5.startAnimation(forcp5);
}
});
//image click events
ivcp1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp1.startAnimation(animation);
openIntro(view1);
}
});
ivcp2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp2.startAnimation(animation);
openWhoWeAre(view);
}
});
ivcp3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp3.startAnimation(animation);
openThwWayWeWork(v);
}
});
ivcp4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp4.startAnimation(animation);
openMisionVision(v);
}
});
ivcp5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp5.startAnimation(animation);
openOurPrinciples(v);
}
});
ivcp6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ivcp6.startAnimation(animation);
openPromoter(v);
}
});
//onclick event for below bar menus
bbhome.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bbhome.setImageResource(R.drawable.home_active);
openMainActivity(v);
}
});
bbmap.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bbprojects.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bbcontact.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
bbprofile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
//open methods to open activity
public void openIntro(View view) {
// Do something in response to button
Intent intent = new Intent(this, IntroductionActivity.class);
startActivity(intent);
}
public void openWhoWeAre(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openThwWayWeWork(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openMisionVision(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openOurPrinciples(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
public void openPromoter(View view) {
// Do something in response to button
Intent intent = new Intent(this, WhoWeAreActivity.class);
startActivity(intent);
}
/*
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
*/
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
System.out.println("video is complete...");
//imageView.setVisibility(View.VISIBLE);
//imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
}
public void openMainActivity(View view) {
// Do something in response to button
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}

Categories

Resources