get EditText value from onclick function - java

How do I get the value of my EditText that the user typed in from the function that was called when the onClick was pressed. I tried this below but its not working. Thanks for the help.
<EditText android:id="#+id/myEditT" />
<Button android:text="MY BUTTON" android:onClick="GetThis" />
public void GetThis(View view) {
EditText x = (EditText)parentView.findViewById( R.id.myEditT);
// alert the x variable
}

EditText x = (EditText) findViewById(R.id.myEditT);
String your_text = x.getText().toString();

In XML,
<EditText android:id="#+id/myEditT"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button android:id="#+id/myButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
In Java,
public void onCreate(Bundle saved){
super.onCreate(saved);
setContentView(R.layout.your_xml);
Button btn = (Button) findViewById(R.id.myButton);
EditText edtText = (EditText) findViewById(R.id.myEdit);
btn.setOnClickListener(new onClickListener(
public void onClick(View v){
String value = edtText.getText().toString();
}
));
}

public void GetThis(View view) {
EditText x = (EditText)view.findViewById(R.id.myEditT);
String edittextvalue = x.getText().toString();
}

EditText x = (EditText)findViewById(R.id.myEditT);
public void GetThis(View view) {
String getEditTextValue = x.getText().toString();
Toast.makeText(getApplicationContext(), getEditTextValue, Toast.LENGTH_LONG).show();
}

EditText x;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml);
x = (EditText)parentView.findViewById( R.id.myEditT);
public void GetThis(View view) {
String s=x.getText().toString();
}

Related

Get string from EditText not working, returning empty string

In a android studio project i need to get user input in edittext. here is my code
Xml file
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/amount"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" />
in .java file
button.setOnClickListener(new View.OnClickListener() {
EditText amount = (EditText) findViewById(R.id.amount);
String amount_text =amount.getText().toString();
#Override
public void onClick(View v) {
Toast.makeText(betNow.this,amount_text ,Toast.LENGTH_LONG).show();
}
});
Everytime it returns empty Toast
I get it.... Its not worked earlier because I tried to get value earlier before user click the button, now I change the code like this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText amount = (EditText) findViewById(R.id.amount);
final String amount_text =amount.getText().toString();
Toast.makeText(betNow.this,amount_text ,Toast.LENGTH_LONG).show();
}
});
Its working fine
You need to Retrieve your amount_text from EditText inside onClick(View v) like below code
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText amount = (EditText) findViewById(R.id.amount);
String amount_text = amount.getText().toString();
Toast.makeText(betNow.this, amount_text, Toast.LENGTH_LONG).show();
}
});
Don't initialize editText in onClick()
EditText amount = (EditText) findViewById(R.id.amount);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String amount_text =amount.getText().toString();
Toast.makeText(betNow.this,amount_text ,Toast.LENGTH_LONG).show();
}
});
Find EditText outside of onClickListener
EditText amount = (EditText) findViewById(R.id.amount);
button.setOnClickListener(new View.OnClickListener() {
String amount_text =amount.getText().toString();
#Override
public void onClick(View v) {
Toast.makeText(betNow.this,amount_text ,Toast.LENGTH_LONG).show();
}
});
Your issue occurs when you put getText outside of onClick.
You should put String amount_text = amount.getText().toString();
in onClick and try again.

editText.getText().toString returns empty

I'm new to Android and with the help of this site I've put together this code :
java:
public class Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
String input1 = ((EditText) findViewById(R.id.editText)).getText().toString().trim();
TextView TextView = (TextView) findViewById(R.id.textView);
if (input1.length() == 0) {
TextView.setText("empty");
}
else {
TextView.setText(input1);
}
}
}
xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:hint="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:layout_marginTop="20dp"
android:id="#+id/textView" />
But the textView shows empty when I type , tried a lot of things but nothing worked .
What I'm trying to do is get the value of editText and store it in a variable so I can work with it . I used a textView to see if it works but returns empty . I want the value to be updated real time , just like Android default calculator app .
EDIT
For my purpose I had to put a textWatcher.
public class Activity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
EditText editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
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) {
textView.setText(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
As pointed out by #Selvin, a Listener for text changes in the EditText would help.
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 0) {
TextView.setText("empty");
}
else {
TextView.setText(charSequence);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
};
EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(textWatcher);
But please remove the Listener (textView.removeTextChangedListener(textWatcher);)
when you're done, as there's a potential for a memory leak here.
You didnt cast the java file properly...
TextView TextView = (TextView) findViewById(R.id.textView);
try replacing with
TextView textView = (TextView) findViewById(R.id.textView);
Change your code to the following :
public class Activity extends AppCompatActivity {
EditText ed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
ed= (EditText) findViewById(R.id.editText);
TextView TextView = (TextView) findViewById(R.id.textView);
String input1 = ed.getText().toString().trim();
if (input1.length() == 0) {
TextView.setText("empty");
}
else {
TextView.setText(input1);
}
}
}
Hope it works well :)

Preference settings in Android studio how to connect the value in EditTextPreference to a TextView

I don't know how to find the value from the EditTextPreference. Like when i write a number in Edittextpreference I want the number shows up in my Textview "numTxt" in my fragment firstlayout xml :(.
Blockquote
The base for the settings menu is the "Settings Activity" where you get in Android studio, new>Activity>Settings Activity.so in the xml pref_general.xml is where the EditTextPreference is.
Please look at the imgur photos where the textfield is going to be edited.
Image-imgur
This is the preference xml file.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="#string/pref_description_social_recommendations"
android:title="#string/pref_title_social_recommendations" />
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:capitalize="words"
android:defaultValue="#string/pref_default_display_name"
android:inputType="textCapWords"
android:key="example_text"
android:id="#+id/textnrisettings"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="#string/pref_title_display_name" />
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:defaultValue="-1"
android:entries="#array/pref_example_list_titles"
android:entryValues="#array/pref_example_list_values"
android:key="example_list"
android:negativeButtonText="#null"
android:positiveButtonText="#null"
android:title="#string/pref_title_add_friends_to_messages" />
</PreferenceScreen>
And here is the java file of the first layout xml
public class FirstFragment extends Fragment {
private View view;
public FirstFragment(){
}
Button sendSMS;
Button sendSMSaon;
Button sendSMSaoff;
Button sendSMSrela1;
Button sendSMSrela2;
EditText msgTxt;
EditText aonTxt;
EditText aoffTxt;
EditText rela1txt;
EditText rela2txt;
Button taframnummer;
//Down below in Textview numTxt is where the value from the Edittextpreference will be shown
TextView numTxt;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_layout, container, false);
sendSMS = (Button)view.findViewById(R.id.skicka);
sendSMSaon = (Button)view.findViewById(R.id.skickaaon);
sendSMSaoff = (Button)view.findViewById(R.id.skickaaoff);
sendSMSrela1 = (Button)view.findViewById(R.id.skickarela1);
sendSMSrela2 = (Button)view.findViewById(R.id.skickarela2);
msgTxt = (EditText)view.findViewById(R.id.Textmeddelande);
numTxt = (TextView)view.findViewById(R.id.nummer);
aonTxt = (EditText)view.findViewById(R.id.aon);
aoffTxt = (EditText)view.findViewById(R.id.aoff);
rela1txt = (EditText)view.findViewById(R.id.rela1txt);
rela2txt = (EditText)view.findViewById(R.id.relä2txt);
taframnummer = (Button) view.findViewById(R.id.taframnummer);
msgTxt.setVisibility(View.INVISIBLE);
aonTxt.setVisibility(View.INVISIBLE);
aoffTxt.setVisibility(View.INVISIBLE);
rela1txt.setVisibility(View.INVISIBLE);
rela2txt.setVisibility(View.INVISIBLE);
sendSMSaoff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mymsgaoff = aoffTxt.getText().toString();
String theNumber = numTxt.getText().toString();
sendMsg(theNumber, mymsgaoff);
}
}
);
sendSMSaon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mymsgaon = aonTxt.getText().toString();
String theNumber = numTxt.getText().toString();
sendMsg(theNumber, mymsgaon);
}
}
);
sendSMS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String myMsg = msgTxt.getText().toString();
String theNumber = numTxt.getText().toString();
sendMsg(theNumber, myMsg);
}
}
);
sendSMSrela1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String myMsgrela1 = rela1txt.getText().toString();
String theNumber = numTxt.getText().toString();
sendMsg(theNumber, myMsgrela1);
}
}
);
sendSMSrela2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mymsgrela2 = rela2txt.getText().toString();
String theNumber = numTxt.getText().toString();
sendMsg(theNumber, mymsgrela2);
}
}
);
return view;
}
private void sendMsg(String theNumber, String myMsg)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(theNumber, null, myMsg, null, null);
}
}
I do not see any text field in your code, if you want to copy the value to the other activity (screen) you have to save the number using
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("numTxt", numTxt.getText());
editor.commit();
and retrieve the number in other activity using
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("numTxt", null);
TextView textView = (TextView) findViewById(R.id.textViewName); //your textField?
textView.setText(restoredText);
//or with EditText
//EditText editText = (EditText) findViewById(R.id.editTextName);
//editText.setText(restoredText);

Crash when i try to update TextView

I'm trying to do an calculator app (school project), so i need a TextView for displaying the numbers. Because it's my first java project i wanted for the beginning to make a counter which displays how many times the button was clicked(just to learn how to work with TextViews). My problem is that after the first update of the TextView(when it is displaying "1") when i press the button for the second time it gives me a crash.
My code :
MainActivity.java
public class MainActivity extends AppCompatActivity {
public Integer number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.no1);
ImageButton button = (ImageButton) findViewById(R.id.zeroB);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number++;
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.no1);
tv.setText(number.toString());
}
});
}
...
content_main.xml
...
<TextView
android:layout_width="172dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/no1"
android:layout_gravity="left|top"
android:editable="true" />
...
Modify your code to this:
public class MainActivity extends AppCompatActivity {
public Integer number = 0;
//i would use this instead of the above
private int number = 0;
private TextView tv;
private ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.no1);
button = (ImageButton) findViewById(R.id.zeroB);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number++;
tv.setText(String.valueOf(number));
}
});
}
...

Can't make the button refer to another activity in android

I want the button to show some texts when it is clicked. When we click the 'formula' button, a new blank page should open up in the phone showing the layout of the activity.. Here are my codes
MainActivity.java
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "";
// ....
public void sendMessage(View view) {
Intent intent = new Intent(MainActivity.this, FormulaActivity.class);
EditText editText = (EditText) findViewById(R.id.edit);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
// Do something in response to button
startActivity(intent);
FormulaActivity.java
public class FormulaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
MainActivity.xml (button coding)
<Button
android:id="#+id/formula"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Formula" />
You just need to intialize the Button and EditText inside your MainActivity.java
XML Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
/>
<Button
android:id="#+id/formula"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Formula"
android:gravity="bottom"
/>
public class MainActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Button F=(Button)findViewById(R.id.formula);
EditText editText = (EditText) findViewById(R.id.edit);
F.setOnClickListener(new View.onClickListener()
{
public void onClick()
{
sendMessage();
}
}
}
public void sendMessage()
{
String message = editText.getText().toString();
Intent intent = new Intent(MainActivity.this, FormulaActivity.class);
intent.putExtra("EXTRA_MESSAGE", message);
startActivity(intent);
}
}
public class FormulaActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra("EXTRA_MESSAGE");
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Your public final static String EXTRA_MESSAGE = ""; is empty string. Try changing it to any valued string.
Hope this will help you.

Categories