unable to get value from edittext into string - java

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..

Related

Android app keeps stopping while building using android studio

When I'm trying to transfer data between activities, I can't get my message and the app keeps crashing,
It show me 'app keeps stopping'
Code in MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Intent outIntent;
EditText edtPhone;
EditText edtMessage;
Button btnNext;
String tempText="";
public static final String PHONE = "PHONE";
public static final String MESSAGE = "MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
outIntent = new Intent(MainActivity.this, ActivityTwo.class);
edtPhone = (EditText) findViewById(R.id.edtPhone);
edtMessage = (EditText) findViewById(R.id.edtMessage);
tempText = edtPhone.getText().toString();
outIntent.putExtra(PHONE,tempText);
tempText = edtMessage.getText().toString();
outIntent.putExtra(MESSAGE,tempText);
startActivity(outIntent);
}
});
}
public void closeMethod(View view) {
finish();
}
}
ActivityTwo.java
package com.example.fir;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
Intent incomingIntent;
TextView txtPhone;
TextView txtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
incomingIntent = getIntent();
txtPhone = (TextView) findViewById(R.id.txtPhone);
txtMessage = (TextView) findViewById(R.id.txtMessage);
txtPhone.setText(incomingIntent.getStringExtra(MainActivity.PHONE));
txtMessage.setText(incomingIntent.getStringExtra(MainActivity.MESSAGE));
}
}
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
Activitytwo.xml
////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
The issue is when I don't put any value into the firstEditText or secondEditText or both of them and click on any button then the app crashes and a pop up shows "myapp keeps stopping".
I cannot get the text and am unsure of why the app keeps crashing.
you have defined your Edit texts in the first activity with id's named
edtPhone
edtMessage
but there was no edtPhone or edtMessage in your xml.
this issue also repeats in your second activity
you have defined txtPhone and txtMessage
but again there are no such ids in your second activity XML.
keep in mind that these might not be the whole problem as you didn't post any log for the errors. but if there were anything else, ask and we'll help you

firebase realtime database is not storing data and the logcat shows no error

I am learning java and trying to register user's info in firebase realtime database when they register in the app ,but the data doesn't get stored in the database and the logcat shows no error so I can't figure out what's the problem.
P.S. - App is connected to the database & firebase database rules are:
{
"rules": {
".read": true,
".write": true
}
}
here's the SignUp.java file code
package com.example.foodapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUp extends AppCompatActivity {
//variables
private TextInputLayout regName,regUsername,regEmail,regPhoneNo,regPassword;
private Button regBtn,regToLogInBtn;
FirebaseDatabase rootNode;
DatabaseReference reference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//hooks
regName = (TextInputLayout )findViewById(R.id.reg_name);
regUsername = (TextInputLayout )findViewById(R.id.reg_username);
regEmail = (TextInputLayout )findViewById(R.id.reg_email);
regPhoneNo = (TextInputLayout )findViewById(R.id.reg_phoneNo);
regPassword = (TextInputLayout )findViewById(R.id.reg_password);
regBtn = (Button)findViewById(R.id.reg_btn);
regToLogInBtn = (Button)findViewById(R.id.reg_login_btn);
//save data in firebase using button click
regBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("users");
Log.d("HELLO", "Register Button Clicked");
//get all value from text fields
String name = regName.getEditText().getText().toString();
String username = regUsername.getEditText().getText().toString();
String email = regEmail.getEditText().getText().toString();
String phoneNo = regPhoneNo.getEditText().getText().toString();
String password = regPassword.getEditText().getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, username, email, phoneNo, password);
reference.child(phoneNo).push().setValue(helperClass);
}
});
}
}
the xml codes are:
<?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">
<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"
tools:context=".SignUp"
android:orientation="vertical"
android:background="#fff"
android:padding="20dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/logo"
android:transitionName="logo_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textSize="40sp"
android:fontFamily="#font/bungee"
android:layout_marginTop="-20dp"
android:textColor="#000"
android:transitionName="logo_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SignUp to start your new Journey"
android:textSize="18sp"
android:transitionName="logo_desc"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_name"
android:hint="Full Name"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
//-----------------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_username"
app:counterMaxLength="15"
android:hint="Username"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:transitionName="username_tran">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
//--------------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_email"
android:hint="Email"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
//-----------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_phoneNo"
android:hint="Phone Number"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
//---------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_password"
android:hint="Password"
app:passwordToggleEnabled="true"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:transitionName="password_tran">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<Button
android:id="#+id/reg_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:text="GO"
android:textColor="#fff"
android:transitionName="button_tran" />
<Button
android:id="#+id/reg_login_btn"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="Already have an account? Login"
android:textColor="#000"
android:transitionName="login_signup_tran" />
</LinearLayout>
</ScrollView>
```
the database is:
[1]: https://i.stack.imgur.com/IlaDs.png
you can refer to this question, after reading those answers, the reason of the issue is either you verification id or code is null or empty SHA certificates is not available on firebase app (console)
Exception when authenticating with wrong verification code

How to fix this margin gap?

I am creating an Faq page using TextViews, So the textviews become visible and hide automatically when clicked, but this was overlapping the upcoming question so I researched and found marginlayoutparams solution but this code won't reset back when clicked again.
Initial state:
After I click Fare Charges:
After I click on it again:
The code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class faq extends AppCompatActivity {
TextView mfaq,mAns,mfaq2,mAns2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_faq);
mfaq=findViewById(R.id.faq1);
mAns=findViewById(R.id.ans);
mfaq2=findViewById(R.id.faq2);
mAns2=findViewById(R.id.ans2);
mfaq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mAns.getVisibility()== View.VISIBLE){
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) mfaq2.getLayoutParams();
marginParams.setMargins(marginParams.leftMargin,
-400, //setting it back to 0 this part isnt working
marginParams.rightMargin,
marginParams.bottomMargin);
mAns.setVisibility(View.GONE);
}
else
mAns.setVisibility(View.VISIBLE);
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) mfaq2.getLayoutParams();
marginParams.setMargins(marginParams.leftMargin,
400, //only changing top margin
marginParams.rightMargin,
marginParams.bottomMargin);
}
});
mfaq2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mAns2.getVisibility()== View.VISIBLE){
mAns2.setVisibility(View.GONE);
}
else
mAns2.setVisibility(View.VISIBLE);
}
});
}
}
I don't know how to set it back to initial state like first image. Please help. Note- I am a beginner so please explain me so I can learn and understand.
Can you print the xml ? try with "wrap_content" in parent layout
You can use Cardview to encapsulate your Faq element :
https://developer.android.com/guide/topics/ui/layout/cardview
Then change the visibility of the component that you want to hide as gone and set a listner to change the visibility if the textView (or your cardView ) is clicked
Textview label = (TextView ) myPosts.findViewById(R.id.cancel);
cancelButton.setOnClickListener(view->{
if(newPostCard.getVisibility() == View.VISIBLE){
TransitionManager.beginDelayedTransition(cardView,
new AutoTransition());
newPostCard.setVisibility(View.GONE);
newPostButton.setVisibility(View.VISIBLE);
}
else{
TransitionManager.beginDelayedTransition(cardView,
new AutoTransition());
newPostCard.setVisibility(View.VISIBLE);
newPostButton.setVisibility(View.GONE);
}
});
The way I got around to this problem was to add a linear layout to the questions and answer fields. here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="#android:color/holo_orange_dark"
tools:context=".faq">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="300dp"
tools:context=".faq">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/Welcome"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="36sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/frequentlyaskedquestions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="#string/faqs"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
android:textSize="18sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/clickonthem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/clickonthem"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
android:textSize="18sp"
app:fontFamily="#font/racing_sans_one"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/frequentlyaskedquestions" />
<LinearLayout
android:layout_width="409dp"
android:layout_height="294dp"
android:layout_marginTop="28dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clickonthem">
<TextView
android:id="#+id/faq1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="true"
android:text="#string/q1"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/ans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="#string/a1"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="18sp"
android:textStyle="bold|italic"
android:visibility="gone" />
<TextView
android:id="#+id/faq2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:focusable="true"
android:text="#string/q2"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textSize="18sp"
android:textStyle="bold|italic" />
<TextView
android:id="#+id/ans2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="#string/a2"
android:textColor="#000000"
android:textColorHighlight="#000000"
android:textColorHint="#000000"
android:textColorLink="#000000"
android:textSize="18sp"
android:textStyle="bold|italic"
android:visibility="gone" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

How do I parse a double and show it in an EdiText after calculation?

I am a beginner, please just tell me what Im doing wrong with a hint. Dont tell me the solution just a good hint. This is my code:
package com.dietel.preserve;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "myApp";
double fedExPivot1 = 35.00;
double fedExPivot2 = 45.00;
double fed1=850.00;
double fed2= 750.00;
double fedAdd= 2500.00;
String p;
double convertedWeight;
double outShippingCost;
String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText weight = (EditText) findViewById(R.id.editText);
p= weight.getText().toString();
convertedWeight= Double.parseDouble(p);
final EditText textvew= (EditText) findViewById(R.id.editText4);
Button calculate = (Button) findViewById(R.id.button);
calculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
outShippingCost =shippingCost(convertedWeight);
s=Double.toString(outShippingCost);
textvew.setText(s);
}
});
}
public double shippingCost(double w) {
if (w < fedExPivot1) {
w= w* fed1+fedAdd;
w=w/100;
} else if (w > fedExPivot2){
w= w*fed2/100;
}else if(w>= fedExPivot1){
w= w*fed1/100;
}
return w;
}
}
I'm just trying to get a value from an editText and then convert it into a double and then show it inside another ediText after calculation. Please help me.
This is my xml code:
<?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="com.dietel.preserve.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="39dp"
android:layout_marginTop="150dp"
android:text="#string/weight"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlHardcoded" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="34dp"
android:text="#string/sale_price"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:ignore="RtlHardcoded" />
<Button
android:id="#+id/button"
android:layout_width="128dp"
android:layout_height="39dp"
android:text="#string/calculate"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.566"
android:layout_marginTop="77dp"
app:layout_constraintTop_toBottomOf="#+id/editText"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/editText4"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:layout_marginRight="53dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="137dp"
tools:ignore="LabelFor,RtlHardcoded"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/editText"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintVertical_bias="0.0"
android:layout_marginRight="53dp"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="LabelFor,RtlHardcoded"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="114dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="#string/total_cost"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintVertical_bias="0.992"
tools:ignore="RtlHardcoded" />
<TextView
android:id="#+id/textView4"
android:layout_width="112dp"
android:layout_height="45dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="256dp"
android:layout_marginTop="92dp"
android:text="#string/enter_info2"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:ignore="RtlHardcoded"
tools:text="Enter Info:" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="104dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="53dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="LabelFor,RtlHardcoded" />
</android.support.constraint.ConstraintLayout>
You asked for a hint: you're fetching the value of the first field in a wrong time, so when you click the button you're processing a wrong value. Examine convertedWeight carefully.

How to use spinners in android?

I am new to android. I am trying to make simple unit converter. The layout contains EditText for input, two spinners (to and from), an ImageButton and EditText to display answer.
I want to add values to spinners and when these values are selected i want to perform action.
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".BitsBytes" >
<Spinner
android:id="#+id/SpinnerFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/inputvalue"
android:layout_below="#+id/textView2"
android:entries="#array/bitsbytesfrom"
android:prompt="#string/from_prompt" />
<Spinner
android:id="#+id/SpinnerTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/SpinnerFrom"
android:layout_below="#+id/textView3"
android:entries="#array/bitsbytesto"
android:prompt="#string/to_prompt" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/SpinnerFrom"
android:layout_below="#+id/SpinnerFrom"
android:layout_marginTop="16dp"
android:text="To:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Value: "
android:textSize="20dp" />
<EditText
android:id="#+id/inputvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:ems="10"
android:hint="Enter value"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/inputvalue"
android:layout_marginTop="16dp"
android:text="From:"
android:textColor="#000000"
android:textSize="20sp" />
<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_marginTop="19dp"
android:text="Bits/Bytes Conversion"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
<ImageButton
android:id="#+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/SpinnerTo"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="#drawable/btnconvert"
android:clickable="true"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/answer"
android:layout_centerHorizontal="true"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
My java code:
package com.easy.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class BitsBytes extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitsbytes);
ImageButton ib = (ImageButton) findViewById(R.id.convertButton);
ib.setOnClickListener(new View.OnClickListener() {
EditText etValue = (EditText) findViewById(R.id.inputvalue);
Spinner spBitsBytesFrom = (Spinner) findViewById(R.id.SpinnerFrom);
Spinner spBitsBytesTo = (Spinner) findViewById(R.id.SpinnerTo);
String txtFromSpinner = spBitsBytesFrom.getSelectedItem().toString();
String txtToSpinner = spBitsBytesTo.getSelectedItem().toString();
TextView Answer = (TextView) findViewById(R.id.answer);
public void onClick(View v)
{
if (txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
});
}
}
I am filling the spinners using string array. I am not able to figure out whats wrong in my code because there is no error in logcat.
Change this,
(txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
to
if((txtFromSpinner.getSelectedItem().toString().equals("Bits")) && (txtToSpinner.getSelectedItem().toString().equals("Bytes")))
this will get the text from the item selected in the spinner.

Categories

Resources