Is it allowed to manually hide/unhide the same Admob Ad after a certain time and does the ad need to be displayed for a particular time to count as an impression?
What i do is create a random int and when the int is value 2 i display the ad for 15s and then i set the visibility to invisible.
My code:
int randomIndex;
Random random = new Random();
randomIndex = random.nextInt(5);
Log.d(TAG,"randomIndex: "+randomIndex);
if (randomIndex == 2) {
if (mFrameLayoutBigAd.getVisibility() == View.INVISIBLE) {
mFrameLayoutBigAd.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mFrameLayoutBigAd.setVisibility(View.INVISIBLE);
}
}, 15000);
}
}else{
mFrameLayoutBigAd.setVisibility(View.INVISIBLE);
}
EDIT
if the ad is set to be visible again i just load the old adrequest again, so i'm not making a new ad request.
Yes, you can hide and show an ad, but there is a risk that the user will accidentally click when the ad reappears, which can be identified as click-inducing, which violates the AdMob guidelines.
Hope this helps.
Related
I am trying to add an animation on my editText hint.I saw it on one of the websites. There is a simple search EditText in the website, but its hint types automatically . What i mean, when user loads website, hintext appears letter by letter as we type on keyboard and it never stops. Is it possible to make such an animation in Android too?
final Handler handler = new Handler();
int count = 0;
String hint = "hint"
final Runnable runnable = new Runnable() {
public void run() {
if (count < hint.length){
editText.setHint(hint.subString(0,count));
count++;
handler.postDelayed(this, 5000);
}
}
};
handler.post(runnable);
This may or may not work as is.
I want my Android application to display ad – InterstitialAd. I want it appears after the welcome screen. It means that I have WelcomeActicity that appears on app start, and recently after this – the ad is displayed.
I create InterstitialAd and start loading ad in application’s OnCreate() method. If application was closed for the long time, ad loads for more then 13 seconds. Of course this is inappropriate – user should not wait for so long. I made this tests building release apk that was directly installed on my phone Xiaomi Redmi 4X.
So the question is – should it be done in some other way? Or ad is always loaded for the long time and I just need to move it forward in user’s workflow?
In app's OnCreate() I start loading
public class MyApp extends Application {
private FullScreenAd mFullScreenAd;
#Override
public void onCreate() {
super.onCreate();
mFullScreenAd = new FullScreenAd(this);
mFullScreenAd.loadAd();
}
}
Implementation is the following:
public class FullScreenAd{
private static final String APP_AD_ID = "ca-app-pub-3940256099942544~3347511713";
private static final String SCREEN_AD_ID = "ca-app-pub-3940256099942544/1033173712";
private String mAdAppId;
private String mAdScreenId;
private InterstitialAd mInterstitialAd;
private long mLoadStart;
public FullScreenAd(Context context) {
super();
DebugLogger.d();
mContext = context;
initAdIDs(null, null);
}
private void initAdIDs(String adAppId, String adScreenId) {
DebugLogger.d();
mAdAppId = (adAppId != null) ? adAppId : APP_AD_ID;
mAdScreenId = (adScreenId != null) ? adScreenId : SCREEN_AD_ID;
mInterstitialAd = new InterstitialAd(mContext);
mInterstitialAd.setAdUnitId(mAdScreenId);
setInterstitialAdListener();
}
public void loadAd() {
// Initialize the Mobile Ads SDK.
MobileAds.initialize(mContext, mAdAppId);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// Start loading the ad in the background.
mAdIsLoaded.set(false);
mInterstitialAd.loadAd(adRequest);
mLoadStart = System.currentTimeMillis();
}
private void setInterstitialAdListener() {
DebugLogger.d();
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
DebugLogger.d();
DebugLogger.d(String.format("Ad loaded in %d ms", System.currentTimeMillis() - mLoadStart));
mAdIsLoaded.set(true);
}
});
}
public void showAd(OnAdClosedAction action) {
DebugLogger.d();
mInterstitialAd.show();
}
}
Loading an AddMob interstitial ad takes some time. Usually it's as quick as a few seconds, but can go up to 20-30 seconds if the connection is poor, or the ad contains animation/video. It might even not load at all!
A good practice is to load and keep in memory the ad upon app launch, and show it later at some point - make it all as seamless as possible. Make sure that ad loading/displaying is not interfering with user experience - don't make the user wait for your ad loading.
Yes, loading interstitial can take some time, it is advisable to load ad in advance:
mInterstitialAd.loadAd(adRequest);
and show it when you want
mInterstitialAd.show();
but as you are saying you want you start your app with an interstitial ad on the launch itself. It may result in google policy violation as it says you can't show ad on app load or exit.
I want interstitial ads to show randomly when I click a button. For example, I want to choose between 1-20. That the ad pops up randomly and not at every click.
float hitPercent = 0.3f; //30% of the time it will show ad
final Random generator = new Random();
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (generator.nextFloat() <= hitPercent) {
ad.show();
}
}
}
(Android) On a music player, you update the seekbar as expected with this:
PRECISION_SEEKBAR = 100000;
((SeekBar) findViewById(R.id.seekBar2)).setMax(PRECISION_SEEKBAR);
timerSeekBarUpdate.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
#Override
public void run() {
if (control == null || player == null) {
cancel();
return;
}
seekBar.setProgress((int) (player.getCurrentPosition() * PRECISION_SEEKBAR / player.getDuration()));
...
However, if the focus is on the seek bar, talkback steadily and nonstop gives feedback for the progress. Like "seek control 25%", "seek control 25%", "seek control 25%", "seek control 26%", "seek control 26%", "seek control 27%"
I'm missing sth but couldnot solve the problem. I have set the contentDescription to other than #null. But then it reads the content description this time without stopping.
On Spotify client, I checked, it reads the progress as "xx percent" just once. Despite saving the focus on the seekbar.
When I edit the precision for 1 or 100, then you lose the precision on the seekbar. It looks like there are a few parts in the song. You either play one or another by swiping on the seekbar.
Has anybody experienced sth like this? I couldn't find anything on google docs, stack network or somewhere else.
You can just override sendAccessibilityEvent() so it ignores description updates:
#Override
public void sendAccessibilityEvent(int eventType) {
if (eventType != AccessibilityEvent.CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION) {
super.sendAccessibilityEvent(eventType);
}
}
As Altoyyr mentioned, this has the side effect of ignore ALL description updates, including scrolling with volume buttons. So you'll need add back sending the event for volume press actions:
#Override
public boolean performAccessibilityAction(int action, Bundle arguments) {
switch (action) {
case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD:
case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
super.sendAccessibilityEvent(AccessibilityEvent.CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION);
}
}
return super.performAccessibilityAction(action, arguments);
}
I had the problem and found that SeekBar reads the percentage on every update.
It helped, that I update the SeekBar only when the percentage changes but still keep a high precision (in my case in ms).
#Override
public void updateSeekBar(final int currentPosInMillis, final int durationInMillis) {
long progressPercent = calculatePercent(currentPosInMillis, durationInMillis);
if (progressPercent != previousProgressPercent) {
seekBar.setMax(durationInMillis);
seekBar.setProgress(currentPosInMillis);
}
previousProgressPercent = progressPercent;
}
private int calculatePercent(int currentPosInMillis, int durationInMillis) {
if(durationInMillis == 0) {
return 0;
}
return (int) (((float)currentPosInMillis / durationInMillis) * 100);
}
previousProgressPercent is initialized to -1.
Please note that this solution is not the same as Spotify does it.
Spotify overrides the message announced by the system when the SeekBar gets selected.
This has following 2 effects:
Updates can be made as often as you want without the percentage beeing repeated
When the percentage changes while the SeekBar is selected then nothing gets announced
Point 2 might me a drawback depending on what you want to achieve.
I have been unable to find a tutorial helping with multi-selects using cursors. As of right now my logic is working the way I want but the check boxes will not update properly. What am I overlooking?
return new AlertDialog.Builder(this).setTitle("Items")
.setMultiChoiceItems(cur, CHECK, EDATE, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int position, boolean checked)
{
DBM.open();
AlertDialog AD = (AlertDialog) dialog;
ListView list = AD.getListView();
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
itemCur = (Cursor) list.getItemAtPosition(position);
if (checked)
{
//update query
DBM.setChecked(checkCur.getInt(checkCur.getColumnIndex(ID)), itemId, userId, 1);
list.setItemChecked(1, true);
} else
{
DBM.setChecked(checkCur.getInt(checkCur.getColumnIndex(ID)), itemId, userId, 0);
list.setItemChecked(1, false);
}
DBM.close();
}
}).setPositiveButton("OK", new DialogButtonClickHandler()).create();
Dialogs on android can't be modified. If you look at the source code you will see that dialogbuilder delegates all the presentation work to some components and you don't have access to them after creation. Thus changing the state of the components you use for building the dialog won't update the dialog components afterwards.
You can see this mechanism here and here : you don't have access to the access controller after onCreate has been called on the alert controller.
The best if you want to achieve this is to rebuild a new activity and give it a dialog theme.
You can just use the setCursor() method for AlertDialog. Its pretty simple so you probably wouldn't need a tutorial.
A relevant SO questions is here and the docs for it are here
So after digging into the issue a bit and going through a couple different iterations I finally found a solution that I am fairly happy with. With school and work pushing hard I have had little time outside to work on extra projects and I have been sitting with this solution for while now but unable to get it posted.
The final piece to my puzzle was finding the changeCursor function, this fixed the issue of the old data that no longer matched the DB to load. My current hurdle is the time it takes to check a box, there is an obvious lag from clicked to updated. I have found that mutliple records update when one is clicked. I have not been able to find a valid reason for these extra updates.
Below is the code I currently have implemented to have the multi-select working. This just the dialog code, for a working demo I will be posting a project on GitHub for a working prototype of it all in action. (Now made public, Multiselect Dialog)
I am a fairly new Android developer, majority of my Android knowledge has been self taught and learned through the knowledge of online resources. I was working on a school project and wanted to implement a multiselect in a dialog that would update the main activity with the selected choices. Please lend any advice you can on how to improve this.
Pros:
- Populates check boxes properly on load.
- Updates database when check is clicked.
- Keeps display updated after data change.
Cons:
- Must click check box to update value.
- Unable to undo changes made while in dialog. The values save onClick, I have not been able to think of a way to temporarily store the new values until confirmed by the user.
- A single click updates multiple records, also sometimes when choices scroll off the screen values update
#Override
protected Dialog onCreateDialog(int id)
{
switch (id) {
case 0:
LayoutInflater factory = LayoutInflater.from(this);
// Setup of the view for the dialog
final View bindListDialog = factory.inflate(R.layout.multi_list_layout, null);
multiListView = (ListView) bindListDialog.findViewById(R.id.multiList);
// Because I do not know how to properly handle an undo in this situation
// I make the dialog only close if the button is pressed and confirms the changes
return new AlertDialog.Builder(MultiSelectDemoActivity.this).setTitle(R.string.multiSelectTitle)
.setCancelable(false).setView(bindListDialog)
.setPositiveButton(R.string.btnClose, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
updateItemList(); // In my implementation there is a list view
// that shows what has been selected.
}
}).create();
default:
return null;
}
}
private static final boolean ONCREATE = true;
private static final boolean ONUPDATE = false;
private void setupMultiList(Boolean newList)
{
demoDBM.open();
multiCur = demoDBM.getList(userId); // Gets all items tied to the user.
startManagingCursor(multiCur);
// Uses the cursor to populate a List item with an invisible ID column,
// a name column, and the checkbox
demoDBM.close();
if (newList)
{
// Creates a new adapter to populate the list view on the dialog
multiAdapter = new SimpleCursorAdapter(this, R.layout.check_list_item, multiCur, new String[] { DemoDBM.ID,
DemoDBM.NAME, DemoDBM.SEL }, new int[] { R.id.itemId, R.id.itemName, R.id.itemCheck });
multiAdapter.setViewBinder(new MyViewBinder());
multiListView.setAdapter(multiAdapter);
} else
{
// updates the previously made adapter with the new cursor, without changing position
multiAdapter.changeCursor(multiCur);
}
}
#Override
protected void onPrepareDialog(final int id, final Dialog dialog, Bundle args)
{
setupMultiList(ONCREATE);
}
public class MyViewBinder implements ViewBinder
{
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex)
{
int checkId = cursor.getColumnIndex(DemoDBM.SEL);
if (columnIndex == checkId)
{
CheckBox cb = (CheckBox) view;
// Sets checkbox to the value in the cursor
boolean bChecked = (cursor.getInt(checkId) != 0);
cb.setChecked(bChecked); // Switches the visual checkbox.
cb.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
return true;
}
return false;
}
}
public class MyOnCheckedChangeListener implements OnCheckedChangeListener
{
#Override
public void onCheckedChanged(CompoundButton checkBox, boolean newVal)
{
View item = (View) checkBox.getParent(); // Gets the plain_list_item(Parent) of the Check Box
// Gets the DB _id value of the row clicked and updates the Database appropriately.
int itemId = Integer.valueOf(((TextView) item.findViewById(R.id.itemId)).getText().toString());
demoDBM.open();
demoDBM.setChecked(itemId, userId, newVal);
demoDBM.close();
setupMultiList(ONUPDATE);
}
}