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
Related
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!
I have an activity_main.xml with
tools:context="com.example.android.newwine.MainActivity"
and
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="goToNext"/>
In my MainActivity.java file I have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
and
public void gaNaarNext(View view){
setContentView(R.layout.activity_main2);
}
I have an activity_main2.xml file with
tools:context="com.example.android.newwine.MainActivity"
When I touch the button next now the activity_main2.xml will be used as ContentView, so that's correct, but when I do anything then (like touching a RadioButton or doing doesn't matter what) in my activity_main2.xml file what calls a method, it says: "New Wine is stopped." Whatever I do what calls a method, the app crashes. I tried much and if I'm going to paste everything I tried here this will be a long, long question:).
But has someone an answer? I really don't know what to do now, so I thought; let's ask something on Stackoverflow. Thanks already!
You might prefer to use another activity or fragments to change your UI as using the method setContentView() multiple times is not recomended because of all the drawing involved.
Since it seems like you don't want to switch activities fragments may be the easiest to use.
This stackoverflow question addresses that matter as well.
After you change your content view, the UI Elements on the screen have changed. All of the variables that you previously set up now point to the old screen - which no longer exists. Once you change your content view, you must set these variables to the correct elements on that layout file. Also, you may want to check out Fragments like Carlos Sifuentes said.
When I do anything then (like touching a RadioButton or doing doesn't matter what) in my activity_main2.xml file what calls a method, it says: "New Wine is stopped." Whatever I do what calls a method, the app crashes.
The problem is you have used activity_main2.xml but you did'nt get the reference of RadioButton of activity_main2.xml. You should get all the view reference after setting new layout using setContentView() and then use as per your needs.
Update your gaNaarNext() method as below:
public void gaNaarNext(View view){
setContentView(R.layout.activity_main2);
// For example: If your activity_main2 contains radio_button then do this
RadioButton radioButton = (RadioButton) findViewById(R.id.radio_button);
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// Do something
}
});
}
Hope this will help~
When turn the switch on it stays on.. however when i leave the activity and come back to it.. it goes back to off. I want it to stay ON OR OFF depending on whats last pressed. I have tried the code below but does not resolve my issue
SwitchButton.setChecked(true);
SwitchButton.setChecked(false);
What you need to do is override these methods in your activity:
#Override
protected void onSaveInstanceState (Bundle outState){
super.onSaveInstanceState(outState);
outState.putBoolean("CHECKED", SwitchButton.isChecked());
}
then in onCreate:
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstaceState);
if(savedInstanceState != null){
boolean isChecked = savedInstanceState.getBoolean("CHECKED");
SwitchButton.setChecked(isChecked);
}
}
If you are minimizing the activity and then returning back to it, and you want all controls to retain their states, then look into implementing saved instance state. This will persist the control values while you minimize / maximize the activity or rotate it. No data is permanently saved to the device. Sample code here:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
If you are closing the app completely and want the app to remember the settings, then consider SharedPreferences, which can be used to save data locally on the device. The data persists until your app explicitly deletes it or you uninstall the app. Sample code here:
http://developer.android.com/training/basics/data-storage/shared-preferences.html
Hey guys, i am making an android application where i want to show a dialog box about legal agreement everytime the application starts, i have a public method showalert(<>); which shows an alertdialog by building a dialog with alertbuilder. I added a call to showalert() method on the onCreate() method of the main activity to show it, but whenever the user rotates the screen, he gets the dialog everytime. The activity restarts itself when the phone is rotated. I tried adding android:configChanges="keyboardHidden|orientation" to my manifest but that doesnt help on this case. Also can i know how to register a new application class on manifest file. I am trying to create an application class and put the code to show dialog on the new class's oncreate method. But i am not being able to load the class when the app starts.
I also checked Activity restart on rotation Android but i dont seem to get a thing. I am pretty much a newbie to android programming, could someone simplify that for me?
Any help would be appreciated. :)
you could maybe look at the onRetainNonConfigurationInstance() activity method, which is called just before destroying and re-creating the activity on screen orientation change.
it allows you to retain an object that could for instance contain a test variable to know if your legal thing was already shown or not.. example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String test = (String) getLastNonConfigurationInstance();
if (!("textAlreadyShown").equals(test)) {
//here : show your dialog
}
}
#Override
public String onRetainNonConfigurationInstance() {
return "textAlreadyShown";
}
Set the main activity to an activity that just shows the legal notice, when it is accepted/cleared, show a second activity ( which is currently the main activity )?
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!