Getting user input from edit text for switch statement to display to textview | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Getting user input from edit text for switch statement to display to textview

can't figure out how to link it all together. i want to display different strings to textview when button is pressed depending on which number 1 through 10 is entered into edit text by user on ui

20th Apr 2018, 11:50 PM
Shon Gosnell
Shon Gosnell - avatar
15 Answers
0
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="enter numbers between 1 - 10 "/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:textColor="#ec070f"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="enter number" android:singleLine="true" android:inputType="number" android:id="@+id/editText"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ok" android:textColor="#fff" android:background="@color/colorPrimary" android:id="@+id/button"/> </LinearLayout>
22nd Apr 2018, 2:57 PM
Eme Ishiwu
Eme Ishiwu - avatar
+ 2
public class MainActivity extends Activity implements View.OnClickListener{ private TextView textView; private Button button; private EditText editText; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); textView = (TextView) findViewById(R.id.text); button = (Button)findViewById(R.id.button); editText = (EditText) findViewById(R.id.editText); button.setOnClickListener(this); } @Override public void onClick(View view) { int id = view.getId(); switch (id){ case R.id.button: String answer = editText.getText().toString(); textView.setText(answer); } } }
22nd Apr 2018, 2:55 PM
Eme Ishiwu
Eme Ishiwu - avatar
+ 2
in your first reply, you are not meant to say "case 10" or "System.Out.println" because : 1) case is an id of what was clicked. eg R.id.button2 2) it is bad practice to use Sytem.out.println in android. you should use "setText()"
23rd Apr 2018, 7:24 PM
Eme Ishiwu
Eme Ishiwu - avatar
+ 1
what I was able to figure out so far is this.. package com.example.myapp import... public class MainActivity extends Activity { @override protected void oncreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView (R.layout.activity_main); public class my class implements View.OnClickListener EditText editText = (EditText)findViewbyid (R.id.editText); Button button = (Button)findViewbyid (R.id.button); TextView text = (TextView)findViewById (R.id.text ); button.setOnClickListener (this); } public void text (View view) { int id =View.getid (); switch (id) { case R.id.button String text_display = editText.getText () text.setText (1); break; default: } } } some things are in red on android studio and some things are not used. red underline: EditText editText = (EditText)findViewbyid(R.id.editText) , (this) in red text: (R.id.text); , View.getid (); , text.setText(1); things not used: text_display , text , (TextView) , (Button) , (myclass) any help would be much thanks
21st Apr 2018, 6:34 PM
Shon Gosnell
Shon Gosnell - avatar
+ 1
sorry for the late reply! i will code it on Android studio and send the code & image to you in an hour
22nd Apr 2018, 1:19 PM
Eme Ishiwu
Eme Ishiwu - avatar
0
thanks so much! the only thing I have left is how would I add more cases to the switch statement for debugging? I tried to add // top line is from your code textView.setText(answer); break; case 10: System.out.println ("successful additional switch case!")
23rd Apr 2018, 6:05 PM
Shon Gosnell
Shon Gosnell - avatar
0
your code works great by the way and I really appreciate it
23rd Apr 2018, 6:05 PM
Shon Gosnell
Shon Gosnell - avatar
0
my bad, I generated the apk and all it did was display to textview whatever I put in editText. how could I just code a switch statement completely in java and have all the outputs written plainly in the switch statement
23rd Apr 2018, 6:43 PM
Shon Gosnell
Shon Gosnell - avatar
0
okay thank you very much. I'm a beginner
23rd Apr 2018, 10:41 PM
Shon Gosnell
Shon Gosnell - avatar
0
that's cool.
25th Apr 2018, 9:03 AM
Eme Ishiwu
Eme Ishiwu - avatar
0
do you mind if i send you a link to a free android online course?
25th Apr 2018, 9:06 AM
Eme Ishiwu
Eme Ishiwu - avatar
0
I don't mind at all
25th Apr 2018, 6:10 PM
Shon Gosnell
Shon Gosnell - avatar
0
I have a code that's giving me trouble button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(view v) { String strinput = editText.getText ().toString (); int final_value = Integer.parseInt (strinput); edittext.setText (final_value / 3.5 ); } }}; } } I know it won't accept a double but I need to find a way to make it work. I need to take an int from editText divide it by a final double and display it to textview. for an example. editText user input = 70 divide user input by 3.5 display output to textview =20 another example editText user input = 100 divide user input by 3.5. display output to textview = 28.57
26th Apr 2018, 6:31 AM
Shon Gosnell
Shon Gosnell - avatar
0
hello, Eme Ishiwu im using android studio, in java language to create an app that finds your lost password to encrypted rar files. i have a keygenerator class, a decoder class, and a possiblekeyfound class. my question is how do i locate the rar file i want to decrypt, and how do i start decrypting it. in my decoder class, i tried using urldecode because the only way i found to locate the file was to save its url, but i got errors everywhere. i heard that you can convert the entire file to binary, and then begin decrypting. what would be my best option? if you could send me working example code, that would be very helpful to learn from
2nd Jun 2018, 6:53 PM
Shon Gosnell
Shon Gosnell - avatar
0
Hello Shon Gosnell, I just saw your massage, is it too late answer it now?
5th Jul 2018, 12:31 AM
Eme Ishiwu
Eme Ishiwu - avatar