I'm new to android programming and not really good at java programming.
Simple question here:How to store value typed in EditText to array once button
is pressed, the way that I will able to compare each index to a constant value of
another variable.
//your array
String[n] array;
//your button
Button b;
///your edittext
EditText e;
if(b.isPressed())
array[x]=edit.getText().toString();
OR using ArrayList
ArrayList<String> n= new ArrayList<String>();
//your button
Button b;
///your edittext
EditText e;
if(b.isPressed())
n.add(edit.getText().toString());
Well this is quite easy.
xml layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/txtField"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
So in your activity you type the following:
EditText text = (EditText) findViewById(R.id.txtField);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String s = text.getText().toString();
// then you do whatever you like with it
}
});
Related
i am basically developing a small mathematics app, in a activity their will be problems like additions subtractions etc. the user has to fill the answers in edittext from the custom buttons from 0-9, a dot, a slash button and a backspace button which i created on the same activity. now i like to add up and down button, so that when the up button is pressed the cursor has to move towards upside edit text and vice versa.
here is a sample code which i used
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView et1;
TextView et2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(android.R.id.et1);
et2 = (EditText) findViewById(android.R.id.et2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) et2.getText(), et1.getSelectionStart());
et2.requestFocus();
}
});
}
}
You could create an array of of your EditTexts and a variable containing your current position, defaulted to 0, meaning the first text box. Then if the user presses the up button, if the variable is greater than 0, set the position -1 and then get the textbox object from the array and call focus(). Below is an example piece of code, its not accurate but should get you started
List<EditText> textfields = null;
int currentTextFieldPosition = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textfields = new ArrayList<EditText>();
textfield1 = (EditText)findViewById(R.id.textfield1);
.....
textfields.add(textfield1);
......
}
protected OnClickListener mBtnUpClickListener = new OnClickListener()
{
public boolean onClick()
{
if (currentTextfieldPosition > 0)
{
currentTextfieldPosition--;
textfields.get(currentTextFieldPosition).focus()
}
}
}
How would I display a certain number of EditTexts on an Android layout based on user input? For example, I am creating a simple GPA Calculator app, and I need the multiple EditTexts based on however many classes the user is taking. I want to make the range from 1 to 6 classes. Would the easiest way be to create 6 EditText fields and only display however many the user needs when he or she specifies, or is there a better way to do this?
Thank you!
You can create the EditText programatically.
btnClick.setOnClickListener(new OnClickListener(){
//loop based on classes needed
EditText myEditText = new EditText(context); // Pass it an Activity or Context
myEditText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(myEditText);
});
Check this out.
// Try this way,hope this will help you...
**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edtNoCreate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter no EditText wan create"
android:inputType="number"/>
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp">
<LinearLayout
android:id="#+id/lnrDynamicEditTextHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
**MainActivity.java**
public class MainActivity extends Activity{
private LinearLayout lnrDynamicEditTextHolder;
private EditText edtNoCreate;
private Button btnCreate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrDynamicEditTextHolder = (LinearLayout) findViewById(R.id.lnrDynamicEditTextHolder);
edtNoCreate = (EditText) findViewById(R.id.edtNoCreate);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(edtNoCreate.getText().toString().length()>0) {
try {
lnrDynamicEditTextHolder.removeAllViews();
} catch (Throwable e) {
e.printStackTrace();
}
int length = Integer.parseInt(edtNoCreate.getText().toString());
for (int i=0;i<length;i++){
EditText editText = new EditText(MainActivity.this);
editText.setId(i+1);
editText.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("EditText "+(i+1));
lnrDynamicEditTextHolder.addView(editText);
}
}
}
});
}
}
for(int i=0;i<3;++i)
{ LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
EditText edttext= new EditText(this);
edttext.setId(i);
edttext.setLayoutParams(params);
layout.addView(edttext);}
}
I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());
I have updated to ADT version 20. After that I can't use EditText. For API level 16 it shows some error. For lower API levels there is no error but I can't get the input of EditText. It shows an empty result.
Sample code I used:
java code
public class sample extends Activity {
EditText edt;
Button btn;
TextView txt;
String str;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView2);
str = edt.getText().toString();
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
}
}
XML CODING
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Search"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10pt"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
Actually, you are trying to get text from EditText in onCreate() of Activity, which has no any text contains yet.
So Just put the line, str = edt.getText().toString(); in button's click() then you will get the result,
Like,,
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
str = edt.getText().toString();
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
Your statement str = edt.getText().toString(); should be inside the onClick handler.
The code cannot access the value because after onCreate ends the value found in the EditText abd str is beyond the scope. So put the str = edt.getText().toString(); statement inside the onClick so that when user clicks the button it can access the value.
I think you are trying to get EditText (i.e. edt) value in onCreate() and I think you are entering value after the completion of onCreate() method.So that means you are not getting value for your str.
That's why you are not able to set the value to TextView.
just put text inside your button otherwise it will not work becz whatever u are getting in strings is also working like u are just cre
I am trying to read a EditText box and send its contents to another method from within a Custom dialog box. My current code causes a Force Close. The logcat is very vague... however I know the uncaught exception takes place in this method:
public void getName(){
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("New Game");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
final EditText inputBox = new EditText(this);
//set up text
final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Enter Your Name...");
//set up button
final Button button = (Button) dialog.findViewById(R.id.namebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = inputBox.getText().toString();
setName(str);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
Here is the custom XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/linearLayout1">
<TextView android:text="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/nameprompt"></TextView>
<EditText android:layout_width="fill_parent" android:id="#+id/editText1" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Player">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="wrap_content" android:id="#+id/namebutton" android:text="Ok" android:layout_width="fill_parent"></Button>
</LinearLayout>
</RelativeLayout>
Any ideas???
You have the EditText in your XML for the Dialog's layout... and you're properly using findViewById() to instantiate your TextView...
You need to do the same for the EditText, also use findViewById() to instantiate it.
final EditText inputBox = (EditText) dialog.findViewById(R.id.editText1);
Something is up with the inputBox object. You create it in this method, but I don't see you actually adding to the layout anywhere. When this method completes, you'll be displaying the dialog box, but the input box won't be displayed anywhere. In fact, I think that inputBox might be garbage collected since there are not references to if around after the getName() method completes. Therefore when you call get input on it, it might be null.
I think what you meant to do was this:
final EditText inputBox = (EditText)dialog.findViewById(R.id.editText1)