Update EditText after Button is clicked - android

This EditText is inside an Activity that is part of a TabHost within the main Activity. It's just supposed to be 4 tabs with an EditText and two Buttons on each, one to increment and one to decrement the EditText field. However, if I ever try to setText() on one of the EditText boxes, the app crashes. So when I call setText() in onCreate() it crashes. Any help would be greatly appreciated!
<EditText
android:label="#+id/LifeForP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoText="true"
android:cursorVisible="false"
android:background="#null"
android:textColor="#999999"
android:color="#null"
android:layout_x="90px"
android:layout_y="0px"
android:textSize="250px"
android:maxLength="3"
android:capitalize="sentences"
android:layout_weight="1"
android:freezesText="true"
android:text="20"
/>
public class ActivityTab1 extends Activity {
private EditText lifeView;
int p1Life = 20;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content1layout);
lifeView = (EditText) findViewById(R.id.LifeForP1);
lifeView.setText(getString(R.string.lifeStart)); //Error here
}
#Override
protected void onResume() {
super.onResume();
}
public void p1GainLifeListener(View view) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("test gain 1");
alertDialog.show();
//String show = String.format("", Integer.toString(++p1Life));
//lifeView.setText(show);
}

In your xml, change
android:label="#+id/LifeForP1" to
android:id="#+id/LifeForP1"

When you bind your EditBox in code you reference to
findViewById(R.id.LifeForP1);
This means the EditBox needs an ID.
If you change the android:label to an android:id your code will run fine

Related

Alert Dialog embedded buttton is not clicking in Android Java

Am trying to click on a button i just added to an AlertDialog but its not responding. The button is on a layout file in the resources folder and then i added this layout to the AlertDialog via the Builder method setView()
I accessed the same Button via my mainactivity after inflating a random view with the layout and set an OnClickListener but its still not working
Here is detail code of what i have tried so far
Layout containing the button
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFF"
android:background="#drawable/round2"
android:textSize="20sp"
android:textAlignment="center"
android:padding="5dp"
android:text="Submit"
android:textAllCaps="false"
android:drawablePadding="10sp"
android:id="#+id/submit"
/>
</Linearlayout>
Then in my MainActivity.class, i access the button this way
class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//inflate layout containing button
datView=getLayoutInflater().inflate(R.layout.data_entry,null);
//access button
submit=datView.findViewById(R.id.submit);
//set event listener
submit.setOnClickListener(this);
}
//the interface
#Override
public void onClick(View v) {
check();
}
//custom method check
private void check(){
Toast.makeText(this,"Registering data into database",Toast.LENGTH_LONG).show();
}
}
Despite all that the toast does not show on clicking the button, please help
Please try using android:onClick = "check" in the button in your xml, and also create a method in your MainActivity called public void check(View view){...} with the toast inside of it

why my listview give me null error in custom drawerlayout? [duplicate]

I have code:
public class HelloWorld extends Activity {
private Button buttonOk;
private Button buttonCancel;
private OnClickListener buttonOkListener = new OnClickListener() {
public void onClick(View v){
EditText editText = (EditText)findViewById(R.id.input);
CharSequence textFromInput = (CharSequence) editText.getText();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,textFromInput,duration);
toast.show();
}
};
private OnClickListener buttonCancelListener = new OnClickListener() {
public void onClick(View v){
EditText editText = (EditText)findViewById(R.id.input);
CharSequence textFromInput = (CharSequence) editText.getText();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,textFromInput,duration);
toast.show();
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// ToDo add your GUI initialization code here
setContentView(R.layout.main);
buttonOk = (Button)findViewById(R.id.buttonOk);
buttonCancel = (Button)findViewById(R.id.buttonCancel);
buttonOk.setOnClickListener(buttonOkListener);
buttonCancel.setOnClickListener(buttonCancelListener);
}
}
and the layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Wpisz swoje imię:"/>
<EditText
android:id="#+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/label"/>
<Button
android:id="#+id/buttonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/input"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
<Button
android:id="#+id/buttonCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/buttonOk"
android:layout_alignTop="#id/buttonOk"
android:text="Anuluj" />
</RelativeLayout>
Line with buttonCancel.setOnClickListener(buttonCancelListener); throws an Exception because buttonCancel is null. What i am doing wrong?
there is no problem with your codes, by right everything should work as per normal.
The most common mistake of encountering null via findViewById() method is when you forgot to call setContentView() or called it for the wrong layout.
I suggest cleaning your project and try again!!!!
I was having the same trouble, but after cleaning my project and running it again it works perfectly.
I'm not 100% sure but you are calling the findviewbyid's in the class initialisation. I think this code is called before the onCreate method so the view's cannot be found. Initializing the listeners in the oncreate method should work.
At times there are few points to be considered!
This can always happen even for experienced developers as well. I would suggest few points below,
Check that the findViewById is added in the appropriate life cycle of the activity or fragment which ever under consideration.
Also verify if the layout is inflated correctly and that the valid layout is set in the setContentView - which was incorrect in my case.
Also as mentioned above there are some cases where cleaning the project could help resolve this issue.

How to Move cursor from one EditText to another one and back to the first edit text if a custom button is clicked?

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

Programmatically adding EditText box

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

Edit text not receiving text

I have tried to solve it for hours but been unable to. What is going on is that I setted this editText, called the layout on the activity, and connect it to my variable on the activity, when you click on the field it opens the visual keyboard, but when you press a key, it takes you to a browser-like search screen, instead of just updating the edittext´s text.
on my xml the editText is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background = "#drawable/splash"
android:orientation = "vertical">
<EditText
android:id="#+id/searchEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/hintGray"
android:textSize="20dp"
android:paddingLeft="50dp"
android:paddingRight="10dp"
android:layout_marginTop="300dp"
android:background="#drawable/a01_text_box"
android:hint="Hint"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/a01_boton_buscar"
android:layout_gravity="center_horizontal" />
</LinearLayout>
on the activity I do this
public class SubscribeActivity {
private EditText searchEdit;
private ImageButton searchButton;
private Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe_main);
this.context = this;
findAndInitViews();
}
protected void findAndInitViews() {
searchEdit = (EditText) findViewById(R.id.searchEdit);
searchButton = (ImageButton) findViewById(R.id.searchButton);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String codeText = searchEdit.getText().toString();
Toast.makeText(context, codeText, Toast.LENGTH_LONG).show();
}
});
}
}
I still can't find a way for it not to happen. Can you help me?
Seems like you are not actually playing with activities
This is wrong:
public class SubscribeActivity
It should be something like:
public class SubscribeActivity extends Activity
first I would suggesting to use the getApplicationContext() Method of Activity - instead of casting the Activity down to your Context-Object. Beside you must extend Activity. Im wondering that your code is running... But I assume it's an copy and paste mistake.
Your onCreate(Bundle b) should be public instead of protected.
If the fault is still there I would comment the code not needed for displaying the EditText. And then step by step including the code back again.
Try it different way.
public class SubscribeActivity extends Activity{
private EditText searchEdit;
private ImageButton searchButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe_main);
searchEdit = (EditText) findViewById(R.id.searchEdit);
searchButton = (ImageButton) findViewById(R.id.searchButton);
}
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String codeText = searchEdit.getText().toString();
Toast.makeText(SubscribeActivity.this, codeText, Toast.LENGTH_LONG).show();
}
});
}
}
Please try to do in this way and let me know the result.

Categories

Resources