I've a very noobish inquiry about an issue with a display - java

package walmart.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
name = (EditText)findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.tvdisplay);
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name.setText("");
}
});
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (name.equals("Electronics")) {
display.setText ('5');
} else if (name.equals("candy")) {
display.setText ('1');
} else if (name.equals("Tobacco")) {
display.setText ("1");
}
}});
};}
No matter what i do, nothing shows up in my output. I'm very, very new to JAVA, so I can't seem to find out why nothing's showing up on my display.
edit Edited my code to what i currently have. Still not working.

Looking at that code, the issue might be with how you are initializing name and search.
Change your code:
name = (EditText) findViewById(getResources().getIdentifier("etName", "id", getPackageName()));
search = (Button) findViewById(getResources().getIdentifier("btnSearch", "id", getPackageName()));
to this:
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
Also, add a TextView to display your answer into your main.xml file, give it an id (let's say display for example) android:id="#+id/display" and then add it to your activity so you can change it:
display = (TextView) findViewById(R.id.display);
and then you can update it by calling:
display.setText('some text');
Thus the start of your file should look like this:
public class WalmartActivity extends Activity {
/** Called when the activity is first created. */
EditText name;
Button search;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText) findViewById(R.id.etName);
search = (Button) findViewById(R.id.btnSearch);
display = (TextView) findViewById(R.id.display);
....
Next update your if statements from:
if (name.equals("Electronics"))
to:
if (name.getText().toString().equals("Electronics")) {
display.setText("something");
}
An example piece of code:
Demo2Activity.java
package com.demo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Demo2Activity extends Activity {
private Button button;
private EditText editText;
private TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textText);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString().equals("Electronics")) {
textView.setText("found");
}
}
});
}
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="main2.xml"
android:id="#+id/textText" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>

Related

How to increase value of a number on button press?

I am trying to create an app to increase the value of a number on screen to the next number when the button is pressed. It is not working. I have given my Java and XML code below.
XML code:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="+" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:paddingLeft="200dp"
android:textSize="25sp"
android:text="0" />
Java code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
}
public void display(final int n) {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(n);
}
});
}
public void increment(View view) {
a = a + 1;
display(a);
}
}
The button is unreactive.
When you click the button, increase the value of a. Then pass the a to display method. Finally display the value to textView.
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
display(a);
}
});
}
public void display(int n) {
txt.setText("" + n);
//txt.setText(n);
}
}
You have to increment your counter and set new text
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText(String.valueOf(++a));
}
});
}
}
if I am not mistake, we can not call `onClickListener in some method.
In any case, change the code in your button to the next one and it should work.
int a = 0;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
txt.setText(a);
}
});
in this case, that code helps. becose it increases value a on 1, all time when we click on the button.
When you click the on the button, increase the value of a and display to the textView
public class MainActivity extends AppCompatActivity {
Button btn;
TextView txt;
protected int a = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a++;
txt.setText(a + "");
}
});
}
}

How to solve the '.MainActivity.<init>(MainActivity.java:16)' error?

EditText object is not created, why?
EditText editText=(EditText)findViewById(R.id.edittext);
This is for an App that extends AsyncTask class. fetchingData is a class that extens AsyncTask class.But it gives error on creating object of EditText.
package com.example.zafar.omdb_7;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String string;
Button button;
public static TextView data;
//
private EditText editText = findViewById ( R.id.edittext );
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=findViewById(R.id.fetched_data);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//
#Override
public void onClick(View v) {
// string = editText.getText ().toString ();
fetchingData process= new fetchingData();
process.execute();
}
});
}
}
Your code should be something like this:
package com.example.zafar.omdb_7;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String string;
Button button;
public static TextView data;
//
private EditText editText;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=findViewById(R.id.fetched_data);
editText = findViewById ( R.id.edittext );
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//
#Override
public void onClick(View v) {
// string = editText.getText ().toString ();
fetchingData process= new fetchingData();
process.execute();
}
});
}
}
You won't find the view before you set it on the activity. I hope this helps.
You can't use the following:
public class MainActivity extends AppCompatActivity {
// This won't work!
private EditText editText = findViewById ( R.id.edittext );
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
}
Because you can't call the findViewById before you set the Activity layout with setContentView(). It means that you can't find a view if you didn't have the layout yet.
So, edit your code to something like this:
public class MainActivity extends AppCompatActivity {
...
private EditText mEdtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Now you have the layout, you can find the view
mEdtText = findViewById(R.id.edittext);
...
}
}

Trouble Updating TextView Quantity via Button

Android Noob here. I am attempting to increment and decrement an TextView via plus and minus buttons. I am running into trouble connecting the method to the image view. Using Java 9. Assuming I'm making a basic mistake.
<TextView
android:id="#+id/home_tds_var"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="24dp"
android:text="0"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/home_touchdowns_header" />
<Button
android:id="#+id/home_td_plus"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="+"
app:layout_constraintStart_toEndOf="#+id/home_tds_var"
app:layout_constraintTop_toBottomOf="#+id/home_touchdowns_header"
android:onClick="homeTDButtonPlus"/>
package com.example.android.scorekeeper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int homeTouchdowns = 0;
public void homeTDButtonPlus(View view) {
homeTouchdowns = homeTouchdowns + 1;
TextView home_tds_var = (TextView) findViewById(R.id.home_touchdowns_var);
home_tds_var.setText(homeTouchdowns);
//Updating the View
//tvId.setText(String.valueOf(homeTouchdowns));
}
}
According to your xml code, your findViewById() has an incorrect id:
TextView home_tds_var = (TextView) findViewById(R.id.home_touchdowns_var);
Should be:
TextView home_tds_var = (TextView) findViewById(R.id.home_tds_var);
I must say, using onClick in xml layout is discourage. You should create listeners in java code to decouple view from other application layers. A simple example:
TextView home_tds_var;
int homeTouchdowns = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home_tds_var = (TextView) findViewById(R.id.home_tds_var);
Button btn = (Button) findViewById(R.id.home_td_plus);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
homeTouchdowns = homeTouchdowns + 1;
home_tds_var.setText(homeTouchdowns);
}
});
}
Change
TextView home_tds_var = (TextView) findViewById(R.id.home_touchdowns_var);
to
TextView home_tds_var = (TextView) findViewById(R.id.home_tds_var);
public void incrementHomeTdsVar(View view) {
final TextView homeTdsView = findViewById(R.id.home_tds_var);
int newValue = Integer.parseInt(homeTdsView.getText().toString()) + 1;
homeTdsView.setText(String.valueOf(newValue));
/* Home Score + 7 Increment */
final TextView homeScoreView = findViewById(R.id.home_score_var);
int homeScorePlus = Integer.parseInt(homeScoreView.getText().toString()) + 7;
homeScoreView.setText(String.valueOf(homeScorePlus));

Why does my app crashes while implementing button?

Here is the code...whenever I launch the app on my phone it just crashes. I'm new to android programming so please help me. After doing some tests it comes out the problem is when I asssign the value to Value1 and Value2(just a guess). please help me out here. Thanks in advance.
MainActivity.java
package com.example.android.add;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
TextView ans;
int Value1;
int Value2;
int result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = Value1 + Value2;
ans.setText(""+result);
}
});
}
}
and the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.add.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Number 1"
android:textSize="40dp"
android:id="#+id/et1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_below="#+id/et1"
android:hint="Number 2"
android:textSize="40dp"
android:id="#+id/et2"
android:inputType="number"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addBtn"
android:layout_below="#+id/et2"
android:text="Add"
android:layout_marginLeft="80dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your Answer"
android:textSize="40dp"
android:layout_margin="40dp"
android:layout_below="#+id/addBtn"
android:id="#+id/tv"/>
</RelativeLayout>
You get the value before it inserted,you have to get value when user click on add button and get value1 and value2 inside onClickListner
change according to this
package com.example.android.add;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
TextView ans;
int Value1;
int Value2;
int result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
ans.setText(""+result);
}
});
}
}
Edit your code to this and it should run. The problem is you are accessing the values in EditTexts too early, even before you type any in them. You need to access these values in onClick() of your add button
package com.example.android.add;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
TextView ans;
int Value1;
int Value2;
int result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
// If you get value from edit texts here it will cause an error
// since onCreate is called at the very beginning and you have not
// entered any value in num1 and num2.
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Instead put them here so you have already inserted values
// into the fields before they are retrieved:
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
ans.setText(""+result);
}
});
}
}
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
this code write into onClick() method.remove from onCreate() method.
Logic is : get edit text value on click listener of Button
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
result = Value1 + Value2;
ans.setText(""+result);
}
catch(Exception e)
{
Log.e("Exc",e.toString());
}
}
});
Using OnClickListeners for button is discouraged. It has been replaced with the XML Attribute: android:onClick = "functionName".
And this function is to be implemented in the Activity to which the layout file is attached to. Hence, in MainActivity.java:
public void funcitonName(View v){
// your code here
}
There may be a number of string conversion error
Value1 = 0;
try {
Value1 = Integer.parseInt(num1.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
Your app is crashing because of these two lines
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
here you are trying to convert string to integer where string is null if editText doesnt have ant values these type of error occur while typeCasting so add validation before typeCasting like below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
num1 = (EditText) findViewById(R.id.et1);
num2 = (EditText) findViewById(R.id.et2);
add = (Button) findViewById(R.id.addBtn);
ans = (TextView) findViewById(R.id.tv);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(num1.getText() != null && num2.getText() != null) {
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
result = Value1 + Value2;
ans.setText("" + result);
}
}
});
}

Android Java implement 2 buttons in 1 page

I finally learned about adding a button to a page and actually making it navigate to another activity "XML Page". Anyway, I have been trying to add 2 buttons in the same page which navigate each to a different XML's Pages. All I did was copy the first button which worked and then change the button name and all other things the first button works but the second isn't. It shows a click but nothing happens after.
Back1 Button works. TMode Button does the trouble.
Eclipse is not showing errors.
Here is my code -
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
/** Called when the activity is first created.*/
Button btn;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.Back1);
btn.setOnClickListener(btn2Listener);
}
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.TMode);
btn.setOnClickListener(btn3Listener);
}
private OnClickListener btn3Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent3=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent3);
}
};
}
Try something like this:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}
You should in your XML file define two buttons
<Button
android:id="#+id/button1"
... />
<Button
android:id="#+id/button2"
... />
And then in your Activity in onCreate() method you do
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
...
})
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
...
});
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent1);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}

Categories

Resources