How to solve this problem? When I restart activity the array works anew and picture doesnt changes.I want to change the picture to follow from array when I restart activity. This button restarts activity
Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
dialog.dismiss();
finish();
startActivity(getIntent());
}
});
Related
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.
When I click on the rate design button it stops, I doubt is my manifest because I have check it already below are my codes. I have also enter the error log. Can someone help me instead of rating down for this question
Your forget to add
setContentView(R.layout.yourlayout);
before Button initialization in onCreate(....)
Correct:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
Button btnRating = (Button) findViewById(R.id.rd);
btnRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NearablesDemoActivity.this, MainActivityRating.class);
startActivity(intent);
}
});
}
I was working in app with 2 interface.
every thing was good until i tried to put a button that take you
to the #2 interface the app crash when i press the button in the emulator
Can someone help explain how to do that ?
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(mIntent);
}
});
so what is wrong?
I've added a button to my activity XML file and I can't get it to open my other activity. Can some please tell me step by step on how to do this?
A. Make sure your other activity is declared in manifest:
<activity
android:name="MyOtherActivity"
android:label="#string/app_name">
</activity>
All activities must be declared in manifest, even if they do not have an intent filter assigned to them.
B. In your MainActivity do something like this:
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
}
});
Using an OnClickListener
Inside your Activity instance's onCreate() method you need to first find your Button by it's id using findViewById() and then set an OnClickListener for your button and implement the onClick() method so that it starts your new Activity.
Button yourButton = (Button) findViewById(R.id.your_buttons_id);
yourButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
});
This is probably most developers preferred method. However, there is a common alternative.
Using onClick in XML
Alternatively you can use the android:onClick="yourMethodName" to declare the method name in your Activity which is called when you click your Button, and then declare your method like so;
public void yourMethodName(View v){
startActivity(new Intent(YourCurrentActivity.this, YourNewActivity.class));
}
Also, don't forget to declare your new Activity in your manifest.xml. I hope this helps.
References;
Starting Another Activity (Official API Guide)
If you declared your button in the xml file similar to this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next activity"
android:onClick="goToActivity2"
/>
then you can use it to change the activity by putting this at the java file:
public void goToActivity2 (View view){
Intent intent = new Intent (this, Main2Activity.class);
startActivity(intent);
}
Note that my second activity is called "Main2Activity"
Button T=(Button)findViewById(R.id.button_timer);
T.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),YOURACTIVITY.class);
startActivity(i);
}
});
Write code on xml file.
<Button android:width="wrap_content"
android:height="wrap_content"
android:id="#+id/button"
android:text="Click"/>
Write Code in your java file
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Secondclass.class));
/* if you want to finish the first activity then just call
finish(); */
}
});
use the following code to have a button, in android studio, open an already existing activity.
Button StartButton = (Button) findViewById(R.id.YOUR BUTTONS ID GOES HERE);
StartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, YOUR ACTIVITY'S ID GOES HERE.class));
}
});
I did the same that user9876226 answered.
The only differemce is, that I don't usually use the onClickListener. Instead I write following in the xml-file: android:onClick="open"
open is the function, that is bound to the button.
Then just create the function open() in your activity class. When you click on the button, this function will be called :)
Also, I think this way is more confortable than using the listener.
Apply the following steps:
insert new layout xml in folder layout
rename window2
add new button and add this line: android:onClick="window2"
mainactivity.java
public void openWindow2(View v) {
//call window2
setContentView(R.layout.window2);
}
}
Use following steps to add the new activity (Manifest file will be automatically update)
File > New > Activity > Empty Activity
In your MainActivity.java file add the following code inside protected void onCreate(Bundle savedInstanceState).
Make sure you call finish(); function at the end. So when you tap the Back button, it will not go back to the previous activity.
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
finish();
}
});