Different Colored Statusbar in each fragment - android

How can I define a different Statusbar and Actionbar color for each fragment ?
At the moment.
How it should look.

First of all, I would highly recommend you to migrate to new approach - Toolbar. It is much more flexible and you can customize it as plain View.
About your question.
You can just get ActionBar object and setBackground programatically.
Here is short example
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR IN HEX 0xFFFF6666 for instance"));
I will show how would I implement this. This is more about architecture and patterns.
Use some base class for Fragment it would be better to have base class for Activity as well. Lets consider
public class BaseFragment extends Fragment
And you Activity class in which your fragment lives.
public class MainActivity extends Activity
And you have to define responsibilities of Activity in this case and create interfaces
In your case to work with ActionBar
Create interface
public interface ActionBarProvider {
void setActionBarColor(ColorDrawable color);
}
Make your activity implement this interface
public class MainActivity extends Activity implements ActionBarProvider {
public void setActionBarColo(ColorDrawable color) {
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(color));
}
}
And finally in BaseFragment in onAttach
public void onAttach(Context context) {
super.onAttach(context);
mActionBarProvider = (ActionBarProvider) context;
}
Make mActionBarProvider variable protected and make each fragment extend BaseFragment and you can change action bar color from any fragment like this mActionBarProvider.setActionBarColor(new ColorDrawable());
Hope this helps.

Related

How to change bottom app bar navigation icon programmatically

I am making an Android application which has one activity and many fragments. The activity contains a bottom app bar and that bottom bar has a navigation icon in it. Like this:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottom_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="#color/colorbottomappbar"
app:fabAlignmentMode="center"
app:navigationIcon="#drawable/ic_menu_green_24dp">
</com.google.android.material.bottomappbar.BottomAppBar>
This navigation menu icon will be shown in every fragment. However, in some fragments I want to change that navigation icon in the bottom app bar to back button/icon. How can I achieve this? Also, currently I handle the navigation icon click in the main activity. How can I handle the click in the case of the back icon? How will it know what the current fragment is and how can I determine which fragment the back icon leads to?
If you look at the documentation, you'll see that BottomAppBar extends from Toolbar and it has an inherited method called setNavigationIcon(int res).
You can implement an interface that your main Activity implements like so:
interface FramentChangedListener {
void onFragmentChanged(int type);
}
Your activity would do something like this:
public class MainActivity extends Activity implements FragmentChangedListener {
// This will keep track of what is currently shown
private int current = 0;
#Override
public void onFragmentChanged(int type) {
if (type == FirstFragment.SOME_TYPE) {
// Update the current fragment value, we're associating each fragment
// with an int value.
current = type;
bottomAppBar.setNavigationIcon(R.drawable.your_back_icon);
}
}
...
}
In your fragment you would do something like this:
public class FirstFragment extends Fragment {
private FragmentChangedListener listener;
public static final int SOME_TYPE = 1;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceOf FragmentChangedListener) {
// context in this case is your activity, which implements FragmentChangedListener
listener = (FragmentChangedListener) context;
// You can call the listener now
listener.onFragmentChanged(SOME_TYPE);
}
}
}
In your Activity, add a listener to the BottomAppBar via setNavigationOnClickListener and whenever you receive the navigation icon event, you can check against the current value that we defined.

How to inherit another class in the navigation drawer Fragment?

I have a new android project with a navigation drawer. Now i want to add a new base class and inherit it in the navigation drawer fragment class. But it already extends Fragment. So how do I inherit the base class in the navigation drawer fragment?
Multiple inheritance does not exit in Java. Essentially what you have to do is make the base class inherit from Fragment and then you can extend your navigation drawer Fragment from the base class.
public class BaseFragment extends Fragment {
...
}
public class NavigationDrawerFragment extends BaseFragment {
...
}

How to add Navigation Drawer to all the activities in the application?

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.

ActionBarSherlock: Change dropdown navigation title

currently i am using ActionBarSherlock for my project. I am creating my actionbar with this code.
setTheme(R.style.Theme_Sherlock);
Context context = getSupportActionBar().getThemedContext();
list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
My question is. After I choose an option from the dropdown navigation, How do I keep that state throughout my activities?
For Example, in the homescreen, I choose "Sports" under my dropdown navigation. The title of the dropdown navigation then becomes "Sports". When I change activites however, the dropdown navigation title defaults back to the first item on the list.
One method I used was to create a base activity that each navigation item / activity extended from. Within the base activity, I overloaded onResume with an int to track which activity was active, and set the selected navigation item in that method.
Example:
public class BaseActivity extends FragmentActivity {
//...
protected void onResume(final int actId) {
super.onResume();
//...setup your action bar via getSupportActionBar() calls...
getSupportActionBar().setSelectedNavigationItem(actId);
}
Then in your individual activities:
public class ExampleActivity extends BaseActivity {
private final int ACT_ID = 1;
//...
protected void onResume() {
super.onResume(ACT_ID);
//...
}
}
Hope that helps!

Changing Title from Activities present in tabs in TabLayout in Android

Im trying to change the title from the activities inside the Tab. but the title remains same as the Title given for TabActivity. I searched for the solution but not succeeded yet. is there a way to do it? please help me on this.
Thanks
In an Activity used as child of a TabActivity you can use simply
getParent().setTitle("your title");
the getParent will return the current instance of TabActivuty, than you can change the title
You could use the singleton methodology with the TabActivity, then use This Method to change the title.
MyTabs.java
class MyTabs extends TabActivity {
private static MyTabs theInstance;
public static getInstance() {
return MyTabs.theInstance();
}
public MyTabs() {
MyTabs.theInstance = this;
}
...
}
ActivityInTab.java
...
TabActivity tabActivity = MyTabs.getInstance();
((TextView)tabActivity.getTabHost().getTabWidget().getChildAt(0).findViewById(android.R.id.title)).setText("New");
...
(Obviously this isn't a complete singleton implementation, but it will suffice for what you're doing. Since the activity can't exist without the parent container, it's safe to assume a reference has been set in the constructor when the object was created.)

Categories

Resources