I have two questions here, both related.
When I create a new project with a blank activity in Android studio the project always creates an app with an ActionBarActivity.
public class MainActivity extends ActionBarActivity {
1.How can I change my project defaults to remove this default and just create a project as:
public class MainActivity
OR
to update it so its is not creating an app with deprecated code. Please see image.
Unfortunately there is no way to reconfigure the content of main activity created by default. Possibly the Google engineers will fix it some time in the future.
What you can do here to save some time is move the caret to the ActionBarActivity while being in the MainActivity.java file and hit Alt+Enter. The quick fix "Replace with AppCompatActiivty" will pop up and all you'll need to do is hit Enter to apply it. It's easier than replacing ActionBarActivity with AppCompactActivity by typing.
For 1. change to public class MainActivity extends Activity
For 2. change to public class MainActivity extends AppCompactActivity
EDIT: Creating project without activity.
Related
I've used this answer: Same Navigation Drawer in different Activities to implement a navigation drawer that is shown in all activities.
I am not sure if this is the right way to go, but at the moment I have a main application, that contains some activities and implements the NavigationDrawer in the BaseActivity.
I would like to start by creating many android library projects that will have activities/fragments in them and their own logic. So for now, besides the main application, I've created one android library project and I can successfully start one of its' activities from the main application (from the NavigationDrawer).
The problem is that I cannot make it extend the BaseActivity and therefore the NavigationDrawer is not shown.
So, at the moment in the main application, I have:
activity_main.xml // it is the launcher activity
activity_one.xml
activity_two.xml
MainActivity.java
OneActivity.java
TwoActivity.java
That works fine. Now I want to add the library project into play, in which I have:
activity_three.xml
ThreeActivity.java
And now the problem is that in the library project, I cannot extend from the BaseActivity like that:
public class ThreeActivity extends BaseActivity{ // <-- There's an error here: Cannot resolve symbol 'BaseActivity'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
}
}
Does anybody have any suggestions how to do that?
Should I just create another library project that has the BaseActivity and the navigation drawer ?
Should I just create another library project that has the BaseActivity and the navigation drawer ?
Either that, or get rid of all the libraries and move everything into the one project. A library cannot inherit from classes from a project that consumes the library. A library can inherit from classes from a project that the library itself depends upon, though.
So, the app/ module could depend upon the three/ library (that has ThreeActivity), which in turn depends upon the base/ library (that has BaseActivity).
I have eclipse(Kepler) and latest sdk installed. Whenever i create a new activity inside android project, activity is extending ActionBarActivity. But i need new activity to extend Activity.class. How to do this?
There is no other way to extend an class with Activity.Class automatically other than changing declaration of the class manually.
I have two different classes. My main activity class and a separate menu.class. I want to be able to get the menu class to load from the mainactivity.class. The menu button does not work on my device or emulator when the mainActivity class is running. I am not sure how to call the menu.class from the mainActivity. I do not want to put the menu java code in the mainActivity.
I am asking for the way to add a snip of code to load a menu activity from one class to another, not to fix my code. I can not find any examples online to show how to load a menu class that is not also written in your main activity class.
public class lifeMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.life_menu);
}
That is a snip of my menu activity. My main activity is called MainActivity
You can make your MainActivity extend your MenuActvity that entends Activity. Hard to know since you did not provide a snip of your menu class.
I have been successfully using HoloEverywhere's PreferenceActivity for a while. I am now importing the SlidingMenu library and has been going well so far until I extended SlidingMenu's SlidingPreferenceActivity:
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingPreferenceActivity;
public class SettingsActivity extends SlidingPreferenceActivity{
...
and
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import org.holoeverywhere.preference.PreferenceActivity;
public class SlidingPreferenceActivity extends PreferenceActivity implements
SlidingActivityBase {
...
In my SettingsActivity I load some Header's to show the top level categories.
My problem is now onHeaderClick() is no longer working. I trace it all the way through and cannot find the error. Following the stack trace I see HoloEverywhere ends up making an Intent it eventually passes to the Android Activity, but I don't see anything that is obviously wrong.
What about the SlidingMenu Library could cause Fragments to stop working in a PreferenceActivity?
Turns out my problem had nothing to do with SlidingMenu. In my manifest I set all my Activitys to be singleInstance, so that I am not making multiple of the same Activity when I am navigating between them using the SlidingMenu. This was preventing the Intent that was created by PreferenceActivity when the Header was clicked from recreating the activity with the given fragment.
I removed the singleInstance reference for now and everything is working.
I am creating an android project . In that i i got the ids from the XML file and wrote the click events in a (class or activity) . I want to use the widgets from another class without getting the id's again. like (Button btn=(Button)findViewById(R.id.button1);) i want to use this code in one class
But I want to use the Button btn in another class and also the click event should work.
If you want to use any View in more than one Activity you can create a class BaseActivity that extends Activity and include all the common Views in that Activity and then extend this BaseActivity to all the Activity in which you want to use common views/layouts/headers/footers. For a Pseudo code you can check my answer here.