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
}
Related
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 m working on an application and i want to make user login only when he/she logouts his previous session but i m unable to do it. whenever i close my program and re-open it. it directs to Homepage again even if i had'nt logout. plz
package in.co.medimap.www.medimap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static in.co.medimap.www.medimap.R.layout.login;
/**
* Created by sony on 28-04-2016.
*/
public class login extends Activity {
TextView signup_text;
Button login_button;
EditText PHONE_NO,PASSWORD;
AlertDialog.Builder builder;
public static final String MyPREFERENCE ="Myprefs";
public static final String PHONE="phone";
public static final String PASS="password";
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
signup_text=(TextView)findViewById(R.id.sign_up);
signup_text.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent(login.this,register.class));
}
});
PHONE_NO=(EditText)findViewById(R.id.phone_no);
PASSWORD=(EditText)findViewById(R.id.password);
login_button=(Button)findViewById(R.id.login_button);
login_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ph = PHONE_NO.getText().toString();
String p=PASSWORD.getText().toString();
if (ph.equals("")||p.equals("")) {
builder = new AlertDialog.Builder(login.this);
builder.setTitle("Something went wrong...");
builder.setMessage("Please fill all the fields...");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
else
{
//see from here
sharedPreferences = getSharedPreferences(MyPREFERENCE,Context.MODE_PRIVATE );
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(PHONE, ph);
editor.putString(PASS, p);
editor.commit();
BackgroundTask backgroundTask = new BackgroundTask(login.this);
backgroundTask.execute("login",ph,p);
}
}
});
}
}
this is my homeactivity where after logging user goes
and i want that if i hadn't logout then whenever i open my app it should start from here
package in.co.medimap.www.medimap;
import android.app.Activity;
import android.content.Context;
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;
import android.widget.TextView;
public class HomeActivity extends Activity {
Button Logout = null;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Logout = (Button) findViewById(R.id.Logout);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Intent intent = new Intent(HomeActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
If you want logout to happen each time user leaves the application, why don't you just do it manually?
you've already set Logout buttons setOnClickListener so just do this:
#Override
protected void onPause() {
super.onPause();
if(Logout != null) {
Logout.callOnClick();
}
}
This means every time user leaves this activity it will logout.
make sure you handle cases where if they leave activity but go to a different activity on your page, it doesn't log out.
=========== EDIT ===========
I'm guessing your login activity is the main activity that starts when application opens so just put this in onCreate before you perform any other tasks. right after setContentView, you should do this.
SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);
String phone = sharedPreferences.getString(PHONE, "");
String pass = sharedPreferences.getString(PASS, "");
if(!phone.isEmpty() && !pass.isEmpty()) {
// this means ID and passwords are already saved so just start your home activity here
startActivity(new Intent(context, MainActivity.class));
finish();
}
In your MainActivity:
Just read the values from SharedPreferences and login if you have to.
Hope this helps.
========== EDIT 2
Also, your Logout should look like this. Don't use commmit or clear. put empty values and use apply
Logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(PHONE, "");
editor.putString(PASS, "");
editor.apply();
Intent intent = new Intent(HomeActivity.this,MainActivity.class);
startActivity(intent);
}
});
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 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/
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Android - How to open Activity by clicking button
i want to open another Activity(GameProcess) from this one (KlikomaniaActivity) through button, but when i tap the button the program crashes. i don't a proffesional android programmer, please say what mistakes i have:
packagecom.makeandroid.klikomania;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class KlikomaniaActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button Butstart = (Button)findViewById(R.id.butstart);
final Button Butrez = (Button)findViewById(R.id.butrez);
Butstart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.makeandroid.klikomania.GameProcess");
// эапускаем деятельнсть
startService(intent);
}
});
}
}
and here GameProcess Acticity:
public class GameProcess extends KlikomaniaActivity {
private static int rezult = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameprocess);
final Button Butklik = (Button)findViewById(R.id.klik);
final TextView TextTime = (TextView)findViewById(R.id.texttime);
final TextView TextKolvo = (TextView)findViewById(R.id.kolvo);
Butklik.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rezult=rezult+1;
TextKolvo.setText(rezult);
}
});
}
}
First make sure that you added your activity to the manifest file :
<activity android:name="com.makeandroid.klikomania.GameProcess"></activity>
second, to start the activity use this code:
final Intent gameProcessIntent= new Intent(this, GameProcess.class);
Butstart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(gameProcessIntent);
}
});
this should work normaly