set title bar name of a activity - android

I'm trying to change my applications activity menu bar title. I managed to change font as follows. Anyone help me to change the name of the title. It may be just a one line, but I can't figure it out.
int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);
if(actionBarTitleView != null){
actionBarTitleView.setTypeface(typeFace);
}

Try this
getActionBar().setTitle("Your title");
or this if you use appcompat-v7
getSupportActionBar().setTitle("Your title");

You can set the title in action-bar using AndroidManifest.xml. Just add label to the activity. Like
<activity
android:name=".DownloadActivity"
android:label="Your Title"
android:theme="#style/AppTheme" />

Not sure you can do it your way or not but you can use the bellow solution:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
final ActionBar actionBar = getActionBar();
// actionBar
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// titleTextView
TextView titleTextView = new TextView(actionBar.getThemedContext());
titleTextView.setText("Title");
titleTextView.setTypeface( your_typeface);
titleTextView.setOtherProperties();
// Add titleTextView into ActionBar
actionBar.setCustomView(titleTextView);
}
By doing this solution, you have FULL control of your textview title.

For the activity you want to change the title bar or action bar name, go to it's java file
for example if you want to change the name of MainActivity then go to MainActivity.java and add the following code
getSupportActionBar().setTitle("name of the action bar");
below this code (it will be there by default)
setContentView(R.layout.activity_main);
in the protected void onCreate(Bundle savedInstanceState)
It worked for me. I hope it will help you too.

Try calling actionbar.setTitle method.
getActionBar().setTitle(title);
OR this if you are using appcompat
getSupportActionBar().setTitle(title);
There is setTitle method in activity class as well.

Related

Getting null at setting actionBar icon

I've got problem with my actionBar. Tryin' to set an icon but still getting null :/ Here's code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setIcon(R.drawable.logo);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#51717e")));
}
Ofc I've got logo in my drawable folder. Both lines are incorrect - Icon and Background color. Help :/
You are getting Actionbar null because the Theme don't have ActionBar.
Try to set the background and icon in your styles.xml
You need to call getSupportActionBar() on an ActionBarActivity. Do not call getActionBar() -- that is not available on older devices, and for the new r21 edition of appcompat-v7, I would expect it to return null all the time, as the new ActionBarActivity disables and replaces the system action bar.and surround these two line in try catch like:
try{
getSupportActionBar().setIcon(R.drawable.logo);
getSupportActionBar() .setBackgroundDrawable(new ColorDrawable(Color.parseColor("#51717e")));
}catch(NullPointerException e)
{
e.printstacktrace();
}
Try this:
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();

Add and Remove views from Toolbar depending on Fragment Displayed

I have an application, that at the moment only has 2 fragments. Fragment 1, this has the nav drawer and the title.
Fragment 2 requires a custom view as adding menu items won't work as I need alignment. So I add the view as follows:
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.LEFT);
//Remove nav drawer "hamburger"
mMainActivity.mActionBarDrawerToggle.setDrawerIndicatorEnabled(false);
//Remove title from Toolbar
bar.setDisplayShowTitleEnabled(false);
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View postToolBar = layoutInflater.inflate(R.layout.upload_content_toolbar, null);
mMainActivity.mToolBar.addView(postToolBar, params);
That is fine and it displays correctly. However, when I want to return to the previous fragment Fragment 1, I then call mMainActivity.mToolBar.removeView(postToolBar); I call this on return to Fragment 1 as the user can navigate either by the back button or by button in the postToolBar. However, the view is still in place. I can't get rid of it. I have now tried setting the visibility to GONE but that won't work either.
This was pretty simple with the Action Bar, however things seem to have gotten a bit complicated with the Tool bar.
I must add that in each of my two fragments I extend a BaseFragment in which I declare the toolbar view.
Can any one help or send me in the direction of a tutorial?
This is how i achieved it at this time:
in activity onCreateView();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowCustomEnabled(true); // enable overriding the default toolbar_home layout
getSupportActionBar().setDisplayShowTitleEnabled(false); // disable the default title element here (for centered title)
getSupportActionBar().setDisplayShowHomeEnabled(false);
cutomToolbarView=getLayoutInflater().inflate(R.layout.custom_toolbar_home, null);
getSupportActionBar().setCustomView(cutomToolbarView);
}
and here is two simple method to do the magic
public void setToolbarTitleEnabled(String title) {
getSupportActionBar().setDisplayShowCustomEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(title);
}
public void setCustomToolbarEnabled() {
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
then simply when you can change actionbar like these:
when to select HomeFragment.java or whatever in your case
setToolbarTitleEnabled(CURRENT_TAG);
and when to select other fragment:
setCustomToolbarEnabled();

Navigation drawer icon gets disappeared on setting CustomView in actionbar

Why navigation drawer icon gets disappeared on setting customView by
actionbar.setCustomView(R.layout.blah);
How can this be resolved?
OK, you don't post any code, so I have to assume stuff here. So if you can update your question and show actual code!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Depending on what your custom Action Bar will do, you might want to disable these!
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.custom_actionbar, null);
// Here is how you can get specific items from your custom view!
TextView titleTextView = (TextView) customView.findViewById(R.id.title_text);
titleTextView.setText("My Own Title");
...
// MAKE SURE THESE ARE SET!! BOTH OF THEM!!
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
}
The biggest issue is probably the code at the very end. Please make sure you have that code!

Android custom actionbar is shown only after default

I have created new Android project with only 1 activity. I have created custom view for ActionBar. But when I launch the app for 1-2 seconds I see blank screen and default black Actionbar with app name on it. Only after second when content is loaded my action bar is shown.
How to remove this default action bar showing on start? Code is simple:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
ActionBar supportActionBar = getSupportActionBar();
supportActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
supportActionBar.setDisplayShowHomeEnabled(false);
supportActionBar.setDisplayShowTitleEnabled(false);
supportActionBar.setDisplayShowCustomEnabled(true);
supportActionBar.setCustomView(R.layout.actionbar);
MainActivity:
private ActionBar actionabar;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
actionabar = getActionBar();
actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
In Manifest:
Inside activity element you have to add these theme
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
For Eg:
<activity
android:name="com.sit.fth.activity.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >

ActionBar on the TOP and BOTTOM on Android App

I'm developing an Android app and I want to have 2 ActionBar (one on the top and another on the bottom) like this: http://developer.android.com/design/media/action_bar_pattern_considerations.png
I'm using Actionbar Sherlock and for now I created only the top ActionBar. I serched in the web, but I didn't find a solution yet, only some piece of code.
Can anyone help?
Take a look at the following links:
http://developer.android.com/guide/topics/ui/actionbar.html
How can I force the Action Bar to be at the bottom in ICS?
Changing position of Android Action Bar
try this...
public class MainActivity extends SherlockActivity {
private View contentView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentView = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(contentView);
LinearLayout layout = (LinearLayout) contentView.getParent().getParent();
View view = layout.getChildAt(0);
layout.removeViewAt(0);
layout.addView(view);
}

Categories

Resources