I am trying to make a projects which reminds users for their vaccines. I have made some progress and have created birth date forms using EditText. I just can't seem to find how to save those strings to a file so when the user closes the app they are saved. Here is the code. There are two activities. I am trying to save dita1 muaji 1 and viti1 which are on the second activity.
---------------------------------First Activity----------------------------------------------------------------------------------
package al.programming.erlisciko.vaksinimi;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyVaccines extends AppCompatActivity {
public Button vendosni_ditelindjen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_vaccines);
OnClickButtonListener();
}
public void OnClickButtonListener(){
vendosni_ditelindjen = (Button)findViewById(R.id.vendosni_ditelindjen);
vendosni_ditelindjen.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent set = new Intent ("al.programming.erlisciko.vaksinimi.SetDate");
startActivity(set);
}
}
);
}
}
----------------------------Second Activity-------------------------------------
package al.programming.erlisciko.vaksinimi;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class SetDate extends AppCompatActivity {
public Button ruani;
public EditText dita, muaji, viti;
public String dita1, muaji1, viti1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_date);
ruani = (Button)findViewById(R.id.ruani);
dita = (EditText)findViewById(R.id.dita);
muaji = (EditText)findViewById(R.id.muaji);
viti = (EditText)findViewById(R.id.viti);
ruani.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Log.v("dita1", dita.getText().toString());
Log.v("muaji1", muaji.getText().toString());
Log.v("viti1", viti.getText().toString());
}
});
System.out.print(dita1);
}
}
-------------------------------XML 1--------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="al.programming.erlisciko.vaksinimi.MyVaccines">
<Button
android:id="#+id/vendosni_ditelindjen"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:text="Vendosni Ditëlindjen"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.481"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.984" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ju lutem vendosni ditëlindjen"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
--------------------------------XML 2-------------------------------------------
<?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="al.programming.erlisciko.vaksinimi.SetDate">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vendosni Ditëlindjen"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.497" />
<EditText
android:id="#+id/dita"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Dita"
android:inputType="date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.229" />
<EditText
android:id="#+id/muaji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Muaji"
android:inputType="date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.431" />
<EditText
android:id="#+id/viti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Viti"
android:inputType="date"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.647" />
<Button
android:id="#+id/ruani"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Ruani Datën"
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.914" />
</android.support.constraint.ConstraintLayout>
Thank You
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences
Write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("Name", yourname);
editor.commit();
Read from Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String name = sharedPref.getString("Name", "");
Related
I have three passages in my scrollview that need to each become visible after an onclick event on one of three buttons.
I have currently set them to all invisible. And since I cannot get it to work, I am only trying it out with one of the passages.
Because of this I created a private textview constant for only the first passage. But after I pass the intent to switch the activity, I also try to turn the view on that package to visible.
I have included my MainActivity.java and the xml file I used to set invisible.
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView passage1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passage1 = findViewById(R.id.passage_1);
}
public void launchPassageOne(View view) {
passage1.setVisibility(view.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageTwo(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageThree(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
}
<?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"
tools:context=".PassageActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/passage_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage1"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage2"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage3"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My program just crashes. And I cannot find any error messages.
How can I make the packages visible whenever I want the activity to change?
There are three passages that I want to each become visible for the respective button, then turn invisible if the back button is pressed.
It seams the three views are in the started activity. And so you can't change their visibility because they haven't been created.
Add this before you start the activity
intent.putExtra("passageNum", 1)
Then call startActivity(intent)
In PassageAactivity onCreate do the following :
If (getIntent().hasExtra("passageNum") && getIntent().getExtras().getInt("passageNum") == 1)
passage1.setVisibility(View.VISIBLE)
And so on for the other views
passage1.setVisibility(View.VISIBLE)
read more about views and how to modify their behavior here :
https://developer.android.com/reference/android/view/View
Use View.VISIBLE, capital V, it's a integer constant from the View class. Remove the View argument from the method launchPassageOne:
public void launchPassageOne() {
passage1.setVisibility(View.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
Image click hereWhatever I understood with your code I got that you are not initializing your methods in On create, whatever defined outside the On create will not be used until or unless called from inside On create method.
Designed some code may help you understanding in a better way.
In below code, I made text views scrollable, but you can only scroll if text is too long to fill the entire textview.
MainActivity.java
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edit1;
EditText edit2;
EditText edit3;
Button btn1;
Button btn2;
Button btn3;
Button btnV;
Button btnI;
TextView t1;
TextView t2;
TextView t3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// defining id for edit texts
edit1=findViewById(R.id.passage_1);
edit2=findViewById(R.id.passage_2);
edit3=findViewById(R.id.passage_3);
// defining id for buttons
btn1=findViewById(R.id.button_1);
btn2=findViewById(R.id.button_2);
btn3=findViewById(R.id.button_3);
btnV=findViewById(R.id.btnvisi);
btnI=findViewById(R.id.btninvisi);
// defining id for text views
t1=findViewById(R.id.textview1);
t2=findViewById(R.id.textview2);
t3=findViewById(R.id.textview3);
// making text views scrollable
t1.setMovementMethod(new ScrollingMovementMethod());
t2.setMovementMethod(new ScrollingMovementMethod());
t3.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setText(edit1.getText().toString());
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t2.setText(edit2.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t3.setText(edit3.getText().toString());
}
});
btnV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Making passages Visible
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.VISIBLE);
t3.setVisibility(View.VISIBLE);
}
});
btnI.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setVisibility(View.INVISIBLE);
t2.setVisibility(View.INVISIBLE);
t3.setVisibility(View.INVISIBLE);
}
});
}
}
Set activitymain.xml as below
<?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="#2196F3"
tools:context=".MainActivity">
<EditText
android:id="#+id/passage_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="passage 1"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
<EditText
android:id="#+id/passage_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Passage 2"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.102" />
<EditText
android:id="#+id/passage_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Passage 3"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.194" />
<Button
android:id="#+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_1"
app:layout_constraintTop_toTopOf="#+id/passage_1" />
<Button
android:id="#+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_2"
app:layout_constraintTop_toTopOf="#+id/passage_2" />
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_3"
app:layout_constraintTop_toTopOf="#+id/passage_3" />
<TextView
android:id="#+id/textview1"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 1"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.446"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview2"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 2"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview3"
android:layout_width="319dp"
android:layout_height="74dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:hint="Passage 3"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.895"
tools:visibility="invisible" />
<Button
android:id="#+id/btnvisi"
android:layout_width="175dp"
android:layout_height="44dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Passage Visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.036"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="#+id/btninvisi"
android:layout_width="174dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="passage invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.886"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
</androidx.constraintlayout.widget.ConstraintLayout>
I hope it makes you understand in a better way,
Thanks
I am quite new to android programming so thanks advance for helping.
On button click, I am trying store the boolean values and a string value into SharedPreferences. After storing it, it will update the mainActivty.xml page. However, after many tries and hours of looking up, i cannot seem to find a way to get rid of the Null Pointer Exception when trying to .setText() the string value. Any help will be much appreciated, thanks
I have tried to create an inflater since some of the issue arise with the view not getting inflated. I have also tried to reassign the location for my textView initialization but to no avail.
This is the SettingPage code:
package com.example.sleep;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class SettingPage extends AppCompatActivity{
public EditText setMinBar;
public Button setTimeEnterButton;
public Switch musicYesSwitch, soundYesSwitch;
public String SP = "New SP", timeStr = "nothing", finalTimeStr = "nil";
public boolean musicYes, soundYes;
public TextView minLeftTV, timerEndsTV;
public SharedPreferences varAcrossApp;
public SharedPreferences.Editor varEditor;
#Override //Creating the setting page
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settingpage);
setMinBar = findViewById(R.id.numMinsBar);
setTimeEnterButton = findViewById(R.id.setTimeButton);
musicYesSwitch = findViewById(R.id.musicSwitch);
soundYesSwitch = findViewById(R.id.soundSwitch);
timerEndsTV = findViewById(R.id.timerEndsTV);
minLeftTV = (TextView) findViewById(R.id.minsLeftDisplay);
varAcrossApp = PreferenceManager.getDefaultSharedPreferences(this);
setTimeEnterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
saveData();
loadData();
updateView();
}
});
}
public void saveData(){ //Declaring method saveData
varEditor = varAcrossApp.edit();
varEditor.putString(timeStr, setMinBar.getText().toString());
varEditor.putBoolean("mYes", musicYesSwitch.isChecked());
varEditor.putBoolean("sYes", soundYesSwitch.isChecked());
varEditor.apply();
}
public void loadData(){ //Declaring method loadData
finalTimeStr = varAcrossApp.getString(timeStr, "0");
musicYes = varAcrossApp.getBoolean("mYes", false);
soundYes = varAcrossApp.getBoolean("sYes", false);
Toast.makeText(getApplicationContext(), "Duration set" +
finalTimeStr, Toast.LENGTH_SHORT).show();
}
public void updateView() {
Log.d(SP, "Time is " + finalTimeStr);
minLeftTV.setText(finalTimeStr);
if (musicYes && soundYes) {
timerEndsTV.setText("-Turn off music /n-Change sound profile to
'Sound'");
} else if (soundYes = true) {
timerEndsTV.setText("-Change sound profile to 'Sound'");
} else if (musicYes = true) {
timerEndsTV.setText("-Turn off music");
} else {
timerEndsTV.setText("-Do nothing");
}
}
}
This is the Homepage.xml page
<?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:id="#+id/homepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<TextView
android:id="#+id/timeLeft"
android:layout_width="191dp"
android:layout_height="49dp"
android:layout_marginStart="146dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="147dp"
android:fontFamily="#font/bree_serif"
android:text="#string/text_header"
android:textAlignment="center"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/timeLeftNum"
android:layout_width="84dp"
android:layout_height="52dp"
android:layout_marginTop="143dp"
android:layout_marginEnd="103dp"
android:layout_marginBottom="138dp"
android:fontFamily="#font/bree_serif"
android:text="#string/text_timeUnit"
android:textAlignment="center"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/timerEndsTV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeLeft" />
<TextView
android:id="#+id/whenTimerEnd"
android:layout_width="132dp"
android:layout_height="20dp"
android:layout_marginStart="54dp"
android:layout_marginEnd="253dp"
android:layout_marginBottom="12dp"
android:fontFamily="#font/bree_serif"
android:text="#string/text_whenTimerEnd"
android:textAlignment="viewStart"
app:layout_constraintBottom_toTopOf="#+id/timerEndsTV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/timerEndsTV"
android:layout_width="299dp"
android:layout_height="54dp"
android:layout_marginStart="54dp"
android:layout_marginEnd="58dp"
android:layout_marginBottom="114dp"
android:fontFamily="#font/bree_serif"
android:text="TextView"
app:layout_constraintBottom_toTopOf="#+id/startButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/startButton"
android:layout_width="382dp"
android:layout_height="52dp"
android:layout_marginStart="13dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:fontFamily="#font/bree_serif"
android:text="#string/text_startButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/minsLeftDisplay"
android:layout_width="155dp"
android:layout_height="144dp"
android:layout_marginStart="68dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="107dp"
android:text="#string/numberInDisplay"
android:textAlignment="textEnd"
android:textSize="125sp"
app:layout_constraintBottom_toTopOf="#+id/whenTimerEnd"
app:layout_constraintEnd_toStartOf="#+id/timeLeftNum"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeLeft" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the SettingPage.xml page:
<?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:id="#+id/settingPageConstrainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/inputLayout"
android:layout_width="299dp"
android:layout_height="81dp"
android:layout_marginStart="56dp"
android:layout_marginEnd="56dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TextboxDurationMins">
<EditText
android:id="#+id/numMinsBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/setTimeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/bree_serif"
android:text="#string/setTimeButton" />
</LinearLayout>
<LinearLayout
android:id="#+id/switchesLayout"
android:layout_width="64dp"
android:layout_height="69dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="218dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<Switch
android:id="#+id/musicSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="#+id/soundSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/textBoxLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginEnd="69dp"
android:layout_marginBottom="230dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/switchesLayout"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/turnOffMusics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/bree_serif"
android:text="#string/offMusic"
android:textAlignment="textStart"
android:textSize="18sp" />
<TextView
android:id="#+id/onSound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/bree_serif"
android:text="#string/onSound"
android:textAlignment="textStart"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:id="#+id/TextboxDurationMins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="176dp"
android:layout_marginEnd="120dp"
android:fontFamily="#font/bree_serif"
android:text="#string/setDurationMins"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the error code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sleep, PID: 16835
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null object
reference
at com.example.sleep.SettingPage.updateView(SettingPage.java:82)
at com.example.sleep.SettingPage$1.onClick(SettingPage.java:53)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3400(View.java:801)
at android.view.View$PerformClick.run(View.java:27301)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
The issue is related to minLeftTV.setText(finalTimeStr);
You are creating an instance by calling
minLeftTV = (TextView) findViewById(R.id.minsLeftDisplay);
but your xml layout does not have any view with that id - #+id/minsLeftDisplay and hence minLeftTV is null and you are getting a null pointer exception.
You have a TextView with id => #+id/TextboxDurationMins which is the view you want to set the text to (I am assuming). Change the id in layout or your activity code.
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 have done an immense amount of research and for whatever reason whatever I try I cannot get an ImageButton to be clickable in Android studio. I have tried numerous things but I must be missing something. I will past XML file below and then Java below that. When I put setOnClickListener method I get a cannot resolve message and same for when I do onClickListener. I would like the button to link to a webpage. Please help!
<?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.autismacademyed.www.autismacademy.AutismAcademy">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.173" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<ImageButton
android:id="#+id/imageButtonYellow"
android:layout_width="109dp"
android:layout_height="125dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#null"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:srcCompat="#mipmap/ic_yellowpuzzlepiece"
android:onClick="onClick"/>
</android.support.constraint.ConstraintLayout>
Here is Java:
package com.autismacademyed.www.autismacademy;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
ImageButton imageButtonYellow = (ImageButton)findViewById(R.id.imageButtonYellow);
imageButtonYellow.setOnClickListener(new View.onClickListener()
public void onClick (View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
Remove the setOnClickListener since you already specify in your view that the onclick function for the button is onClick. To avoid confusion rename your button android:onClick="onClick", like android:onClick="imageButtonOnClick".
And in your java code you can just use this
package com.autismacademyed.www.autismacademy;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
public void imageButtonOnClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
Just copy and paste this two file will work for you. You declare two click listener that's why its not working.
xml file
<?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.autismacademyed.www.autismacademy.AutismAcademy">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.173" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<ImageButton
android:id="#+id/imageButtonYellow"
android:layout_width="109dp"
android:layout_height="125dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:background="#null"
android:scaleType="centerCrop"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:srcCompat="#mipmap/ic_yellowpuzzlepiece"
android:onClick="buttonClick"/>
</android.support.constraint.ConstraintLayout>
jAVAFILE
public class AutismAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_autism_academy);
}
public void buttonClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.aaed.org"));
startActivity(browserIntent);
}
}
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.