Android studio, what is wrong with this onBackPressed() code? - android

I used below code to close app after back button pressed. Some time ago it worked but I tried to use it again and have:
Error:(88, 13) error: class, interface, or enum expected.
If I remove this code app can be build, I don't see where is problem?
Here is the MainActivity where onBackPressed is implemented:
package com.example.chab.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.os.Handler;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView) findViewById(R.id.a);
ImageView image1 = (ImageView) findViewById(R.id.b);
ImageView image2 = (ImageView) findViewById(R.id.c);
ImageView image3 = (ImageView) findViewById(R.id.d);
ImageView image4 = (ImageView) findViewById(R.id.e);
ImageView image5 = (ImageView) findViewById(R.id.f);
ImageView image6 = (ImageView) findViewById(R.id.g);
Picasso.with(this).load("http:/1.jpeg").into(image);
Picasso.with(this).load("http://1.jpeg").into(image1);
Picasso.with(this).load("http://1.jpeg").into(image2);
Picasso.with(this).load("http://1.jpeg").into(image3);
Picasso.with(this).load("http://1.jpeg").into(image4);
Picasso.with(this).load("http://1.jpeg").into(image5);
Picasso.with(this).load("http://1.jpeg").into(image6);
Button btnOne = (Button) findViewById(R.id.Btn);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Activitydwa.class);
startActivity(intent);
}
});
}
} //THIS BRACKET MUST BE MOVED TO THE END OF CODE!
private Boolean exit = false;
#Override
private void super.onBackPressed() {
if (exit) {
this.finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
EDIT:
Solved.
Bracket before Boollean must be moved to the end of code. Then all works. Thank you.

Your first problem is that you have your method implemented outside of the class. In java, methods need to belong to a class, interface, or enum. Double check your brackets and move your method to the inside of your class brackets. Your second problem is that you have the wrong signature for the onBackPressed method. See the code below:
This is what you have:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
}
private Boolean exit = false;
#Override
private void super.onBackPressed() {
// ...
}
This is what you need:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
}
private Boolean exit = false;
#Override
public void onBackPressed() {
// ...
}
}

Try to put
super.onBackPressed()
before finish()

Try this.finish() OR
Try super.onBackPressed(); OR
call NavUtils.navigateUpFromSameTask(this); to get back to previous screen/ activity

Related

Clear previous responses in main activity

I have a simple form in the main activity. On submitting the form, activity 2 starts. When I go back to the main activity (by clicking on the back button), I want to clear all the responses.
When you assign an id to any widget, and the activity or fragment stop, then the onSaveState is called for any widget to save the actual value and show it when you come back to that activity.
So, to clean up your EditText when you came back from another activity you'll have to do it manually, I suggest you to do it in onRestart function according to the Activity life cycle
You could overwrite onResume() in your main activity and clear the corresponding EditTexts there like
#Override
public void onResume() {
editText.setText("");
}
or
You can overwrite onBackPressed() in activity 2 like this
#Override
public void onBackPressed() {
Intent intent = new Intent(Activity2.this, MainActivity.class);
startActivity(intent);
}
I don't know an elegant solution, but it should works:
public class MainActivity extends AppCompatActivity {
private boolean secondActivityLaunched;
#Override
protected void onCreate(Bundle savedInstanceState) {
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
secondActivityLaunched = true;
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
#Override
protected void onStart() {
super.onStart();
if (secondActivityLaunched) {
secondActivityLaunched = false;
clearForm();
}
}
}
1st Activity: MainActivity.java
package com.test.activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText name=(EditText) findViewById(R.id.name);
EditText email=(EditText) findViewById(R.id.email);
EditText phone=(EditText) findViewById(R.id.phone);
Button submit = (Button)findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent it=new Intent(MainActivity.this,Activity2.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
});
}
}
2nd Activity: Activity2.java
package com.test.activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent it=new Intent(Activity2.this,MainActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(it);
finish();
}
}

Android: Click on back button twice?

I am a beginner in android.
My Scenario
I have a screen A which has 2 buttons
Button A
Button B.
When I open my application screen A opens up with the above 2 buttons, when I click on Button B a textview and Edittext is displayed.
What I want ?
When the back button is pressed the textview and edittext should hide and when I press back again, I should exit out of Screen A.
What have I tried so far ?
Is my below code correct for what I want ?
Main Activity.xml
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView title;
EditText userinput;
Button buttonA,buttonB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
userinput = (EditText)findViewById(R.id.userinput);
title = (TextView)findViewById(R.id.title);
buttonA = (Button)findViewById(R.id.buttonA);
buttonB = (Button)findViewById(R.id.buttonB);
buttonA.setOnClickListener(this);
buttonB.setOnClickListener(this);
}
#Override
public void onBackPressed() {
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.buttonA:
break;
case R.id.buttonB:
title.setVisibility(View.VISIBLE);
userinput.setVisibility(View.VISIBLE);
break;
}
}
}
I referred this and this link but did not understand. If some one could help me in achieving me want I want 1
When the back button is pressed the textview and edittext should hide
and when I press back again,
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE) {
super.onBackPressed();
return;
}
title.setText(null);
userinput.setText(null);
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
Do like this.
#Override
public void onBackPressed() {
if(title.getVisibility()==View.VISIBLE)
{
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
else
{
finish();
}
}
Hop it will do what you want.
Change the code to below
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE){
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
super.onBackPressed();
}

Android TextView and Buttons not working in my app

I am learning Android and trying one simple Android app development, I got one demo code from my lecture and the teacher simply do the following:
There are 2 buttons, 1 textview. When touching button A, it will show "text A" in the textview, while touching button B, it will present "text B" in textview.
I followed the code and rewrote it, but i can't get the correct result when I ran with emulator.
When I touch either button, there's no content in the TextView. But my teacher's reference code works:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
How is report(View v) called? I can't understand how this class is called. Could someone please help me out?
You need to let your button know that, when pressed, report() should be called. This may be done through the android:onClick attribute of your button on your layout's XML:
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:onClick="report" />
Or by code, attaching an OnClickListener to the button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
report(v);
}
});
}
Hope it helps.
You should implements the View.OnClickListener, and override that OnClick method. You also need to setOnClickListener on your buttons. So your code should be like:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity implements View.OnClickListener{
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 =(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
button2 =(Button)this.findViewById(R.id.button2):
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}

toggling background of button not working

I have created an app that suppose to toggle the button background color on every click. Here is the code:
package com.example.flash.light;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button screen = (Button) findViewById(R.id.screen);
Drawable background = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(background);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(background);
}
}
});
}
Click on the button doesn't response.
Thanks
I belive the problem here is that the resource returned by your
getResources().getDrawable(int id)
is different everytime you call it. Android just constructs new instance of drawable class instead of returning old one. (At least I belive that there is no caching in this case)
Comparing three different instances with == operator will never return true.
Second thing is an obvious bug in the code:
Button screen = (Button) findViewById (R.id.screen);
Drawable background = screen.getResources().getDrawable(screen.getId());
if(background == getResources().getDrawable(R.drawable.black)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
screen.setBackgroundDrawable(**newBackground**);
}
if(background == getResources().getDrawable(R.drawable.white)){
Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
screen.setBackgroundDrawable(**newBackground**);
}
instead of background you should have newBackground there.
Try this :
public class MainActivity extends Activity {
private boolean isBlack = false;
#Override
public void onCreate(Bundle savedInstanceState) {
// your code
final Button screen = (Button) findViewById (R.id.screen);
isBlack = true;
screen.setBackgroundColor(Color.BLACK);
screen.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
if (isBlack){
screen.setBackgroundColor(Color.WHITE);
isBlack = false;
}
else{
screen.setBackgroundColor(Color.BLACK);
isBlack = true;
}
}
});
}
}

I can't setText() in another class in android

I have a TextView with the id android:id="#+id/yazi", and I have a button that has build in android:OnClick="gonderB"
and I can complie this code:
package com.seri.bir;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
Bilmez b;
TextView t;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = new Bilmez();
t = (TextView) findViewById(R.id.yazi);
}
public void gonderB (View v,TextView t,Bilmez b){
String s = " ..."+this;
b.yaziYaz(v,s,t);
}
}
class Bilmez {
public void yaziYaz(View v,String s,TextView t){
t.setText(s);
}
}
However I have an error.
Can I setText in another class?
You can overwrite onClick of the activity. Avoid the using of the android:OnClick="gonderB" line in the xml file. I think it is better to implement the onClickListener and attach it to View Objects within your code.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Bilmez b;
TextView t;
Button bt;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = new Bilmez();
t = (TextView) findViewById(R.id.yazi);
Button bt = (Button) findViewById(R.id.btn);
bt.setOnClickListener(this);
}
#Override
public void onClick(View clickedView) {
switch (clickedView.getId()) {
case R.id.btn:
String s = "...." + this;
b.changeText(t,s);
break;
}} //end of main class }
In the changeText method you change the text of the TextView. This method can if be placed in another class if you like that.
class Bilmez {
public void changeText(TextView t, String s){
t.setText(s);
}
}
Perhaps what you are experiencing is a need to run the function on the UI thread?
public void yaziYaz(View v,final String s,final TextView t) {
runOnUiThread(new Runnable() {
public void run() {
t.setText(s);
}
});
}
i think you should do that:
public void gonderB (new View v,TextView t,Bilmez b){
String s = " ..."+this;
b.yaziYaz(v,s,t);
}

Categories

Resources