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~
Related
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
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 am trying to pass an object to an activity through Intent, and as I'm debugging, I noticed that apparently I never even get into the onCreate() method of the activity. Even though the activity starts, I have the layout and everything, I don't get a chance to debug, because the breakpoint inside the onCreate() method is never reached. How is this possible, what am I not understanding here? Help :)
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
String selection = itemsArray[groupPosition][childPosition];
Class calc = null;
switch (selection) {
case "Calc1":
calc = Calc1.class;
break;
case "Calc2":
calc = Calc2.class;
break;
}
Intent intent = new Intent(Main.this, calc);
intent.putExtra("controller", getController());
startActivity(intent);
return false;
}
});
The activity is started on a OnChildClick event, the object I'm trying to pass is the controller (returned by getController() method)
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.calc1);
controller = (Controller) getIntent().getSerializableExtra("controller");
setController(controller);
editA = (EditText) findViewById(R.id.editA);
editB = (EditText) findViewById(R.id.editB);
}
This is the onCreate(), I set the breakpoint at setContentView(), and like I said, apparently I never get there... I feel like I'm missing something here.
Btw. I'm brand new to android ;)
Thanks for reading!
The Android activity lifecycle can be a bit tricky at first. Here's a diagram that explains it fairly well: https://github.com/xxv/android-lifecycle and you can get a lot more detail on all the activities here: https://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)
It's possible that the activity goes straight to onResume or onRestart, but if you want to find out for sure (and this is a great way to learn more about the activity lifecycle!) put some logs in a bunch of the different activity methods (onCreate, onPause, onRestart, etc...). Then try things like navigating between the different activities, killing the app, pressing the home button, locking your screen, rotating the screen, etc. You'll notice that the flow is not exactly the same, and it will help you figure out where to put your code in your particular use case. Hope this helps!
When set break point, did you run in Debug Mode ?
To run in debug mode
try menu Run > Debug 'app' (Alt + Shift + D)
To run in release mode
try menu Run > Run 'app' (Alt + Shift + X)
If you are running in release mode, when application started, to start debug, choose menu Run > Attach debugger to Android process > choose your process then go to your activity you want to debug again.
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
I have an application that helps users organize prescriptions. I have one listview that shows medications and I use an EditText to allow the user to filter the list. The problem I'm having is that the CursorLoader is replaced each time the orientation changes.
From what I understand, the LoaderManager.initLoader() function should be called in onActivityCreated(). In my particular case, I don't have a fragment so I put the initLoader() call inside onPostCreate():
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getSupportLoaderManager().initLoader(MEDICATION_LOADER, null, this);
}
And here is the filter I'm using:
// Set text filter
mMedication.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mMedicationAdapter.getFilter().filter(s);
}
});
I removed the empty beforeTextChanged and afterTextChanged methods to shorten this.
So, what appears to be happening (from using the debugger) is that each time I change the device orientation, initLoader() is called again and the entire list of medications are displayed, not just the filtered ones. Is there a way to implement onSaveInstanceState() to store the current filtered state of the adapter?
Or am I using the text filter wrong? Should I pass the charSequence as a Bundle argument to the loader, or create another loader that handles the text as an argument?
A solution is you can keep the current activity when orientation changes, Just set the Activity to handle config changes in AndroidManifest.xml:
Android config changes
e.g.
<activity
android:name="com.test.MyActivity"
android:configChanges="screenSize|orientation"/>
If your edittext was inflated through a layout file and it has an id; the framework should be automatically save its text.
You could try checking right before you attach your textwatcher, if the edittext has any text inside, then filter your list and then attach your textwatcher.