Describing the functionality of my application: I have put in a Relative Layout a TextView, an EditText and a button. All I am trying to do is: when the user writes something in the EditText and push the button, then the content of the EditText is appeared in the TextView(just like a chat-virtual chat). Everything works perfectly, but when the EditText is empty,and the button get pushed, an empty line is appeared in the TextView(and i don't want to..). Although I've tried to solve it using an if the empty line is still appearing in the TextView. I would be really greatfull, if you could help!!! Than you in advance!
Here is my code:
package teiath.android.appliacation;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class M_chat extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**Code for the scroll bars in the TextView. */
final TextView tv = (TextView)findViewById(R.id.TEXT_VIEW);
tv.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
/** Code for the scroll bars in the EditText. */
final EditText wr = (EditText)findViewById(R.id.EDIT_TEXT);
wr.setMovementMethod(new ScrollingMovementMethod());//for the scroll bars
final Button button = (Button) findViewById(R.id.btn1);//find the button by id in main.xml
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String wrcontent = wr.getText().toString();//gets the text of the EditText and put it in "tvcontent" variable.
String tvcontent = tv.getText().toString();//gets the text of the textView and put it in "tvcontent" variable.
if (wrcontent!="")//if the EditText is not empty
{
//check if the TextView is empty or not
if (tvcontent!="")//If it is not empty...
{
tv.setText(tvcontent + "\n" + wrcontent);//add its current(TextView's text) text, new line and the text of the EditText as the new text of TextView.
//tv.setVisibility(0);//makes visible the textView with the cloud1.png background
wr.setText("");//set the text of the Edit Text as empty
//wrcontent = "";
}
else//if the TextView is empty...
{
tv.setText(wrcontent);//add the text of the editText as the new text of the TextView
wr.setText("");
}
}
/**if (wrcontent=="")
{
}*/
//finish();
}
});
}
}
Don't use !="" for String comparison. To check for empty text, use something like
if ( wrcontent != null && wrcontent.trim().length() == 0 ) {
Better yet, include Guava libraries in your code.
Related
I'm working on an app that's main layout consists of a row of buttons on top of a LinearLayout which holds a column of EditTexts. I've created a Form object to hold all the values to be displayed in the Editexts, and there will be a Form object for every button in the row. Each button is clicked to change the form's data to show the values held in each Form object. I have the Form objects kept in a List called formList, so that I can access each one when the corresponding button is pressed.
When I click one of the buttons, I want the EditText values to change to a different set of values which correspond to that button. But when I click these buttons, the previously entered values do not show up.
When I click Next Drink, it dynamically creates a button as well as a Form object, and clears the EditTexts so that it looks like a new form has been created for the new button. I should then be able to add strings to the EditTexts, and the strings should be stored in the Form object so that if I click that button again, the Form object will fill the EditTexts with the strings stored in that object.
So, if I click button 3 after having clicked button 2, it should set the text of each EditText to the values contained in the the third Form object, which contains strings for Name, Volume, Percentage, Price, and Quantity.
My problem is the values do not show up in the EditTexts after clicking a button. So if I click Button 3, the values from Form 3 don't show up. I've been at this for a long time and cannot figure out why, so I'd really appreciate any help. Here is the activity:
package com.example.DrinkApp;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
//view variables
final Button newDrink[] = new Button[100];
LinearLayout drinkSelectionContainer;
EditText name;
EditText vol;
EditText perc;
EditText pri;
AutoCompleteTextView quant;
//counters
int numOfDrinks = 0;
int selectedForm = 0;
//array to hold all Form objects.
List<Form> formList = new ArrayList<Form>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get views
drinkSelectionContainer = (LinearLayout) findViewById(R.id.drinkSelectContainer);
name = (EditText) findViewById(R.id.etName);
vol = (EditText) findViewById(R.id.etVol);
perc = (EditText) findViewById(R.id.etPerc);
pri = (EditText) findViewById(R.id.etPrice);
quant = (AutoCompleteTextView) findViewById(R.id.etQuantity);
Button saveDrink = (Button) findViewById(R.id.bSaveForm);
Button nextDrink = (Button) findViewById(R.id.bNextDrink);
Button crunkOut = (Button) findViewById(R.id.bCalc);
///////////////////////// next drink method
addNewDrinkButton();
name.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
formAdd();
}
#Override
public void afterTextChanged(Editable editable) {
// mNames.add(selectedForm, name.getText().toString());
// newDrink[selectedForm].setText(name.getText().toString());
}
});
nextDrink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewDrinkButton();
}
});
}
//listener for drink select buttons. when button is clicked, it finds out which one it was,
//and then displays the correct values.
public View.OnClickListener drinkSelectListener = new View.OnClickListener() {
public void onClick(View v) {
selectedForm = v.getId();
Form form = formList.get(selectedForm);
Button v1 = (Button) findViewById(selectedForm);
v1.setText(" " + selectedForm);
// Do something depending on the value of the tag
Log.v("My Logs:", "Select Drink " + selectedForm + " button pressed.");
Log.v("My Logs:", "Drink " + selectedForm + " name: " + form.getName());
formDisplay(form);
}
};
private void addNewDrinkButton() {
formClear();
formAdd();
numOfDrinks++;
selectedForm = numOfDrinks;
Log.v("My Logs:", "Next Drink button pressed.");
newDrink[numOfDrinks] = new Button(getBaseContext());
newDrink[numOfDrinks].setOnClickListener(drinkSelectListener);
newDrink[numOfDrinks].setId(numOfDrinks);
newDrink[numOfDrinks].setText("" + selectedForm);
newDrink[numOfDrinks].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// name.setText("Drink " + (numOfDrinks+1));
drinkSelectionContainer.addView(newDrink[numOfDrinks]);
//TODO: save form values, clear edit texts, and create new variables for edittexts.
}
void formAdd(){
Form tempForm = new Form();
tempForm.setId(selectedForm);
tempForm.setName(name.getText().toString());
tempForm.setVolume(vol.getText().toString());
tempForm.setPercentage(perc.getText().toString());
tempForm.setPrice(pri.getText().toString());
tempForm.setQuantity(quant.getText().toString());
formList.add(selectedForm, tempForm);
}
void formDisplay(Form form){
name.setText(form.getName());
vol.setText(form.getVolume());
perc.setText(form.getPercentage());
pri.setText(form.getPrice());
quant.setText(form.getQuantity());
}
void formClear(){
name.setText("");
vol.setText("");
perc.setText("");
pri.setText("");
quant.setText("");
}
}
!Here's the main layout. So if I click button 4, I want to have the EditTexts display the values
I'd entered the last time I'd clicked on it. It's really very simple.
You are calling formClear() before formAdd()?
Setting EditText values on button press
edittext.setText("nexteditText");
edittext.requestfocus();
I'm working on an app that's main layout consists of a row of buttons on top of a LinearLayout which hold a form made up of edittexts. I've created a Form object to hold all the values to be displayed in the edittexts for each form. Each button is clicked to change the form's data to show the values held in each Form object. I have the Form objects kept in a List so that I can access each one when the corresponding button is pressed.
EDIT: I've realized I didn't quite explain myself properly. When I click one of the buttons, I want the EditText values to change to a different set of values which correspond to that button. But when I click these buttons, the previously entered values do not show up. I referred to the column of EditTexts a form, but it's just a column of EditTexts.
When I click Next Drink, it dynamically creates a button as well as a Form object, and clears the EditTexts so that it looks like a new form has been created for the new button. I should then be able to add strings to the EditTexts, and the strings should be stored in the Form object so that if I click that button again, the Form object will fill the EditTexts with the strings stored in that object.
So, if I click button 3 after having clicked button 2, it should set the text of each EditText to the values contained in the the third Form object, which contains strings for Name, Volume, Percentage, Price, and Quantity.
My problem is the values do not show up in the EditTexts after clicking a button. So if I click Button 3, the values from Form 3 don't show up. I've been at this for a long time and cannot figure out why, so I'd really appreciate any help. Here is the activity:
package com.example.DrinkApp;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
//view variables
final Button newDrink[] = new Button[100];
LinearLayout drinkSelectionContainer;
EditText name;
EditText vol;
EditText perc;
EditText pri;
AutoCompleteTextView quant;
//counters
int numOfDrinks = 0;
int selectedForm = 0;
//array to hold all Form objects.
List<Form> formList = new ArrayList<Form>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get views
drinkSelectionContainer = (LinearLayout) findViewById(R.id.drinkSelectContainer);
name = (EditText) findViewById(R.id.etName);
vol = (EditText) findViewById(R.id.etVol);
perc = (EditText) findViewById(R.id.etPerc);
pri = (EditText) findViewById(R.id.etPrice);
quant = (AutoCompleteTextView) findViewById(R.id.etQuantity);
Button saveDrink = (Button) findViewById(R.id.bSaveForm);
Button nextDrink = (Button) findViewById(R.id.bNextDrink);
Button crunkOut = (Button) findViewById(R.id.bCalc);
///////////////////////// next drink method
addNewDrinkButton();
name.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
formAdd();
}
#Override
public void afterTextChanged(Editable editable) {
// mNames.add(selectedForm, name.getText().toString());
// newDrink[selectedForm].setText(name.getText().toString());
}
});
nextDrink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewDrinkButton();
}
});
}
//listener for drink select buttons. when button is clicked, it finds out which one it was,
//and then displays the correct values.
public View.OnClickListener drinkSelectListener = new View.OnClickListener() {
public void onClick(View v) {
selectedForm = v.getId();
Form form = formList.get(selectedForm);
Button v1 = (Button) findViewById(selectedForm);
v1.setText(" " + selectedForm);
// Do something depending on the value of the tag
Log.v("My Logs:", "Select Drink " + selectedForm + " button pressed.");
Log.v("My Logs:", "Drink " + selectedForm + " name: " + form.getName());
formDisplay(form);
}
};
private void addNewDrinkButton() {
formClear();
formAdd();
numOfDrinks++;
selectedForm = numOfDrinks;
Log.v("My Logs:", "Next Drink button pressed.");
newDrink[numOfDrinks] = new Button(getBaseContext());
newDrink[numOfDrinks].setOnClickListener(drinkSelectListener);
newDrink[numOfDrinks].setId(numOfDrinks);
newDrink[numOfDrinks].setText("" + selectedForm);
newDrink[numOfDrinks].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// name.setText("Drink " + (numOfDrinks+1));
drinkSelectionContainer.addView(newDrink[numOfDrinks]);
//TODO: save form values, clear edit texts, and create new variables for edittexts.
}
void formAdd(){
Form tempForm = new Form();
tempForm.setId(selectedForm);
tempForm.setName(name.getText().toString());
tempForm.setVolume(vol.getText().toString());
tempForm.setPercentage(perc.getText().toString());
tempForm.setPrice(pri.getText().toString());
tempForm.setQuantity(quant.getText().toString());
formList.add(selectedForm, tempForm);
}
void formDisplay(Form form){
name.setText(form.getName());
vol.setText(form.getVolume());
perc.setText(form.getPercentage());
pri.setText(form.getPrice());
quant.setText(form.getQuantity());
}
void formClear(){
name.setText("");
vol.setText("");
perc.setText("");
pri.setText("");
quant.setText("");
}
}
!Here's the main layout. So if I click button 4, I want to have the EditTexts display the values
I'd entered the last time I'd clicked on it. It's really very simple, I'm having trouble explaining myself today.
The code provided doesn't seem to coincide with your question. You want to put values into an EditText view and then hit a button that takes the values from that EditText view and places it inside of a "form"? If so, then inside of onCreate() and after you have setContentView() try:
EditText name = (EditText) findViewById(R.id."edit_text_id"); // where "edit_text_id" is your your EditText view id set in your xml
String text = name.getText().toSting();
TextView textView = (TextView) findViewById(R.id."forms_text_field"); // where "forms_text_field" is your TextView id set in your form's xml
textView.setText(text);
Repeat this for each of your EditText views (setting a corresponding TextView in your form to the desired EditText field value). This should work just fine.
Below is my main activity file. I am stuck with the bottom area (commented out). I want to make it so that when I hit button b1, the text changes from what it is for default. So say it says "hello" by default, I want to switch the text to something like "how are you" when you press button b1. I have tried many things, but I always get errors. Can someone help me?
package com.wdmc85.donottouch;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class DoNotTouchActivity 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 b1 = (Button) this.findViewById(R.id.b1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.b1:
//WHAT DO I PUT IN HERE?? I WANT IT SO WHEN YOU HIT BUTTON B1, THE
//TEXT CHANGES FROM WHAT IT IS FOR DEFAULT. SO SAY IT SAYS "HELLO"
//AS DEFAULT, HOW DO I MAKE IT SO WHEN YOU PRESS BUTTON B1 THE TEXT
//CHANGES FROM "HELLO" TO "HOW ARE YOU?" I HAVE TRIED ALL SORTS OF
//THINGS BUT NOTHING HAS WORKED
break;
}
}
}
if want to change text of the button itself
switch(v.getId())
{
case R.id.b1:
Button b1 = (Button) v;
b1.setText("HOW ARE YOU?");
break;
}
if want to change text of the any other Textview
switch(v.getId())
{
case R.id.b1:
TextView tv = (TextView ) this.findViewById(R.id.<tvid>);
tv .setText("HOW ARE YOU?");
break;
}
You should use below method to change text of text view.
textView.setText("your text");
reference link is >> textview>>setText()
can use same for button and other views.
I have a image button and i want that image button when pressed it changes the text in a textbox and it changes a image to a different image how do i do this?
You need an OnClickListener: http://developer.android.com/reference/android/view/View.OnClickListener.html
When clicked your text can be changed with (something like) text.setText("new text");
I'll find a link in a minute which will help more.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class TestesetetActivity extends Activity {
/** Called when the activity is first created. */
TextView textview = null;
ImageButton buttonResume = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonResume = (ImageButton) findViewById(R.id.imageButton1);
textview = (TextView) findViewById(R.id.textView1);
buttonResume.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textview.setText("test");
buttonResume.setImageResource(R.drawable.push_pin);
}
});
}
}
In the xml file, you can give the image an onClick attribute, which calls a method in the java class, which calls the xml resource, and passes it the id of the ImageView.
In that java method, you can use
((ImageView) view).setImageResource(int id)
or
setImageDrawable(Drawable d)
to change the image.
Likewise, you can identify the TextView you wish to change using, for example,
TextView tv = (TextView) findViewById( id )
with id being the id of the TextView you wish to find.
You can then use
tv.setText(String s)
to set the text in this view.
In your OnClickListener, you could use
button.setBackgroundResource(YOUR_BUTTON_ID);
or
button.setImageResource(YOUR_BUTTON_ID);
I want to program a function that the toast exist when there is nothing
in the "edittext" box (id / password), but it dosen't work.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.text.Editable;
public class Test extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
EditText id = (EditText) findViewById(R.id.id);
EditText pwd = (EditText) findViewById(R.id.pwd);
Editable Str_id;
Editable Str_pwd;
Str_id = id.getText();
Str_pwd = pwd.getText();
button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v){
if(Str_id==null || Str_pwd == null){
Toast.makeText(Test.this, "Please Enter User ID / Password", Toast.LENGTH_LONG).show();
}
});
}
}
move the following lines inside onClick and try:
Str_id = id.getText();
Str_pwd = pwd.getText();
EDIT: Move
Editable Str_id;
Editable Str_pwd;
Str_id = id.getText();
Str_pwd = pwd.getText();
inside onClick().
I do not recommend toasting a message that a field is required. You're better off just giving the EditText focus, this focus tells the user that this field is required. Its less information on the screen and its subtle, which is important. You don't want to disturb the user too much.
The idea is...
When the submit button is clicked, check the EditTexts if any of them are empty, notify the user through requesting focus, also make sure your UI tells them before hand that the fields required. The hint of the EditText works well for this.