Shared preferences logging system in android - java

I searched in stackoverflow and i could not find anything like this. I want to make a logging system like registration and login. But the following code does not work when clicked LogIN. There is probably error in the if logic. please help'.
MainActivity.java
package com.example.asifsabir.sharedpref;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText ed1,ed2;
Button b1,b2,b3;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String email = "emailKey";
public static final String password = "passwordKey";
public static final String safety = "safetyKey";
SharedPreferences sharedpreferences;
SharedPreferences sharedpreferences2;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText1);
ed2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = ed1.getText().toString();
String password = ed2.getText().toString();
if(sharedpreferences.getString(password,"").equals(password))
Toast.makeText(MainActivity.this, "Logged on Successful", Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = ed1.getText().toString();
String password = ed2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(email,email);
editor.putString(password,password);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks! Signed up",Toast.LENGTH_LONG).show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preference \n login system"
android:id="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="35dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:layout_below="#+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="email id: " />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_below="#+id/editText1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="password: " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/button"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="forgot password?"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>

I think there is something wrong with you understanding of SharedPref. It stores key-value pairs. If you use the same key to store all the emails, you would still need a way to correlate between the username and the password. One way to achieve this could be to do something like this:
String email = ed1.getText().toString();
String password = ed2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(email, password);
editor.commit();
Then, retrieval would be as simple as...
String email = ed1.getText().toString();
String password = ed2.getText().toString();
if(sharedpreferences.getString(email,"").equals(password))
** LOG USER IN **
An alternative way is to store all emails with the same key, and all passwords with the same key. Here, you would need to do something to ensure that emails and the passwords are connected to each other. One way could be to append email to the end of the password, and then save it.
String email = ed1.getText().toString();
String password = ed2.getText().toString();
password += email;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(this.email,email);
editor.putString(this.password,password);
editor.commit();
Instead of using getString(..) you would now need to use [getStringSet].
Edit.
It would also be a good idea to change the names of these 2 fields.
public static final String email = "emailKey"; // to emailKey
public static final String password = "passwordKey"; // to passwordKey
This will avoid confusion with the contents of the EditText boxes.

//LoginActivity.java
public class LoginActivity extends Activity {
/*Componentes gráficos*/
Button btnLogin;
TextView txtOmitir;
EditText etUser, etPass;
/*Variables globales*/
int value, valueLogin, servicesLogin;
Intent intent;
String favoriteService;
String username, password;
private SharedPreferences.Editor editor;
private SharedPreferences prefs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutlogin);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
prefs = getSharedPreferences("MisPreferencias", Context.MODE_PRIVATE);
editor = prefs.edit();
Bundle num = getIntent().getExtras();
if (num != null) {
servicesLogin = num.getInt("serviceslogin");
}
favoriteService = prefs.getString("valueService", "");
etUser = (EditText) findViewById(R.id.etUser);
etPass = (EditText) findViewById(R.id.etPass);
btnLogin = (Button) findViewById(R.id.btnLogin);
txtOmitir = (TextView) findViewById(R.id.txtOmitir);
}
public void btnClickLogin(View view) {
username = etUser.getText().toString();
password = etPass.getText().toString();
if (username.equals("") || password.equals("")) {
Toast.makeText(this, "Introduzca sus llaves",
Toast.LENGTH_SHORT).show();
return;
} else {
intent = new Intent();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
intent.setClass(LoginActivity.this, HomeActivity.class);
startActivity(intent);
Toast.makeText(this, "Log-In Completado..",
Toast.LENGTH_SHORT).show();
}
}//btnClick
public void btnClickOmitir(View view) {
intent = new Intent();
editor.putString("username", "");
editor.putString("password", "");
editor.commit();
intent.setClass(LoginActivity.this, HomeActivity.class);
startActivity(intent);
}
}//class
//layoutlogin.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center_horizontal|center_vertical">
<ImageView
android:layout_marginTop="10dp"
android:layout_width="180dp"
android:layout_height="180dp"
android:id="#+id/imgLogin"
android:src="#drawable/ic_launcher"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="#+id/etUser"
android:hint="username"
android:maxLength="16"
android:singleLine="true"
android:textAlignment="center"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:focusableInTouchMode="true"
android:text="root" />
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="#+id/etPass"
android:singleLine="true"
android:hint="password"
android:textAlignment="center"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:inputType="textPassword"
android:focusableInTouchMode="true"
android:text="masterkey" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recordar"
android:id="#+id/checkBox"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Empleado"
android:id="#+id/textView13"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switch1"
android:layout_gravity="center_horizontal"
android:checked="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Alumno"
android:id="#+id/textView"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Log in"
android:onClick="btnClickLogin"
android:id="#+id/btnLogin"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_weight="1" />
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="OMITIR"
android:onClick="btnClickOmitir"
android:id="#+id/txtOmitir"
android:textColor="#1619ef"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:layout_weight=".5"
android:background="#00000000"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
</ScrollView>
//you can check with it
prefs = getSharedPreferences("MisPreferencias", Context.MODE_PRIVATE);
String email = ed1.getText().toString();
String password = ed2.getText().toString();
passwordLogin=prefs.getString("password","");
userLogin = prefs.getString("username", "");
editor = prefs.edit();
if (userLogin.equals(email) && passwordLogin.equals(password )) {
//do something //Login succesfull
}

Related

Get value of a EditText in another layout

I have an dialog that appears where the user can costumize how many points they get from each action. The dialog is based on a layout that is not the main_activity. the main activity is the layout that is used in setContentView in the onCreate method.
when I try to get the values I am only getting null, (because the layout where the EditText is is not set as the contentview.... maybe?)
How do I solve this problem so that I get the value of the edittext in the "settings" layout and can use that value in the MainActivity?
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings();
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(){
EditText newBurnedPoints = findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}
SETTINGS LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:id="#+id/settings_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_one"
style="#style/settings_headings"
/>
<TextView
android:id="#+id/name_two_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_two"
style="#style/settings_headings"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/name_one_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_one"
android:maxLength="20"
android:singleLine="true"
/>
<EditText
android:id="#+id/name_two_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_two"
android:maxLength="20"
android:singleLine="true"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/burned_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/burned_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="3"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one_hand_lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/one_hand_lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/home_run_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/home_run_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rev_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/rev_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
</LinearLayout>
MAIN LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/team_names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_one"
style="#style/Headings"
/>
<TextView
android:id="#+id/name_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_two"
style="#style/Headings"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/team_points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/points_one"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/field_team"/>
<ToggleButton
android:id="#+id/field_team_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="#string/team_one"
android:textOn="#string/team_two"
style="#style/buttons"/>
</LinearLayout>
<TextView
android:id="#+id/points_two"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/events"
android:paddingTop="16dp"
style="#style/Headings"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
>
<Button
android:id="#+id/burned_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="burned"
android:text="#string/burned"
style="#style/buttons"/>
<Button
android:id="#+id/lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="lyre"
android:text="#string/lyre"
style="#style/buttons"
/>
<Button
android:id="#+id/one_hand_lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneHandLyre"
android:text="#string/oneHandLyre"
style="#style/buttons"
/>
<Button
android:id="#+id/homeRun_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="homeRun"
android:text="#string/homeRun"
style="#style/buttons"
/>
<Button
android:id="#+id/rev_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="rev"
android:text="#string/rev"
style="#style/buttons"
/>
</TableLayout>
<Button
android:id="#+id/reset_button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="16dp"
android:onClick="reset"
android:text="#string/reset"
style="#style/buttons"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/points_edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#android:drawable/ic_menu_edit"
android:layout_margin="16dp" />
</LinearLayout>
I would strongly recommend using SharedPreferences. This way, you can access the data even if you restart your application. If that does not work for you, you can use Intent.
Consider reading this StackOverflow post.
Using SharedPreferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.points), pointsPerActivity);
editor.commit();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 1;
// or you can store the value somewhere
// getResources().getInteger(R.integer.defaultPointsPerActivity);
int pointsPerActivity = sharedPref.getInt(pointsPerActivity, defaultValue);
pass view object in applySettings(v);
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings(v);
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(View view){
EditText newBurnedPoints = view.findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = view.findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = view.findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = view.findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = view.findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = view.findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =view.findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}

Open activity from other activity

I have a main activity (mottoscreen) after which an activity called circles opens up then from that activity I want to open up either one of the other two activities(sc_activity and or_activity). I have written the code for it but on clicking the buttons present in circles activity the next activity isn't showing up.
Circles Java file-
package com.apsdevelopers.mr.meteout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
public class circles extends mottoscreen {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.circles);
}
public void onButtonClick(View v)
{
if (v.getId() == R.id.GOsc)
{
Intent I = new Intent(circles.this, sc_activity.class);
startActivity(I);
}
else if (v.getId() == R.id.GOor)
{
Intent j = new Intent(circles.this, or_activity.class);
startActivity(j);
}
}
}
Sc_activity Java file-
package com.apsdevelopers.mr.meteout;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class sc_activity extends mottoscreen
{
EditText name, ph, address, mass, thing;
Button msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sc_activity);
name = (EditText)findViewById(R.id.name);
EditText p = (EditText)findViewById(R.id.pin);
String pinc = p.getText().toString();
final int apsnumber= Integer.parseInt("8763597264");
if (pinc.equals("753001") || pinc.equals("753002") || pinc.equals("753003") || pinc.equals("753004") || pinc.equals("753005") || pinc.equals("753006") || pinc.equals("753007") || pinc.equals("753008") || pinc.equals("753009")) {
ph = (EditText)findViewById(R.id.ph);
address = (EditText)findViewById(R.id.address);
mass = (EditText)findViewById(R.id.mass);
thing = (EditText)findViewById(R.id.thing);
msg = (Button)findViewById(R.id.msg);
msg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String number = ph.getText().toString();
String message1 = name.getText().toString();
String message2 = address.getText().toString();
String message3 = mass.getText().toString();
String message4 = thing.getText().toString();
Intent i = new Intent(getApplicationContext(), sc_activity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(String.valueOf(apsnumber), null, number+message1 + message2 + message3 + message4, pIntent, null);
Toast.makeText(getApplicationContext(), "Form sent successfully ! , now click on DONE",
Toast.LENGTH_LONG).show();
}
});
}
else
{
Toast.makeText(getApplicationContext(), "ERROR: WE DONOT COVER THE PINCODE ENTERED BY YOU, PLZ ENTER A VALID PINCODE OF (CTC, ODISHA)",
Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
public void onButtonClick(View v)
{
if (v.getId() == R.id.msg)
{
Intent I = new Intent(sc_activity.this, th_activity.class);
startActivity(I);
}
}
}
Sc_activity XML file-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#9acef6fe"
android:id="#+id/sc_activity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="NAME"
android:ems="10"
android:id="#+id/name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/ph"
android:layout_below="#+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="PHONE NUMBER"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.-5kg, 100 bottles... "
android:id="#+id/textView12"
android:layout_below="#+id/mass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.- metal scrap+newspapers, bottles+tyres...+"
android:id="#+id/textView11"
android:layout_below="#+id/thing"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/address"
android:text="ADDRESS"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_below="#+id/pin"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thing"
android:text="THING"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_below="#+id/address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/mass"
android:text="AMOUNT"
android:imeOptions="actionDone"
android:textColor="#d4375a5c"
android:layout_below="#+id/textView11"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/pin"
android:layout_marginTop="36sp"
android:text="PINCODE"
android:textColor="#d4375a5c"
android:layout_below="#+id/ph"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND MESSAGE"
android:id="#+id/msg"
android:background="#e33a9179"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/thing"
android:layout_alignRight="#+id/textView11"
android:layout_alignEnd="#+id/textView11" />
</RelativeLayout>
Or_activity Java file-
package com.apsdevelopers.mr.meteout;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class or_activity extends mottoscreen
{
EditText nam, pho, addres, mas, thinge;
Button msg2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.or_activity);
nam = (EditText) findViewById(R.id.nam);
EditText p = (EditText) findViewById(R.id.pin1);
String pinc = p.getText().toString();
final int apsnumber= Integer.parseInt("8763597264");
if (pinc.equals("753001") || pinc.equals("753002") || pinc.equals("753003") || pinc.equals("753004") || pinc.equals("753005") || pinc.equals("753006") || pinc.equals("753007") || pinc.equals("753008") || pinc.equals("753009")) {
pho = (EditText) findViewById(R.id.ph);
addres = (EditText) findViewById(R.id.address);
mas = (EditText) findViewById(R.id.mass);
thinge = (EditText) findViewById(R.id.thing);
msg2 = (Button) findViewById(R.id.msg2);
msg2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String number = pho.getText().toString();
String message1 = nam.getText().toString();
String message2 = addres.getText().toString();
String message3 = mas.getText().toString();
String message4 = thinge.getText().toString();
Intent i = new Intent(getApplicationContext(), or_activity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(String.valueOf(apsnumber), null, number+message1 + message2 + message3 + message4, pIntent, null);
Toast.makeText(getApplicationContext(), "Form sent successfully ! , now click on DONE",
Toast.LENGTH_LONG).show();
}
});
}
else
{
Toast.makeText(getApplicationContext(), "ERROR: WE DONOT COVER THE PINCODE ENTERED BY YOU, PLZ ENTER A VALID PINCODE OF (CTC, ODISHA)",
Toast.LENGTH_LONG).show();
}
}
public void onButtonClick(View v)
{
if (v.getId() == R.id.msg2)
{
Intent I = new Intent(or_activity.this, th_activity.class);
startActivity(I);
}
}
}
Or_activity XML file-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#9acef6fe"
android:id="#+id/or_activity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="NAME"
android:ems="10"
android:id="#+id/nam"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/pho"
android:text="PHONE NUMBER"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_below="#+id/nam"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/pin1"
android:text="PINCODE"
android:layout_below="#+id/pho"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="43dp"
android:imeOptions="actionNext"
android:textColor="#d4375a5c" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/addres"
android:text="ADDRESS"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/mas"
android:text="AMOUNT"
android:imeOptions="actionNext"
android:textColor="#d4375a5c"
android:layout_marginTop="25dp"
android:layout_below="#+id/textView11"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.- toys , clothes ... "
android:id="#+id/textView11"
android:layout_below="#+id/thinge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thinge"
android:text="THING"
android:imeOptions="actionDone"
android:textColor="#d4375a5c"
android:layout_below="#+id/addres"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND MESSAGE"
android:id="#+id/msg2"
android:background="#e33a9179"
android:textColor="#ffffff"
android:textSize="20dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView11"
android:layout_alignEnd="#+id/textView11"
android:layout_toRightOf="#+id/thinge"
android:layout_toEndOf="#+id/thinge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="E.g.-5 books,2 cricket bats..."
android:id="#+id/textView12"
android:layout_below="#+id/mas"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Please guide me where I did a mistake in the code.
Can you please check the following in your code:
Your circles.xml has two Buttons with id GOsc and GOor?
In your circles.xml you have set android:onClick="onButtonClick"for both buttons?
If both of the above result to yes, can you please paste your circles.xml file here as well?

Can't set values in second activity

So I'm making a simple application that just takes some information from the user i.e Name, address etc and puts it in the the textviews in the next activity but when I put in the values and move to the next activity there's nothing displayed in the TextViews.
Here's my first activity
public class MainActivity extends Activity {
public String Name;
public String Age;
public String Address;
public String City;
public String phoneno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText name = (EditText) findViewById(R.id.editText1);
Name = name.getText().toString();
EditText Amge = (EditText) findViewById(R.id.agee);
Age = Amge.getText().toString();
EditText Address2 = (EditText) findViewById(R.id.address);
Address = Address2.getText().toString();
EditText City2 = (EditText) findViewById(R.id.city);
City = City2.getText().toString();
EditText phone2 = (EditText) findViewById(R.id.phone);
phoneno = phone2.getText().toString();
final ImageView D = (ImageView) findViewById(R.id.done);
D.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);}}
Here's my second activity
public class second extends MainActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView N1 = (TextView) findViewById(R.id.name1);
N1.setText(Name);
TextView A1 = (TextView) findViewById(R.id.age1);
A1.setText(Age);
TextView Ad1 = (TextView) findViewById(R.id.address1);
Ad1.setText(Address);
TextView C1 = (TextView) findViewById(R.id.city1);
C1.setText(City);
TextView P1 = (TextView) findViewById(R.id.phone1);
P1.setText(phoneno);}
public static void main(String[] args) {
// TODO Auto-generated method stub
}}
Here's my xml for the main activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
android:background="#drawable/bg"
tools:context="com.example.randomtests.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:textSize="21dp"
android:textColor="#ffffff"
android:text="Please enter the required info"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"
android:src="#drawable/bar" />
<EditText
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/agee"
android:layout_below="#+id/agee"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="#string/address"
android:inputType="textPostalAddress"
android:textColor="#000000" />
<EditText
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/address"
android:layout_below="#+id/address"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="#string/city"
android:textColor="#000000" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:gravity="top"
android:textColor="#000000"
android:hint="#string/namu"
android:inputType="textPersonName" />
<EditText
android:id="#+id/agee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="#string/age"
android:inputType="phone"
android:textColor="#000000" />
<EditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/city"
android:layout_below="#+id/city"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/phone"
android:inputType="phone"
android:textColor="#000000" />
<ImageView
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/phone"
android:layout_marginRight="14dp"
android:layout_marginTop="12dp"
android:src="#drawable/button" />
Here's my xml for the second activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg" >
<TextView
android:id="#+id/info1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#ffffff"
android:textSize="33dp"
android:layout_centerHorizontal="true"
android:text="#string/yourinfo"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/info1"
android:textColor="#ffffff"
android:layout_marginLeft="19dp"
android:layout_marginTop="26dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/age1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/name1"
android:layout_below="#+id/name1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/age1"
android:layout_below="#+id/age1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/city1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/address1"
android:layout_below="#+id/address1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/city1"
android:layout_below="#+id/city1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
After you declare your Intent , try sending your data with this
//this use key-value
intent.putExtra("USER_NAME" , "your string");
intent.putExtra("USER_AGE" , "your string");
and etc.
and to other Activity try use
Bundle extra = getIntent().getExtras();
String userName = extra.getString("USER_NAME");
String userAge = extra.getString("USER_AGE");
When you declare your Intent here:
Intent intent = new Intent(MainActivity.this, second.class);
Try sending some data with it using Extras based on what view is being clicked on
For example:
intent.putExtra(Intent.EXTRA_INTENT, **Your String Here**);
And in the activity receiving the intent, use something like:
String data = intent.getStringExtra(Intent.EXTRA_INTENT);
To fetch the data from the intent

Build login,registration system with php and mysql for android,but app can run

I currently work with an login and registration system for android app.I use mysql as database,php to doing that,but after doing all the system files,the app just stop when clicking to login button..pls help,below is all the code i made
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/images" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:layout_marginLeft="19dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp"
android:text="Plot Out"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignTop="#+id/textView1"
android:layout_marginTop="1dp"
android:text="Bring Me Out"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="84dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="53dp"
android:layout_marginTop="2dp"
android:src="#drawable/logo" />
<Button
android:id="#+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:layout_marginLeft="40dp"
android:text="Login"
android:width="100sp" />
<Button
android:id="#+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:text="Register"
android:width="100sp" />
<TextView
android:id="#+id/link_to_forgotpassword"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2"
android:layout_centerHorizontal="true"
android:text="Forgot Your Password?"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<!-- Header Starts-->
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<!-- Logo Start-->
<ImageView android:src="#drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<LinearLayout android:orientation="horizontal"
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="#layout/footer_repeat"
android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->
<!-- Login Form -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="#id/header">
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:id="#+id/loginEmail"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Email"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="20dip"
android:singleLine="true" />
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Password"/>
<EditText android:layout_width="fill_parent"
android:id="#+id/loginPassword"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:password="true"/>
<!-- Error message -->
<TextView android:id="#+id/login_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#e30000"
android:padding="10dip"
android:textStyle="bold"/>
<!-- Login button -->
<Button android:id="#+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Login"/>
<!-- Link to Registration Screen -->
<TextView
android:id="#+id/link_to_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip"
android:layout_marginTop="40dip"
android:gravity="center"
android:text="New to Plot Out?Register here"
android:textColor="#0b84aa"
android:textSize="20sp" />
</LinearLayout>
<!-- Login Form Ends -->
</RelativeLayout>
</ScrollView>
register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#fff">
<!-- Header Starts-->
<LinearLayout android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<!-- Logo Start-->
<ImageView android:src="#drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<LinearLayout android:id="#+id/footer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="#layout/footer_repeat"
android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->
<!-- Registration Form -->
<!-- Registration Form Ends -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textColor="#372c24" />
<EditText
android:id="#+id/registerName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:singleLine="true" >
<requestFocus />
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textColor="#372c24" />
<EditText
android:id="#+id/registerEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:singleLine="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password"
android:textColor="#372c24" />
<EditText
android:id="#+id/registerPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:password="true"
android:singleLine="true" />
<!-- Error message -->
<TextView android:id="#+id/register_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#e30000"
android:padding="10dip"
android:textStyle="bold"/>
<Button
android:id="#+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Register New Account" />
<TextView
android:id="#+id/link_to_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip"
android:layout_marginTop="40dip"
android:gravity="center"
android:text="Already has account! Login here"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
LoginActivity.java
package com.plotout.loginandregister;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.plotout.loginandregister.library.DatabaseHandler;
import com.plotout.loginandregister.library.UserFunctions;
public class LoginActivity extends Activity {
Button btnLogin;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
TextView link_to_register;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
link_to_register = (TextView) findViewById (R.id.link_to_register);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
TextView registerScreen = (TextView) findViewById(R.id.link_to_login);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
}
}
RegisterAvtivity.java
package com.plotout.loginandregister;
import org.json.JSONException;
import org.json.JSONObject;
import com.plotout.loginandregister.library.DatabaseHandler;
import com.plotout.loginandregister.library.UserFunctions;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RegisterActivity extends Activity {
Button btnRegister;
TextView link_to_login;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
link_to_login= (TextView) findViewById(R.id.link_to_login);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error occured in registration");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
// Listening to Login Screen link
loginScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Closing registration screen
// Switching to Login Screen/closing register screen
finish();
}
});
}
}
Below is the logcat shown in ecplise
i just change the id of edit text in login xml,is solve the ClassCastException error..
<
EditText android:layout_width="fill_parent"
android:id="#+id/loginEmail"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="20dip"
android:singleLine="true" />
Now is become a new error, "java.lang.NullPointerException"..So where is the reason again ya??
All experts here pls take a look ya.pls help..thank you
Your button doesn't do anything because the following two lines in LoginActivity throw a ClassCastException because you can't cast from an Android.widget.TextView to an Android.widget.EditView.
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
In main.xml change the first two <TextView ... /> to <EditText ... /> :
<TextView
android:id="#+id/textView1"
...
android:textAppearance="?android:attr/textAppearanceLarge" />
Change to :
<EditText
android:id="#+id/textView2"
...
android:textAppearance="?android:attr/textAppearanceLarge" />

how can access the value of variable in setOnCheckedChangeListener listener from onclick

I want to access value of gender variable from onClick(View v).
value of gender produce in onCheckedChanged(RadioGroup arg0, int selectedId)
and I dont know how can access this value
MAIN ACTIVITY
package com.dietandroidproject;
import Databasedata.Person;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioGroup genderselected = (RadioGroup) findViewById(R.id.selectgender);
genderselected.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup arg0, int selectedId) {
selectedId=genderselected.getCheckedRadioButtonId();
RadioButton genderchoosed = (RadioButton) findViewById(selectedId);
gender = genderchoosed.getText().toString();
}
});
Button saveinformation = (Button) findViewById(R.id.saveinformation);
saveinformation.setOnClickListener(new View.OnClickListener() {
EditText weighttext = (EditText) findViewById(R.id.weighttext);
EditText heighttext = (EditText) findViewById(R.id.heighttext);
EditText usernametext = (EditText) findViewById(R.id.usernametext);
EditText agetext = (EditText) findViewById(R.id.agetext);
//RadioGroup genderselected = (RadioGroup) findViewById(R.id.selectgender);
Spinner activitytext = (Spinner) findViewById(R.id.chooseactivity);
Button saveinformation = (Button) findViewById(R.id.saveinformation);
//TextView genderchoosed = (TextView) findViewById(genderselected
//.getCheckedRadioButtonId());
//String gender = genderchoosed.getText().toString();
String pa = activitytext.getSelectedItem().toString();
#Override
public void onClick(View v) {
int weight = (int) Float.parseFloat(weighttext.getText()
.toString());
float height = Float.parseFloat(heighttext.getText()
.toString());
String username = usernametext.getText().toString();
int age = (int) Float.parseFloat(agetext.getText().toString());
String pa = activitytext.getSelectedItem().toString();
//BMI======================================================
int Bmivalue = calculateBMI(weight, height);
String bmiInterpretation = interpretBMI(Bmivalue);
float idealweight = idealweight(weight, height, gender, pa, age);
double dailycalories=dailycalories(weight,height,gender,pa,age);
//DB insert====================================================
Person person = new Person();
person.setUsername(username);
person.setHeight(height);
person.setWeight(weight);
person.setAge(age);
person.setGender(gender);
person.setPa(pa);
person.setBmivalue(Bmivalue);
person.setBmiInterpretation(bmiInterpretation);
person.setIdealweight(idealweight);
person.setDailycalories(dailycalories);
Databasedata.DatabaseAdapter dbAdapter = new Databasedata.DatabaseAdapter(
MainActivity.this);
dbAdapter.insertPerson(person);
Toast.makeText(getApplicationContext(),
Bmivalue + "and you are" + bmiInterpretation,
Toast.LENGTH_LONG).show();
}
});
}
XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundmain"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/personinformation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.98" >
<EditText
android:id="#+id/heighttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/usernametext"
android:layout_below="#+id/usernametext"
android:ems="10"
android:hint="Enter Your Height" >
</EditText>
<EditText
android:id="#+id/usernametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter Username" />
<EditText
android:id="#+id/weighttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/heighttext"
android:layout_below="#+id/heighttext"
android:ems="10"
android:hint="Enter Your Weight" />
<EditText
android:id="#+id/agetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/weighttext"
android:layout_below="#+id/weighttext"
android:ems="10"
android:hint="Enter Your Age" >
<requestFocus />
</EditText>
</RelativeLayout>
<View
android:layout_width="250dp"
android:layout_height="1dip"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#aaa" />
<RelativeLayout
android:id="#+id/choosegender"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.78" >
<TextView
android:id="#+id/choosefemaleormale"
android:layout_width="match_parent"
android:layout_height="30dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dip"
android:gravity="center"
android:text="Gender : "
android:textAlignment="center"
android:textColor="#555"
android:textSize="19sp" />
<RadioGroup
android:id="#+id/selectgender"
android:layout_width="220dip"
android:layout_height="wrap_content"
android:layout_below="#+id/choosefemaleormale"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/femaleselected"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:checked="true"
android:text="female"
/>
<RadioButton
android:id="#+id/maleselected"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="male"
/>
</RadioGroup>
</RelativeLayout>
<View
android:layout_width="250dp"
android:layout_height="1dip"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#aaa" />
<RelativeLayout
android:id="#+id/choosepa"
android:layout_width="250dip"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center" >
<Spinner
android:id="#+id/chooseactivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:entries="#array/activityitems"
android:gravity="center"
android:prompt="#string/level_of_activity" />
</RelativeLayout>
<Button
android:layout_width="90dp"
android:layout_height="0dp"
android:layout_gravity="right"
android:layout_marginBottom="10dip"
android:layout_marginRight="20dp"
android:layout_weight="0.46"
android:background="#drawable/recent_foods_depressed"
android:hint="save"
android:text="save"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
android:onClick="saveinformation"
android:id="#+id/saveinformation"/>
</LinearLayout>
I need value of gender in float idealweight = idealweight(weight, height, gender, pa, age); and also double dailycalories=dailycalories(weight,height,gender,pa,age);
can any one help me?
String sex = null;
...
onCheckedChanged(RadioGroup arg0, int selectedId){
if(selectedId == R.id.femaleselected){
sex = "female";
} else {
sex = "male";
}
}
Hope this helps.
follow these steps:
add member String mGender to your activity.
MainActivity extend ...
String mGender = null;
onCreate ...
replace the gender by mGender and everything should work
onClick(View v) {
if(mGender != null){
// use mGender
// float gender = Float.valueOf(mGender);
//double gender = Double.valueOf(mGender);
}
}
genderselected.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup arg0, int selectedId)
{
switch(selectedId)
{
case R.id. femaleselected:
Log.d("","female selected");
break;
case R.id. maleselected:
Log.d("","male selected");
break;
}
}
});

Categories

Resources