As a newbie in Android programming I'm playing with some Material Design. So I wanted to implement a Navigation Drawer. After looking for libraries I decided to use #neokrees MaterialNavigationDrawer (https://github.com/neokree/MaterialNavigationDrawer). I added it into my build.gradle and followed the instructions from the wiki.
As a base I'm using a MainActivity which holds a toolbar and a fragment.
I created a custom style for the drawer and wrote a simple NavigationDrawer subclass.
import it.neokree.materialnavigationdrawer.MaterialNavigationDrawer;
public class NavigationDrawer extends MaterialNavigationDrawer {
#Override
public void init(Bundle savedInstanceState) {
this.addSection(newSection("Section", new MainActivity.PlaceholderFragment()));
}
}
Then I added this to AndroidManifest.xml
<activity android:name=".NavigationDrawer"android:theme="#style/MyNavigationDrawerTheme" />
So everything compiles fine, the app starts up but nothing happens. So as first try I added a section to the drawer, relating to an activity. As I saw that the first item should be a fragment, I replaced it accordingly. Then I looked into the example app, but I don't get it to compile. Anyway, there is also no drawer-relevant code in the MainActivity.
So it's most probably a really dumb failure, but I have no idea anymore. So I would be thankful for any help.
Related
I'm aware the View structure for Android is completely different (ie. Activities instead of View Controllers) but I need to create a Navigation Bar that persists between views.
Is the best way really to have just one single Activity and then a lot of Fragments?
If so, has this implementation already been done somewhere else that I can use? It seems like something that would come up a lot as I've seen numerous Android apps do this.
I made a Framework (github) to provide a hierarchical navigation pattern, with animations to provide sense of navigation, rather than launching new Activities every time.
Here's how to use it:
Add the framework to your project as a Module
Add a new Java class in your project ("File - New - Java Class").
Note: If you are editing the Activity.java file that provides you the template, delete all its implementations and leave it empty.
Make it extend NavigationActivity
Implement all the NavigationActivity abstract methods
(in Android Studio if you click Alt + insert and select implement - methods all the function definitions are automatically generated).
public class NavigationTest extends NavigationActivity{
#Override
public Fragment firstFragment() {
//return the first fragment that will be shown
}
#Override
public Boolean showBackButtonInFirstFragment() {
//show back button already in the first Fragment
//set to True if this activity is called by another Activity
//the back button will then pop back to the previous Activity
}
#Override
public Boolean showMasterDetailLayoutInTablets() {
//set to false if you don't want a master-detail layout in tablets
}
}
Presenting a new Fragment
You can present a new fragment (with a nice animation) by calling the pushFragment method from NavigationActivity.
public void pushFragment(Fragment newFragment, animationType animation, boolean showAsDetailFragmentIfPossible)
newFragment (Fragment): New Fragment that will be presented
animation (animationType): Animation type enum: RIGHT_TO_LEFT, BOTTOM_TO_TOP, FLIP
showAsDetailFragmentIfPossible (boolean): If set as True, the user is in a Tablet, and you are using a master-detail layout, the Fragment will be shown in the detail Fragment (the panel in the right)!
Since you can access the activity from any Fragment with the getActivity() method, you can show a new Fragment from the currently displaying Fragment.
For example you can put this code within a button click listener:
NextFragment f = new NextFragment();
NavigationActivity nav =((NavigationActivity)getActivity());
nav.pushFragment(f,NavigationActivity.animationType.RIGHT_TO_LEFT,false);
You don't have to worry about implementing the back button behaviour. This is handled automatically by the NavigationActivity class.
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 joined Android Studio and Android App project a few week ago, and I'm trying to create a simple app with ActionBar options.
When I start Android Studio, following Android Dev. Training, I meet always this rendering error:
Android Studio doesn't found android.support.v7.app.ActionBarActivity (ecc...)
I resolved this error by setting a different theme. But whenever I try a new project i will do this again and again. First question: there is a way to fix this rendering problem? I meet this problem also in the MainActivity.java, where the extends ActionBarActivity is deleted with a line, telling me it is deprecated and advising me to use AppCompatActivity. Should i follow this tip?
Question number two: i read like 100 post on guys who can't show the actionbar in the activity, and I tried everything, but when I link actionbar menu with activity through:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I still can't view the menu I created on the actionbar.
Some images can maybe help me to explain better my problem:
http://i.stack.imgur.com/5nPFx.png
And there is the layout of my activity:
http://i58.tinypic.com/oau8ed.png
As you see, there is no icon button i added and no setting button like menu layout show.
ActionBarActivity is now deprecated use AppCompatActivity
to avoid this error:
Android Studio doesn't found android.support.v7.app.ActionBarActivity
you have to Download the latest version of the Support Library! and then you can use
import android.support.v7.app.AppCompatActivity
public class MainActivity extends AppCompatActivity{
...
...
...
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.
On the iPhone I use a Navigation Controller to push and pop Views from. Very handy.
Is there an equivalent in Android?
This is an old question, but I believe the answer has changed. It is now possible to imitate the Nav stack in iOS in android using Fragments.
http://developer.android.com/reference/android/app/Fragment.html
Basically instead of jumping from Activity to Activity you instead stay in one Activity that controls the display, organization, and animation of Fragments which each contain their own behavior much like the NavController / UIViewController model in iOS.
It is also backwards compatible as a static library so you can implement it on pre-Honeycomb devices.
Strategies for Honeycomb & backward compatibility
Typically in android, each view is displayed in its own Activity. You can read about activities in the application fundamentals documentation. To move to a new Activity, or view, you use an intent.
If you haven't done so yet, I'd highly recommend reading through those introductary android docs. They aren't too long, and do a good job of explaning the basic program structure.
I made a Framework (github) to provide a hierarchical navigation pattern, with animations to provide sense of navigation, rather than launching new Activities every time.
Here's how to use it:
Add the framework to your project as a Module
Add a new Java class in your project ("File - New - Java Class").
Note: If you are editing the Activity.java file that provides you the template, delete all its implementations and leave it empty.
Make it extend NavigationActivity
Implement all the NavigationActivity abstract methods
(in Android Studio if you click Alt + insert and select implement - methods all the function definitions are automatically generated).
public class NavigationTest extends NavigationActivity{
#Override
public Fragment firstFragment() {
//return the first fragment that will be shown
}
#Override
public Boolean showBackButtonInFirstFragment() {
//show back button already in the first Fragment
//set to True if this activity is called by another Activity
//the back button will then pop back to the previous Activity
}
#Override
public Boolean showMasterDetailLayoutInTablets() {
//set to false if you don't want a master-detail layout in tablets
}
}
Presenting a new Fragment
You can present a new fragment (with a nice animation) by calling the pushFragment method from NavigationActivity.
public void pushFragment(Fragment newFragment, animationType animation, boolean showAsDetailFragmentIfPossible)
newFragment (Fragment): New Fragment that will be presented
animation (animationType): Animation type enum: RIGHT_TO_LEFT, BOTTOM_TO_TOP, FLIP
showAsDetailFragmentIfPossible (boolean): If set as True, the user is in a Tablet, and you are using a master-detail layout, the Fragment will be shown in the detail Fragment (the panel in the right)!
Since you can access the activity from any Fragment with the getActivity() method, you can show a new Fragment from the currently displaying Fragment.
For example you can put this code within a button click listener:
NextFragment f = new NextFragment();
NavigationActivity nav =((NavigationActivity)getActivity());
nav.pushFragment(f,NavigationActivity.animationType.RIGHT_TO_LEFT,false);
You don't have to worry about implementing the back button behaviour. This is handled automatically by the NavigationActivity class.
There are thee basic types in Android to show UI in Android:
View
Fragment
Activity
Google IO 2018 introduced Navigation component which should make life easier. It is a wrapper under a standard mechanisms.
Here you can find NavGraph which looks like storyboard and NavController which help to navigate to destination