How to display a something from an EditText? - android

I thought making thing part of the app would be easy, however I was wrong. I wish to have a textView display whatever the user wrote in the editText. This is what I tried.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myTextView.setText(myEditText.getText().toString());
// of course I would use variables in place of the
// myTextView and myEditText
}
});
This is another way I tried to get this done.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//num1 is my String variable
num1 = myEditText.getText().toString();
myTextView.setText(num1);
}
});
Both times the textView comes up with nothing in it.
Thank you for any help!

onClickListener merely responds to user clicks. You need to implement a TextWatcher on your EditText. The most straightforward way of doing this is to implement TextWatcher in your class, then make a call to myEditText.addTextChangedListener(this).
I recommend adding something like the following to your onTextChanged method:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
myTextView.setText(myTextView.getText()+s);//or something like this...
}

I usually use GetDlgItemText.
char Buffer[120];
GetDlgItemText(hwndDlg, (control), buffer, sizeof(buffer));
This will read it and store it in buffer.

In the EditText the getText call should you return the String, I don't believe you need to call the ToString method on it. The way you are using it in the onClickListener implies you have a button that should be calling a function to set the text into the textview. If you want it dynamically you should be able to use onTextChanged to fill in the data.

First of all check whether the control is coming to your setOnClickListener(). Put in a Log to find that out.
Next make sure that "add" is the button or item that u r using to initiate the copy process.
This statement of yours is correct.
myTextView.setText(myEditText.getText().toString());
Though you do not require the toString(). Doesnt really make a difference. I suggest you check that your textview and edittext is fine.

have you check the visibility of textview ?before clicking add button it is invisible rite?then u have to set the visibility on add button click.

From your code i understood that there is a button here too so try this should work:
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.mybutton);
btn.setOnClickListener(btncall);
}
private OnClickListener btncall = new OnClickListener()
{
public void onClick(View v)
{
TextView mytextView = (TextView) findViewById(R.id.MytextView);
EditText myeditText = (EditText) findViewById(R.id.MyeditText);
mytextView.setText(myeditText.getText().toString());
}
};
}

Related

Android view isDirty inside onClick

I want to check if the text in some EditText is changed, after user clicks some Button. But View#isDirty seems not to return the correct state of the EditText if called inside onClick. For instance, I wrote something like this:
public class MainActivity extends Activity {
EditText editText;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.f);
editText = (EditText) findViewById(R.id.e);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
}
}
before i make any change to the editText, it outputs is clean, as expected. But the same is clean is printed even after I write something in editText.
When will isDirty be called? And is it the correct way to do this at all?
Update:
I also want to check if some Switch and Spinner values are changed. Is isDirty() the correct way to do this?
By the time you click your button edittext is no longer dirty - text is already updated and view redrawn. Maybe if you change your onclick handler you will understand better what is going on.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText = (EditText) findViewById(R.id.e);
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
isDirty will return true only as long as view has not been redrawn. This happens quite quickly and basically you do not have (and dont need) any control over this.
I think you need to use some other methods to achieve what you want.
I would suggest to use:
https://stackoverflow.com/a/9459848/5684335
The comment from Okas is a good explanation why.

How to Add to a textview when a button is pressed

I know this has been asked before but I cannot make this work so here is what I have so far
class Click extends Activity {
int i=0;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
final TextView mTextView = (TextView) findViewById(R.id.Counter);
mTextView.setText(""+i);
final Button button = (Button) findViewById(R.id.AddOne);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
mTextView.setText(Integer.toString(i));
}
});
}
Every time I run the app in an emulator it crashes
java.lang.IllegalStateException: Could not find a method Click(View) in the activity class com.scouting.corbin.frc_201415_scouting.MainActivity for onClick handler on view class android.widget.Button with id 'AddOne'
I know this is probably something completely stupid but I am new to this and need help thank you in advance.
As per your logcat.
java.lang.IllegalStateException: Could not find a method Click(View)
in the activity class
com.scouting.corbin.frc_201415_scouting.MainActivity for onClick
handler on view class android.widget.Button with id 'AddOne'
I suggest you to add Click(View v) in your MainActivity
public void Click(View v)
{
}
You need to take the root element here. Depending on parent layout include this line in the activity after setContentView().
RelativeLayout layout=(RelativeLayout)findViewById(R.id.yourLayoutId);// If its some other layout change "RelativeLayout" to your opted layout.
and in onClick() method of button, add following.
layout.add(tv);
Yopu want To add one Linearlayout in xml file
and set id for your LinearLayout.
android:id="#+id/linearlayout"
And change your addTextView method to following
public void addTextView(String text){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView textView=new TextView(this);
textView.setText(text);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(textView);
}
and call this method from your Forloop
Perhaps consider using the android:onClick="example_method" attribute for the button in your xml file. Then create the appropriate method in the class. public void example_method(View v) {} Then place the code you have in your onClick function into the new one. It's easier than using an listener.
Ok so all of you helped I completely got rid of that code which was too comlicated for what I was trying to do. After taking bits of suggestions and some reasearch I came up with this
public void AddOne(View v) {
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
tv.setText(""+i);
}
As you can see much simpler than what I had before and this one works thank you all

Change View Text Color by code

Well, I developed a menu with eight Buttons for an App. So, every time the user clicks on in one of the buttons, such button changes its background. And I would would like to change its color as well. But I got now idea how, since setTextColor does not work with Views.
I'm using View because its part of onClick method that I override in order to achieve what I want. So, here go the code:
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setBackgroundResource(R.drawable.degrade_menu);
}
So, how could I change the text color?
Cheers,
Cast your v to TextView and then set the text color. Do not forget to read color from resourse
((TextView)v).setTextColor(getResources().getColor(R.color.errorColor));
quick solution:
final Button button = (Button) findViewById(<id>);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button.setTextColor(<color>);
}
};
better solution: use states
Cast the view to a button. Then you can use settextcolor

I have an int value; now how do I present it in a TextView to the user? Android

Beginning I should say that I am very new to programming.
I am building an android application which includes a calculator function. What I want to do is on button click to get the user input from two EditTexts, add them together and then display the result in a TextView. Similar questions have been covered by others, but what they don't cover is how do you actually display the result in a TextView (or at least I didn't find one myself). So I tried the following which was suggested in many posts:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.team2);
//finds the references for the view in the layouts
b_t2p_ok = (Button) findViewById(R.id.bOK2);
EditText1 = (EditText) findViewById(R.id.etPaixnidi2);
EditText2 = (EditText) findViewById(R.id.etPontoi2);
b_t2p_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//First I am trying to do this for one EditText
String myEditValue = etPaixnidi20.getText().toString();
int myEditNum = Integer.parseInt(myEditValue);
myEditNum.setText(textOut);
}
});
}
However when I try to display the int myEditNum in a TextView(textOut1) using the setText() method I get an error for the setText method (saying: ''Cannot invoke setText(TextView) on the primitive type int'').
I also tried (below) to convert int myEditNum to a String and then display it to a TextView but still doesn't work as I get the folloing error for the setText method: ''The method setText(TextView) is undefined for the type String''.
EditText myEdit = (EditText) findViewById(R.id.edittext1);
String myEditValue = myEdit.getText().toString();
int myEditNum = Integer.parseInt(myEditValue);
String texting = Integer.toString(myEditNum);
texting.setText(textOut1);
Why does this happen and how do I fix this?
You need to call setText(string) on a Textview. So you can do something like:
// Replace R.id.textview1 with the id of your textview
TextView tv = (TextView) findViewById(R.id.textview1);
tv.setText(myEditValue);
You seem to be using the wrong variable to settext here. the View class can do setText()
Try:
...
public void onClick(View v)
{
//First I am trying to do this for one EditText
String myEditValue = etPaixnidi20.getText().toString();
EditText1.setText( myEditValue );
// or
EditText2.setText( myEditValue );
}
...
you should use
textOut1.setText(texting)
You have the TextView and the String the wrong way round.
textOut1.setText(texting);

ParseInt Exception

I am creating a small calc app with EditText views and Im running into an runtime exception when the user leaves an EditText view empty causing the ParseInt to try and Parse nothing. Ive read that I need to 'Try' and 'Catch' this error before it occurs, but Im unsure of where and how to do this!
Any advice is much appreciated!
Here is my code:
public class HandlerExamples extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.testButton);
button.setOnClickListener(this);
public void onClick(View v) {
String a,b,t;
double vis;
EditText txtbox1 = (EditText)findViewById(R.id.A);
EditText txtbox2 = (EditText)findViewById(R.id.B);
EditText txtbox3 = (EditText)findViewById(R.id.t);
TextView tv = (TextView) findViewById(R.id.Answer);
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
t = txtbox3.getText().toString();
vis = ((Integer.parseInt(a)*1) + (Integer.parseInt(b)*2)) / (Double.parseDouble(t));
tv.setText(double.toString(vis));
}
}
Thanks so much!
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.xx:
//do things xx click
break;
case R.id.yy:
//do things yy click
break;
}
}
you can get the view id to know whick widget was clicked.
Changwei Yao defined one way you can do this, but here's the way most Android programmers would do this (programmatically), since it's a little easier to read and figure out what your widgets are doing:
But first, remove the implements OnClickListener from your Activity, as it's not needed.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your button to do when clicked
}
}
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// what you want your EditText to do when clicked
// (such as editText.setText(""))
}
}
Another way to do the same thing is to define android:onClick="insert_method_name_here" for the widgets that you want perform an action when clicked. In your case, in your main.xml (since that's what you're using in your Activity), you could write something like...
<Button android:id="#+id/testButton"
(other attributes you wish to apply to the button)
android:onClick="buttonAction" />
<EditText
(other attributes)
android:onClick="textAction" />
And then, in your Activity, you define the methods buttonAction(View v) and textAction(View v). Note that these methods must be public void, and must take the sole argument View v.
(One advantage of the XML method is that you don't necessarily have to define an android:id attribute for these widgets, unless you need to be able to manipulate them or extract information from them in your code (which means you will need to define an android:id for your EditText since you'll likely want the user's input))
If you only need to exclude the empty text field then hotveryspicy's solution is probably the quickest. For a secure solution: catching the NumberFormatException will filter anything that can not be converted to an integer.
int vis;
try {
vis = Integer.parseInt(a);
}
catch(NumberFormatException nfe) {
Log.e(TAG,"trying to convert:"+a+" to integer failed");
vis = 0;
}

Categories

Resources