In the code below, when the 'submit' button is pushed, I'm trying to get the values from three editTexts. After I get the String values, I want to convert them to int values and check whether they are in the specified range.
However I am having trouble getting the values from editText. I'm not sure if this is the problem or if there is another even bigger flaw with my code.
When I enter the three editTexts and press the 'submit' button, the app stops with the 'submit' button pressed down. It seems as if it's in some sort of loop - just a guess. So, the toast msg doesn't appear on my screen(Even after I changed my code with what #sunnyday mentioned). The app just seems stalled. It won't even go back to the previous activity.
I am just starting Android and any suggestions would be greatly appreciated.
Java Code:
package activities;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import com.example.chloe.myapplication.R;
public class PayActivity extends ActionBarActivity implements View.OnClickListener {
Button submitButton, flip;
Switch switchButton;
EditText et_totalPeople, et_payPeople, et_amount;
TextView tv_payPeople, tv_people;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pay_numofpeople);
submitButton = (Button) findViewById(R.id.submitButton);
switchButton = (Switch)findViewById(R.id.switchButton);
et_totalPeople= (EditText) findViewById(R.id.et_totalPeople);
et_payPeople= (EditText) findViewById(R.id.et_payPeople);
et_amount = (EditText) findViewById(R.id.et_amount);
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true) {
et_payPeople.setEnabled(false);
et_payPeople.setClickable(false);
} else {
et_payPeople.setEnabled(true);
et_payPeople.setClickable(true);
}
}
});
submitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.submitButton:
int people = Integer.parseInt(et_totalPeople.getText().toString());
//tested with toast here.. didn't work, which means it can't even read from editText
Toast.makeText(getApplicationContext(), people, Toast.LENGTH_LONG).show();
while(people<1 || people>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
et_totalPeople.setSelectAllOnFocus(true);
people = Integer.parseInt(et_totalPeople.getText().toString());
}
if(switchButton.isChecked()==false) { /*Only certain people pay*/
int payer = Integer.parseInt(et_payPeople.getText().toString());
while(payer<1 || payer>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
et_payPeople.setSelectAllOnFocus(true);
payer = Integer.parseInt(et_payPeople.getText().toString());
}
}
int amount = Integer.parseInt(et_amount.getText().toString());
while(amount<1 || amount>10000000) {
Toast.makeText(getApplicationContext(), "Enter values 1~10,000,000", Toast.LENGTH_LONG).show();
et_amount.setSelectAllOnFocus(true);
amount = Integer.parseInt(et_amount.getText().toString());
}
/*now all three values are valid, continue*/
break;
}
}
}
xml code:
<?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/restaurant2_lighter"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:orientation="vertical">
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Everyone pays: "
android:textSize="15dp"
android:textColor="#ffffff"/>
<Switch
android:id="#+id/switchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="NO"
android:textOn="YES"
android:textSize="15dp"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter\nthe number\nof people"
android:textSize="15dp"
android:textColor="#ffffffff"
android:layout_marginRight="40dp"/>
<EditText
android:id="#+id/et_totalPeople"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="ex) 1~99 "
android:layout_gravity="center"
android:background="#4be3cc86"
android:textColor="#ffffffff"
android:padding="10dp"
android:inputType="numberDecimal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" people"
android:textColor="#ffffffff"
android:layout_gravity="center"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/tv_payPeople"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter\nthe number\nof people\nto pay"
android:textColor="#ffffffff"
android:layout_marginRight="40dp"
android:textSize="15dp"/>
<EditText
android:id="#+id/et_payPeople"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="ex) 1~99 "
android:layout_gravity="center"
android:background="#4be3cc86"
android:textColor="#ffffffff"
android:padding="10dp"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/tv_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" people"
android:textColor="#ffffffff"
android:layout_gravity="center"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter\nthe total\namount"
android:textSize="15dp"
android:textColor="#ffffffff"
android:layout_marginRight="40dp"/>
<EditText
android:id="#+id/et_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="ex) 50000 "
android:layout_gravity="center"
android:background="#4be3cc86"
android:textColor="#ffffffff"
android:padding="10dp"
android:inputType="numberDecimal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" won"
android:textColor="#ffffffff"
android:layout_gravity="center"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#6effc118"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="#+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="submit"
android:padding="10dp"
android:textSize="20dp"
android:layout_marginTop="30dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Thank you for your time :)
You get stuck in an endless loop in your validations as soon as a wrong value is entered. Lets say I enter 101 for people then you end up in this loop as soon as you hit the submit button.
while(people<1 || people>100) {
Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
et_totalPeople.setSelectAllOnFocus(true);
people = Integer.parseInt(et_totalPeople.getText().toString());
}
The reason is, that your while loop runs in the UI thread of your app and therefore you cannot enter a different value, because for that to be possible the edit text field would need the UI thread for itself.
To solve this, do something like this (in pseudo code):
onclick() {
if (people < 1 || people > 100) {
showToast();
return;
}
if (other-validation-condition == false) {
showOtherToast();
return;
}
if (yet-another-validation-condition == false) {
showYetAnotherToast();
return;
}
// now, everything should be valid...
continueDoingStuffWithValidatedValues();
}
Toast.makeText(getApplicationContext(), people, Toast.LENGTH_LONG).show();
Must be :
Toast.makeText(getApplicationContext(), String.valueOf(people), Toast.LENGTH_LONG).show();
Toast.makeText(Context, int, long). At there, int is resource id.
Related
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>
Im a little stuck, how can i get a variable from main activity to be displayed on a second activity?
a code example would be great.
Also a second Problem:
How can i create a function in mainactivty when a button is pressed in a second activity?
This is what i have so far, but when i press the button in the second activity, the app crashes.
the button's function needs to be able to change a variable's value in MainActivity and Run a toast saying it was selected.
Main Activity
//SETTING THE DRINK SIZE BASED ON POPUP BUTTONS
public int DrinkSize;
public void SetDrinkSize_Small(View view) {
DrinkSize = 1;
Toast Small = Toast.makeText(getApplicationContext(),
"Drink Size Set To Small",
Toast.LENGTH_SHORT);
Small.show();
}
public void SetDrinkSize_Medium(View view) {
DrinkSize = 2;
Toast Medium = Toast.makeText(getApplicationContext(),
"Drink Size Set To Medium",
Toast.LENGTH_SHORT);
Medium.show();
}
public void SetDrinkSize_Large(View view) {
DrinkSize = 3;
Toast Large = Toast.makeText(getApplicationContext(),
"Drink Size Set To Large",
Toast.LENGTH_SHORT);
Large.show();
}
CustomPopUp.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Small">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Green"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Medium">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Large">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
You can easily send and receive data using intents, as you can see in this Android tutorial: Intent tutorial.
And in case you want to send some data back to the first one, you can use this post: Sending info back.
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 making a Quiz app where I have created 2 buttons ,one for submit and other to move to next Question
I Want to code it in such a way that if I hit Submit button the response gets recorded and doesn't change after it:
My XML Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vidit.project3.question1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/q1"
android:textAlignment="center"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:id="#+id/tv"
/>
<RadioGroup
android:layout_marginTop="20dp"
android:layout_below="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:orientation="vertical"
android:id="#+id/rg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a2" />
<RadioButton
android:id="#+id/correct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/a4"
android:id="#+id/last" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="#+id/rg"
android:id="#+id/submit"
android:text="SUBMIT"
android:layout_marginTop="20dp"
android:onClick="submit1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/submit"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="NEXT"
android:onClick="toQuestion2"/>
</RelativeLayout>
</ScrollView>
Java Code:
package com.example.vidit.project3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
public class question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
public void submit1(View v)
{
RadioButton rb= (RadioButton) findViewById(R.id.correct);
if(rb.isChecked())
{
Toast toast = Toast.makeText(getApplicationContext(),"Whopiee! Correct Answer",Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast = Toast.makeText(getApplicationContext(),"Arghh! Incorrect Answer",Toast.LENGTH_LONG);
toast.show();
}
}
public void toQuestion2(View v)
{
Intent intent = new Intent(this,question2.class);
startActivity(intent);
}
}
You can achieve this by storing the response of answer in Map and need to be check in the map if the Question exists , then disable the options otherwise open the options.
Map<Question,Response> quizResponse = new HashMap();
Now add your all questions one by one in this Map and check
if(quizResponse.contains(question))
{ radioGroup.getChildAt(i).setEnabled(false); // Disable the button
}
else
{
// show the question
quizResponse.put(question, response); // this response can be blank and update once you got user response
}
This will disable all the radio buttons. Is that what you want?
for (int i = 0; i < radioGroup.getChildCount(); i++)
{
radioGroup.getChildAt(i).setEnabled(false);
}
I am adding TextViews to a simple application, running some code to determine what text should show in each one, then displaying it based on what a user previously entered in a bundle.
The problem is no text is showing up at all!
I am running code in the onCreate method that determines what text should show based on passed in values from a previous activity in a bundle. The values seem to pass in without an error and the other Textviews in the activity reflect the changes, but not the income, expenses or incomeExpenses TextView.
Here is my code:
package androidbro.costoflivingcalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.*;
import java.io.Console;
public class FinalGrade extends ActionBarActivity {
//Grade and message strings
public String myGrade;
public String finalMessage;
//Doubles for calculated values
public double rentMortgage;
public double utilities;
public double insurance;
public double phoneInternet;
public double food;
public double carPayment;
public double misc;
public double myIncome;
public double totalExpenses;
public double expensesToIncome;
//Calculate grade based on expenses to income ratio
public String getGrade(double expensesToIncome) {
String grade = "A";
if (expensesToIncome<=0.3) {
grade = "A";
} else if (expensesToIncome<=0.4){
grade = "B";
} else if (expensesToIncome<=0.6) {
grade = "C";
} else if (expensesToIncome<=0.8) {
grade = "D";
} else {
grade = "F";
}
return grade;
}
//Determine final message
public String getFinalMessage(String grade){
String finalMessage;
if (grade == "A"){
finalMessage = "You are living well within your means! You could probably afford to spend more. Either way, great work!";
} else if(grade == "B"){
finalMessage = "You are doing great! Your expenses are well below your income. There are a few areas to improve, but good work!";
} else if(grade == "C") {
finalMessage = "Not too bad, but not great either. You might be in trouble if you lost your income. Try to improve a little!";
} else if(grade == "D") {
finalMessage = "Uh oh! You are spending well over half the money you earn! Consider saving and investing more or it could be trouble.";
} else {
finalMessage = "Oh no! You are spending almost all your income! Save a little and live within your means or else you'll be broke!";
}
return finalMessage;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_grade);
//Income, expenses and bundles
Bundle b = getIntent().getExtras();
rentMortgage = b.getDouble("rentmortgage");
utilities = b.getDouble("utilities");
insurance = b.getDouble("insurance");
phoneInternet = b.getDouble("car");
food = b.getDouble("phone");
carPayment = b.getDouble("food");
misc = b.getDouble("misc");
myIncome = b.getDouble("myincome");
totalExpenses = rentMortgage+utilities+insurance+phoneInternet+food+carPayment+misc;
expensesToIncome = totalExpenses/myIncome;
//Set final message and grade values
myGrade = getGrade(expensesToIncome);
finalMessage = getFinalMessage(myGrade);
//TextViews for setting values
TextView gradeView = (TextView)findViewById(R.id.gradeView);
TextView messageView = (TextView)findViewById(R.id.messageView);
TextView income = (TextView)findViewById(R.id.income);
TextView expenses = (TextView)findViewById(R.id.expenses);
TextView incomeExpenses = (TextView)findViewById(R.id.incomeExpenses);
//Display final grade and message
gradeView.setText(myGrade);
messageView.setText(finalMessage);
income.setText(Double.toString(myIncome));
expenses.setText(Double.toString(totalExpenses));
incomeExpenses.setText(Double.toString(expensesToIncome));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_final_grade, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Display expenses, income and expenses to income percentage on final grade
}
And here is my XML:
<ScrollView
android:layout_width="fill_parent"
android:id="#+id/scrollView"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<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="ericleeconklin.costoflivingcalculator.FinalGrade">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Grade:"
android:id="#+id/textView2"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="A"
android:id="#+id/gradeView"
android:textSize="85dp"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="You are living well within your means! You could probably afford to spend more. Either way, great work!"
android:id="#+id/messageView"
android:layout_below="#+id/gradeView"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Monthly Income:"
android:id="#+id/yourIncome"
android:layout_below="#+id/messageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/income"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/yourIncome"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="sdsdfsd"
android:textColor="#color/abc_background_cache_hint_selector_material_dark"
android:textIsSelectable="true"
android:textSize="23dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Expenses:"
android:id="#+id/yourExpenses"
android:paddingTop="20dp"
android:layout_below="#+id/income"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/yourIncome"
android:layout_alignEnd="#+id/yourIncome"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/incomeExpenses"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/textView4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/expenses"
android:paddingTop="20dp"
android:background="#ff77ff75"
android:width="180dp"
android:height="25dp"
android:layout_alignBottom="#+id/yourExpenses"
android:layout_alignLeft="#+id/incomeExpenses"
android:layout_alignStart="#+id/incomeExpenses"
android:text="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Expenses/Income"
android:id="#+id/textView4"
android:paddingTop="20dp"
android:layout_below="#+id/yourExpenses"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/incomeExpenses"
android:layout_toStartOf="#+id/incomeExpenses"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Here are some tips:"
android:id="#+id/textView5"
android:paddingTop="350dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</ScrollView>
Instead of this:
income.setText(Double.toString(myIncome));
expenses.setText(Double.toString(totalExpenses));
incomeExpenses.setText(Double.toString(expensesToIncome));
Do this:
income.setText(String.valueOf(myIncome);
expenses.setText(String.valueOf(totalExpenses));
incomeExpenses.setText(String.valueOf(expensesToIncome));
I have updated my answer
For passing double value between Activity:
Intent inent = new Intent(thisActivity.this, nextActivity.class);
Bundle b = new Bundle();
b.putDouble("myincome", myIncome); // myIncome is your double value here
intent.putExtras(b);
startActivity(intent);
To get value in next Activity:
Bundle b = getIntent().getExtras();
double myIncome = b.getDouble("myincome");
Check your procedure of passing double value, I have mentioned it above
Edited XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tv_BigHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Your Grade:"
android:textSize="40sp" />
<TextView
android:id="#+id/tv_Grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_BigHeading"
android:layout_centerHorizontal="true"
android:text="A"
android:textSize="80sp"
android:textStyle="bold" />
<TextView
android:id="#+id/messageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_Grade"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="You are living well within your means! You could probably afford to spend more. Either way, great work!"
android:textSize="14sp" />
<TextView
android:id="#+id/yourIncome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/messageView"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="Monthly Income:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/incomeValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/messageView"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/yourIncome"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/yourExpenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/yourIncome"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Expenses:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/expenseValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/incomeValue"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/yourExpenses"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/Expenses_Income"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/yourExpenses"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Expenses/Income:"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/Expenses_Income_Value"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:layout_below="#+id/expenseValue"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/Expenses_Income"
android:background="#ff77ff75"
android:text="1000 $"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/tips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Expenses_Income"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:text="Here are some tips:"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
This will solve your issue now, and use all my recommendations e.g: income.setText(String.valueOf(myIncome);
for setting the textview values