I have created a baseactivity with a navigation drawer and searchview. I am trying to extend this in other activities, but also have different content. However if I extend the base activity, and try to set a different content view, I don't get the navigation drawer/search view.
How do i extend the baseactivity, but still customize the ui for each activity?
Thanks for you help!
The best way to do this would be to use Fragments. However, to use multiple activities, extend the base activity and then use setContentView() with the layout for that activity. Ensure that the xml for that layout contains the navigation drawer layout too.
Sounds like you want to do something like this. You'll see that any of the Activities that inherit from this one and call setContentView will now be adding their content to the content frame in your already inflated navigation layout.
public class BaseActivity extends FragmentActivity
{
private FrameLayout mContentFrame;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// notice we call super.setContentView here
super.setContentView(R.layout.activity_main);
// and other nav stuff here
mContentFrame = findViewById(R.id.content_frame);
}
#Override
public void setContentView(int layoutResID)
{
setContentView(getLayoutInflater().inflate(layoutResID, mContentFrame, false));
}
#Override
public void setContentView(View view)
{
mContentFrame.removeAllViews();
mContentFrame.addView(view);
}
#Override
public void setContentView(View view, LayoutParams params)
{
mContentFrame.removeAllViews();
addContentView(view, params);
}
#Override
public void addContentView(View view, LayoutParams params)
{
mContentFrame.addView(view, params);
}
}
Related
I used this link to create navigation drawer in my app. the question is :is there any way to create a basic activity witch have this drawer and extend other activity from this? i read this but all these links used DrawerLayout not Fragment Navigation Drawer and i can not use them. Is there any tutorial to solve my problem?
Here is what I do :
I create an abstrac class called RootActivity that extends Activity and which inflate the layout with the Drawer.
This class has an abstract method createPage in which you will inflate your activity layout.
Here is the basic code for the RootActivity :
public abstract class RootActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourRootlayout); //The root layout wich contain your Drawer
/**
* This FrameLayout is used has a container for your activity
* layout as you would do with a fragment container.
*/
FrameLayout container = (FrameLayout)findViewById(R.id.yourContainer);
View childActivityLayout = createPage(savedInstanceState);
if (childActivityLayout != null) {
container.addView(childActivityLayout);
}
}
public abstract View createPage(Bundle saveInstanceState);
}
And here is how you extend this root class :
public class ExampleActivity extends RootActivity {
#Override
public View createPage(Bundle saveInstanceState) {
View rootView = ...
//Inflate your layout
return rootView;
}
}
I have a parent activity (from which all other activities extend) - with a 'Navigation Drawer Toggle' icon in window title.
I want to change the 'Navigation Drawer Toggle' image everytime drawer icon is clicked ( to open/close drawer)
public class ParentActivity extends Activity {
private ImageView imgDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
....
setContentView(R.layout.window_title);
imgDrawer = (ImageView)findViewById(R.id.imgdrawer);
...
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
}
...
public void openDrawer()
{
...
//Switch Image
imgDrawer.setImageDrawable(getResources().getDrawable( R.drawable.ic_close_drawer));
}
public void closeDrawer()
{
...
//Switch Image
imgDrawer.setImageDrawable(getResources().getDrawable( R.drawable.ic_open_drawer));
}
...
}
Problem is that the 'imgDrawer' is not changing, when openDrawer and closeDrawer is invoked.
Is this becz the 'imgDrawer' icon is in window title or something else?
Probably setContentView and setFeatureInt layout is same and calling getWindow().setFeatureInt before setting layout for Title. do it as:
#Override
protected void onCreate(Bundle savedInstanceState) {
....
setContentView(R.layout.window_title);
...
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.window_title);
imgDrawer = (ImageView)getWindow().findViewById(R.id.imgdrawer);
}
or pass a layout which contain ImageView with different is from setContentView layout
I'm trying to add a PreferenceFragmentin my application. The problem is, it's auto placed on top of my NavigationDrawer.
public class SetPreferenceActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
navigationDrawer(); // Loads the NavigationDrawer
getFragmentManager().beginTransaction().replace(android.R.id.content,
new Settings()).commit();
}
As you can see I load the "SettingsFragment" and replace the content with it? (I'm unsure) but it places it on top of everything else.. Here's my Settings fragment.
public class Settings extends PreferenceFragment {
static final String TAG = "MAIN";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
Everything work as expected, BUT the PreferenceFragment are loaded in front, covering up the NavigationDrawer slideout, I tried calling bringToFront(); on the listview, with no luck.
A picture for reference :
Is it possible to tell the fragment to load behind the listview? I also tought about loading the fragment in a ViewPager, but I get an error that the Pager Adapter wont accept fragments of type PreferenceFragment.
Don't replace android.R.id.content, use the the id of the FrameLayout you have in the layout that contains your DrawerLayout.
In addition to what adneal said (in case others have the same problem):
The Activity which calls the PreferenceFragment needs to have the setContentView() method if you extend the Activity with your NavigationDrawer:
public class SetPreferenceActivity extends MyNavigationDrawer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
getFragmentManager().beginTransaction().replace(R.id.drawer_frame_layout,
new Settings()).commit();
}
And the settings.xml should only contain a FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I had same issue and i resolved it by replacing android.R.id.content to R.id.container.
Whats the efficient way to add Navigation Drawer on all the activities? I don't want to repeat the code for Navigation Drawer in all the activities and their layouts. Is it possible somehow to add Nav. Drawer in BaseActivity(custom class) and then every other activity will extend BaseActivity inorder to have the Navigation Drawer ?
Is it possible somehow to add Nav. Drawer in BaseActivity(custom class) and then every other activity will extend BaseActivity inorder to have the Navigation Drawer ?
Yes this is definitly the cleanest way to go.
public BaseActivity extends Activity {
#Override
protected void onCreate()
super.onCreate(); // calls Activity.onCreate()
// setup your Navigation Drawer
}
public FirstActivity extends BaseActivity {
#Override
protected void onCreate()
super.onCreate(); // will call the BaseActivitiy.onCreate()
// do something in the FirstActivity
}
public SecondActivity extends BaseActivity {
#Override
protected void onCreate()
super.onCreate(); // will call the BaseActivitiy.onCreate()
// do something in the SecondActivity
}
The "hard work" will be the layouts. Have one baseLayout for the BaseActivity with a place holder for the Content View (the visible part of the Activities). For all other Activitys use this layout and include your Content View.
I have a pager that contains three fragments
adapter.addFragment (new PlainColorFragment (Color.red));
adapter.addFragment (new PlainColorFragment (Color.green));
adapter.addFragment (new PlainColorFragment (Color.blue));
My question is whether it is possible to detect that fragmentation has focus or is being displayed to the user.
For example, when the green fragment is the one on screen or has focus, show a "toast" on the screen
I hope I have explained my question correctly.
thanks
Simple:
greenFragment.isVisible();
If you're looking for some kind of event, you would have to manage that manually wherever your fragment switching happens, or in your fragment class, you could execute your code in the fragment's OnHiddenChanged event (double checking, of course, that it is currently visible)
You could set an OnPageChangedListener to your ViewPager and show a different toast depending on the position.
You can create an Interface, implementing it in your Fragment and then, on parent activity, you can implement BackStackChangedListener as in example below:
public interface MyFragmentOnScreen {
public void onActiveFragment();
}
public class MyFragment extends Fragment implements MyFragmentOnScreen{
[...]
#Override
public void onActiveFragment() {
//Things you should do when your fragment becomes active
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
[...]
getSupportFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
// Update your UI here.
Log.v(MainActivity.TAG, "Backstack changed");
if (getSupportFragmentManager().findFragmentById(R.id.frMain) instanceof MyFragmentOnScreen) {
((MyFragmentOnScreen) getSupportFragmentManager().findFragmentById(R.id.frMain)).onActiveFragment();
}
}
}
});
}
}
where frMain is the holder in MainActivity layout for your Fragment.