setVisibilty to visible from gone not updated in real time - android

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>

Related

Show tooltip arrow at top of icon which is align end of text in TextView

I have a TextView in which i am setting text dynamically(text length is not fixed ) and end of the text there is a icon,So My question is I want to show tooltip like tooltip arrow should be at top of info icon , as shown in attached image
You can follow this way
XML File
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/ivDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:src="#drawable/ic_done_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tvShipInBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Ship in Box($10 Extra per box)"
android:textColor="#E83535"
app:layout_constraintBottom_toBottomOf="#id/ivDone"
app:layout_constraintStart_toEndOf="#id/ivDone" />
<ImageView
android:id="#+id/ivInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:src="#drawable/ic_info_black_24dp"
app:layout_constraintBottom_toBottomOf="#id/ivDone"
app:layout_constraintStart_toEndOf="#id/tvShipInBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/clToolTip"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:background="#drawable/ic_tooltip_bg"
app:layout_constraintBottom_toTopOf="#id/clMain"
app:layout_constraintHeight_percent="0.075"
app:layout_constraintStart_toStartOf="#id/clMain"
app:layout_constraintWidth_percent="0.65">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginHorizontal="8dp"
android:alpha="0.87"
android:text="A box increases an items volumetric weight and costs more."
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java File
public class MainActivity extends AppCompatActivity {
ImageView ivInfo;
ConstraintLayout clToolTip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivInfo = findViewById(R.id.ivInfo);
clToolTip = findViewById(R.id.clToolTip);
ivInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ivInfo.setVisibility(View.INVISIBLE);
ivInfo.postDelayed(new Runnable() {
public void run() {
clToolTip.setVisibility(View.VISIBLE);
}
}, 7000);
}
});
}
}

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

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>

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);
}
}
});

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