Android custom actionbar is shown only after default - android

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" >

Related

Android Navigation Drawer three line hamburger icon

I'm going crazy trying to figure out what I'm doing wrong and tried all the solutions on SO but still seem to be missing something. This is the first time I am creating a navigation drawer and took the help of an online tutorial.
Everything works great, except the icon shows the back arrow instead of the three lines.
The first bunch of answers deal with the v4 library tied to the drawable icon which didn't apply to me since I am using the v7 library
On the v7, the first answer I tried was to use below, but still remained as arrow.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
I also tried this trick, but same result:
drawerLayout.post(new Runnable() {
#Override
public void run() {
drawerToggle.syncState();
}
I then decided to give up and try to just force the icon and downloaded the ic_drawer icon and used below to just set it and be done, but still no go:
toolbar.setNavigationIcon(R.drawable.ic_drawer);
I then went so far as to remove everything tied to making the icon to start from scratch, but even after making my code just as below, the back arrow still persists and clicking it opens the navigation drawer:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
};
Even if I comment out the toolbar and that drawer toggle to have nothing in my onCreate, I am now still stuck with that back arrow. The navigation drawer still works great and opens when I click on it. I just don't know where that back arrow is coming from so I can kill it and try to work back the other code to get the three lines to show up.
Any suggestion would be greatly appreciated!
Below is the code in full. With all the navigation drawer stuff commented out, I can still see the back arrow, and pressing it or swiping opens the fragment perfectly. If I take the comments off, its still the same thing
public class Main extends AppCompatActivity {
public ArrayList<String> strTrips = new ArrayList<String>();
ArrayAdapter<String> adpTrips;
ActionBarDrawerToggle drawerToggle;
//String[] strDefaultList={"Click the '+' on the toolbar to add a new trip","","Once you have at least one trip, you select it to view/enter information"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
//toolbar.setNavigationIcon(R.drawable.ic_drawer);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/* DrawerLayout drawerLayout=(DrawerLayout) findViewById(R.id.navDrawer);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_open, R.string.nav_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getActivity().invalidateOptionsMenu();
//drawerToggle.syncState();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
//getActivity().invalidateOptionsMenu();
// drawerToggle.syncState();
}
};*/
/* drawerLayout.addDrawerListener(drawerToggle);
*/
//drawerToggle.syncState();
/* drawerLayout.post(new Runnable() {
#Override
public void run() {
//drawerToggle.syncState();
}
});*/
subLoadTrips();
}
/*
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//drawerToggle.syncState();
}
*/
The manifest file is below. The activity where I am adding the NavigationDrawer is 'Main'.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dimbu.tripx"
android:versionCode="10"
android:versionName="1.0">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity android:name=".AddTrip">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Main" />
</activity>
</application>
</manifest>
This one line code should help you. 😎☺
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The answer was provided by Mike M in comments.
With all the code being correct, his suggestion to clean/rebuild the project worked and then the changes took effect!
toolbar is not your app toolbar here!
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
It's just a view. Instead of this you should get current app toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_drawer);

set title bar name of a activity

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.

How to remove Action Bar and set the custom title of FragmentActivity of Sherlock

Hello I am using Sherlock library to achieve the Tabs. Here I am looking to remove the ActionBar at the top and set the custom title layout instead of it.
I achieved a functionality to add the Tabs but could not set the custom title over ActionBar. Any way to do this ?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pay_stub);
// adding action bar tabs to the activity
ActionBar mActionBar = getSupportActionBar();
Tab firstTab = mActionBar.newTab().setText(getString(R.string.paystub_current_tab)).setTabListener(this);
Tab secondTab = mActionBar.newTab().setText(getString(R.string.paystub_all_tab)).setTabListener(this);
// add the tabs to the action bar
mActionBar.addTab(firstTab);
mActionBar.addTab(secondTab);
// set the navigation mode the Action Bar Tabs
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
Thanks in advance.
I resolved this by adding background color and icon of the ActionBar.
ActionBar actionBar = getSupportActionBar();
setTitle("");
actionBar.setIcon(R.drawable.headerlogo);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F4A401")));

Start activity without ActionBar and add it in the onCreate event

I'm using the AppCompat/ActionBarCompat library and I need to create a custom ActionBar. I need to open the activity without an ActionBar and enable it only when I add the custom view. How can I do this?
PS: I need to define the activity to not use an ActionBar through the AndroidManifest.xml and my application minimum API level is 10.
.hide() the action bar and then .show() the action bar when your ready for it
import android.support.v7.app.ActionBar;
...
private ActionBar mActionbar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionbar = getSupportActionBar();
mActionbar.hide()
}
...
somewhere when something cool happens
mActionbar.show();

hide activity name from action bar just in time of creation

Just in time of creation and for about one second or less the action bar shows the name of activity as declared in manifest. Is there any way to avoid this? Of course when activity is created i change programmatically the title.The manifest:
<activity
android:name="com.abc.HomeActivity"
android:screenOrientation="portrait"
android:label="Home"
android:icon="#drawable/x_logo">
</activity>
and code from fragment which is shown by activity. I change the title of action bar based on what fragment is shown.
((ActionBarActivity) getActivity()).getSupportActionBar().setTitle("variable name");
from activity:
super.onCreate(savedInstanceState);
getSupportActionBar().setTitle("");
If hiding the activity name means simply avoiding the text, then call the following in onCreate of your Activity:
getActionBar().setTitle(null); //if using ActionBar theme
setTitle(null); //if using standard theme
Update
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final CharSequence title = getActionBar().getTitle();
getActionBar().setTitle(null);
//sets the title again after 1 second delay
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getActionBar().setTitle(title);
}
}, 1000);
}
Use this line to hide a name of activity this.requestWindowFeature(Window.FEATURE_NO_TITLE);
just after onCreate.
Example.
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.serverfilelist);

Categories

Resources