Renaming Toolbar based on the bottom navigator - java

How do I change Toolbar Titles in different bottom navigation activities, I currently have one displayed in all which is one my manifest
android:label="#string/home"
but since I have four activities on my bottom navigator how can I rename them all on the activities, I tried adding a toolbar it added below the original, home. Tried renaming manifest particular activities doesn't work. Thanks in advance.
I tried setting titles on the activities like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collections);
setTitle(R.string.collections);
}
I want to have something like this, Image One Image two

You can use setTitle in your activity.

Related

add toolbar menu only at specific fragments

So I have this app and in the toolbar, there is a drop-down menu and I am looking for a solution on how to show only specific items at specific fragments.
I know that I have to make a java class and link it to the menu, but how do I find out what fragment I am on?
You can move the implementation of setting the toolbar for your fragments in the activity that launches these fragments. In that case, you may have the toolbar in your activity and the activity should have a fragment container where you will put your fragment. Let us consider a layout like the following for your activity.
<RelativeLayout>
<!-- Your toolbar here in the activity -->
<ToolBar>
android:id="+#id/toolbar"
...
</ToolBar>
<!-- Your fragment container here -->
<FrameLayout>
android:id="+#id/fragment_container"
...
android:layout_below="#id/toolbar"
</FrameLayout>
</RelativeLayout>
Now, you have the ToolBar here in your activity and hence, you can set the toolbar when you are loading the fragments in the fragment container. Your activity might have the following functions.
public void launchFrag1() {
// replace the fragment 1 in the fragment container
loadToolbarForFrag1();
}
public void launchFrag2() {
// replace the fragment 2 in the fragment container
loadToolbarForFrag2();
}
Now call the specific function of your activity to load the fragment and the toolbar dynamically as well.
If you are trying to call the method from the fragment, you can always call those methods declared in your activity like the following.
((YourActivity) getActivity()).launchFrag2();
I hope you get the idea.
Please note that this is just a basic implementation of how you can get your dynamic toolbar working in your app. However, your implementation might vary based on the use case that you have.
I hope that helps!
I have an ImageView in the toolbar which I hide as follows:
// Show image in fragment 1
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.VISIBLE);
// hide image in fragment 2
ImageView img = getActivity().findViewById(R.id.toolbarMenu);
img.setVisibility(View.GONE);
Try these codes in onCreateView after inflating your layout

Android - How to set title for custom Toolbar in Manifest file? Label attribute doesn't work

I'm using a custom Toolbar widget in my app, I found that the label attribute in Manifest file doesn't work. Any suggestion except using "setTitle()" method in Activity/Fragment? Thank you.
<activity
android:name=".CartActivity"
android:label="#string/cart_activity_label"
android:parentActivityName=".CatalogActivity" />
I wish the title can be shown by using attributes in Manifest file.
use a textview inside the Toolbar widget or if you are using support library, use setsupportActionbar(toolbar); in your activity.
getActionBar().setTitle(R.string.logout);
or better,
getSupportActionBar().setTitle(R.string.logout);`
in onCreate method
Ok...I just found out that I forgot to link the View (Toolbar) object to the View id. Follwing is the complete setup for using a Toolbar as a default ActionBar:
private Toolbar toolbar;
toolbar = findViewById(R.id.toolbar); //I missed this
setSupportActionBar(toolbar);
Toolbar title explain:
You change the default system style to ".NoActionBar", the default ActionBar disappears.
You add a custom Toolbar to the Layout, an "ActionBar" without title appears.
There is no title on the Toolbar even though you've already set the label attribute in Manifest file, because the Toolbar is not recognized as default ActionBar by the system, that's why we need to use "setSupportActionbar()" method to make it work like a normal ActionBar.
Don't forget to link the View of the Toolbar.

Change Title/Label of an Activity in Android 5 recents screen

With Android 5, Google supports multiple activities in the recents screen for a single application.
I managed to implement multiple activities in the recents screen with this official doc.
How can I change the title of the activity? Switching to recents screen always shows my app name as title.
Activity.setTitle("title");
changes the title on the toolbar, but I want it to change in the recents screen, just like Google does in Google Chrome App.
I need to change the title programatically.
In your activity you can use new method:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout);
setTaskDescription(new ActivityManager.TaskDescription("Activity 2"));
}
setTaskDescription sets information about activity in recents task list. In this example only title.

Handling screen orientation changes

My app functions like this: an imageView that can be visible in portrait or landscape, and the user can swipe between images to change the image in the imageView. The issue is that if you swipe to select a different image and then rotate the device to landscape it shows the original image the user selected, not the current image that was being displayed in the imageView. I am wondering how I can get the id of the current image before it is rotated, and then set it for when it is in landscape. Would it be in the onPause method, or is there a onConfigurationChanged method I could use for this?
See here: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
According to the documentation, the activity is destroyed when the orientation changes. In order to save additional state information in such an event, you can override the onSaveInstanceState() method and add key-value pairs to the bundle object, like so:
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putInt("CurrentImageId", [current image id here]);
super.onSaveInstanceState(Bundle savedInstanceState);
}
Then, when the application is restored after the orientation change, you can retrieve the current image's id by overriding either the onCreate(Bundle savedInstanceState) method or the onRestoreInstanceState(Bundle savedInstanceState) method:
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int currentImageId = savedInstanceState.getInt("CurrentImageId");
//Set the current image here, after retrieving the id
}
Of course, I don't know exactly how you're identifying your images, so I can't tell you exactly how to set/retrieve the current image's id.
Try to add android:configChanges="orientation" below <activity> in your AndroidManifest.xml

when I open an android soft, why the actionbar show for a little while?

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ActionBar actionBar = getActionBar();
actionBar.hide();
}
I hide the actionbar, why the actionBar still shows for a while.
And another question like this: I changed the actionBar's title and subtitle in code like showed. Why the actionBar's title stile show the string I put in AndroidMinefest file for a while and then changed to the title and subtitle I code in the java file. Can the actionBar show the title and subtitle at start?
As Nathan Villaescusa said, the activity is not fully loaded, so the actionbar will show, then execute actionBar.hide(), the ActionBar will show for a long time especially if the device is 3-4 years old.
As to how to prevent this situaion, Activity Theme can always be set in AndroidManifest.xml in <Activity> entry, use the #android:style/Theme.Holo.Light.NoActionBar' theme. And removeactionBar.hide()` in activity.

Categories

Resources