Not getting an output from my inputs - android

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

Related

Check if EditText Number Decimal is Empty

I tried everything.
if (editText == null)...
if (etNumero1.getText().toString().isEmpty())...
if (etNumero1.getText().toString().equals("")...
if (etNumero1.getText().toString().trim().length() >= 0))
Nothing works. Even my teatcher cant tell whats wrong.
Sorry for bad english im Brazilian.
Check, If the value is not an Empty
if (!etNumero1.getText().toString().isEmpty())
{
if(etNumero1.getText().toString().contains("."))
{
//Do your functionality here
}
}
else
{
//
}
Try
if (Integer.parseInt(etNumero1.getText().toString()) > 0)
If you specify your question, I could answer in better condition.
Use TextUtils.isEmpty("") check for null if not convert to formart to ###.##
Make sure that you are not making mistake on editText etNumero1??
I know editText is Edit Text but what is etNumero1?? please replace etNumero1 to editText, it should work
if (editText.getText().toString().isEmpty())...
Example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.editText);
Button button = findViewById( R.id.button );
button.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick (View v) {
if(editText.getText().toString().isEmpty()){
System.out.println("empty string");
}else{
System.out.println("" + editText.getText().toString());
}
}
});
}
if(String.valueOf(editText.getText() != null){
Double double = Double.parseDouble(editText.getText().toString);
}

EditText visibility

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

saving/printing text Android

I have 2 TextView, and 1 Button. I want to be able to click the button and to save the text on the first TextView and show the value on the second TextView.
I've tried this code:
public void buttonOnClick(){
Button submit = (Button) findViewById(R.id.submit);
editName = (EditText) findViewById(R.id.name);
editEmail = (EditText) findViewById(R.id.email);
textout = (TextView) findViewById(R.id.outputText);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textout.setText(editName.getText()+"\n"+editEmail.getText());
}
});
}
But I get an error on 'textout'. When I clock the red light buble, it says 'create a local variable', field text'.
Try this
public void onClick(View v) {
textout.setText(editName.getText().toString()+"\n"+editEmail.getText().toString());
}
Your problem stimulates from the fact that EditText getText() returns Editable Object. It's not a String and you can't concat 2 Editable Objects.
You have 2 Options:
textout.setText(editName.getText().toString() + "\n" + editEmail.getText().toString());
And Second you can use SpanableStringBuilder
SpannableStringBuilder sb = new SpannableStringBuilder(editName.getText());
sb.append("\n").append(editEmail.getText());
The Second options allows you also to decorate the Text and save you the Need to build a String which is (maybe) better.

text view in android can't be changed

I have TextView in my code. I want to test if an EditText is empty then fill the TextView with "some thing" or take the text from the EditText; but it doesn't change the text. Here is the code(in onCreate() method):
if ((textCity.length())==0){
cityText.setText("something");
}
else
cityText.setText(textCity);
where textCity
textCity=editText1.getText();
and cityText is the TextView
You could do something like this:
if (textCity.getText().toString().trim().length() == 0) {
cityText.setText("something");
}
else
cityText.setText(textCity);
Try this
if ((textCity.length() < 0)){
cityText.setText("something");
}
textCity=editText1.getText().toString(); //add this when you're grabbing the text from the
//textview
I assume that you haven't invoked it in the right place. You need to place a TextWatcher on your EditText. Try this out:
editText1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
cityText.setText(String.valueOf(s));
}
});

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