I am using the following code to show “Hello” message with ok button when the user clicks a button. In some applications this is working fine. ie while clicking the ok button, the activity is dismissed. But in one application, this is not getting dismissed after clicking the ok button. What to do? Please help.
public class MyClass extends Activity {
private TextView labelTxt;
private Button okBtn;
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.message);
labelTxt = (TextView) findViewById(R.id.txt);
labelTxt.setText("Hello");
okBtn = (Button) findViewById(R.id.okBtn);
okBtn.setOnClickListener(okBtnClickListener);
}
private final OnClickListener okBtnClickListener = new OnClickListener() {
public void onClick(View v) {
finish();
}
};
}
I have tested your code and modified little. Please check below
package test.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btnOK);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
Try like this....
okBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();// Closing Activity
}
});
In your layout make sure your Button is named:
android:id="#+id/okBtn"
.....
Did you import:
import android.view.View.OnClickListener;
Please look at this tutorial, it should help:
http://martin.cubeactive.com/android-onclicklitener-tutorial/
Related
I want this code to run just once after installing the app.
Page 1 has a button and after clicking it, the user will be directed to page 2.
Page 2 is the disclaimer page and after checkboxes are marked and agree button is clicked, the user is directed to the home page.
When the user clicks for the second time, this disclaimer page should not open again.
I tried to modify my code for it, but I couldn't because of the button as I'm not sure how to use it.
Page 1
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button btn_Shap;
public void first(){
btn_Shap = (Button)findViewById(R.id.btn_Shap);
btn_Shap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent btn = new Intent(MainActivity.this, disclaimer.class);
startActivity(btn);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first();
}
}
Page 2
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class disclaimer extends AppCompatActivity {
public Button button2;
public Button button1;
public void second(){
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent btn = new Intent(disclaimer.this, home.class);
startActivity(btn);
}
});
}
public void first(){
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent btn = new Intent(disclaimer.this, MainActivity.class);
startActivity(btn);
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disclaimer);
second();
first();
}
}
Inside the desclaimer page store a boolean value using shared preference and check it before open the desclaimer page. like this
Inside the Desclaimer page
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
Before open the desclaimer page
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if ( firstRun )
{
//Open desclaimer
}else{
//Do not open desclaimer
}
The Home Activity where the login button is
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//findview for the login button
findViewById(R.id.button_login).setOnClickListener(new login());
}
onClickListener for the login button to be clicked and take the user to the login page.
class login implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Home.this, login.class);
startActivity(intent);
}
}
}
The login screen:
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Login extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
Try This and please Confirm Login Activity is declared in the manifest
package com.example.james.assignment1_18094969;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
public class Home extends AppCompatActivity implements View.OnClickListener {
Button Login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Login=findViewById(R.id.button_login);
login.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
}
this will be more correct approach to put click Listener's instead of defining inner class for click Listener. Or you can use functions instead.
public class Home extends AppCompatActivity {
Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
loginButton = (Button) findViewById(R.id.button_login);
loginButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
});
}
Acitivity names should be used correctly.
Do this!
findViewById(R.id.button_login).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Home.this, Login.class);
startActivity(intent);
}
}
I have tried Everything to my knowledge and nothing seems to work. Any help would be appreciated.
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class activity_main extends Activity {
TextView txtCount;
Button btnCount;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount= (TextView) findViewById(R.id.textView);
txtCount.setText(String.valueOf(count));
btnCount= (Button)findViewById(R.id.button);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
}
}
}
No errors appear at any point and the program works without errors; the button clicks and does not crash, but no click is counted numerically in the textView.
you onClick() listener is wrong.
try this one
btnCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});
I finally learned about adding a button to a page and actually making it navigate to another activity "XML Page". Anyway, I have been trying to add 2 buttons in the same page which navigate each to a different XML's Pages. All I did was copy the first button which worked and then change the button name and all other things the first button works but the second isn't. It shows a click but nothing happens after.
Back1 Button works. TMode Button does the trouble.
Eclipse is not showing errors.
Here is my code -
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
/** Called when the activity is first created.*/
Button btn;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.Back1);
btn.setOnClickListener(btn2Listener);
}
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.TMode);
btn.setOnClickListener(btn3Listener);
}
private OnClickListener btn3Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent3=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent3);
}
};
}
Try something like this:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}
You should in your XML file define two buttons
<Button
android:id="#+id/button1"
... />
<Button
android:id="#+id/button2"
... />
And then in your Activity in onCreate() method you do
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
...
})
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
...
});
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent1);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}
I have just started android development in eclipse with android and am trying to program a button, this is my code,
package my.Apprentice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class ApprenticeVoteActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startVotingListener();
}
private void startVotingListener() {
final Button startVoting = (Button) findViewById(R.id.startVoting);
startVoting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
};)
}
}
The location of my error is commented above. I really have no idea on how to get rid of it, I have tried cleaning my project with no success. Does anyone have any ideas ? Thanks !
The Updated and working code is shown below:
package my.Apprentice;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ApprenticeVoteActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startVotingListener();
}
private void startVotingListener() {
final Button startVoting = (Button) findViewById(R.id.startVoting);
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
Try });
import android.view.View;
import android.view.View.OnClickListener;
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
}); // Sequence is wrong
Did you try to remove the ';' after the listener declaration ? Remove also the View reference :
startVoting.setOnClickListener(new View.OnClickListener() { <-- HERE
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
};) // <--- HERE
Final :
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});