I have taken 3 edittext in my form and three spinner. I can easily get the data from edit text and display it on Textview, but the problem is how to get the data from spinner and display all the edit text and spinnner values in textview.
I am creating a form in which when the button is clicked all the data from edittext text and spinner is displayed on Textview
Try this:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v){
String item= spinner .getSelectedItem().toString();
textView .setText(item);
}
});
Try this
Button btn = (Button) findViewById(R.id.your_btn);
btn.setOnClickListner(new OnClickListener {
public void onClick(View v)
{
String spinnerStr = yourSpinner.getSelectedItem().toString();
String editTextStr = yourEditText.getText().toString();
yourTextView.setText(spinnerStr + editTextStr);
}
}
);
please try this one:
String textValue= yourSpinner.getSelectedItem().toString();
Related
Here I have created EdiIexts dynamically by clicking the button, how can I take values from these EditTexts? I have seen many examples but I am unable to get values!
final LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
Button add_btn=new Button(this);
add_btn.setText("Click to add TextViiews and EditTexts");
ll.addView(add_btn);
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText et=new EditText(getApplicationContext());
ll.addView(et);
You can get dynamically created edittext's value the same way you would do with edittext of .xml file.
String value;
add_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
value = et.getText().toString();
}
});
add this code where you want to get the edittext's value
EditText et2=(EditText)ll.getChildAt(l1.getChildCount()); //make sure to create new edittext variable do not use "et"
String s=et2.getText().toString();
Declare EditText et as a Class level Member variables for your Activity,
private EditText et = null;
Now, in Button's onClick
add_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
et = new EditText(getApplicationContext());
ll.addView(et);
Now, you can get the EditText et values any where in your Activity scope, using
if(et != null)
String value = et.getText().toString();
try this,
LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
EditText edit = (EditText) linearLayoutForm.findViewById(id);
String value = edit.getText().toString();
I want to delete the characters in my edittext one by one. I've research quite a bit but there are some problems, please advise. this is my sample code.
I created a delete button "ImageButton buttonDelete;"// XML imageButton1
and my edittext is "EditText display;"
display = (EditText) findViewById(R.id.editText1);
buttonDelete.setOnClickListener(new View.OnClickListener()
{
public void onClick()
{
// Get edit text characters
String textInBox = display.getText():
//Remove last character//
String newText = textInBox.substring(0, textInBox.length()-1);
// Update edit text
display.setText(newText);
Try this:
// Get edit text characters
String textInBox = display.getText().toString();
if(textInBox.length() > 0)
{
//Remove last character//
String newText = textInBox.substring(0, textInBox.length()-1);
// Update edit text
display.setText(newText);
}
BtonBackSpace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String bs = null;
if(Tfield.getText().length()>0){
StringBuilder Strb = new StringBuilder(Tfield.getText());
Strb.deleteCharAt(Tfield.getText().length() - 1);
bs = Strb.toString();
Tfield1.setText(bs );
}
Hello I want to know how to highlight all Words that is inputted in the EditText and will appear in the TextView this post is related to this one Highlight Textview Using EditText
Say et is your EditText and tv is TextView object. Use the following code:
public class MotivationalQuotesActivity extends Activity {
/** Called when the activity is first created. */
Button next;
EditText et;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et);
tv = (TextView) findViewById(R.id.tv);
tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");
next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");
String ett =et.getText().toString();
String tvt =tv.getText().toString();
int ofe = tvt.indexOf(ett,0);
Spannable WordtoSpan = new SpannableString( tv.getText() );
for(int ofs=0;ofs<tvt.length() && ofe!=-1;ofs=ofe+1)
{
ofe = tvt.indexOf(ett,ofs);
if(ofe == -1)
break;
else
{
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
}
}
}
});
}
}
The result is:
One easy and quick way of highlighting a text, is to use string replace method. Replace the desire string with font tag
Spanned strHTML= Html.fromHtml("<center>"+full_string.replaceAll(strSrch,"<font color='yellow'>"+strSrch+"</font>")+"</center><br/>");
TextView tv=(TextView) v.findViewById(R.id.txtPager);
tv.setText(strHTML,TextView.BufferType.SPANNABLE);
I'm a beginner in application development. My problem is, that when I run my app and I click on the Calculate button, the program stops. The code:
public class screen1 extends Activity {
private EditText name;
private CheckBox box1;
private Spinner spinner;
private TextView text1, text2, text3, text4, text5, text6;
private Button calcbutton, closebutton;
String strength;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.military_ranks , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hubSpinner.setAdapter(adapter);
name = (EditText)findViewById(R.id.editText1);
strength = name.getText().toString();
box1 = (CheckBox)findViewById(R.id.checkBox1);
text1 = (TextView)findViewById(R.id.textView4);
text2 = (TextView)findViewById(R.id.textView6);
text3 = (TextView)findViewById(R.id.textView8);
text4 = (TextView)findViewById(R.id.textView10);
text5 = (TextView)findViewById(R.id.textView12);
text6 = (TextView)findViewById(R.id.textView14);
final Button calcbutton = (Button) findViewById(R.id.button1);
calcbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
double sebzes;
if(box1.isChecked()){
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1*(1+1/100);
text1.setText(Double.toString(sebzes));
}
else{
sebzes = (((rank-1)/20+0.3)*((str/10)+40))*1;
text1.setText(Double.toString(sebzes));
}
}
});
final Button closebutton = (Button) findViewById(R.id.button2);
closebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
In the edittext component you should be able to write numbers only. I don't know why it's not working.
The problem are these two lines:
int str = Integer.valueOf(strength);
int rank = spinner.getSelectedItemPosition()+1;
The first won't fail if you use only numbers in your EditText but it would be better to ensure that or at least catch the exception that is thrown when you try to convert a character to a numberical value. Additionally you could also use Integer.valueOf(strength).intValue(); even so it is normally not really necessary.
The real problem is the second line. You declared the variable spinner but you never instantiate it. That's why you will get a NullPointerException there.
On an unrelated note: You also should start your class name with a capital letter to follow the Java naming conventions.
You're not instantiating spinner anywhere, but you're referencing it in the second line of your button click method. Probably a null reference problem.
I am unsure as to how to approach this.
I have taken some guidelines given by another person on here and have now gotten to the point of it crashing when I push the button (the monitor)
http://i.stack.imgur.com/C8WgN.png
I want to be able to post a toast notification when the button is pushed, and then have it go away after a few seconds
Also, is there a way to do it to where it will still create a toast notification although some of the info (like line2) is empty?
basic.java:
public class Basic extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic);
Spinner spinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.state_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final Button display = (Button) findViewById(R.id.Button01);
display.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText firstField = (EditText) findViewById(R.id.First);
EditText lastField = (EditText) findViewById(R.id.Last);
EditText line1Field = (EditText) findViewById(R.id.Line1);
EditText line2Field = (EditText) findViewById(R.id.Line2);
EditText cityField = (EditText) findViewById(R.id.City);
EditText stateField = (EditText) findViewById(R.id.Spinner01);
EditText zipField = (EditText) findViewById(R.id.Zip);
//EditText phoneField = (EditText) findViewById(R.id.Telephone);
final Editable firstName = firstField.getText();
final Editable lastName = lastField.getText();
final Editable lineOne = line1Field.getText();
final Editable lineTwo = line2Field.getText();
final Editable city = cityField.getText();
final Editable state = stateField.getText();
final Editable zip = zipField.getText();
//final Editable phoneNumber = phoneField.getText();
Toast toDisplay = Toast.makeText(null, firstName, 10);
toDisplay.show();
}
});
}
}
I would like the layout of the toast to be multi-lined, and looking like a postal address
First Last
Street Line 1
Line 2
City, State Zip
Toast.makeText(Change.this, "First Last \n Street Line 1 \n Line 2 \n City, State Zip", Toast.LENGTH_SHORT).show();
By using above toast we can display the output in multilines.In above statement i am using static text in that place u put required fieds like
Toast.makeText(Change.this, firstField.getText().toString()+"\n"+ lastField.getText().toString()+"\n"+ City.getText().toString(), Toast.LENGTH_SHORT).show();