EditText to be saved to Integer Arraylist - android

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

Related

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>

ListView is jumping out of screen after touching EditText

I'm making a simple ToDo-List app to learn how to develop Android apps. First, I had an EditText, a normal Button and a ListView. Everything was fine, I could add and delete tasks. Than I wanted to replace the Button with an ImageButton. I made the same constraints. Now, as soon as I touch the EditText, my ListView is jumping up and out of the screen, but a part of it is still on the screen covering my ImageButton, so I can't use it anymore. But if I press "Ok" on the Soft Keyboard, the ListView is jumping back, and than I can use the ImageButton again to add the task. What am I doing wrong?
activity-main.xml:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/editTextHint"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="#string/addTaskButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/editText"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/ic_input_add" />
<ListView
android:id="#+id/listView"
android:layout_width="365dp"
android:layout_height="428dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
MainActivity.java:
ListView lv;
ArrayList<Task> taskList;
TaskArrayAdapter taskAdapter;
EditText editText;
ImageButton btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.listView);
editText = findViewById(R.id.editText);
btn = findViewById(R.id.addButton);
taskList = new ArrayList<>();
handleButton();
taskAdapter = new TaskArrayAdapter(taskList, this);
}
public void handleButton() {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
taskList.add(new Task(editText.getText().toString()));
lv.setAdapter(taskAdapter);
taskAdapter.notifyDataSetChanged();
editText.setText(null);
}
});
}
#Override
public void onCheckedChanged(CompoundButton checkedTask, boolean isChecked) {
int position = lv.getPositionForView(checkedTask);
Task t = taskList.get(position);
t.setSelected(isChecked);
taskList.remove(t);
lv.setAdapter(taskAdapter);
taskAdapter.notifyDataSetChanged();
}
}
Yeah, I'm new to coding in general, so just let me know, if I made some other mistakes. Thank you :)
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_task"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btn_add"
android:hint="Enter task"
/>
<Button
android:id="#+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintTop_toBottomOf="#+id/et_task"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</android.support.constraint.ConstraintLayout>

forwarding to new page based on checkbox input

I am trying to forward to a new layout based on the selection of a checkbox entry.
What would be the process in doing so?
<Button
android:id="#+id/button"
android:layout_width="127dp"
android:layout_height="38dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="PROCEED"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="1.0" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="108dp"
android:layout_marginStart="8dp"
android:layout_marginTop="116dp"
android:text="DOG"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.434"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" /> /
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="108dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:text="CAT"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.424"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/checkBox2" />
</android.support.constraint.ConstraintLayout>
You implement OnCheckedChangeListener. More information here.
yourCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
//launch activity, change fragment, load view,
}
else{
//whatever you want
}
}
});
Or you can set an attribute that change its value according to the checked checkbox and use that attribute in the implementation of what happens when you click the button.

How to loop TextView and PlainText?

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

Android spinner implementation not showing selected item

I have 3 Spinners and I am selecting values from each of them. But when I declare the setOnItemSelected() method outside of the onClickListener() of the next button, The selected value doesn't show up in the Toast. When I declare the setOnItemSelected() method inside the onClickListener of the button, it works but then I can't hide my edittext when I select "Set Limit" from the last spinner.
Please help.
Below is my .java file.
public class AvailabilityActivity extends AppCompatActivity {
private Button next;
private Spinner advanceNotice, shortestTrip, longestDist;
private Bundle bundle;
private EditText setLimit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_availability);
bundle = getIntent().getExtras();
intializeSpinners();
setLimit = (EditText) findViewById(R.id.setlimit);
ArrayAdapter<CharSequence> adapteradvNotice = ArrayAdapter.createFromResource(this, R.array.advNoticeArray, R.layout.spinner_layout);
adapteradvNotice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
advanceNotice.setAdapter(adapteradvNotice);
ArrayAdapter<CharSequence> adaptershortestTrip = ArrayAdapter.createFromResource(this, R.array.shortestTripArray, R.layout.spinner_layout);
adaptershortestTrip.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shortestTrip.setAdapter(adaptershortestTrip);
ArrayAdapter<CharSequence> adapterlongestDist = ArrayAdapter.createFromResource(this, R.array.longestDistanceArray, R.layout.spinner_layout);
adapterlongestDist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
longestDist.setAdapter(adapterlongestDist);
onNextPressed();
}
private void intializeSpinners() {
advanceNotice = (Spinner) findViewById(R.id.acceptAdvanceNotice);
shortestTrip = (Spinner) findViewById(R.id.acceptShortestTrip);
longestDist = (Spinner) findViewById(R.id.acceptLongestTrip);
}
private void onNextPressed() {
next = (Button) findViewById(R.id.nextButton1);
final String[] setLimitText = {""};
final String[] selectedlongestDist = new String[1];
longestDist.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedlongestDist[0] = parent.getItemAtPosition(position).toString();
if (selectedlongestDist[0].equals("Set Limit")){
setLimit.setVisibility(View.VISIBLE);
setLimitText[0] = setLimit.getText().toString();
}
if (selectedlongestDist[0].equals("No Limit")) {
setLimit.setVisibility(View.INVISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
if (setLimitText[0] == "")
setLimitText[0] = selectedlongestDist[0];
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String[] selectedNotice = new String[1];
advanceNotice.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedNotice[0] = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final String[] selectedshortestTrip = new String[1];
shortestTrip.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedshortestTrip[0] = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Toast.makeText(getApplicationContext(), setLimitText[0], Toast.LENGTH_LONG).show();
bundle.putString("advancenotice", selectedNotice[0]);
bundle.putString("shortesttrip", selectedshortestTrip[0]);
bundle.putString("longesttrip", setLimitText[0]);
Intent intent = new Intent(getBaseContext(),ImageActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
Below is my layout 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.example.csci567.dailyrentals.AvailabilityActivity">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="How much advance notice do you need to confirm a trip request?"
android:textColor="#000000"
android:textSize="18dp"
android:layout_marginLeft="15dp"
android:id="#+id/advanceNoticeRequestText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.035"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Advance notice"
android:textSize="14dp"
android:id="#+id/advanceNoticeText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.13"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/acceptAdvanceNotice"
android:hint="Advance Notice"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.18"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Block trips that don't give you enough advance notice."
android:textSize="16dp"
android:id="#+id/blockNoticeText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.26"
android:layout_marginStart="15dp" />
<View android:background="#a8a8a6"
android:layout_width = "0dp"
android:layout_height="1dp"
android:id="#+id/separatorLine1"
android:layout_marginTop="25dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.3"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="How long would you like trips to last?"
android:textSize="18dp"
android:textColor="#000000"
android:id="#+id/tripDurationText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.4"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Shortest possible trip"
android:textSize="14dp"
android:id="#+id/shortestTripText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.46"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/acceptShortestTrip"
android:hint="Enter Shortest Trip"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.52"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Longest Possible trip"
android:textSize="14dp"
android:id="#+id/longestTripText"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.6"
android:layout_marginStart="15dp" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/acceptLongestTrip"
android:hint="Enter Longest Trip"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_marginLeft="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.66"
android:layout_marginStart="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/setlimit"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.76"
android:hint="Enter the value of longest possible trip"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:text="Next"
android:textSize="18dp"
android:textColor="#ffffff"
android:background="#8b36bc"
android:id="#+id/nextButton1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="1.0"/>
</android.support.constraint.ConstraintLayout>
The way you're coding is the problem.
First thing you must change is the place you set listeners. You should avoid setting listeners inside other listeners.
Secondly, the code will execute all the code out of those listeners and then execute the code inside those listeners.
Thirdly, if you wanna show a Toast with a message generated inside a Spinner, you should put it inside the spinner's listener.
Try to reorganize you code thinking of those tips and see if you'll have the problem again.

Categories

Resources