I am creating an android app, i am trying to hide default action bar and us my own custom toolbar as action bar but the problem is that default navigation icon and title of action bar are not being hidden and when i open navigation drawer from right side my custom navigation icon from left side hides and android's default navigation icon is displayed instead.
i have tried using NoActionBar and NoTheme styles too but its not working.
Here is a sample of my code, where i am going wrong
Any help will be appreciated.
public class MyActivity extends AppCompatActivity
{
Toolbar main_toolbar;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setUpToolBar();
}
public void setUpToolBar()
{
main_toolbar=(Toolbar)findViewById(R.id.toolbar)
//set up the home_main_toolbar
android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
setSupportActionBar(main_toolbar);
if (activity.getSupportActionBar() != null)
main_toolbar.setNavigationIcon(R.drawable.img_toolbar);
main_toolbar.setNavigationOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
//Open navigation drawer
}
});
}
}
<activity android:name=".MainActivity" android:screenOrientation="portrait" android:theme="#style/AppTheme.NoActionBar" />
use this code in your android menifest , surely it will work
try it in manifest where your activity is defined.
android:theme="#style/AppTheme.NoActionBar"
Go to Styles.xml
add this style there
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and use it in manifest
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
Related
I want to implement the Android ActionBar back button in my StreamActivity but I'm getting this error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.schoolteacher/com.example.schoolteacher.StreamActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
I don't know why this is not working, despite I followed all instructions from the documentation! plus, I did exactly this in other projects and it works.
Here is my code :
StreamActvivty.java
public class StreamActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stream);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar= getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
styles.xml
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
manifest
<activity android:name=".StreamActivity"
android:label="#string/stream"
android:parentActivityName=".ClassActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ClassActivity" />
</activity>
Try this
Add this in onCreate
ActionBar actionBar = this.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Add this outside of the onCreate, this is used to navigate back to the parent activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
And also set Parent activity
<activity
android:name=".activity.CreateAccountActivity"
android:parentActivityName=".activity.LoginActivity" />
To use Toolbar as an ActionBar, first ensure the AndroidX support library is added to your application build.gradle (Module:app) file:
dependencies {
...
implementation 'androidx.appcompat:appcompat:1.0.0'
}
You have to disabled the theme provided for ActionBar. Change your theme from styles.xml as:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
Is there toolbar inside your activity_stream.xml?
Also check your imports.
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, BaseActivity.class);
startActivity(i);
}
and my next activity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_base);
}
I still see blue bar with application name at top.
I tried changing my application manifest to use a no-title theme and the program crashed because the activities inherit from AppCompatActivity.
How do I inherit from AppCompatActivity and get rid of the application title name up top?
It's the actionbar, you can use the theme Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar or a theme which descents from them.
To make disappear is the action bar also this should work:
getSupportActionBar().hide();
Try this...
If you don't want the action bar in your activity then,
Create a style
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and apply this style for the activity in Manifest for which you don't need action bar,
<activity android:name=".BaseActivity"
android:theme="#style/AppTheme.NoActionBar"></activity>
Do this in your onCreate() method.
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here);
this refers to the Activity
OR
You can modify your AndroidManifest.xml:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
You can use this for each activity in your project.
OR
also change from style.xml
<style name="generalnotitle" parent="general">
<item name="android:windowNoTitle">true</item>
</style>
Hope this helps you.
I am trying to remove title bar from my activity but it is not getting removed.
Here is my code
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
}
}
With or without the requestWindow line the output is the same then what is the use of that line?
I would reccomend editing your theme with no title bar. For example use of of these...
<style name="Theme.NoTitle" parent="#android:style/Theme.NoTitleBar"></style>
<style name="Theme.FullScreen"
parent="#android:style/Theme.NoTitleBar.Fullscreen"></style>
Apply this theme in your activity declared in the Manifest File.
<activity
android:name=".SplashScreenActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
</activity>
Can you try adding this to your manifest inside activity tag :
<activity
android:name="your_app_packagename.activity_name"
android:theme="#android:style/Theme.NoTitleBar" >
also try this in your activity :
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Add this in onCreate
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
I am creating the framework for an app and wish to hide the action bar for the login activity. The app is going to be entirely composed of fragments because they are awesome.
I can hide the action bar fine for my activity, but every time I run the app I see my inflated blank fragment and default action bar for about 2 seconds before it manages to inflate the login fragment.
Any idea on how / where I should hide the action bar, (whilst keeping my framework) to stop this from happening?
LoginActivity.java
public class LoginActivity extends SingleFragmentActivity {
#Override
protected Fragment createFragment() {
return new LoginFragment();
}
}
SingleFragmentActivity.java
public abstract class SingleFragmentActivity extends ActionBarActivity {
protected abstract Fragment createFragment();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(this instanceof LoginActivity){
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
LoginFragment.java
public class LoginFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, parent, false);
return v;
}
}
I originally had SingleFragmentActivity extend FragmentActivity and did this in the if statement:
requestWindowFeature(Window.FEATURE_NO_TITLE);
But I've got a feeling that's just a different way of doing the same thing
Edit:
I have also tried setting the fragment_activity to use a different style but it doesn't seem to work either:
<style name="NoActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
</style>
Okay after a few hours I think I have found the solution to keeping fragments in place for everything and removing the action bar (for all devices) for the launch activity only:
Android Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.company.app.LoginActivity"
android:theme="#style/NoActionBar"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Where the style is defined as like so:
<style name="NoActionBar" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
</style>
That took a while to figure out because there is no Theme.AppCompat.Light.NoActionBar , the action bar is basically known as the title bar in older APIs, and when I applied that style to the activity itself (which I thought was the only way of using dynamic themes) it doesn't seem to work at all.
So hopefully this simple solution will keep the action bar for any other activities and it may help someone in the future :)
try this:
if(this instanceof LoginActivity) {
if (getActionBar().isShowing()) getActionBar().hide();
} else {
if (getActionBar().isShowing()) getActionBar().hide();
}
or
if(this instanceof LoginActivity){
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
} else {
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
Daniel Wilson's comment above does work but just a minor difference in how I did it.
I did not define style at all, all I did in AndroidManifext.xml is that I added this to my activity:
android:theme="#style/Theme.AppCompat.NoActionBar"
So, I did not have to redefine style for NoActionBar, I just added it from Theme.AppCompat
I'm using this code in Activity.onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hiding the title bar has to happen before the view is created
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// ...
}
I've got a problem trying to add a SearchView widget to the ActionBar in my activity - I get a null value when calling getActionView to get my SearchView object.
I've been following the Android Developer guide and also went through a ton of SO questions as well as some other links on the Internet; all to no avail. It could be something simple but I wasn't able to figure it out - it seems to me my code is basically the same as that from google (apart from changing some names etc.) but it still won't work.
Any help would be appreciated.
Included below are relevant bits of the code. Please let me know if anything's unclear of if you have any idea as to what could possibly be wrong.
Activity code:
private ListView workflowListView;
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_workflow_list);
workflowListView = (ListView) findViewById(R.id.workflowListView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.drawer_list);
drawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_launcher, /* nav drawer icon to replace 'Up' caret */
R.string.app_name, /* "open drawer" description */
R.string.app_name /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// getActionBar().setTitle("Closed drawer");
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// getActionBar().setTitle("Open drawer");
}
};
drawerLayout.setDrawerListener(drawerToggle);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setIcon(android.R.color.transparent);
String[] testData = {"a", "b", "c", "d"};
ArrayList<String> workflowList = new ArrayList<String>();
for (String s : testData) {
workflowList.add(s);
}
ArrayAdapter<String> workflowAdapter = new ArrayAdapter<String>(this.getApplicationContext(), R.layout.workflow_list_item, workflowList);
workflowListView.setAdapter(workflowAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.workflow_list, menu);
inflater.inflate(R.menu.options_menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
// The below line returned null even though it was used in Google sample code
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
xml/searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint"
android:includeInGlobalSearch="false" />
menu/options_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_action_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Try to replace the failing line with:
mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (EnglishVerbSearchView) MenuItemCompat.getActionView(mSearchMenuItem);
Where R.id.action_search is the id of your search item in the menu.
EDIT
Your manifest should look like that:
<activity
android:name="com.bronzelabs.twc.activities.WorkflowListActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.SearchResultActivity"
android:theme="#style/Theme.AppCompat.Light"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"
android:value=".activities.SearchResultActivity" />
</activity>
The way you call setSearchableInfo is:
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(), SearchResultActivity.class)));
EDIT 2
Make sure your menu xml file is like that (pay attention to those 2 attributes with yourapp namespace - this is needed when you use the compat library):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/action_search"
yourapp:showAsAction="always|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
I had a similar probably that appeared on my release builds but not my debug builds when switching over to the v21 support library. Turned out to be an obfuscation problem, and adding this line to my proguard-rules.txt file fixed it:
-keep class android.support.v7.widget.SearchView { *; }
First check you are using app namespace for actionViewClass.
android:actionViewClass="android.support.v7.widget.SearchView"-this throws NPE
app:actionViewClass="android.support.v7.widget.SearchView"-use this
and second
Check if you are using same searchView in menu.xml and in your Activity
I had a very similar issue where the following line was returning null
SearchView searchView = (android.support.v7.widget.SearchView)MenuItemCompat.getActionView(searchMenuItem);
The fix was in my styles.xml file, i changed my AppTheme's parent to Theme.AppCompat.Light
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
For anyone still searching, I followed everything above and could not seem to get things going. I wrapped my searchView.setSearchableInfo... in a null check and voila! It ran just like it ought to.
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(getApplicationContext(), SearchActivity.class)));
}
for me, This is the right answer:
In menu/options_menu.xml, use this:
app:actionViewClass="android.support.v7.widget.SearchView"
I think you're occupying FragmentActivity, you need to extend of ActionBarActivity
I had the same problem. My solution is:
replace
android:actionViewClass="android.support.v7.widget.SearchView"
with
android:actionViewClass="android.widget.SearchView"