getting text from EditText in Android on click of button - java

Hi I am working on calculator in android.
I am having a EditText in wchich no will displayed depending on click of particular no.for example when user press 1 it will display 1.now when user clicks on 2 it will display 12 and so on.
i have things like this
public class Main extends Activity implements OnClickListener {
public static int no1=0,no2=0,op=0,flag=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText display1=(EditText) findViewById(R.id.display);
Button one= (Button) findViewById(R.id.one);
one.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()==R.id.one)
{
switch(flag)
{
case 1:no1*=10+1;display1.setText(no1);
case 2:no2*=10+1;display1.setText(no2);
}
}
but here i am getting error as "display1 cannot be resolved".
any solution to make work..??

Sometimes a complete code example helps:
public class Main extends Activity implements OnClickListener {
private int no1=0;
private int no2=0;
private int op=0;
private int flag=0;
private EditText display1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
display1=(EditText) findViewById(R.id.display);
Button one= (Button) findViewById(R.id.one);
one.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId()==R.id.one)
{
switch(flag)
{
case 1:no1*=10+1;display1.setText(no1);
case 2:no2*=10+1;display1.setText(no2);
}
}
}
Basically you want to declare display1 as a private instance variable. The other variables do not need to be static, and should probably be declared private. And declarations formatted on separate lines for improved readability.
Have fun.

Because it's only available in your onCreate method. Declare it as a field in your class.
private EditText display1;

It should be better to keep a string showing in the EditText so that you have no difficulties appending and erasing. Then you can parse it to an integer so that you will be able to do math with it.
First you should declare your buttons and edit text globally:
public class Main extends Activity implements OnClickListener {
private EditText display1 = (EditText) findViewById(R.id.display);
private Button one= (Button) findViewById(R.id.one);
private Button two= (Button) findViewById(R.id.two);
If you have the button label set as the number you can use it like this:
public void onClick(View view){
display1.setText(display1.getText.toString() + ((Button) view).getText().toString());
}
That way, you can append the numbers to the end of the string in the display. If you want to use the number it's showing, you can use the following code:
int number = Integer.parseInt(display1.getText().toString());

Declare both your EditText and Button globally.

Related

Change image view with radio button and two activitis

I need to help whith my school project.
I have two activitis - main activity whith image view and setting activiti whit two radio button. I want the image on main activity to change when I clicked on radio button on setting activity.
This is my code of setting activity:
private RadioGroup radioGroup;
private ImageView imageView;
private Integer []photos = {R.drawable.red, R.drawable.blue};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView ) findViewById(R.id.imageView);
this.radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);
int index = radioGroup.indexOfChild(radioButton);
imageView.setImageResource(photos[index]);
}
});
}
Thanks for the answers.
First thing you can't findViewById in class B within the id in class A
Second thing there are many ways to do that what you want
for example you can use interface
or static variable and this solution is so easy
just create an static variable in main activity and in OnResume function do your logic
somthing like this
static Integer photos = R.drawable.red;
Whey static??
because you can access it from any where
so just change it from your setting activity

going back to previous layout makes the button stop working

I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style

How to get Button Value from another class on android studio

I have 2 class, which the first class i created Loop button
and second class i want to get button value from first class..
Here is my code
public class FirstClass extends AppCompatActivity {
public static Button[] btn = new Button[8];
#Override
protected void onCreate(Bundle savedInstanceState) {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rlid);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
for(int i=0; i<btn.length; i++)
{
btn[i] = new Button(this);
btn[i].setTextColor(Color.BLUE);
btn[i].setText((i + 0) + " ");
btn[i].setLayoutParams(lp);
relativeLayout.addView(btn[l]);
}
}
}
public class SecondClass extends Activity {
Button btn2= (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button bb = FirstClass.btn[2]; //get button no 2 from 1st class
bb.setTextColor(Color.YELLOW); // try to change into yellow but nothing happen
}
});
Although bad practice of using views as static fields, your code should work.
Another problem is that you have created buttons in the first activity and done nothing with them. You should probably add them to some layout of the activity.
The good practice would be not to store buttons or any views in static fields, and get results using Intents. https://developer.android.com/training/basics/intents/result.html

How to edit global variables with EditText to pass the value to an other activity?

I have a problem here with variables. I created a simple Android game. The levels depend on the input the user does (birthay and birthmonth are the variables).
I made a simpler app which refers to the global variable problem only.
Here are the files:
Global.java
import android.app.Application;
public class Globals extends Application {
private String someVariable;
public String getSomeVariable() {
return someVariable;
}
public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}
}
MainActivity.java:
public class MainActivity extends Activity {
EditText editText;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
String m = editText.getText().toString();
((Globals) this.getApplication()).setSomeVariable(m);
}
public void saveName(View v) {
Intent first = new Intent(this, FirstActivity.class);
startActivity(first);
}
}
FirstActivity.java:
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
TextView textf = (TextView) findViewById(R.id.textfirst);
String s = ((Globals) this.getApplication()).getSomeVariable();
textf.setText(s);
}
}
The variable is a String so I made the EditText to a String with getText().toString() but it doesn't work.
When I give the value a concrete String value it works properly also
((Globals) this.getApplication()).setSomeVariable(m); to
((Globals) this.getApplication()).setSomeVariable("message");
Any idea how to solve it?
Thank you ^^
You're assigning the EditTexts content to the variable in your onCreate() method where the EditText is probably empty. Do
((Globals) this.getApplication()).setSomeVariable(m);
in your saveName(View v) method and you should be fine.
PS: "it doesn't work" is not a great description of your problem, try to be a bit more precise (what did you expect to happen, what did actually happen).
PPS: Passing your String to the next activity via intent.putExtra() would be a nicer way to pass the data.

How to reference button for onClickListener?

I am trying to create an Android app, and i want to create a on click listener, here is what I have so far.
public void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}
As you see, i am in the very early stages, but where I first referenced amazonButton (before the = sign) button, it turns into red text and it says Cannot resolve symbol 'amazonButton'. Also, I have referenced this method in the onCreate method
This is how you'd go about creating a button and setting a click listener to it.
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
}
}
You can also put the initializations in a single method and call that method, like you've tried :
public class MainActivity extends YouTubeFailureRecoveryActivity {
Button amazonButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
}
private void initializeViews() {
//Make sure you've declared the buttons before you initialize them.
amazonButton = (Button) findViewById(R.id.amazonButton);
amazonButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Define what should happen when the button is clicked.
}
});
// Add more Views initialization here ...
....
}
}
You need to give the type of the variable when you declare it.
Button amazonButton = (Button) findViewById(R.id.amazonButton);
The alternative is to declare it (but not initialize it) outside of any method and then initialize it later.
Button amazonButton;
/* ... */
private void amazonListener() {
amazonButton = (Button) findViewById(R.id.amazonButton);
}

Categories