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>
Related
First of all, I wanna say sorry about my English.
I am trying to build my first android application using android studio. I created the main page containing two taps one for login activity and the other one for the About section.
what I did exactly was create the Login activity for the fragments to hold the tabs and the basic design and one layout only without the java file for the about_tab_fragment section and finally the login_tab activity.
the problem is that I can not get the input data from the view and no action happened when I click on the login button.(and there is no error in the Run).
and this is my code:
Login activity (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"
tools:context=".Login">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="#drawable/login_header"
app:layout_constraintHeight_percent=".27"
android:scaleType="centerCrop"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent=".78"
app:layout_constraintVertical_bias="1"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintHeight_percent=".7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tab_layout"/>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/google_img"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/facebook_img"
app:layout_constraintHorizontal_bias="1"
android:layout_marginRight="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/fab_google"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_insta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/insta_img"
app:layout_constraintHorizontal_bias="0"
android:layout_marginLeft="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fab_google"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Login activity (Java)
public class Login extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
FloatingActionButton facebook,google,instagram;
float v = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.view_pager);
facebook = findViewById(R.id.fab_facebook);
google = findViewById(R.id.fab_google);
instagram = findViewById(R.id.fab_insta);
tabLayout.addTab(tabLayout.newTab().setText("Login"));
tabLayout.addTab(tabLayout.newTab().setText("About"));
tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);
final LoginAdapterClass adapter = new LoginAdapterClass(getSupportFragmentManager(),this,tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//For animations
facebook.setTranslationY(300);
google.setTranslationY(300);
instagram.setTranslationY(300);
tabLayout.setTranslationY(300);
facebook.setAlpha(v);
google.setAlpha(v);
instagram.setAlpha(v);
tabLayout.setAlpha(v);
facebook.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(400).start();
google.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(600).start();
instagram.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(800).start();
tabLayout.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(100).start();
}
}
about_tab_fragment (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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Login_tab (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"
tools:context=".LoginTab">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="24dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_email_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_lock_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<Button
android:id="#+id/loginBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="16sp"
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_constraintVertical_bias="0.758"
app:layout_constraintWidth_percent="0.8" />
<TextView
android:id="#+id/forgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintVertical_bias="0"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
My problem occurs here:
I can not take the text from the inputs email and password plus that I can not add event to the loginBtn. I tried to put Toast in the On Click Listener but nothing happened.
Login_tab (Java)
public class LoginTab extends AppCompatActivity {
EditText emailLogin,passwordLogin;
Button loginBtn;
TextView forgetPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_tab);
emailLogin = findViewById(R.id.email);
passwordLogin = findViewById(R.id.password);
loginBtn = findViewById(R.id.loginBtn);
forgetPassword = findViewById(R.id.forgetPassword);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Login.this,"This is the login button",Toast.LENGTH_LONG).show();
if(emailLogin.getText().toString().isEmpty()){
emailLogin.setError("Email is required");
return;
}
if(passwordLogin.getText().toString().isEmpty()){
passwordLogin.setError("Password is required");
return;
}
}
});
}
}
Can any one help me to fix the problem.
I did a lot of searches but I could not find any answer.
I found the problem.
the problem because I have to put the action in the class loginTab that extends Fragment not AppCompatActivity.
public class LoginTabFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_login_tab,container,false);
Button b = root.findViewById(R.id.loginBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(root.getContext(), "This is the Login button....",Toast.LENGTH_LONG).show();
}
});
return root;
}
}
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);
}
});
}
}
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
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>
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.