Excuse my ignorance but I was trying to show an interstitial ad while user clicks on multiple button. I have four buttons and each lead to different activity. How can I show same ad (without creating extra ad units & variables) on multiple button click? Here is what I've done so far:
Button btn1, btn2, btn3, btn4;
interstitialAd.setAdListener(new AdListener()
{
#Override
public void onAdClosed() {
super.onAdClosed();
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
}
}
);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
}
}});
Use onAdClosed method in Interstitial listener to loadAd again if it's already shown and put show funtion on click listeners of the button. I thought four buttons are in the same activity (correct me if I'm wrong)
In your button's onClickListener you used if else statements like show Ads or Go to this activity I mean if unable to show ads then go to this activity, well if the ad was shown to the user then the user will stay at the same activity.
Try this :
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
//***This is simple, when the button was clicked Ad will show, also the Activity1 will open.
} else {
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
}
}});
or You should add Interstitial Listener inside the Click listener method of the button, so you can open another activity when Ad was closed.
You can create a instance of OnClickListener like
OnClickListener onClickListener=new OnClickListener() {
#Override
public void onClick(View view) {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Intent intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
}
}
and assign this all button clicks like
btn1.setOnClickListener(onClickListener);
btn2.setOnClickListener(onClickListener);
onButttonClick show ad like Ratish said
OnClickListener onClickListener=new OnClickListener() {
#Override
public void onClick(View view) {
position = 1(for first activity)
if (interstitialAd.isLoaded()) {
interstitialAd.show();
} else{
nextActivity();
}
}
then on Ad closed do go to next activity do this :
make a String array and store all package names in it and declare an int like :
String optionSelected[] = {"com.sb.android.acg.upgrade.activity.firstActivity", "com.sb.android.acg.upgrade.activity.SecondActivity", "com.sb.android.acg.upgrade.activity.ThirdActivity", "com.sb.android.acg.upgrade.activity.FourthActivity", "com.sb.android.acg.upgrade.activity.fifthActivity", "com.sb.android.acg.upgrade.activity.SixthActivity"};
int position
and in onAdClosed:
#Override
public void onAdClosed() {
super.onAdClosed();
nextActivity();
}
and in nextActivity
public void nextActivity(){
Intent intent;
Class<?> aclass = Class.forName(optionSelected[position]);
intent = new Intent(MainActivity.this, aclass);
startActivity(intent);
}
hope you understood how to call next activity. If any doubt tell me in comment
Related
for example i have made a Nodemcu server and there are 3 links on the main webpage(192.168.18.100).
the thing i want to try is that when i click an android button this link get accessed without opening any new activity i.e i want to stay on the main activity.
this is my code so far that works but not according to what i want it.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button on = (Button) findViewById(R.id.button);
Button off =(Button) findViewById(R.id.button2);
Button pink = (Button) findViewById(R.id.button3);
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16236607"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_SEND, uri);
startActivity(intent);
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16203967"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
pink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16214167"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
You can try to implement a WebView on your MainActivity. When you click on any of your button, load the WebView with your desired url.
You can find the way to implement WebView in this link
So I want this button and TextView and LinearLayout to be invisible until a button is pressed on another activity, but when I return to MainActivity(the activity with the button and textview and linearlayout) the stuff is still invisible.
Thank you in advance.
MainActivity.java
textView.findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);
ToggleButton button=findViewById(R.id.button);
button.setVisibility(View.INVISIBLE);
LinearLayout alarmLayout=findViewById(R.id.alarmLayout);
alarmLayout.setVisibility(View.INVISIBLE);
String value=getIntent().getStringExtra("buttonStatus");
if(value.equals("Visible")){
button.setVisibility(View.VISIBLE);
alarmLayout.setVisibility(View.VISIBLE);
textView.setVisibility(View.VISIBLE);
AlarmFrequency.java
Button create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(alarmFrequency.this,
MainActivity.class);
intent.putExtra("buttonStatus","Visible");
I made little changes to your code to run it on my pc.
MainActivity.java
textView=findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);
//make textView visible
Intent i= getIntent();
String value = i.getStringExtra("buttonStatus");
if (i!=null && value!=null && value.equals("Visible")) {
textView.setVisibility(View.VISIBLE);
}
//button to go to second activity
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
SecondActivity.java
Button create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("buttonStatus", "Visible");
startActivity(intent);//launch main activity again
}
});
This makes the textView visible in MainActivity.
But if you press back button to go back to MainActivity you will see no change.
When your application starts, the application stack has only MainActivity in it. Lets call it MainActivity1.
Then, you press a button to go to SecondActivity. Your application stack contents are now :- MainActivity1 / SecondActivity
Then when you launch MainActivity again from SecondActivity MainActivity2 get in the stack.
Stack becomes MainActivity1 / SecondActivity / MainActivity2.
Changes are visible in MainActivity2, not in MainActivity1. If you use back button to go back to MainActivity1, textView will still be invisble.
Also note that, MainActivity1 was launched from another intent, and MainActivity2 was launched from different intent in SecondActivity.
Edit:-
If you want changes in your MainActivity1, you need to use startActivity for result. Following code changes will be required:-
MainActivity.java
textView=findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i,100);
}
});
//outside onCreate
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null)
{
String value = data.getStringExtra("buttonStatus");
if(value!=null && value.equals("Visible")) {
Log.d("Debug", "i am here.");
textView.setVisibility(View.VISIBLE);
}
}
}
SecondActivity.java
Button create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("buttonStatus", "Visible");
setResult(RESULT_OK, intent);
finish();
}
});
Read more about this changes here how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android
Your if(value.equals("Visible")) is in onCreate? It should be under onResume otherwise it will only execute once when onCreate is called.
So I want to have my Floating Action Button open the activity "add", but I am lost on startActivity (line 9). Everything I do just says 'expression expected'. Please Help!
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Do you want to open the activity?", Snackbar.LENGTH_LONG)
.setAction("YES", new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(THIS IS WHERE I AM LOST);
}
Inside of onClick(View view) method use below code:
#Override
public void onClick(View view) {
Intent intent = new Intent(CurrentActivity.this, DesiredActivity.class);
//Add extra if you want
intent.putExtra(Key, value);
startActivity(intent);}
Intent intent=new Intent(yourCurrentActivity.this,activityWhereYouWantToGo.class);
startActivity(intent);
where activityWhereYouWantToGo is your next activity name and yourCurrentActivity is your current activity name or you can simply pass activity context. just pest this code in on
public void onClick(View v) { }
You should write the desired activity's intent in the startActivity();
for example suppose that user clicks on Yes and as a result you want to run the DesiredActivity.class. So you should have:
Intent desiredActivityIntent = new Intent(CurrentActivity.this, DesiredActivity.class);
startActivity(myActivity);
So when user clicks Yes , that activity will run.
I have an activity which starts when my app is first launched(Only once).The activity lets the users select the topics.Then when they press done, I finish() the activity.This leads the user to the MainActivity.But when the user press the back button it goes back to the activity which was first launched(the activity which I closed using finish()).But what i want is that the app should close when the user presses the back button from the MainActivity(always).I overrided onBackPressed in both the classes.
My onBackPressed method in both the classes looks like this:
#Override
public void onBackPressed() {
finish();
}
MainActivity(Relevant part of code):
public class MainActivity extends AppCompatActivity
{
TopicAdapter adapter;
private AdView mAdView ;
//-------GLOBAL VARIABLES-------------------
AdRequest adRequest;
ArrayList<Sections> gameList = new ArrayList<>();
String json;
//-------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPreferences =
getSharedPreferences("ShaPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
boolean firstTime=sharedPreferences.getBoolean("first", true);
//Launch the topics selection activity for the first time
if(firstTime)
{
editor.putBoolean("first",false);
editor.commit();
Intent intent = new Intent(MainActivity.this, channels_activity.class);
intent.putExtra("isFirst",true);
startActivity(intent);
}
}
#Override
public void onBackPressed()
{
finish();
}
}
You just need to call finish()
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
And Set android:noHistory="true" on the activity in your manifest
See here
I've a simple problem.I'm trying to switch layout between Main Menu and About pages.In Main Menu, there is no problem when i click the "about" button.But in "about" layout, when i click "return to menu" button it just doesn't work.and the code of that layout is in about.java, which also extends Activity.Please have a look.
in MainActivity.java:
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), About.class);
setContentView(R.layout.about);
}
});
works just fine.But in About.java:
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
nothing happens.I tried every combination of inside onClick() but just doesn't work.What are your ideas?Thanks and have a nice day.
In Main Activity,java, it's not starting any activity, it's basically just changing the view. It seems to be working but actually it's not.
You should declare the intent and then call the start activity method. The other activity should have a method onCreate where you can set the content view (using the method setContentView).
It should be something like this:
MainActivity.java
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), About.class);
startActivity(intent);
}
});
About.java
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
For more information, check this http://developer.android.com/training/basics/firstapp/starting-activity.html
Try to do the same like in your MainActivity in your AboutActivity:
button1_.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
setContentView(R.layout.activity_main); //should be without this line if you set the layout in your onCreate method in the MainActivity (respectively AboutActivity)
startActivity(intent);
}
});
If it works once, should work twice as well