EditText visibility - android

I have code:
...
final EditText edit_1= (EditText) findViewById(R.id.editText1);
edit_1.setVisibility(View.VISIBLE);
final EditText edit_2= (EditText) findViewById(R.id.editText2);
edit_2.setVisibility(View.INVISIBLE);
...
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (edit_1.getText().toString() == "1")
{
edit_2.setVisibility(View.VISIBLE);
}
}
});
On default, first EditText is visible and second one - invisible. I want to set second editor visible after button pressing, if in the first editor typed "1". It doesn't work, What is wrong?

To compare two string the == operand doesn't works, you need to use equals() method.
Inside OnClick() you need something like this:
if (edit_1.getText().equals("1"))
{
edit_2.setVisibility(View.VISIBLE);
}

You should try something like this:
//***IN ONCLICK LISTENER***//
String text = edit_1.getText().toString();
if (text.equals("1")) {
edit_2.setVisibility(View.VISIBLE);
}

Related

Not getting an output from my inputs

http://pastebin.com/YmeB2D1N
Essentially, my program has two EditTexts, a Button, and a TextView. You input a number/string into the EditText, and an output is supposed to be shown in the TextView. However, all i get is a 'There is no Department'. How do i fix this?
I don't see anywhere that you are setting the values of department or name variables.
you can do:
EditText departmentEt = (EditText)findViewById(r.id.department_edit_text));
EditText nameEt = (EditText)findViewById(r.id.name_edit_text));
search.setOnClickListener(new OnClickListener) {
public void onClick(View v) {
if(deparmentEt.length() > 0) {
department = Integer.parseInt(departmentEt.getText().toString());
}
if(nameEt.length() > 0) {
name = nameEt.getText().toString();
}
//The rest of your code
}
}
use onTextChangedListener for EditText and
in that method write get value of EditText and set it to TextView.
I hope it works.
Code
textView= (TextView)findViewById(R.id.text);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
textView.setText(textMessage.getText().toString());
}
});

How to display a something from an EditText?

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());
}
};
}

How to populate the EditText with the button click?

I want to get the values in the button to be populated in the edit text box. i.e I am creating a login page in which the pin has to entered I have given a set of numbers as buttons, when I click the button the corresponding values has to be populated in the edit text box( just like the ATM action).
I am now using:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
String text=(String) b1.getText();
pincode.setText(text);
}
});
with the above code I am getting the values populated, but I have a set of buttons, so is there any simplified way to get the values populated?
*The values currently replacing the previous one , I need multiple values in the edit text like if we press buttons 1,2,3 (values) the values in the edit text should be 123 *
To do this, in your activity implements OnClickListener
then add this listener to all your button as
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
and write onClick event as
public void onClick(View v) {
Button b1 = (Button)v;
editText.setText(editText.getText().toString()+b1.getText().toString());
}
and you are done :)
on your button onClick you got to do this
button1.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"1");
});
button2.setOnClickListener(new View.OnClickListener() {
yourEditText.setText(yourEditText.getText().toString+"2");
});
this way your buttons will work
onClick(View v)
{
editText.setText(editText.getText() + v.getText());
//plz adjust casting and spanned as per requirement
}
You can add text like this:
String a = "random text";
input = your_edit_text_box;
input.setText(a);
Link: http://developer.android.com/reference/android/widget/EditText.html#setText%28java.lang.CharSequence,%20android.widget.TextView.BufferType%29
Button btnNO = (Button) findViewById(R.id.btn);
EditTExt edtText = (EditText) findViewById(R.id.editText);
btnNO.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text= btnNo.getText();
edtText.setText(text);
// or direct use like, edtText.setText(btnNo.getText());
}
});

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);

Change EditText by Button

I am having EditText and using the numbers inside the EditText for my variables.
I am also having two buttons, one to increase the number in EditText by one and another to decrease it by one.
Can somebody tell me the code to make this possible?
Use the following line in the xml of EditText for setting the input type as number :
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="200"
android:id="#+id/editText1"
android:layout_alignParentLeft="true">
</EditText>
And in source file use the following code to decrease the number by 1:
final EditText ed=(EditText)findViewById(R.id.editText1);
Button b1=(Button)findViewById(R.id.button01);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int a=Integer.parseInt(ed.getText().toString());
int b=a-1;
ed.setText(new Integer(b).toString());
}
});
Similarly add one more button in the xml to increase the number by one.
You need to create a on click listener for each button, doing something like:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.edit_text);
int newVal = ... //retrieve the previous val and increment it (or decrement it)
editText.setText(newVal, TextView.BufferType.EDITABLE);
}
});
Simply you have to use the click event of both the buttons, on increase button get the text in edittextbox and increment it and set it in the edittextbox, same way do for decrement button also and decrement the value.
button.setOnClickListener(new OnCLickListener(){
#Override
public void onClick(View arg0) {
if(arg0.equals(button1))
{
String s = editText.getText().toString();
Integer i = Integer.parseInt(s);
i=++i;
s = s.valueOf(i);
editText.setText(s);
}
if(arg0.equals(button2))
{
//decrement
}
}
})

Categories

Resources