I got stuck showing interstitial ads after an interval. my code displays only once (after 60 seconds). I want to show the interstitial ads in every 60 seconds interval. I know this is not a good idea to implement ads this way but I need this.. my code is below:
package com.ronie.admobads;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.mInterstitialAd.show();
}
}, 60000);
}
});
}
}
#Abhishek, I tried your way but Interstitial Ads doesn't show. I can see only banner ads. Would you please check if my I am wrong anywhere inside the code?
Full code is below:
package com.ronie.admobads;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Banner Ad
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Prepare the Interstitial Ad
mInterstitialAd = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
#Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
mInterstitialAd.show();
createRequest();
}
}, 1,1, TimeUnit.MINUTES);
}
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
You can schedule your event in this way :
createRequest();
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
#Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
if(mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
}, 1, 1, TimeUnit.MINUTES);
And createRequest method
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
EDIT
My mistake, I apologize, thread become dead because of java.lang.IllegalStateException, Need to call on the main UI thread.
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
});
}
}, 1, 1, TimeUnit.MINUTES);
Related
so i trying to make a button that on ever click is change color in that order red yellow green and the
it repeat itself and it dosnt work for my do you got tips for my ??
enter code here
package com.example.colorbender;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for need to be not ending need to be infinite loop
for (int i = 0; i < 10; i++) {
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setBackgroundColor(Color.parseColor("red"));
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setBackgroundColor(Color.parseColor("YELLOW"));
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setBackgroundColor(Color.parseColor("green"));
}
});
}
});
}
});
}
}
}
so sould i try different way or sould i work on my loop becouse it the only thing that dosnt work in my code
sorry about my eng and have a great day :)
public class MainActivity extends AppCompatActivity {
protected Button btn;
protected int count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count = 0;
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
switch(count % 3) {
case 0:
btn.setBackgroundColor(Color.RED);
case 1:
` btn.setBackgroundColor(Color.YELLOW);
case 2:
btn.setBackgroundColor(Color.GREEN);
}
count ++;
}
}
}
}
I have an application that is getting price,title etc. data from website. I get data with using jsoup with html parsing. I use a different class for html parsing. I also added membership system from Firebase Authentication to my application. Each user have different product lists. Products are located with user id in database. I want app to get values from database table but it gets titles of tables. My problem is getting titles from database table.
You can see the problem at this photo.
You can see another class at this photo
How can i get values ?
package com.example.lkuygulama;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
public class UrunEklemeActivity extends AppCompatActivity {
FirebaseDatabase firebaseDatabase;
DatabaseReference reference;
private String userId;
private FirebaseAuth.AuthStateListener mAuthlistener;
public ArrayList<String> goster = new ArrayList<String>();
ListView view;
ArrayAdapter<String> arrayAdapter;
public Button aramam,bildirim,liste;
private FirebaseAuth mAuth;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_urun_ekleme);
Urun urun = new Urun();
bildirim=(Button) findViewById(R.id.bildirimButon);
liste = (Button) findViewById(R.id.urunListeButon);
view=(ListView)findViewById(R.id.listele);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,goster);
view.setAdapter(arrayAdapter);
mAuthlistener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = mAuth.getCurrentUser();
userId = user.getUid();
}
};
tanimla();
listele();
bildirim.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gonderBildirim();
}
});
}
private void tanimla() {
firebaseDatabase = FirebaseDatabase.getInstance();
reference = firebaseDatabase.getReference();
}
private void listele() {
reference.child("urunler").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
String valeu =dataSnapshot.getKey();
goster.add(valeu);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void gonderBildirim() {
Intent intent = new Intent(this, BildirimActivity.class);
startActivity(intent);
}
private void gonderAra() {
Intent intent = new Intent(this,AramaActivity.class);
startActivity(intent);
}
private void gonderListe() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
You should use getValue(String.class).
Like this :
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
String valeu =dataSnapshot.getValue(String.class);
goster.add(valeu);
arrayAdapter.notifyDataSetChanged();
}
I recommend addListenerForSingleValueEvent instead of addChildEventListener.
I try to create a book with pdf library i found this library , i created my book everything is ok , i just wanna add interstitial admob and show it every 30 seconds , i tried with handler runnable but it's still show just one time.
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
You can use ScheduledExecutorService, and schedule a periodic action.
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
// don't show Ad here
}
#Override
public void onAdClosed() {
createRequest(); //load request whenever ad closed by user
}
});
createRequest();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
else
createRequest();
}
});
}
}, 30, 30, TimeUnit.SECONDS);
And createRequest() method
public void createRequest(){
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
I am at the point of abandoning using codenameone to build an app that allows me to play video from storage or to capture and play. Its either it is playing but no video is showing (most common ) or it plays only once and thats it - the fast forward and rewind buttons not working. I don't know if this is a simulator problem/limitation or whether I just don't know what I am doing.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.ixzdore.ziem.renderer.widgets;
import com.codename1.capture.Capture;
import com.codename1.components.ImageViewer;
import com.codename1.components.MediaPlayer;
import com.codename1.io.FileSystemStorage;
import com.codename1.io.Log;
import static com.codename1.io.Log.e;
import com.codename1.media.Media;
import com.codename1.media.MediaManager;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.layouts.BorderLayout;
import com.ixzdore.ziem.models.DocumentElement;
import com.ixzdore.ziem.models.ReportDocument;
import java.io.IOException;
/**
*
* #author jamesagada
*/
public class NVideo extends Widget {
private final Label _imageLabel = new Label();
private MediaPlayer _mPlayer;
private Container imageVideoContainer;
public NVideo(DocumentElement element, ReportDocument doc, Boolean isEditing) {
super(element, doc, isEditing);
imageVideoContainer = new Container(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_SCALE)) {
protected Dimension calcPreferredSize() {
return new Dimension(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayWidth());
}
};
_mPlayer = new MediaPlayer();
setupWidgetFromProperty();
}
#Override
public void renderEdit() {
//super.initLayout();
super.setEnabled(true);
}
public void renderSummary() {
//super.initLayout();
super.setEnabled(false);
getIconContainer().remove();
revalidate();
}
#Override
public void renderView() {
//find the
//super.initLayout();
super.setEnabled(false);
getIconContainer().remove();
revalidate();
}
#Override
public void setupWidgetFromProperty() {
super.setupWidgetFromProperty();
String videoFile = documentElement.getVideoFileFromValue(documentElement.getPropertyValue());
if (videoFile != null) {
try {
Media video = MediaManager.createMedia(videoFile, true);
if (_mPlayer == null) {
_mPlayer = new MediaPlayer();
}
_mPlayer.setDataSource(videoFile);
_mPlayer.setAutoplay(true);
if (imageVideoContainer == null) {
imageVideoContainer = new Container(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_SCALE)) {
protected Dimension calcPreferredSize() {
return new Dimension(Display.getInstance().getDisplayWidth(), Display.getInstance().getDisplayWidth() );
}
};
}
getWidgetContainer().removeAll();
imageVideoContainer.add(BorderLayout.CENTER, _mPlayer);
imageVideoContainer.revalidate();
getWidgetContainer().add(imageVideoContainer);
getWidgetContainer().revalidate();
getWidgetContainer().repaint();
documentElement.setPropertyValue(videoFile);
} catch (Exception err) {
Log.e(err);
}
}
}
#Override
public void setupPropertyButtons() {
super.setupPropertyButtons();
//add a capture button and a browse button for gallery
//save captured picture and put the URL value in the propertyValue
final Widget _this = this;
_captureButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//open the capture button and take a video
String photoFile = Capture.captureVideo();
if (photoFile != null) {
try {
_mPlayer.setDataSource(photoFile);
_mPlayer.setAutoplay(false);
_mPlayer.revalidate();
_mPlayer.repaint();
imageVideoContainer.revalidate();
_this.revalidate();
documentElement.setPropertyValue(photoFile);
} catch (Exception err) {
Log.e(err);
}
}
}
});
_captureButton.setText("");
getIconContainer().add(_captureButton);
getIconContainer().revalidate();
getIconContainer().repaint();
getLabelContainer().repaint();
_findButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Display.getInstance().openGallery(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ev) {
if (ev != null && ev.getSource() != null) {
String file = (String) ev.getSource();
try {
_mPlayer.setDataSource(file);
_mPlayer.setAutoplay(false);
_mPlayer.revalidate();
_mPlayer.repaint();
imageVideoContainer.revalidate();
imageVideoContainer.repaint();
_this.revalidate();
documentElement.setPropertyValue(file);
} catch (Exception err) {
Log.e(err);
}
}
}
}, Display.GALLERY_VIDEO);
}
});
_findButton.setText("");
getIconContainer().add(_findButton);
getIconContainer().revalidate();
getIconContainer().repaint();
getLabelContainer().repaint();
this.revalidate();
}
public void render(boolean editing) {
if (isEditing) {
renderEdit();
}
if (!isEditing) {
renderSummary();
}
}
}
`
What am I doing wrong or show me working code sample to achieve what I am trying to achieve.
I have created a form using GUI builder and another form via code which loads the google map. There is a button in the form which was created by using GUI builder, I need to go to the map while clicking the button. How to do it ? please help.
Add an actionListener to your button and call the new form in the method generated in StateMachine.
Your form should be created in StateMachine.
For example:
protected void onPage2_ButtonAction(Component c, ActionEvent event) {
Form hi = new GmapForm("Native Maps Test");
hi.show();
}
Gmap Form Class:
public class GmapForm extends Form {
public GmapForm(String title) {
t.setTitle(title);
setLayout(new BorderLayout());
final MapContainer cnt = new MapContainer();
addComponent(BorderLayout.CENTER, cnt);
addCommand(new Command("Move Camera") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-33.867, 151.206));
}
});
addCommand(new Command("Add Marker") {
public void actionPerformed(ActionEvent ev) {
System.out.println("Marker");
try {
cnt.setCameraPosition(new Coord(41.889, -87.622));
cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
} catch(IOException err) {
// since the image is iin the jar this is unlikely
err.printStackTrace();
}
}
});
addCommand(new Command("Add Path") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-18.142, 178.431));
cnt.addPath(new Coord(-33.866, 151.195), // Sydney
new Coord(-18.142, 178.431), // Fiji
new Coord(21.291, -157.821), // Hawaii
new Coord(37.423, -122.091) // Mountain View
);
}
});
addCommand(new Command("Clear All") {
public void actionPerformed(ActionEvent ev) {
cnt.clearMapLayers();
}
});
revalidate();
}
Remove the above code from your Gmap class and Uncomment // new StateMachine("/theme");
// Map loading class
import com.codename1.io.Log;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import userclasses.StateMachine;
import com.codename1.maps.Coord;
import com.codename1.ui.Command;
import com.codename1.ui.Dialog;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import java.io.IOException;
public class Gmap {
private Form current;
public void init(Object context) {
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
try {
Resources theme = Resources.openLayered("/theme");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
} catch(IOException e){
e.printStackTrace();
}
}
public void start() {
if(current != null){
current.show();
return;
}
// new StateMachine("/theme");
Form hi = new Form("Native Maps Test");
hi.setLayout(new BorderLayout());
final MapContainer cnt = new MapContainer();
hi.addComponent(BorderLayout.CENTER, cnt);
hi.addCommand(new Command("Move Camera") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-33.867, 151.206));
}
});
hi.addCommand(new Command("Add Marker") {
public void actionPerformed(ActionEvent ev) {
System.out.println("Marker");
try {
cnt.setCameraPosition(new Coord(41.889, -87.622));
cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
} catch(IOException err) {
// since the image is iin the jar this is unlikely
err.printStackTrace();
}
}
});
hi.addCommand(new Command("Add Path") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-18.142, 178.431));
cnt.addPath(new Coord(-33.866, 151.195), // Sydney
new Coord(-18.142, 178.431), // Fiji
new Coord(21.291, -157.821), // Hawaii
new Coord(37.423, -122.091) // Mountain View
);
}
});
hi.addCommand(new Command("Clear All") {
public void actionPerformed(ActionEvent ev) {
cnt.clearMapLayers();
}
});
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
}
// action for the button which I have created by using GUI builder
protected void onPage2_ButtonAction(Component c, ActionEvent event) {
// Here I want to redirect to the map page
}