Get Value of a Edit Text field
By using getText():
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
How to continously get values from a Edit Text in android
if you want to get the data continuously from the EditText.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//save it to file here or what ever you want
}
@Override
public void afterTextChanged(Editable s) {}
});
How do I get an EditText input into a variable
In your Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et = (EditText) findViewById(R.id.editText);
String text= et.getEditableText().toString();
}
Android getText from EditText field
Sample code for How to get text from EditText
.
Android Java Syntax
EditText text = (EditText)findViewById(R.id.vnosEmaila);
String value = text.getText().toString();
Kotlin Syntax
val text = findViewById<View>(R.id.vnosEmaila) as EditText
val value = text.text.toString()
How do i take value from EditText with button onclick and show it in TextView
try out this:
Java file:
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
textView=(TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
textView.setText(editText.getText().toString());
}
});
}
}
xml file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:visibility="gone"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Click here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="62dp"
android:id="@+id/button"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="some text"
android:ems="10"
android:layout_alignParentTop="true"
android:id="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
android-can't get value of edit text in fragment
The FirstName EditText is null because the findViewById method is trying to find an editext with the id of FirstName
but the correct id is PatientFirstName
To solve the issue, replace the
final EditText FirstName=(EditText) v.findViewById(R.id.FirstName);
with
final EditText FirstName=(EditText) v.findViewById(R.id.PatientFirstName);
Get EditText input values and display some calculation result in an TextView
Base on your xml layout content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/id_a"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/id_b"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/id_c"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/id_buton_rata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="calculateFunction" />
<TextView
android:id="@+id/id_result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Example #1 (without LiveData
and ViewModel
)
I create some new simple fragment (as example)
public class CalculatorWeightedFragment extends Fragment {
private Button button;
private EditText editTextA;
private EditText editTextB;
private EditText editTextC;
private TextView textViewD;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calculator_w, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
button = view.findViewById(R.id.id_buton_rata);
editTextA = view.findViewById(R.id.id_a);
editTextB = view.findViewById(R.id.id_b);
editTextC = view.findViewById(R.id.id_c);
textViewD = view.findViewById(R.id.id_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double result = calculateResult();
String output = String.format("Result: %s", result);
textViewD.setText(output);
}
});
}
private double calculateResult() {
try {
int a = Integer.parseInt(editTextA.getText().toString());
int b = Integer.parseInt(editTextB.getText().toString());
int c = Integer.parseInt(editTextC.getText().toString());
// Result
return 0.25 * a + 0.25 * b + 0.5 * c;
} catch (NumberFormatException e) {
e.printStackTrace();
return 0;
}
}
}
Example #2 (with LiveData
and ViewModel
)
When you are using LiveData
and ViewModel
, you no need button in your Fragment
. You can observe all of the changes.
Activity where you are creating this fragment. To have ViewModel in Fragment, you can use dependency injection (e.g. using Dagger library) or get it from ViewModelProviders
(or AndroidViewModelFactory
).
Here, to make example as simple as possible, I'm passing it via constructor.
Activity
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CalculatorWeightedViewModel vm = new CalculatorWeightedViewModel();
CalculatorWeightedFragment fragment = new CalculatorWeightedFragment(vm);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frame_layout, fragment);
transaction.commit();
}
}
ViewModel
You can store you values (a
, b
, c
) as fieldsfor different purpose. Here, I'm recalculating result live, without any extra fiels. To make it as simple as possible.
package com.example.stack_android;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class CalculatorWeightedViewModel extends ViewModel {
private MutableLiveData<String> testString = new MutableLiveData<>();
public LiveData<String> takeText() {
return testString;
}
void updateValues(double a, double b, double c) {
double result = recalculate(a, b, c);
String value = "Result: " + result;
testString.postValue(value);
}
void showError() {
testString.postValue("Wrong Value");
}
private double recalculate(double a, double b, double c) {
return 0.25 * a + 0.25 * b + 0.5 * c;
}
}
Fragment:
Additionaly you can create separate listeners (for each of the fields). Here, I'm using one (named TextWatcher listener
) for all of the fields.
public class CalculatorWeightedFragment extends Fragment {
private CalculatorWeightedViewModel viewModel;
private EditText editTextA;
private EditText editTextB;
private EditText editTextC;
private TextView textViewD;
public CalculatorWeightedFragment(CalculatorWeightedViewModel viewModel) {
this.viewModel = viewModel;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calculator_w, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
editTextA = view.findViewById(R.id.id_a);
editTextB = view.findViewById(R.id.id_b);
editTextC = view.findViewById(R.id.id_c);
textViewD = view.findViewById(R.id.id_result);
TextWatcher listener = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textChanged();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Not used
}
@Override
public void afterTextChanged(Editable s) {
// Not used
}
};
editTextA.addTextChangedListener(listener);
editTextB.addTextChangedListener(listener);
editTextC.addTextChangedListener(listener);
viewModel.takeText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(String newValue) {
textViewD.setText(newValue);
}
});
}
private void textChanged() {
try {
int a = Integer.parseInt(editTextA.getText().toString());
int b = Integer.parseInt(editTextB.getText().toString());
int c = Integer.parseInt(editTextC.getText().toString());
viewModel.updateValues(a, b, c);
} catch (NumberFormatException e) {
viewModel.showError();
e.printStackTrace();
}
}
}
It should works like there:
Related Topics
How to Make My Custom Objects Parcelable
"Conversion to Dalvik Format Failed With Error 1" on External Jar
Developing For Android in Eclipse: R.Java Not Regenerating
Android Kotlin: Getting a Filenotfoundexception With Filename Chosen from File Picker
How to Pass Values Between Fragments
Jdbc VS Web Service For Android
How Exactly Does the Android:Onclick Xml Attribute Differ from Setonclicklistener
How to Use External Jars in an Android Project
How to Get Ip Address of the Device from Code
Why Does an Image Captured Using Camera Intent Gets Rotated on Some Devices on Android
How to Enable Multidexing With the New Android Multidex Support Library
How to Use Putextra() and Getextra() For String Data
How to Implement Onbackpressed() in Fragments
How to Stop Edittext from Gaining Focus When an Activity Starts in Android
How to Get the Source Code from an Apk File
Save Arraylist to Sharedpreferences
Null Pointer Exception - Findviewbyid()
Background Task, Progress Dialog, Orientation Change - Is There Any 100% Working Solution