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.
Related
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.
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
So I am currently creating an app and one of the small things that have been bothering me is the fact that I have to click a button twice for it to work.
This is my code and I can't see anything wrong with it:
public void signUpButtonClickAction(View v){
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
}
xml code for my button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUps"
android:id="#+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="signUpButtonClickAction"/>
It is probably a small fix but even I can't spot this bug
Solution
Remove the line android:onClick="signUpButtonClickAction" and add
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
to the onCreate method of your activity or the onCreateView method of your fragment.
Alternative Solution
Alternatively, change the code to this
public void signUpButtonClickAction(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
Explanation
The line android:onClick="signUpButtonClickAction" in the xml is causing an internal call to signUpButtonClick.setOnClickListener(), so you don't have to set up an onClickListener in the signUpButtonClickAction again.
Initializing multiple buttons
private void initializeButtons() {
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
Button anotherButton = (Button) findViewById(R.id.anotherButton);
anotherButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG", "Clicked on another button!");
}
});
}
Now simply call initializeButtons() from the onCreate method of your activity.
The problem is that you are setting two times a onClick action. In your xml code you have just asign an onClick() to your button, you don't need to setOnClickListener() inside the signUpButtonClickAction(View v). You have two options:
Leave the xml file like it is and inside signUpButtonClickAction(View v) do :
public void signUpButtonClickAction(View v){
startActivity(new Intent(MainActivity.this, Signup.class));
}
OR
Remove the onClick of your xml file:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signUps"
android:id="#+id/signUpButton"
android:layout_marginBottom="38dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
And do this in your Activity:
Button yourButton = (Button) findViewById(R.id.signUpButton);
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
The cause of the problem is : onclick() and onClickListener are literally the same! And you are implementing both, the end result is you'll need to press the button twice to start the Activity!
See this question for more info
FIX:
The solution to your problem is :
1:
public void signUpButtonClickAction(View v)
{
startActivity(new Intent(MainActivity.this, Signup.class));
}
2:
Button signUpButtonClick = (Button) findViewById(R.id.signUpButton);
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
as mcwise said
android:onClick="signUpButtonClickAction"
and
signUpButtonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Signup.class));
}
});
does the same thing. so you have to go with one of them. Having the two is causing the problem
For whom it may concern:
I had the same issue but none of the solutions above solved it.
For some reason I cannot understand, I had in my button this line of code:
android:textIsSelectable="true"
Deleting this attribute from the button makes it work.
This obviously made the first click to select the text, and the second click triggered the onClick button.
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
I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.