going back to previous layout makes the button stop working - java

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

Related

My Android App crashes after adding an OnclickListener

I tried to build an easy Android App using Java and, obviously, Android studio. After adding an OnClickListener the app keeps crashing on the emulator.
public class MainActivity extends AppCompatActivity {
EditText first = findViewById(R.id.firstInput);
EditText second = findViewById(R.id.secondInput);
EditText result = findViewById(R.id.result);
private Button add;
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
clicked();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = new Button(this);
add.setOnClickListener(onClickListener);
};
void clicked(){
int i = Integer.parseInt(first.getText().toString());
int z = Integer.parseInt(second.getText().toString());
int r = i + z;
result.setText(r);
}
};
Wrong location of object declaration
It should be below the setContentView
EditText first = findViewById(R.id.firstInput);
EditText second = findViewById(R.id.secondInput);
EditText result = findViewById(R.id.result);
Button haven't added to the layout
add = new Button(this);
addContentView(add, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Note: This is impractical, you should have added this on your layout xml unless you wanted to create dynamic layout
Warning issue of setting a text by integer
From:
result.setText(r);
To:
result.setText(String.valueOf(r));

How to make android app in java with counter automatically increment integer by 1 every 24 hours?

I want to make the app increase the integer by 1 every 24 hours, I have set a sharedpreferences to save the value in the app whenever it closes.
How can I get the app to calculate days passed whenever opened?
I also want the button to reset the counter.
I have tried searching google but I'm not finding a solution specific to my problem.
public class MainActivity extends AppCompatActivity {
private int days;
Button bt;
TextView textView;
private SharedPreferences counter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = getApplicationContext().getSharedPreferences("shareware", Context.MODE_PRIVATE);
days = counter.getInt("days",0);
bt = (Button) findViewById(R.id.bt);
textView = (TextView) findViewById(R.id.textView);
textView.setText(Integer.toString(days));
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
days++;
textView.setText(Integer.toString(days));
counter.edit().putInt("days", days).apply();
Log.i("Test: ", Integer.toString(counter.getInt("days", days)));
Button bt = (Button)findViewById(R.id.bt);
bt.setText("Reset");
}
});
}
}

Unable to display textview on button click in android

I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method

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

Created a button in Android and want to keep track of clicks but click count isn't being recorded correctly

EDIT: Just fixing up my old questions and I can see the problem younger me had. It would be clicked once and it would run through the whole loop.
I wanted to create a button and keep track of the number of clicks with a for loop.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Connects the button by ID and reference to the design button
Button button= (Button) findViewById(R.id.button);
//When the button is clicked it will display the amount of times the button is clicked
button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView text= (TextView) findViewById(R.id.text);
for(int i = 0;i < 20;++i) {
String s = Integer.toString(i);
text.setText(s);
}
}
}
);
When I run it and click the button once it automatically goes to 19 rather than start at 0.
The onClick method is called once every time the button is clicked. You have a for loop in there counting up to 19 so every time the button is clicked you are rapidly setting the text again with the numbers 1-19 until it reaches 19.
If you want to keep a seperate counter you can make a global variable then increment it by one in the onClick method. This should provide the desired behaviour:
private int count = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Connects the button by ID and reference to the design button
Button andrewsButton = (Button) findViewById(R.id.andrewsButton);
//When the button is clicked it will display the amount of times the button is clicked
andrewsButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView andrewsText = (TextView) findViewById(R.id.andrewsText);
count++;
andrewsText.setText(String.valueOf(count));
}
}
);

Categories