How to loop TextView and PlainText? - android

I am editing the text view and plain text in the xml file always, when the new options comes in HashMAp. But, I am supposed to give the new options or more number of options in Hashmap and that should automatically change the new options in interface.
For example here there are three options in the map
gateway_ip: 162.198.23.10
net_mask: 178.465.37.34
server_ip: 243.798.76.59
I should be able to add more options or edit options in the HashMap and that should be automatically updated in the interface like this.
gateway_ip: 162.198.23.10
net_mask: 178.465.37.34
server_ip: 243.798.76.59
URL: 345.678.456.34
ip : 901.234.123.45
Basically I should change in hashmap that should update in interface automatically. Can anyone help me here ?
[Interface image][1]
/-------------------------MainActivity.java--------------------------/
package com.puchagmail.ganesh.trail2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import java.util.HashMap;
import static android.R.attr.data;
public class MainActivity extends AppCompatActivity {
TextView gateWay ;
TextView netMask;
TextView serverIp;
EditText gW;
EditText nM;
EditText sIp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gateWay = (TextView) findViewById(R.id.textView);
netMask = (TextView) findViewById(R.id.textView2);
serverIp = (TextView) findViewById(R.id.textView3);
gW = (EditText) findViewById(R.id.editText );
nM = (EditText) findViewById(R.id.editText2);
sIp = (EditText) findViewById(R.id.editText3 );
setValues(dummyValues());
}
public void setValues(HashMap<String,String>data){
gW.setText(data.get("gateway_ip"));
nM.setText(data.get("net_mask"));
sIp.setText(data.get("server_ip"));
}
public HashMap<String,String> dummyValues(){
HashMap<String,String> data = new HashMap<>();
data.put("gateway_ip","162.198.23.10");
data.put("net_mask","178.465.37.34");
data.put("server_ip","243.798.76.59");
return data;
}
}
/---------activity_main.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="com.puchagmail.ganesh.trail2.MainActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="66dp"
android:layout_height="39dp"
android:text="net_mask"
android:layout_marginLeft="24dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="34dp"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView"
android:layout_width="77dp"
android:layout_height="39dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="96dp"
android:text="gateway_ip"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="67dp"
android:layout_height="37dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="34dp"
android:text="server_ip"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="44dp"
android:layout_marginTop="78dp"
android:ems="10"
android:inputType="textPersonName"
android:text="192.168.85.100"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/textView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="44dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="textPersonName"
android:text="254.100.34.567"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/textView3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2"
app:layout_constraintVertical_bias="0.078" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="44dp"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPersonName"
android:text="465.798.86.345"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/textView2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</android.support.constraint.ConstraintLayout>

You can use android two way data binding using which you can achieve following things.
1) If you will change in hashmap manually then UI will automatically update.
2) If will enter any thing in edit text that will update in hashmap and on UI as well.
so you may learn about data binding using following link.
https://developer.android.com/topic/libraries/data-binding/index.html

Related

EditText.getText().toString() is not returning current value

I´m trying to get the text that was inserted into a EditText in the UI.
The UI looks like this. In a fragment the EditText for name has a default value "Hello".
After the user has entered a new value (for example "Hello2") I´d like to get the new value when the user clicks the Add Button.
But what I recive is still the default value "Hello".
My Code looks like this:
XML
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Name"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textview_addingredient"
app:layout_constraintVertical_bias="0.050" />
JAVA Code in Fragment
public class AddIngredient extends Fragment{
//Controls
public EditText etN;
public EditText etK;
public EditText etF;
public EditText etC;
public EditText etP;
public TextView tv1;
//ViewModel
AddIngredientViewModel model;
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_addingredient, container, false);
// Get the values
tv1 = rootView.findViewById(R.id.textview_addingredient);
etN = rootView.findViewById(R.id.editText_name);
etK = rootView.findViewById(R.id.editText_kcal);
etF = rootView.findViewById(R.id.editText_fat);
etC = rootView.findViewById(R.id.editText_carbs);
etP = rootView.findViewById(R.id.editText_protein);
// Inflate the layout for this fragment
return rootView;
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.fragmentbutton_addingredient).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Get current values
String name = etN.getText().toString();
int kcal = Integer.parseInt(etK.getText().toString().trim());
int fat = Integer.parseInt(etF.getText().toString().trim());
int carbs = Integer.parseInt(etC.getText().toString().trim());
int protein = Integer.parseInt(etP.getText().toString().trim());
model = new ViewModelProvider(requireActivity()).get(AddIngredientViewModel .class);
Ingredient ingredient = new Ingredient(name, kcal, fat, carbs, protein){};
model.SaveIngredient(ingredient);
}
});
}
}
So has anyone an idea, what I have tho change to recive the current value? Thanks!
-- EDIT --
Entire XML:
<?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="#color/screen_background"
tools:context=".fragments.AddIngredient">
<TextView
android:id="#+id/textview_addingredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Add Ingredient"
android:textColor="#color/headercolor"
android:textSize="#dimen/header_fontsize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.126"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<!-- Name -->
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Name"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textview_addingredient"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
android:text="Hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_name"
app:layout_constraintVertical_bias="0.0" />
<!-- Kcal -->
<TextView
android:id="#+id/textView_kcal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Kcal"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_name"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_kcal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_kcal"
app:layout_constraintVertical_bias="0.0" />
<!-- Fat -->
<TextView
android:id="#+id/textView_fat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Fat"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_kcal"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_fat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_fat"
app:layout_constraintVertical_bias="0.0" />
<!-- Carbs -->
<TextView
android:id="#+id/textView_carbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Carbs"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_fat"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_carbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_carbs"
app:layout_constraintVertical_bias="0.0" />
<!-- Protein -->
<TextView
android:id="#+id/textView_protein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Protein"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.078"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_carbs"
app:layout_constraintVertical_bias="0.052" />
<EditText
android:id="#+id/editText_protein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_protein"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/fragmentbutton_addingredient"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/button_background"
android:gravity="left|center_vertical"
android:padding="20dp"
android:text="Add"
android:textColor="#color/button_foreground"
android:textSize="#dimen/button_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.243"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_protein"
app:layout_constraintVertical_bias="0.250" />
</androidx.constraintlayout.widget.ConstraintLayout>
Remove the default static text set to the editTextName field in your xml layout file. Due to this, the value is always getting stored as Hello. You shouldnt set text in EditTextview.
<EditText
android:id="#+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
android:text="Hello" //Remove this line.

Successfull Build in Android Studio still crashes on Device

I'm pretty much a beginner still and I've tried to make an App to ease my work in the Lab. It's just some basic calculation. I want to make an App that is compatible to the most devices, in case its useful information. Even though the App seems to Build successfully, as soon as it starts on my Device it just crashes immediatelly. Files used are listed below. I tried all the hints Android Studio gave me with no result. App still crashed.
Would be very thankful for some hints and constructive feeback!
Main Activity.java
package com.e.concalc;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText protein_sp;
private EditText salt_sp;
private EditText protein_st;
private EditText salt_st;
private EditText volume_sp;
private TextView tv_resultH2O;
private TextView tv_resultSalt;
private TextView tv_resultProtein;
private Button button1;
public MainActivity(TextView tv_resultH2O, TextView tv_resultSalt, TextView tv_resultProtein, Button button1) {
this.tv_resultH2O = tv_resultH2O;
this.tv_resultSalt = tv_resultSalt;
this.tv_resultProtein = tv_resultProtein;
this.button1 = button1;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protein_sp = findViewById(R.id.edit1);
protein_st = findViewById(R.id.edit2);
salt_sp = findViewById(R.id.edit3);
salt_st = findViewById(R.id.edit4);
volume_sp = findViewById(R.id.edit5);
button1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
makeCalculations();
}
});
}
private void makeCalculations() {
double p_sp = Double.valueOf(protein_sp.getText().toString());
double p_st = Double.valueOf(protein_st.getText().toString());
double s_sp = Double.valueOf(salt_sp.getText().toString());
double s_st = Double.valueOf(salt_st.getText().toString());
double v_sp = Double.valueOf(volume_sp.getText().toString());
double resultH2O;
double resultSalt;
double resultProtein;
resultProtein = p_sp * v_sp / p_st;
resultSalt = s_sp * v_sp / s_st;
resultH2O = v_sp - resultProtein - resultSalt;
tv_resultH2O.setText(Double.toString(resultH2O));
tv_resultSalt.setText(Double.toString(resultSalt));
tv_resultProtein.setText(Double.toString(resultProtein));
}
}
activity_main.xml - Layout
<?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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.77"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/SampleVolume"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.35" />
<EditText
android:id="#+id/edit1"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toBottomOf="#+id/text1"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit2"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toBottomOf="#+id/text2"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit4"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text4"
app:layout_constraintTop_toBottomOf="#+id/text4"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit3"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toBottomOf="#+id/text3"
android:importantForAutofill="no" />
<EditText
android:id="#+id/edit5"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text5"
app:layout_constraintTop_toBottomOf="#+id/text5"
android:importantForAutofill="no" />
<Button
android:id="#+id/button1"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginBottom="128dp"
android:text="#string/button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:text="#string/button2"
app:layout_constraintBottom_toBottomOf="#+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.745"
app:layout_constraintStart_toEndOf="#+id/button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/button1" />
<TextView
android:id="#+id/tv_resultH2O"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultSalt"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultProtein"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.9"
app:layout_constraintVertical_bias="0.6"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You forgot to findViewById the button1.So in onCreate method, before button1.setOnClickListener.... add this line:
button1 = findViewById(R.id.button1);
Remove your custom constructor.
Activities must have a no-argument constructor that Android uses to create your class and when you define any override then the no-argument constructor is no longer automatically created for you in Java...

EditText to be saved to Integer Arraylist

I am trying to get numeric user input on an EditText and I want it to be saved to an Integer Arraylist. I have code here that has numbers added programatically to an arraylist called number. I have also added Textviews to display the entire array and the first element of the array to see if it is good.
What I need help with is the syntax for saving user input into the EditText to the same Arraylist. I have searched this site and found nothing suitable.
I would also like to see the result in the Textview to make sure its working.
enter code hereI am trying to get numeric user input on an EditText and I want it to be saved to an Integer Arraylist. I have code here that has numbers added programatically to an arraylist called number. I have also added Textviews to display the entire array and the first element of the array to see if it is good.
What I need help with is the syntax for saving user input into the EditText to the same Arraylist.
I would also like to see the result in the Textview to make sure its working.
enter code here #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Integer> number = new ArrayList<Integer>();
look = (TextView)findViewById(R.id.tv1);
look2 = (TextView)findViewById(R.id.tv2);
setUIViews();
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display1.setText("1");
}
});
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display1.setText("2");
}
});
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
display1.setText("3");
}
});
number.add(1);
number.add(22);
number.add(45);
number.add(17);
number.add(0,7);
for (int i=0; i < number.size(); i++){
look2.setText(look2.getText() +" " + number.get(i) + " , ");
look.setText("First element is: "+number.get(0));
}
}
private void setUIViews (){
one = (Button)findViewById(R.id.btn1);
display1 = (EditText)findViewById(R.id.et1);
two = (Button)findViewById(R.id.btn2);
display1 = (EditText)findViewById(R.id.et1);
three = (Button)findViewById(R.id.btn3);
display1 = (EditText)findViewById(R.id.et1);
}
}
enter code here
enter code here<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=".MainActivity">
<EditText
android:id="#+id/et1"
android:layout_width="323dp"
android:layout_height="58dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.017" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="244dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="244dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:inputType="number"
android:text="1"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.212" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:inputType="number"
android:text="2"
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.212" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:inputType="number"
android:text="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.921"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.212" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.355" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.416" />
<Button
android:id="#+id/btnview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="View Arraylist"
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.798" />
enter code here
What I need help with is the syntax for saving user input into the EditText to the same Arraylist.
I think you mean this:
number.add(Integer.parseInt(display1.getText().toString()));
You Can handle EditText.AddonTextChangeListner and cast every 'char' to Int and add it to array and remove in case of deleting i think this will be efficient

setVisibilty to visible from gone not updated in real time

I'm trying a simple act of changing a layout visibility from 'gone' to 'visible'.
the flow is quite simple, clicking on a 'fab' will change the visibilty of a specific layout (in this case: 'threeVal_layout') from gone to visible.
For some reason, the code is working when running without debugging(which means the layout does change to visible), but once I use debug mode the layout is not updated after the code is executed, only after I return to the screen and click on the fab one more time.
I do see the error attached in the image inside View.class, once I step in the 'setVisiblty' function.
my code:
public class SubmitStringsActivity extends AppCompatActivity{
RandomFunctions randomFunc = new RandomFunctions();
EditText firstVal,secVal,thirdVal;
TextInputLayout firstVal_layout,secVal_layout;
public static TextInputLayout threeVal_layout;
FloatingActionButton btn_go,btn_add;
Integer minValOpt= 0;
String firstChoErr,secChoErr,strResult;
public static String[] choicesArr = new String[2]; //Initialize array for 2 since minimum options is 2
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_strings);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*First Value*/
firstVal = findViewById(R.id.firstVal);
firstVal_layout = findViewById(R.id.firstVal_layout);
/*Second Value*/
secVal = findViewById(R.id.secVal);
secVal_layout = findViewById(R.id.secVal_layout);
/*Third Value*/
thirdVal = findViewById(R.id.thirdVal);
threeVal_layout = findViewById(R.id.threeVal_layout);
btn_add = findViewById(R.id.fab_add);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
threeVal_layout.setVisibility(View.VISIBLE);
threeVal_layout.invalidate();
RandomFunctions.addChoiceOption();
}
}); btn_go = findViewById(R.id.button);
btn_go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DisplayResultActivity.class);
// Clean any errors
firstVal.setError(null);
// minVal_layout.setErrorEnabled(false);
secVal_layout.setError(null);
// maxVal_layout.setErrorEnabled(false);
// _End_
if (firstVal.getText().toString().equals("")) {
firstChoErr= getString(R.string.emptyChoiceStr); /**Get the error string from the res*/
firstVal_layout.setError(firstChoErr); /**Set error in case minimum value is empty*/
return;
}
else if (secVal.getText().toString().equals("")){
secChoErr= getString(R.string.emptyChoiceStr);
secVal_layout.setError(secChoErr); /**Set error in case maximum value is empty*/
return;
}
else {
/** Called when the user taps the Go button */
choicesArr[0] = (firstVal.getText().toString());
choicesArr[1] = (secVal.getText().toString());
int resVal = randomFunc.calculateNums(minValOpt, choicesArr.length-1); //Send minimum and maximum values to random function
strResult = choicesArr[resVal];
intent.putExtra("intValName", strResult);
startActivity(intent);
//finish();
}
}
}
);
}
}
XML:
<LinearLayout
android:id="#+id/linearStringLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:background="#color/mainBackGroundHalf1"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputLayout
android:id="#+id/firstVal_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
app:layout_constraintEnd_toEndOf="#+id/linearStringLayout"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/linearStringLayout">
<EditText
android:id="#+id/firstVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#color/inputText"
android:ems="10"
android:hint="#string/firstString"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLength="9"
android:selectAllOnFocus="false"
android:singleLine="true"
android:textAlignment="center"
android:textAppearance="#android:style/TextAppearance.Material"
android:textColorHint="#color/inputText"
app:layout_constraintBottom_toBottomOf="#+id/linearStringLayout"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/secVal_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
app:layout_constraintEnd_toEndOf="#+id/linearStringLayout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/firstVal_layout">
<EditText
android:id="#+id/secVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:backgroundTint="#color/inputText"
android:ems="10"
android:hint="#string/secondString"
android:inputType="text"
android:maxLength="9"
android:selectAllOnFocus="false"
android:singleLine="true"
android:textAlignment="center"
android:textAppearance="#android:style/TextAppearance.Material"
android:textColorHint="#color/inputText" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/threeVal_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#+id/linearStringLayout"
app:layout_constraintEnd_toEndOf="#+id/linearStringLayout"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/secVal_layout">
<EditText
android:id="#+id/thirdVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#color/inputText"
android:ems="10"
android:hint="#string/threeString"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLength="9"
android:selectAllOnFocus="false"
android:singleLine="true"
android:textAlignment="center"
android:textAppearance="#android:style/TextAppearance.Material"
android:textColorHint="#color/inputText"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#+id/linearLayout"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>

One of the Android Button work while the other does not

I did my best to solve the problem and I hope someone would be able to help me finding a solution.
I delcared the following layout for my activity:
<?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"
android:layout_gravity="center"
android:background="#android:color/holo_orange_light"
android:visibility="visible"
tools:context="com.example.youssef.mylocation.Main2Activity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/menumessage"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText1"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="128dp"
android:ems="10"
android:hint="#string/phone_number"
android:inputType="phone"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="196dp"
android:ems="10"
android:hint="#string/address"
android:inputType="textPersonName"
android:textColor="#android:color/holo_orange_dark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="#string/place_name"
android:inputType="textPersonName"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="340dp"
android:textColor="#color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="24dp"
android:textColor="#color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<Button
android:id="#+id/CancelBtn"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginBottom="40dp"
android:layout_marginStart="32dp"
android:background="#android:color/holo_red_dark"
android:text="#string/cancel"
android:onClick="Cancelonbuttonclickfunc"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="#string/city"
android:inputType="textPersonName"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<TextView
android:id="#+id/textView6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="144dp"
android:text="Required !"
android:textColor="#android:color/holo_red_dark"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/editText1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/AddBtn"
android:layout_width="124dp"
android:layout_height="60dp"
android:layout_marginStart="96dp"
android:layout_marginTop="464dp"
android:text="Add"
android:background="#color/Color"
android:onClick="buttonClickFunction"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/CancelBtn"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The related java code is the following:
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.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
TextView textView;
TextView laltitude;
TextView longitude;
Bundle bundle;
Double lal;
Double longt;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=(TextView)findViewById(R.id.textView6);
editText = (EditText) findViewById(R.id.editText1);
laltitude =(TextView)findViewById(R.id.textView2);
longitude =(TextView)findViewById(R.id.textView3);
bundle = getIntent().getExtras();
lal = bundle.getDouble("laltitude");
longt = bundle.getDouble("longitude");
laltitude.setText("laltitude : " + lal.toString());
longitude.setText("longitude : " + longt.toString());
//hhhhhhhhhhhhhhhhh going back to main activity
// CancelButton.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
//
// }
// });
//hhhhhhhhhhhhhhh
// AddButton.setOnClickListener({});
// if (editText.getText() == null)
// textView.setVisibility(View.VISIBLE);
// else
// textView.setVisibility(View.INVISIBLE);
// });
}
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG);
if (editText.getText().toString() == "")
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
public void Cancelonbuttonclickfunc(View view) {
startActivity(new Intent(Main2Activity.this, MainActivity.class));
}
}
The problem is that only one button seems to work (cancelbutton) but the
but the add button does not seem work.
I did declare the button and use the findViewById method but no luck so far.
In your button click method you're comparing strings using the == operator which tests for reference equality and not value equality. This is why your textView's visibility never get set to VISIBLE. To check for value equality you should use equals method instead, like this:
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG).show();
if (editText.getText().toString().equals(""))
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
Note that you also forgot to put .show() at the end of your Toast.
Try this
mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String text= edittext.getText().toString().trim();
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
if (text.length() <= 0) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.INVISIBLE);
}
}
});

Categories

Resources