I am trying to increment and decrement the middle textview via buttons on the sides. The application starts up finely but by the time I click on any of the buttons it gets closed with following error.
Error: process <package> has stopped unexpectedly.
My main.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="250dp"
android:text="+"
android:textSize="40dp" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="80dp"
android:layout_toRightOf="#+id/button1"
android:layout_marginTop="75dp"
android:layout_marginLeft="80dp"
/>
<Button
android:id="#+id/button2"
android:layout_width="50dp"
android:layout_height="250dp"
android:layout_alignParentRight="true"
android:text="-"
android:textSize="40dp" />
My java file:
public class IncrementDecrementActivity extends Activity {
int counter;
Button add, sub;
TextView tv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.button1);
sub = (Button) findViewById(R.id.button2);
tv = (TextView) findViewById(R.id.tv1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
tv.setText(counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
tv.setText(counter);
}
});
}
}
Well. This should be.
In this statement
counter--;
tv.setText(counter);
It should be
counter--;
tv.setText(String.valueOf(counter));
I guess the error is
ResourceNotFoundException
How you encountered this error?
on the code above.
you declared int counter;
let's consider the value of counter is 0
and then
you called
counter--; // take note this is an integer
tv.setText(counter);
counter is now -1
by calling setText(counter);
will search first a string value from strings.xml with an integer -1 and set the text to textview
If android fails to find that string it will throw ResourceNotFoundException
:)
Related
I am trying to make a customisation app for wooden signs. When the user clicks say on a 10 x10 size sign i want the edit text box to change size to demonstrate what they have done. Can this be done on android studio?
This is my code so far, I think what i have done is changed the size of the text but it crashes when the button is clicked so i don't know.
Java File:
public class Letters extends AppCompatActivity {
int txtSize = 14;
EditText Wood;
Button bSize, bSize1, bSize2, bSize3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_letters);
Button bSize = (Button) findViewById(R.id.bSize);
Button bSize1 = (Button) findViewById(R.id.bSize1);
Button bSize2 = (Button) findViewById(R.id.bSize2);
Button bSize3 = (Button) findViewById(R.id.bSize3);
bSize.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 15) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 16) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 17) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
bSize3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (Wood.getTextSize() == 18) {
txtSize--;
Wood.setTextSize(txtSize);
} else {
txtSize += 0;
Wood.setTextSize(txtSize);
}
}
});
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="60dp"
android:layout_width="match_parent"
android:weightSum="1">
<Button
android:id="#+id/bSize"
android:text="15 x 10 cm "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bSize" />
<Button
android:id="#+id/bSize1"
android:text="20 x 15 cm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bSize1" />
<Button
android:id="#+id/bSize2"
android:text="30 x 7 cm"
android:layout_width="103dp"
android:layout_height="61dp"
android:onClick="bSize2" />
<Button
android:id="#+id/bSize3"
android:text="30 x 20 cm"
android:layout_width="0dp"
android:layout_marginBottom="20dp"
android:layout_height="160dp"
android:onClick="bSize3"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/Wood"
android:textSize="40dp"
android:layout_alignTop="#+id/button3"
android:layout_centerHorizontal="true"
android:background="#795548"
android:hint="Choose Letters.."/>
</LinearLayout>
Your application crashes probably because of a NullPointerException. This is because you have not initialized your Wood variable in the onCreate method.
What you should do is this:
Wood = (EditText) findViewById(R.id.Wood);
Button bSize = (Button) findViewById(R.id.bSize);
Button bSize1 = (Button) findViewById(R.id.bSize1);
Button bSize2 = (Button) findViewById(R.id.bSize2);
Button bSize3 = (Button) findViewById(R.id.bSize3);
When your app crashes, there is a logcat error stacktrace in Android Studio. The stacktrace shows you what exception have caused the crash. You can easily solve problems by looking at the logcat logs.
You never initialized your EditText which is causing NullPointerException
Add this
Wood = (EditText) findViewById(R.id.Wood);
inside onCreate method.
Update: This is how you can change the height and width of EditText on button Click
final LayoutParams lparams = new LayoutParams(150,50); // First param is Width and second is height
Wood.setLayoutParams(lparams);
Can someone tell me how to use a Text Field with an onclick listener on a button in android studio and give me the xaml code too.
First you need write in xml file what you need, we need button and textview:
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="textEdit"
android:layout_marginLeft="5dp"
android:textColor="#fff"
android:layout_marginRight="2dp"
android:background="#333"
android:onClick="ButtonOneClick"
android:layout_marginBottom="4dp"
/>
<TextView
android:id="#+id/txtView"
android:layout_width="50dp"
android:layout_height="30dp"
android:hint="text view"
/>
After xml you need define this in class. Because we define android:onClick="ButtonOneClick" in xml,
we can just call it , and set what you want to do.
TextView tv;
tv =(TextView)findViewById(R.id.txtView);
public void ButtonOneClick(View view) {
tv.setText("Test");
}
Other method is to declare button to, and set onClickListener.
Button butt;
butt= (Button) findViewById(R.id.button1);
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv.setText("Test")
}
});
in tampil.xml
<Button
android:id="#+id/button1"
android:text="PENCET"
android:onClick="onPencet" />
and in u're class, use public void nameonClick(view v){ statement }
public class Tampil {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onPencet(View v){
**U're statement }
}
I have Googled and read several Stack Overflow answers and I am pretty sure the problem is with my layout file here. I also cleaned my project so my R.java file was deleted, but I think after I find the error it will be auto-generated.
Here is my XML code.
<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="vertical"
android:padding="20sp"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/update"
android:textSize="25sp" />
<EditText
android:id="#+id/guess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="26sp" />
<TextView
android:id="#+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="6sp"
android:paddingTop="6sp"
android:text="#string/default"
android:textSize="18sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="evaluate"
android:text="#string/btn" />
</LinearLayout>
Here are the java lines where there is an error shown, in case there's a problem here:
Button button = (Button) view;
TextView log = (TextView)(findViewById(R.id.display));
EditText field = (EditText)(findViewById(R.id.guess));
Update: Here is my evaluate method from the MainActivity.java file
public void evaluate(View view)
{
Button button = (Button) view;
TextView log = (TextView)(findViewById(R.id.display));
EditText field = (EditText)(findViewById(R.id.guess));
if(button.getText().toString().charAt(0) == 'S')
{
int guess = Integer.parseInt(field.getText().toString());
if(guess == num)
{
log.setText("Congratulations! You guessed it in " + guessCount + "moves");
button.setText("New game");
}
else
{
guessCount++;
if(guess < num)
log.setText("Try again. Too low");
else
log.setText("Try again. Too high");
}
}
else
refresh(button, log);
}
R.java is deleted whenever there exists any compile error in res folder or in AndroidManifest.xml file. Assuming you have no compile errors(except the files that should be referencing fields in R.java), my first call is to look at evaluate method to see if it is in the current invoking Activity as follows:
Java Code:
public class MyActivity extends Activity {
Button button;
TextView log;
EditText field;
...
...
#Override
public void onCreate(Bundle savedInstanceState) {
button = (Button) new Button(this);
log = (TextView) findViewById(R.id.display);
field = (EditText) findViewById(R.id.guess);
...
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
evaluate(v);
}
});
}
...
...
// **evaluate** is visible(public) and
// within the same activity where the **button** is
public void evaluate(View v) {
...
...
}
}
`
doesn't eclipse show you any error in the foler of res?
in the line of: android:onClick="evaluate", I haven't seen such value before, is it correct?
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 need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});