How to recreate buttons in previous activity? - java

I have a problem with code in Android Studio.
I have ActivityA and ActivityB.
In ActivityA I have buttons. ActivityB is about settings. For example, I can choose the theme of the app. All done using SharedPreferences.
If I change theme to DARK with this code:
Button Settings = (Button) findViewById(R.id.settings);
Settings.setTextColor(Color.BLACK);
Settings.setBackgroundResource(R.drawable.shapestylethis3);
and I press back to go o ActivityA - then buttons are changed.
Now when I'm in ActivityB and I wanna change back for theme LIGHT then I would like to get back this default button on ActivityA:
style="#android:style/Widget.Button.Small"
But I don't know how to achieve that. ActivityB is changing right after clicking the button "save" because apart from saving to SharedPreferences I used also recreate(); in onClick.
But when I put recreate() in the onResume in ActivityA, then it's like an infinite loop. I will be really thankful for helping me finding a solution.
Thank you in advance.

You can easily avoid the recrate() going in the infinite loop in your ActivityA using a public static variable or a SharedPreference (any of these two you might prefer).
Let us have a public static variable in ActivityA like the following.
public static boolean shouldRecreate = false;
Now when you are changing the style from ActivityB, set the ActivityA.shouldRecreate = true and do not call the recreate().
Now in the onResume function of your ActivityA check the value of shouldRecreate and call the recreate() function accordingly.
#Override
protected void onResume() {
super.onResume();
if (shouldRecreate) {
recreate();
shouldRecreate = false;
}
}
Hope that helps!

Related

How to use onBackPressed in an activity that swipes the screen with another activity?

I've a little problem. I've two activities (GalleryActivity and GalleryVideoActivity) and after swiping between them, i would to come back to the first activity (GalleryActivity) pressing just one time on back button because i've to press back button as many times as i swipped. Is it possible? Thanks in advance to everyone!
So if i understand your issue is that you are moving between activities and everytime you move they create new instances so as a solution to suggest is as following ,
in your manifest file , in activity section add launchMode="SingleInstance" , as this would create only one instance of that activity .
This is an example
<activity
android:name=".ui.apppassword.PasswordRestoreActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"/>
In GalleryVideoActivity override onBackpress method and navigate back to the GalleryActivity by putting simple intent.
Here is what you need to add to GalleryVideoActivity in order to implement onBackPressed as Mike stated:
#Override
public void onBackPressed() {
Intent toGalleryActivity = new Intent(this, GalleryActivity.class);
startActivity(toGalleryActivity);
}
This starts GalleryActivity everytime the back button is pressed in GalleryVideoActivity.

How to setTheme to dark on one Button

Guys are possible setTheme from ThemeUtils with one button ?? look at screenshot just one button 'invert' to set Light to Dark and Dark to Light ? if possible how to do it ?? pls help me
my code :
case R.id.Invert:
ThemeUtils.setTheme(this, "dark");
return true;
look this image : https://drive.google.com/file/d/0B5SpWSpauPDuSGtWREgybnB6aEk/view?usp=drivesdk
You have to call the setTheme method on the Activity before calling SetContentView
Therefore to change theme of an Activity that is already open, you would need to restart it.
For example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(YOUR_THEME_FROM_SHARED_PREFS);
setContentView(...)
}
and
case R.id.Invert:
// Set theme in shared Prefs here
this.recreate(); // restart the activity
return true;
Yes, sure. Define theme, you want to set in style folder. And instead of your string write code R.style.YourOwnTheme

Updating textView constantly

I need to update 2 textViews everytime someones opens the activity they are on. The problem is that if you tap the back button to go to that activity the onCreate method won't activate and the data won't update.
Put the code to update in activity onResume method.
If you enter another app, your activity is paused. Remember that OnCreate is only called when your activity is started. You need to run your code also in OnResume:
#Override
public void onResume(){
super.onResume();
// Your code to activate TextView
}

Android QR Scanner: How to quit ZXingScannerView.ResultHandler to get back to where I came from

In my first steps in exploring Android I now start with QR scanning.
Works all pretty well. But I am not able to come back from the ResultHandler after read the QR successfully to my MainActivity.
public class MainActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler
{
private ZXingScannerView mScannerView
....
#Override
public void handleResult(Result rawResult)
{
// my results are ok in rawResult
// the scanner does not scan anymore but it is still there
// how to go back to my main activity???
}
public void ClickButton (View view)
{
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
}
}
I tried
mScannerview.stopCameraPreview
mScannerView.stopCamera
this.finish
setContentView(R.layout.activity_main); // shows my activity_main
// but I can not click anything
Thanks!!
EDIT
I added some code to describe it a bit better. The idea is from
https://www.numetriclabz.com/android-qr-code-scanner-using-zxingscanner-library-tutorial/
Your question isn't clear but I'm assuming you want to restart the scan process. Normally, you'd have to restart the SurfaceHolder to be in preview mode. Luckily for you the ZXingScannerView already has a method to do that. Call mScannerView.resumeCameraPreview(this) to restart the scan process.
Otherwise can you clarify? You say you want to go back but you're already in MainActivity
If you want to go back into activities/fragments stack you can try Activity.onBackPressed()
if you are in a fragment you must call this method against attached Activity
What do you want is not going back to your activity. You want to restore activity's layout.
I think the better choice is to add ScannerView to your activity's layout file with android:visibility="gone". Then in on click you can get this view and change it's visibility to VISIBILE.
Then when you have handled scanning result, you can reset yuoir ScannerView to visibility = GONE
I too was stuck with this problem for an hour, just like you. And later realised..
To solve this problem DON NOT implementing the ZXingScannerView in the same activity or fragment. Instead start a new activity when you click the button and this activity is just for the ZXingScannerView
Once the Scan is done finish and pass the data back to your activity or fragment
Just restart your MainActivity before this.finish()
the code below will start your main activity through intent...
worked fine for me
startActivity(new Intent(this,MainActivity.class));
this.finish();
remove from onCreate method this line setContentView(your layout) and when you finish scan write it after you stoped the camera then you can use your layout after scan
I had a bit of a look into android concepts and activities.
I put the QR handling in a 2nd activity and it worked well with the finish ().
Thanks for help anyway!!
I think it's too late, but I came with the same problem and I had so find a solution by myself.
You were on the right way, you need two steps more.
I called the methods where I link and set the listener of any buttons
There are the methods
Basically you were right where you set the content view, but you need to give the buttons their functionality back.
(I know its late, but better late than never). Good luck!

The difference between implementation of back and cancelbutton?

I m a newbie an trying to learn Java/Android-programming.
I m doing an app for Android in Eclipse and created some buttons.
I have a back and a cancel button.
Example:
I have a EditText there you can write in your name. If you write yourname and press the backbutton, then u will go back to the previous Activity, but if you go to the same Activity, then you will still see the name that you wrote in the EditText.
But if you press the cancelbutton, you will go back to the previous Activity, but when you come back, yourname will be empty. I will "kill" or "stop" the Activity.
This is the code I use for the Backbutton, what would you use for the Cancel Button?
Thank YOU.
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonBack:
Intent intent = new Intent (AllActivity.this, MenuActivity.class);
startActivity(intent);
break;
For the cancel button you can use the below method, this will kill the activity.
finish()
so in your code it will look something like this:
public void onClick(View v) {
switch(v.getId()){
case R.id.cancel:
finish();
break;
There was little difference in this as per requirement of process or application flow. For cancel and back as work are same for example if you open any dialog and provide cancel button will close/dismiss your dialog same way the back button do this. While for implementing with the Activity you if you implement for closing current activity you can just finish with both option by just calling finish() method. As back button was normally work for finish you current activity and back.
Another way to do this that you may be interested in is to wipe out the content of the EditText yourself.
You would need to have in your xml file an id defined for the EditText so that you could access it programatically.
<EditText
layout stuff here:
android:layout_width="fill_parent"
...
and then the id attribute
android:id="#+id/edit_text_id"
>
then in your code you would put the following in your class (not inside any method):
EditText anEditText;
then in your onCreate(), after the inflation of the layout (if it comes beforehand it will cause the app to crash):
anEditText = (EditText) findViewById(R.id.edit_text_id);
the name edit_text_id is not significant, but it is what we used in the layout file
next add to the onClick method for cancel (after the case statement):
//this wipes the text from the textbox
anEditText.setText("");
// add the rest of the back button code after this and your good!
Best of luck! Remember that we were all newbies once. If you want to be a good android programmer, I suggest that you get a strong background in Java first. This free book helped me very much!
Java Notes

Categories

Resources