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?
Related
This problem occurs throughout the app but I am only posting my register activity.
Here is a screenshot of my register activity:
Here is the xml for my register activity. I don't think you need to read it.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#6633ff"
tools:context=".RegisterActivity">
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left_guideline_edit_text"
app:layout_constraintGuide_percent=".12077"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right_guideline_edit_text"
app:layout_constraintGuide_percent=".87923"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_name"
app:layout_constraintGuide_percent=".09821"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_name"
app:layout_constraintGuide_percent=".17857"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_email"
app:layout_constraintGuide_percent=".21429"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_email"
app:layout_constraintGuide_percent=".29464"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_password"
app:layout_constraintGuide_percent=".33036"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_password"
app:layout_constraintGuide_percent=".41071"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_city"
app:layout_constraintGuide_percent=".44420"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_city"
app:layout_constraintGuide_percent=".52455"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_country"
app:layout_constraintGuide_percent=".56027"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/left_guideline_register_button"
app:layout_constraintGuide_percent=".27536"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/right_guideline_register_button"
app:layout_constraintGuide_percent=".72222"
android:orientation="vertical"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_register_button"
app:layout_constraintGuide_percent=".70313"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_edit_text_country"
app:layout_constraintGuide_percent=".64063"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_register_button"
app:layout_constraintGuide_percent=".78125"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_edit_text_view_already_account"
app:layout_constraintGuide_percent=".80692"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_text_view_already_account"
app:layout_constraintGuide_percent=".84933"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/top_guideline_text_view_log_in"
app:layout_constraintGuide_percent=".84933"
android:orientation="horizontal"/>
<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bottom_guideline_text_view_log_in"
app:layout_constraintGuide_percent=".89174"
android:orientation="horizontal"/>
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_name"
android:hint="Name"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textCapSentences"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_name"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_name"/>
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_email"
android:hint="Email"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textEmailAddress"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_email"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_email" />
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_password"
android:hint="Password"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textPassword"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_password"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_password" />
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_city"
android:hint="City"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textCapSentences"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_city"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_city" />
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_text_shape"
android:id="#+id/edit_text_country"
android:hint="Country"
android:textColorHint="#bbbbbb"
android:textColor="#000000"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:inputType="textCapSentences"
android:maxLines="1"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_edit_text"
app:layout_constraintRight_toRightOf="#+id/right_guideline_edit_text"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_country"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_edit_text_country"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/register_button"
android:background="#drawable/button_shape"
android:fontFamily="#font/inter_extrabold"
android:textSize="28sp"
android:text="Register"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#6633ff"
app:layout_constraintLeft_toLeftOf="#+id/left_guideline_register_button"
app:layout_constraintRight_toRightOf="#+id/right_guideline_register_button"
app:layout_constraintTop_toTopOf="#+id/top_guideline_register_button"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_register_button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view_already_account"
android:text="Already have an account?"
android:fontFamily="#font/inter_thin"
android:textSize="20sp"
android:textColor="#ffffff"
android:textAlignment="center"
android:textAllCaps="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/top_guideline_edit_text_view_already_account"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_text_view_already_account"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view_log_in"
android:text="#string/LogIn"
android:fontFamily="#font/inter_semibold"
android:textSize="28sp"
android:textColor="#ffffff"
android:textAlignment="center"
android:textAllCaps="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/top_guideline_text_view_log_in"
app:layout_constraintBottom_toBottomOf="#+id/bottom_guideline_text_view_log_in"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is my code for the activity. The main part of the code is at the bottom in the goToRegisterAs() method.
package com.example.treeapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class RegisterActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText emailEditText;
private EditText passwordEditText;
private EditText cityEditText;
private EditText countryEditText;
private Button registerButton;
private TextView alreadyAccountTextView;
private TextView logInTextView;
private String name;
private String email;
private String password;
private String city;
private String country;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
nameEditText = findViewById(R.id.edit_text_name);
emailEditText = findViewById(R.id.edit_text_email);
passwordEditText = findViewById(R.id.edit_text_password);
cityEditText = findViewById(R.id.edit_text_city);
countryEditText = findViewById(R.id.edit_text_country);
registerButton = findViewById(R.id.register_button);
alreadyAccountTextView = findViewById(R.id.text_view_already_account);
logInTextView = findViewById(R.id.text_view_log_in);
name = nameEditText.getText().toString();
email = emailEditText.getText().toString();
password = passwordEditText.getText().toString();
city = cityEditText.getText().toString();
country = countryEditText.getText().toString();
registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
goToRegisterAs(view);
}
});
countryEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_ENTER) {
goToRegisterAs(view);
return true;
} else {
return false;
}
}
});
logInTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), MainActivity.class);
view.getContext().startActivity(intent);
}
});
}
void goToRegisterAs(View view) {
if (!(name.equals("") || email.equals("") || password.equals("") ||
city.equals("") || country.equals(""))) {
if (email.contains("#") && email.contains(".")) {
if (MainActivity.database.donorsDao().checkEmail(email) == 0 &&
MainActivity.database.plantersDao().checkEmail(email) == 0) {
Intent intent = new Intent(view.getContext(), RegisterAsActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("password", password);
intent.putExtra("city", city);
intent.putExtra("country", country);
view.getContext().startActivity(intent);
} else {
alreadyAccountTextView.setText("Already registered. Please log in.");
}
} else {
alreadyAccountTextView.setText("Please enter correct email address.");
}
} else {
alreadyAccountTextView.setText("Please enter all fields");
alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
}
}
}
The app was not working as I wanted it to. So after some trying, I figured out that my EditTexts are all returning an empty string. In the last line in the code alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country), the alreadyAccountTextView becomes empty. I wrote this line here because this block of code was being executed even though I had input text in every field. I have read many questions and answers on StackOverflow about getting text from EditTexts and all of them say that I should use EditText.getText().toString(). But it isn't working for me, due to some reason.
The problem is you initialise your Strings in your OnCreate function. This function is called only once, when your activity starts; therefore, at this point your EditTexts are all empty without any text in it.
To get the current text located in your EditTexts, you should move your initialisations in your goToRegisterAs() function.
void goToRegisterAs(View view) {
name = nameEditText.getText().toString();
email = emailEditText.getText().toString();
password = passwordEditText.getText().toString();
city = cityEditText.getText().toString();
country = countryEditText.getText().toString();
if (!(name.equals("") || email.equals("") || password.equals("") ||
city.equals("") || country.equals(""))) {
if (email.contains("#") && email.contains(".")) {
if (MainActivity.database.donorsDao().checkEmail(email) == 0 &&
MainActivity.database.plantersDao().checkEmail(email) == 0) {
Intent intent = new Intent(view.getContext(), RegisterAsActivity.class);
intent.putExtra("name", name);
intent.putExtra("email", email);
intent.putExtra("password", password);
intent.putExtra("city", city);
intent.putExtra("country", country);
view.getContext().startActivity(intent);
} else {
alreadyAccountTextView.setText("Already registered. Please log in.");
}
} else {
alreadyAccountTextView.setText("Please enter correct email address.");
}
} else {
alreadyAccountTextView.setText("Please enter all fields");
alreadyAccountTextView.setText(name + " " + email + " " + password + " " + city + " " + country);
}
}
I'm trying to make an android app which will show a Toast on button click. That Toast contains the number entered by the user in edittext field. The problem is that i am entering text to edittext(Numeric) field and on button click, Toast isn't showing the text entered by me. Toast is completely blank.
Here is my code:-
Activity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class login extends AppCompatActivity {
String username, password;
Button payNGO, payGO;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.activity_login);
payNGO = (Button) findViewById(R.id.payngo);
payGO = (Button) findViewById(R.id.paygo);
usernameField = (EditText) findViewById(R.id.forno);
passwordField = (EditText) findViewById(R.id.dob);
username = usernameField.getText().toString();
password = passwordField.getText().toString();
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
}
}
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".login"
android:background="#drawable/back">
<ImageView
android:id="#+id/imageView"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/forno"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.438"
app:srcCompat="#drawable/ico" />
<EditText
android:id="#+id/forno"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="20dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Number"
android:inputType="number"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/dob"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/dob"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="124dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Date of Birth"
android:inputType="numberPassword"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="92dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/payngo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="20dip"
android:layout_marginStart="15dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (NGO)"
tools:layout_editor_absoluteX="204dp"
tools:layout_editor_absoluteY="440dp" />
<Button
android:id="#+id/paygo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="15dip"
android:layout_marginStart="20dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (GO)"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="440dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="#drawable/rect_back_text"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dip"
android:layout_weight="1"
android:text="#string/linef"
android:textAlignment="textEnd"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="30dp"
tools:layout_editor_absoluteY="505dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#drawable/heart"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dip"
android:layout_weight="1"
android:text="#string/linee"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="171dp"
tools:layout_editor_absoluteY="505dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Since you are getting the value in onCreate which will be executed even before you enter anything, your Toast shows blank data.
Move the getText() methods to onClick to achieve expected result as follows:
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
Toast.makeText(login.this, usernameField.getText().toString(), Toast.LENGTH_LONG).show();
you can use this code it definitely work..
I am new to app development, I am stuck on this following example in my book.
Can some one please help me that why my play button is not working? After clicking on the play button the game should start and it should navigate to next activity.
Main page code
<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"
tools:context="com.packtpub.mathgamechapter3a.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Math Game"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="30sp" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="#+id/buttonPlay"
android:layout_marginTop="28dp"
android:layout_below="#+id/imageView"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="High Scores"
android:id="#+id/button2"
android:layout_below="#+id/buttonPlay"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quit"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignRight="#+id/buttonPlay"
android:layout_alignEnd="#+id/buttonPlay"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Intent i;
i = new Intent(this, GameActivity.class);
startActivity(i);
}
}
Game page where it should navigate
<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"
tools:context="com.packtpub.mathgamechapter3a.GameActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartA"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x"
android:id="#+id/textOperator"
android:layout_alignTop="#+id/textPartA"
android:layout_centerHorizontal="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="2"
android:id="#+id/textPartB"
android:layout_alignTop="#+id/textOperator"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="70sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="="
android:id="#+id/textView4"
android:layout_below="#+id/textOperator"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:textSize="70sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/buttonChoice1"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="99dp"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/buttonChoice2"
android:layout_alignTop="#+id/buttonChoice1"
android:layout_centerHorizontal="true"
android:textSize="40sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/buttonChoice3"
android:layout_alignTop="#+id/buttonChoice2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="40sp" />
</RelativeLayout>
Java code
package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GameActivity extends Activity implements View.OnClickListener{
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//The next line loads our UI design to the screen
setContentView(R.layout.activity_game);
//Here we initialize all our variables
int partA = 9;
int partB = 9;
correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer - 1;
int wrongAnswer2 = correctAnswer + 1;
/*Here we get a working object based on either the button
or TextView class and base as well as link our new objects
directly to the appropriate UI elements that we created previously*/
TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);
//Now we use the setText method of the class on our objects
//to show our variable values on the UI elements.
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partA);
//which button receives which answer, at this stage is arbitrary.
buttonObjectChoice1.setText("" + correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
buttonObjectChoice1.setOnClickListener(this);
buttonObjectChoice2.setOnClickListener(this);
buttonObjectChoice3.setOnClickListener(this);
}//onCreate ends here
#Override
public void onClick(View view) {
//declare a new int to be used in all the cases
int answerGiven=0;
switch (view.getId()) {
case R.id.buttonChoice1:
//initialize a new int with the value contained in buttonObjectChoice1
//Remember we put it there ourselves previously
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
//is it the right answer?
if(answerGiven==correctAnswer) {//yay it's the right answer
Toast.makeText(getApplicationContext(),
"Well done!", Toast.LENGTH_LONG).show();
}else{//uh oh!
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice2:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),
"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice3:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Sorry that's wrong",
Toast.LENGTH_LONG).show();
}
break;
}
}
}
i checked your code it is correct .
I think you forget to register your activity in manifests
please check that
well you need to do a if statement on the being clicked on.
if(view.getId() == R.id.buttonPlay)
{
Intent intent = new Intent(getBaseContext(), GameActivity.class);
startActivity(intent);
}
also make sure game activity is registered in the manifest file.
Intent in = getIntent();
is missing..
the 2nd activity should contain this line
also add this
if(view.getId() == R.id.buttonPlay)
{
Intent inte = new Intent(MainActivity.this, GameActivity.class);
startActivity(intent);
}
and do register in the manifest file
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
}
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;
}
}
});