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();
Related
I am a total beginner, I am wondering if anyone could help me out with the code. I am trying to make ideal daily water intake apps.
There will be only one edit text for user to input their weight and I want it to divide for example 0.024. Have button to calculate and then display the answer on screen.
public class WaterCalculate extends Activity {
//Declare textviews as fields, so they can be accessed throughout the activity.
EditText weightuser;
TextView tv4;
ImageButton calculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.water_calculate);
//Bind the EditText views
weightuser = (EditText)findViewById(R.id.weight);
tv4 = (TextView)findViewById(R.id.res);
calculate = (ImageButton)findViewById(R.id.calc);
calculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
calculate();
}
});
}
private void calculate() {
//get entered texts from the edittexts,and convert to integers.
Double value1 = Double.parseDouble(weightuser.getText().toString());
//do the calculation
Double calculatedValue = (value1/0.024);
//set the value to the textview, to display on screen.
tv4.setText(String.valueOf("You need " + calculatedValue + "\nliters of water per day" ));
}
}
When i run the apps the button to calculate its not working and it show the app has stopped. Appreciate for the help.
I thought the problem is in this line.
tv4.setText(String.valueOf("You need " + calculatedValue + "\nliters of water per day" ));
Give a try like this...
tv4.setText("You need "+Double.toString(calculatedValue)+"\nliters of water per day");
and also ensure that you catched the exceptions at necessary places like converting editText value into double.
Try the below code, am sure it will work, when you use click listener for button you should use View.Onclick
class WaterCalculate extends Activity {
// Declare textviews as fields, so they can be accessed throughout the
// activity.
EditText weightuser;
TextView tv4;
ImageButton calculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.water_calculate);
// Bind the EditText views
weightuser = (EditText) findViewById(R.id.weight);
tv4 = (TextView) findViewById(R.id.res);
calculate = (ImageButton) findViewById(R.id.calc);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calculate();
}
});
}
private void calculate() {
// get entered texts from the edittexts,and convert to integers.
Double value1 = Double.parseDouble(weightuser.getText().toString());
// do the calculation
Double calculatedValue = (value1 / 0.024);
// set the value to the textview, to display on screen.
tv4.setText(String.valueOf("You need " + calculatedValue
+ "\nliters of water per day"));
}
}
I know that title is fancy but this is going on in my android app.
I am storing the names of websites which user wants to open in an arraylist of strings. Then an array of buttons is made whose size depends on the size of arraylist.
Then I set the text of the buttons using the arraylist. This works perfectly and all the buttons are assigned correct text.
Then I set the on click listeners of each button and create an implicit intent to open the web site.
Problem that is occuring is that despite the fact the text of the buttons are set perfectly , There is some problem with the listener because no matter which button i press, always the site of last button is opened and hence last button dictatorship.
Here is my code.
package com.example.hp.myproject;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created #CreativeLabsworks
*/
public class Act3 extends Activity
{
LinearLayout ll1; Button[] bt_arr; String s;
LinearLayout.LayoutParams params1;
ArrayList<String> sites;TextView t1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent=getIntent();
sites= intent.getStringArrayListExtra("arraylist");
/*Creating a linear layout to hold editTexts*/
ll1=new LinearLayout(this);
ll1.setOrientation(LinearLayout.VERTICAL);
/*set the width and height of the linear layout*/
params1=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
setContentView(ll1, params1);
/*Initializing the button array*/
bt_arr = new Button[sites.size()];
/*Initialize each button*/
for(int i=0;i<bt_arr.length;++i)
{
bt_arr[i] = new Button(this);
ll1.addView(bt_arr[i]);
}
setText();
attach_listeners();
}
public void attach_listeners()
{
/*this function attaches listeners with all buttons in the button array*/
for(int i=0;i<bt_arr.length;++i)
{
s=sites.get(i);
bt_arr[i].setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://"+s));
startActivity(i);
}
});
}
}
public void setText()
{
/*This function sets the text of the various buttons*/
int j;
for(int i=0;i<bt_arr.length;++i)
{
j=sites.get(i).indexOf(".com");
bt_arr[i].setText(sites.get(i).substring(4,j));
}
}
}
The problem that you have is that
s=sites.get(i);
S is being set by the last element.
Whenever a button is clicked it is taking the S from the last element since S is outside the onClickListener instance that you have.
I would suggest to save the url in the Button tag and then use it When the button is clicked.
for (int i = 0; i < bt_arr.length; ++i) {
s = sites.get(i);
bt_arr[i].setTag(s);
bt_arr[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String website = (String)v.getTag();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://" + website));
startActivity(i);
}
});
}
Intent i=new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://"+s));
startActivity(i);
Maybe you do not write i.setclass()?
I have one main layout with one LinearLayout. In this LinearLayout I inflates xml layouts that contain a form with multiple edittext fields(4 EditText) in it. every time i click on add button xml layouts that contain a form with multiple edittext fields(4 EditText) add in linearlayout.
On first attempt I show only one form. There is button for adding more forms. Suppose If user clicks on "Add" button two times then user have three total forms. I successfully get all these three forms.
i am set Tag child view so easy to get one bye one edittext value
but my problem is when i click on save button all edittext data
get and add in array ..
Button buttonAdd = (Button) findViewById(R.id.pdBtnAddDoc);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myRoot = (LinearLayout) findViewById(R.id.linearLayoutAdd);
LayoutInflater inflater = (LayoutInflater)PrimaryDoctorFragment.this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
childView = inflater.inflate(R.layout.fragment_primary_doc_add,myRoot);
// childView.setId(tag++);
childView.setTag(tag++);
}
});
here is my save button:-
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < tag; i++) {
System.out.println("i:- " + i);
View aView = myRoot.findViewById(i);
EditText name = (EditText) aView
.findViewById(R.id.childEtName);
Log.d("Gettext :- ", name.getText().toString());
}
}
});
on save button all four text field data get and add in array..
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.
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.