Add value of an edittext to a textview with button click - android

Im currently working on a small project. Which has a textview, a button and a edittext. The textview is set to 1 and when the button is clicked the textview goes up by 1. I can fill the edittext with any number (including -1 for example). I need to get the value from the edittext and add it to the current value in the textview with the click of the same button. How do i do this? this sounds really easy but i cant seem to find a solution.
MainActivity
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int numberText = 1;
EditText editTextID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textViewID = (TextView) findViewById(R.id.textViewID);
textViewID.setText("1");
}
public void changeValue(View view){
numberText+=1;
TextView textViewID = (TextView) findViewById(R.id.textViewID);
textViewID.setText(numberText+"");
}
}
Activity.xml
<?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.myapplication.MainActivity">
<TextView
android:id="#+id/textViewID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
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.238" />
<Button
android:id="#+id/PlusID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeValue"
android:text="#string/plus"
tools:ignore="MissingConstraints"
android:layout_marginTop="50dp"
app:layout_constraintTop_toBottomOf="#+id/textViewID"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="#+id/textViewID"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="#+id/textViewID" />
<EditText
android:id="#+id/editTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintTop_toBottomOf="#+id/PlusID"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="#+id/PlusID"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="#+id/PlusID" />
</android.support.constraint.ConstraintLayout>

You can get EditText value as follows within changeValue:
EditText editTextID = (EditText ) findViewById(R.id.editTextID);
try {
int editValue = Integer.parseInt(editTextID.getText().toString());
} catch (NumberFormatException e) {
// edit text value not a valid integer
}
Then it should be simple to add editValue to numberText and display in the TextView.

Here is how:
public void changeValue(View view) {
// Retrieve the content from EditText
editTextID = (EditText) findViewById(R.id.edit_text);
String content = editTextID.toString();
// Then the value of that content
// Wrap below statement in a try-catch for a NumberFormatException
int value1 = Integer.parseInt(content);
// Add it to value from TextView
TextView textViewID = (TextView) findViewById(R.id.textViewID);
// Wrap below statement in a try-catch for a NumberFormatException
int value2 = Integer.parseInt(textViewID.toString());
// Finally set the correct value
int value = value1 + value2;
textViewID.setText(value + "");
}
Also, store the references to TextView and EditText as class fields rather than calling findViewById() again.

int currentNum=Integer.parseInt(textView.getText().toString());
int addedNum=Integer.valueOf(editText.getText().toString());
currentNum+=addedNum;
textView.setText(String.valueOf(currentNum));

Try initialising the required views first & storing the View instances... It would make your task a lot easier.
public class MainActivity extends AppCompatActivity {
private TextView displayValueTextView;
private EditText valueEditText;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
displayValueTextView = (TextView) findViewById(R.id.tv_value);
valueEditText = (EditText) findViewById(R.id.et_input);
button = (Button) findViewById(R.id.button);
}
}
Here's the XML layout so you don't get confused about the Views and their associated Id's;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rl_activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.myapplication.MainActivity">
<TextView
android:id="#+id/tv_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26sp"
android:layout_centerInParent="true"
android:maxLines="1"
android:text="Hello World"/>
<EditText
android:id="#+id/et_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:ems="9"
android:hint="#string/hint_text"
android:maxLines="1"/>
<Button
android:id="#+id/button"
android:text="#string/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeValue"
android:layout_toRightOf="#+id/et_input"
android:layout_alignBottom="#+id/et_input"/>
</RelativeLayout>
The onClick attribute of the Button is set to changeValue this is our method that will change the value of our TextView, dependent on what number is on your EditText. This method is shown below:
public void changeValue(View view) {
double numberToAdd;
double currentValue;
try {
numberToAdd = Double.parseDouble(valueEditText.getText().toString());
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, "Edit Text must contain a number.", Toast.LENGTH_LONG).show();
valueEditText.setText("");
numberToAdd = 0;
}
try {
currentValue = Double.parseDouble(displayValueTextView.getText().toString());
} catch (Exception ex) {
ex.printStackTrace();
currentValue = 0;
Toast.makeText(this, "Current value is not a number. It is now set to 0.", Toast.LENGTH_LONG).show();
displayValueTextView.setText("0");
}
double finalValue = currentValue + numberToAdd;
displayValueTextView.setText(finalValue + "");
}
The numberToAdd variable is the number we are adding to our current TextView... We must;
Extract the value the EditText holds by getting it's text and parsing it as a double (this will allow us to handle decimals numbers in the EditText).
We then extract the current value of our TextView (if it is not a number an exception will be thrown and the textview will default to 0).
Finally we add the two values and set the TextView as the string representing the added values.

Related

How to display Stacked Bar graph/chart in android studio?

I have to create a Stack-bar graph and my requirement is to graph should look like this.
I have checked many graph libraries but no one is providing this type of UI.
is this possible to achieve?
So I finally found a way for that.
Step 1: Add 3 views with respective colors and a button like in below XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<View
android:layout_width="75sp"
android:layout_height="15sp"
android:layout_marginTop="100sp"
android:layout_marginStart="10sp"
android:background="#ff0000"
android:id="#+id/viewOverdue"/>
<View
android:layout_width="75sp"
android:layout_height="15sp"
android:layout_marginTop="100sp"
android:layout_marginStart="2sp"
android:background="#FFDD00"
android:layout_toEndOf="#+id/viewOverdue"
android:id="#+id/viewActive"/>
<View
android:layout_width="75sp"
android:layout_height="15sp"
android:layout_marginTop="100sp"
android:layout_marginStart="2sp"
android:background="#00FF00"
android:layout_toEndOf="#+id/viewActive"
android:id="#+id/viewDone"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Overdue"
android:id="#+id/editOverdue"
android:layout_below="#+id/viewActive"
android:layout_marginTop="100sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Active"
android:layout_below="#+id/editOverdue"
android:id="#+id/editActive"
android:layout_marginTop="2sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Done"
android:layout_below="#+id/editActive"
android:id="#+id/editDone"
android:layout_marginTop="2sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:id="#+id/btnSubmit"
android:layout_below="#id/editDone"
android:layout_marginTop="20sp"/>
</RelativeLayout>
This is how it will Look:
Do note that views are set to invisible initially.
Step 2: The coding part:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText typeActive, typeDone, typeOverdue;
private Button btnSubmit;
private View viewDone, viewOverDue, viewActive;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
typeActive = findViewById(R.id.editActive);
typeDone = findViewById(R.id.editDone);
typeOverdue = findViewById(R.id.editOverdue);
btnSubmit = findViewById(R.id.btnSubmit);
viewDone = findViewById(R.id.viewDone);
viewActive = findViewById(R.id.viewActive);
viewOverDue = findViewById(R.id.viewOverdue);
viewDone.setVisibility(View.INVISIBLE);
viewOverDue.setVisibility(View.INVISIBLE);
viewActive.setVisibility(View.INVISIBLE);
//Type all the number of tasks and press the submit button
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String tasksActive = typeActive.getText().toString();
final String tasksDone = typeDone.getText().toString();
final String tasksOverdue = typeOverdue.getText().toString();
int intTaskActive = Integer.parseInt(tasksActive);
int intTaskDone = Integer.parseInt(tasksDone);
int intTaskOverdue = Integer.parseInt(tasksOverdue);
int totalTasks = intTaskActive + intTaskOverdue + intTaskDone;
float percentageActive = (intTaskActive*100.0f)/totalTasks;
float percentageDone = (intTaskDone*100.0f)/totalTasks;
float percentageOverdue = (intTaskOverdue*100.0f)/totalTasks;
int intPercActive = (int)percentageActive;
int intPercDone = (int)percentageDone;
int intPercOverdue = (int)percentageOverdue;
viewDone.setVisibility(View.VISIBLE);
viewOverDue.setVisibility(View.VISIBLE);
viewActive.setVisibility(View.VISIBLE);
viewDone.setLayoutParams(new RelativeLayout.LayoutParams(intPercDone*10, 50));
viewActive.setLayoutParams(new RelativeLayout.LayoutParams(intPercActive*10, 50));
viewOverDue.setLayoutParams(new RelativeLayout.LayoutParams(intPercOverdue*10, 50));
}
});
}
}
This is the output after execution:
So how this works is you type the number of tasks in the edittext. It doesn't matter how you get the number of tasks but you need to convert it to percentage of total length of the stackBar or whatever you call it.
So finally you just assign each view the respective width as per the number.

Retrieving and sending a Textview value from/to a Server

I have an app with a button and also a textview (Numeric ex . 100)
i am click button then increment Textview (value) +5.
I am trying to send the value to Php Table Row and Also Retrieve Table Row Data in Same Text View.
in app was close and Open then fetch PHP Data(server) as per Texview
my_table
id
user_Name
amount
MyActivity.java
public class MainActivity extends AppCompatActivity {
int minteger = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increaseInteger(View view) {
minteger = minteger + 5 ;
display(minteger);
}
private void display(int number) {
TextView displayInteger = (TextView) findViewById(
R.id.integer_number);
displayInteger.setText("Integer: " + number);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the plus button to increase integer number" />
<TextView
android:id="#+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textSize="30sp"
android:text="Integer: 0" />
<Button
android:id="#+id/increase"
android:onClick="increaseInteger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INCREASE" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:textSize="20sp"
android:text="viralandroid.com"/>
</LinearLayout>
Initialize the TextView & your Button inside onCreate first and remove the initialization from display method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayInteger = (TextView) findViewById(
R.id.integer_number);
Button myButton = (Button) findViewById(
R.id.yourButtonId);
// Here you'll need onClickListener for the button to handle if button clicked, increase the number and etc or calling methods.
}
Follow Android Button Onclick for handle Android Button click.
But, the point is, if you add onClickListener to the Button, you may call display or just the setText inside onClickListener and no need for another method.(Simplifying)
Also add:
TextView displayInteger;
In above of onCreate to have access all over the Activity and the current class.
To send string to server side, follow this link : How send data to website by using android app
You'll need to first get the text by getText().toString() then sending as string to server: How to get EditText value and display it on screen through TextView?

Programmatically adding EditText box

I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
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());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());

myEditText.setText not refreshing on page

I have a simple activity which needs to accept a 4-digit PIN number using a custom pin-pad (buttons on the screen). The digits are stored in four EditTexts.
When I click a button, the text in the button is stored in a char[] (myEditText.getText() shows that this is happening), the focus is moved to the next EditText through an onFocusListener() and the Log output shows that this is all happening correctly.
11-20 10:19:56.969: I/DebugA(17742): Pin1 updated to: 1 // Pin1 is the ID
11-20 10:19:58.289: I/DebugA(17742): Pin2 updated to: 2 // '2' is Pin2.getText()
11-20 10:19:58.849: I/DebugA(17742): Pin3 updated to: 3
11-20 10:19:59.659: I/DebugA(17742): Pin4 updated to: 4
The screen itself is simply not being updated. The EditTexts all appear empty, even though the code executes perfectly.
I have looked through a whole heap of answers on SO and tried many of the suggestions with absolutely no luck, so I am posting this question which I hope someone can shed some light on! Has anyone else had this happen?
Here's some of the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" >
<EditText
android:id="#+id/Pin1"
android:tag="Pin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="2"
android:gravity="center_vertical|center_horizontal"
android:nextFocusDown="#+id/Pin2"
android:maxLength="1"
android:inputType="textPassword" />
...
...
...
</RelativeLayout>
...and the buttons are:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:layout_below="#+id/layout1" >
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:text="1" />
<Button
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="2" />
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="3" />
</LinearLayout>
The activity is a standard activity (MyActivity extends Activity).
Any thoughts?
UPDATE
Here's some of the java as requested:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.activity_login, null);
EditText Pin1 = (EditText) layout.findViewById(R.id.Pin1);
Pin1.setOnFocusChangeListener(focusListener);
Pin1.setText("1"); // <--THIS HAS NO EFFECT
EditText Pin2 = (EditText) layout.findViewById(R.id.Pin2);
Pin2.setOnFocusChangeListener(focusListener);
EditText Pin3 = (EditText) layout.findViewById(R.id.Pin3);
Pin3.setOnFocusChangeListener(focusListener);
EditText Pin4 = (EditText) layout.findViewById(R.id.Pin4);
Pin4.setOnFocusChangeListener(focusListener);
...
...
...
}
The Onclick method is:
public void btnPinClick(View btnPin) {
String PinNumber = ((Button)btnPin).getText().toString();
RelativeLayout layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_login, null);
ArrayList<View> views = (ArrayList<View>) layout.getFocusables(View.FOCUS_DOWN);
EditText nextItem = null;
for (int i = 0; i < views.size(); i++) {
// THIS IS THE CORRECT EditText FOR EACH OF THESE
EditText textBox = (EditText) views.get(i);
if (textBox.getTag().toString().equals(currentText)) {
textBox.setText(PinNumber);
pin[i] = PinNumber.toCharArray()[0];
Log.i("DebugA", textBox.getTag().toString() + " updated to: " + textBox.getText().toString());
textBox.invalidate();
textBox.refreshDrawableState();
if(i+1 < views.size()) {
nextItem = (EditText) views.get(i+1);
}
else {
doCheckPin();
}
break;
}
}
if(nextItem != null){
nextItem.requestFocus();
currentText = nextItem.getTag().toString();
}
}
UPDATE 2
Here is the code for the OnFocusChangeListener as requested:
protected OnFocusChangeListener focusListener = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//THIS SIMPLY SETS A VARIABLE STRING TO SAY WHICH
//EditText HAS FOCUS.
currentText = v.getTag().toString();
}
}
};
It looks like you're using a layout inflater in addition to setContentView in your activity. And affecting the edittexts referenced by the inflater. Try removing use of the inflater and rely solely on setContentView for this part of the activity. You might be performing in an incorrect view hierarchy.

Can't get text from edittext?

I have updated to ADT version 20. After that I can't use EditText. For API level 16 it shows some error. For lower API levels there is no error but I can't get the input of EditText. It shows an empty result.
Sample code I used:
java code
public class sample extends Activity {
EditText edt;
Button btn;
TextView txt;
String str;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView2);
str = edt.getText().toString();
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
}
}
XML CODING
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Search"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10pt"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
Actually, you are trying to get text from EditText in onCreate() of Activity, which has no any text contains yet.
So Just put the line, str = edt.getText().toString(); in button's click() then you will get the result,
Like,,
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
str = edt.getText().toString();
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
Your statement str = edt.getText().toString(); should be inside the onClick handler.
The code cannot access the value because after onCreate ends the value found in the EditText abd str is beyond the scope. So put the str = edt.getText().toString(); statement inside the onClick so that when user clicks the button it can access the value.
I think you are trying to get EditText (i.e. edt) value in onCreate() and I think you are entering value after the completion of onCreate() method.So that means you are not getting value for your str.
That's why you are not able to set the value to TextView.
just put text inside your button otherwise it will not work becz whatever u are getting in strings is also working like u are just cre

Categories

Resources