How can show the interstitial ads after 2 button clicks in this code java ?
"Lastes SDK Admob 20.2.0"
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"Button 1 Clicked");
if (mInterstitialAd != null) {
save_id(btn1.getId());
mInterstitialAd.show(main_activity.this);
}
else {
Log.d("TAG", "The interstitial ad wasn't ready yet");
Intent intent = new Intent(main_activity.this, getstarted.class);
startActivity(intent);
}
}
});
You can store a variable like clickCount and everytime its even value ie. modulo by 2 == 0 you could show the ad right?
int clickCount = 0;
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"Button 1 Clicked");
clickCount++;
if (mInterstitialAd != null && (clickCount % 2 == 0)) {
clickCount = 0;
save_id(btn1.getId());
mInterstitialAd.show(main_activity.this);
}
else {
Log.d("TAG", "The interstitial ad wasn't ready yet or the button was only clicked once");
Intent intent = new Intent(main_activity.this, getstarted.class);
startActivity(intent);
}
}
});
Related
I have made an alarm clock app however, I am trying to make a homepage where the user can see all the alarms they have set. I want to infinitely run a block of code that updates the homepage of my app, but the methods I have found have not worked (i.e using a timer loop, while, and for loop). Any suggestion would help me a lot, also any critique to the code would help to, Thanks everyone!
I have tried
Timers, Do-While, While, and For Loops
The While loop that is in there currently cause an error and the app does not even run.
All the code I need help with is in the While Loop.
I think this is the error
(I/zygote64: Rejecting re-init on previously-failed classjava.lang.Class: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarms);
final Button selectAlarm1 = (Button) findViewById(R.id.selectAlarm1);
final Button selectAlarm2 = (Button) findViewById(R.id.selectAlarm2);
final Button selectAlarm3 = (Button) findViewById(R.id.selectAlarm2);
selectAlarm1.setVisibility(View.INVISIBLE);
selectAlarm2.setVisibility(View.INVISIBLE);
selectAlarm3.setVisibility(View.INVISIBLE);
final TextView textView = (TextView) findViewById(R.id.textView);
final Button createAlarm = (Button) findViewById(R.id.createAlarm);
final Button createAlarm2 = (Button) findViewById(R.id.createAlarm2);
final Button createAlarm3 = (Button) findViewById(R.id.createAlarm3);
createAlarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openMainActivity();
createAlarm2.bringToFront();
}
});
createAlarm2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openMainActivity2();
createAlarm3.bringToFront();
}
});
createAlarm3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openMain3Activity();
}
});
selectAlarm1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openMainActivity();
}
});
selectAlarm2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openMainActivity2();
}
});
selectAlarm3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openMain3Activity();
}
});
while(true){
if (Global.intHour1 != 0 && Global.intMinute1 != 0) {
textView.setVisibility(View.GONE);
selectAlarm1.setVisibility(View.VISIBLE);
//fixing minute format
if (Global.intMinute1 <= 9) {
selectAlarm1.setText("Alarm 1 is set for " +
Global.intHour1 + ":0" + Global.intMinute1);
} else {
selectAlarm1.setText("Alarm 1 is set for " +
Global.intHour1 + ":" + Global.intMinute1);
}
} else if (Global.intHour1 == 0 && Global.intMinute1 == 0) {
textView.setVisibility(View.VISIBLE);
}
if (Global.intHour2 != 0 && Global.intMinute2 != 0) {
selectAlarm2.setVisibility(View.VISIBLE);
//fixing minute format
if (Global.intMinute2 <= 9) {
selectAlarm2.setText("Alarm 1 is set for " +
Global.intHour2 + ":0" + Global.intMinute2);
} else {
selectAlarm2.setText("Alarm 1 is set for " +
Global.intHour2 + ":" + Global.intMinute2);
}
} else {
}
if (Global.intHour3 != 0 && Global.intMinute3 != 0) {
selectAlarm3.setVisibility(View.VISIBLE);
//fixing minute format
if (Global.intMinute3 <= 9) {
selectAlarm3.setText("Alarm 1 is set for " +
Global.intHour3 + ":0" + Global.intMinute3);
} else {
selectAlarm3.setText("Alarm 1 is set for " +
Global.intHour3 + ":" + Global.intMinute3);
}
}
else {
}
if (selectAlarm1.getVisibility() == View.INVISIBLE &&
selectAlarm2.getVisibility() == View.INVISIBLE &&
selectAlarm3.getVisibility() == View.INVISIBLE) {
textView.setVisibility(View.VISIBLE);
}
else
{
}
}
}
public void openMainActivity () {
Intent openAlarm = new Intent(this, MainActivity.class);
startActivity(openAlarm);
}
public void openMainActivity2 () {
Intent openAlarm2 = new Intent(this, MainActivity2.class);
startActivity(openAlarm2);
}
public void openMain3Activity () {
Intent openAlarm3 = new Intent(this, Main3Activity.class);
startActivity(openAlarm3);
}
public void makeTextVisible () {
}
}
I want the code to recognize when the intHour1-3 and intMinute1-3 are changed and change the visibility state of a button so that when the user returns to the homepage they can see which alarms are set.
For some reason, the repeat and shuffle buttons are not doing anything.
The buttons previous and next work perfectly.
In my app I am communicating with my service through broadcasts.
mediaPlayer is a public static in my service class and I am importing it in other activity.
public static MediaPlayer mediaPlayer = new MediaPlayer(); (In Service.class)
Activity
This is the code for the buttons
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
updateProgressBar();
}
}
mBtnShuffle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isShuffle){
isShuffle = false;
Toast.makeText(getContext(), "Shuffle is off", Toast.LENGTH_SHORT ).show();
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}else{
isShuffle = true;
Toast.makeText(getContext(), "Shuffle is on", Toast.LENGTH_SHORT).show();
mBtnShuffle.setImageResource(R.drawable.ic_shuffle_on);
isRepeat = false;
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}
}
});
mBtnRepeat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isRepeat){
isRepeat = false;
Toast.makeText(getContext(), "Repeat is off", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}else{
isRepeat = true;
Toast.makeText(getContext(), "Repeat is on", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_repeat_on);
isShuffle = false;
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}
}
});
mBtnPlayPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mBtnPlayPause.setImageResource(R.drawable.ic_action_play);
} else {
mediaPlayer.start();
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
mBtnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextSong();
}
});
mBtnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevSong();
}
});
Code in onCompletionListener
#Override
public void onCompletion(MediaPlayer mp) {
if (isRepeat){
//Store current songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}else if(isShuffle){
Random random = new Random();
songIndex = random.nextInt((songList.size() - 1) + 1);
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
//Store random songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}else if (songIndex < songList.size()-1){
mediaPlayer.reset();
nextSong();
tvSongListSize.setText((songIndex + 1) + "/" + songList.size());
}else{
songIndex = 0;
tvSongListSize.setText((1) + "/" + songList.size());
//Store random songIndex in mSharedPreferences
StorageUtil storageUtil = new StorageUtil(getContext());
storageUtil.storeSongIndex(songIndex);
//Send media with BroadcastReceiver
Intent broadCastReceiverIntent = new Intent(Constants.ACTIONS.BROADCAST_PlAY_NEW_SONG);
if (getActivity() != null) {
getActivity().sendBroadcast(broadCastReceiverIntent);
}
}
}
Broadcast receiver
private BroadcastReceiver NewSongBroadCastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
songList = new StorageUtil(getApplicationContext()).getSongs();
songIndex = new StorageUtil(getApplicationContext()).loadSongIndex();
if (songIndex != -1 && songIndex < songList.size()){
activeSong = songList.get(songIndex);
}else{
stopSelf();
}
stopSong();
mediaPlayer.reset();
if (mMediaSessionManager == null) {
try {
initMediaSession();
initMediaPlayer();
} catch (RemoteException e) {
e.printStackTrace();
stopSelf();
}
}
initMediaPlayer();
updateMetaData();
NotificationBuilder(PlaybackStatus.PLAYING);
}
};
The buttons mBtnShuffle and mBtnRepeat do not trigger onCompletion. They are just 2 ordinary buttons that you use to set the flags isShuffle and isRepeat.
onCompletion is triggered when a song has been completed.
So if you want something to happen when the 2 buttons are tapped you should put some code in their listeners.
So I basically want to know how would I allow the user to proceed to the next page when they hit pay if either txtCode or StreetCode is filled, only one of these two needs to be filled in order to pass to the next page but how would i get that to work as currently they both must filled to pass to the next page.
btnPay = (Button)findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText)findViewById(R.id.txtReg);
txtCode = (EditText)findViewById(R.id.txtCode);
txtStreetName = (EditText)findViewById(R.id.txtStreetName);
dlCostTime = (Spinner)findViewById(R.id.dlCostTime);
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
Replace this code
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
with this
if( txtReg.getText().toString().trim().equals("")){
txtReg.setError("required!");
}else{
if((txtCode.getText().toString().trim().equals("") && !txtStreetName.getText().toString().trim().equals("")) ||
(!txtCode.getText().toString().trim().equals("") && txtStreetName.getText().toString().trim().equals(""))){
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}else{
//show message enter either code or street name not both
}
Replace this code
btnPay = (Button) findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText) findViewById(R.id.txtReg);
txtCode = (EditText) findViewById(R.id.txtCode);
txtStreetName = (EditText) findViewById(R.id.txtStreetName);
dlCostTime = (Spinner) findViewById(R.id.dlCostTime);
if (txtCode.getText().toString().trim().length() > 0 || txtStreetName.getText().toString().trim().length() > 0) {
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
} else if (txtStreetName.getText().toString().trim().length() == 0) {
txtStreetName.setError("required!");
} else if (txtReg.getText().toString().trim().length() == 0) {
txtReg.setError("required!");
} else if (txtCode.getText().toString().trim().length() == 0) {
txtCode.setError("required!");
}
}
}
I am a beginner and I started by creating a radio app that has many stations but I have one problem.
When I hit "Play" for the radio, it starts playing then I come back to the precedent activity (stations list) and when I come back again to the same radio that is playing and hit "Pause" it diffuses "Not Playing". (I made a toast so that when I hit "Pause" while the radio is not playing it diffuses "Not Playing") but the radio is still "Playing".
Here is the code for the 2 buttons (Play and Pause):
btnPlayMFM.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isOnline() == true) {
if (isPlaying() == false) {
Toast t = Toast.makeText(MosaiqueFM.this, "Loading \nPlease Wait", 10000);
t.show();
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(stream_url);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
} else if (isPlaying() == true) {
Toast t2 = Toast.makeText(MosaiqueFM.this, "Playing", 2000);
t2.show();
}
}
if (isOnline() == false){
Toast t = Toast.makeText(MosaiqueFM.this, "Internet Connection Required", 3000);
t.show();
}
}
});
btnPauseMFM.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPlaying() == true) {
mediaPlayer.stop();
} else if (isPlaying() == false){
Toast t = Toast.makeText(MosaiqueFM.this, "Not Playing", 2000);
t.show();
}
}
});
public boolean isPlaying() {
return(mediaPlayer != null && mediaPlayer.isPlaying());
}
public boolean isOnline() {
cm = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
If you have two activities - one more showing stations list and another for play/pause a particular radio station then apparently when you'll leave the second activity , your activity will get destroyed and mediaPlayer will become null and so , when you'll come back, isPlaying() will be false.
And that's why you are getting this Toast-
Toast t = Toast.makeText(MosaiqueFM.this, "Not Playing", 2000);
Suggestion - For these kind of apps, you don't want the radio to stop when the user leaves your app. So I suggest you use service for play purpose.
I defined a dialog the following way:
public void displayAvailableDevices(Vector<UserDevice> availableDevices) {
connector.setDevicesFound(true);
UserDevice[] devices = new UserDevice[availableDevices.size()];
for (int i = 0; i < availableDevices.size(); i++) {
devices[i] = availableDevices.elementAt(i);
}
Arrays.sort(devices);
Vector<UserDevice> sorted = new Vector<UserDevice>();
for (int j = 0; j < devices.length; j++) {
sorted.add(devices[j]);
}
allAvailableDevices = sorted;
connector.stopNeighbourDiscovery();
if (dialog != null) {
dialog.dismiss();
checkedUser = null;
}
dialog = new Dialog(this);
dialog.setContentView(R.layout.available_users);
dialog.setTitle("Available Devices");
RelativeLayout rootLayer = (RelativeLayout) dialog.findViewById(R.id.dialogRoot);
rootLayer.setBackgroundColor(Color.WHITE);
ListView userTable = (ListView) dialog.findViewById(R.id.userList);
adapter = new UserAdapter();
userTable.setAdapter(adapter);
userTable.setOnItemClickListener(new UserListener());
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
connect = (Button) dialog.findViewById(R.id.userList_connect);
connect.setMinWidth(dm.widthPixels / 2);
connect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (checkedUser != null) {
dialog.dismiss();
connector.connectToDevice(checkedUser);
}
}
});
Button start = (Button) dialog.findViewById(R.id.userList_start);
start.setMinWidth(dm.widthPixels / 2);
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
startProfileView();
}
});
Button refresh = (Button) dialog.findViewById(R.id.userList_refresh);
refresh.setMinWidth(dm.widthPixels / 2);
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
getGameBaseLayer().resetAvailableUserDevices();
connector.refresh();
}
});
Button cancel = (Button) dialog.findViewById(R.id.userList_cancel);
cancel.setMinWidth(dm.widthPixels / 2);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
dialog.show();
if (checkedUser == null) {
connect.setEnabled(false);
}
}
If this dialog is displayed and the user presses the back button, the dialog is dismissed. However I would like to do some further processing, but how can i detect if the user has pressed the back button?
I'm quite positive that this article can help a lot. As far as I understand, pressing 'BACK' is like cancelling the dialog (like pressing 'CANCEL'). They show how to make a dialog uncancelable and how to implement a listener to catch the 'cancel' events.