In my android app I have a default app theme which has no action bar since I am adding my own toolbar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
So this works well with my other activities except settings activity, So as a quick fix I've decided to add a custom style for it via
<style name="SettingsMenu" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorBackground">#color/colorPrimaryDark</item>
<item name="textColor">#color/colorAccent</item>
</style>
and also in the manifest I've added
<activity
android:name=".backend.settings.SettingsActivity"
android:theme="#style/SettingsMenu" //theme here
android:label="#string/title_activity_settings">
</activity>
So in my settings activity which extends AppCompatPreferenceActivity i have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
So the above still doesnt show the toolbar.
I have also tried adding a custom toolbar via
private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat
if (rootView != null) {
View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
ActionBar actionBar = getSupportActionBar();
//here assign the toolbar
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
So in my both tries neither toolbar nor actionbar works
Where am i going wrong?
Related
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent+?actionBarSize">
you can see 3rd line of code how to add this two value and get the height with adding two vale "android:layout_height="match_parent+?actionBarSize" in the our program in android studio in java.
Your query is not clear.
You can't do it in XML
android:layout_height="match_parent+?actionBarSize"
However if you want to hide ActionBar from the entire App, you can use
1. styles.xml
<resources>
<!---Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!---Customize your theme here.-->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
2. Hide ActionBar from any particular activity using Java code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Take instance of Action Bar
// using getSupportActionBar and
// if it is not Null
// then call hide function
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
}
}
3. Hide ActionBar while user interaction using WindowManager
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// set Windows Flags to Full Screen
// using setFlags function
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
}
4. Hide ActionBar from any particular activity using try-catch
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// try block to hide Action bar
try {
this.getSupportActionBar().hide();
}
// catch block to handle NullPointerException
catch (NullPointerException e) {
}
}
}
I am getting error message : ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
But I have called this in my code , still getting same error and the toggle button is not visible.
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//layout variables
mDrawerLayout = findViewById(R.id.main_drawer_layout);
mNavigationView = findViewById(R.id.main_nav_view);
mActionDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mActionDrawerToggle);
mActionDrawerToggle.syncState();
//changed as keeps throws NULLPOINTEXCEPTION
if(getSupportActionBar() != null) {
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// assigning ID of the toolbar to a variable
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
// using toolbar as ActionBar
setSupportActionBar(toolbar); //set
ActionBar actionbar = getSupportActionBar(); //get
actionbar.setTitle("SharePixel");
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_home_menu);
}
setHomeAsUpIndicator() won't get executed as the if(getSupportActionBar() != null) condition shouldn't be valid in drawer-based activity, because it requires a custom Toolbar, and you call this condition before setting the custom toolBar.
This condition won't be achieved unless your activity theme has an ActionBar set in your styles/themes XML. (if you've no customized activity theme set, then it's inherited from the base application theme.
To fix this:
Make sure that your activity theme inherits from a NoActionBar decedent; for instance Theme.MaterialComponents.DayNight.NoActionBar (or if your activity doesn't have customized style; do that on the base application theme).
Remove the if(getSupportActionBar() != null) condition.
I'm trying to set my tabs text textAllCaps to false.
For some reason only firs tab has needed results, but the second is still all caps.
https://imgur.com/a/fnv2DzY
I've set tabTextAppearance to MyCustomTextAppearance and in styles two attributes are set to textAllCaps, android:textAllCaps are set to false
activity:
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/appbar"
app:tabTextAppearance="#style/MyCustomTextAppearance" />
in styles:
<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
<item name="tabTextAppearance">#style/MyCustomTextAppearance</item>
</style>
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
in Java:
public class NewLoginActivity extends BaseActivity implements
RegisterFragment.OnFragmentInteractionListener,
LoginFragment.OnFragmentInteractionListener {
public ActivityNewLoginBinding binding;
private List<String> titles;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_new_login);
setSupportActionBar(binding.toolbar);
setToolBarTitle(getSupportActionBar(), getResources().getString(R.string.login_action_title));
if (getSupportActionBar() != null){
getSupportActionBar().setHomeButtonEnabled(false); // Disable the button
getSupportActionBar().setDisplayHomeAsUpEnabled(false); // Remove the left caret
getSupportActionBar().setDisplayShowHomeEnabled(false); // Remove the icon
}
binding.tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
binding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
titles = new ArrayList<>();
titles.add(getString(R.string.login_action_title));
titles.add(getString(R.string.regi));
Bundle bundle = getIntent().getExtras();
LoginTabAdapter tabAdapter;
tabAdapter = new LoginTabAdapter(getSupportFragmentManager(), this, titles);
binding.viewPager.setAdapter(tabAdapter);
binding.viewPager.setOffscreenPageLimit(1);
binding.tabLayout.setupWithViewPager(binding.viewPager);
if (bundle != null && bundle.containsKey("signup")) {
binding.viewPager.setCurrentItem(2);
}
}
I have my app as a single Activity with different fragments. I navigate the fragments via the Navigation Drawer on the left. The thing is that I need to have different toolbars for each fragment.
What I did was set the toolbar in the layout of the fragment like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hectorviov.###.activity.HomeFragment">
<include
android:id="#+id/toolbar_logo"
layout="#layout/toolbar_logo" />
...
And set it as SupportActionBar on my fragment with with:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatActivity act = (AppCompatActivity)getActivity();
Toolbar mToolbar = act.findViewById(R.id.toolbar_logo);
act.setSupportActionBar(mToolbar);
}
It works correctly, the thing is, it doesn't show the hamburger icon on the left of the action bar. It shows me a warning:
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
So I tried different ways to do this after googling a lot. The last one I tried is:
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
And no matter what I try, it always gives me a NullPointerException:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hectorviov.###/com.hectorviov.###.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
Please help!!
try with this
AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
ActionBar ab = activity.getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
}
refer this link will help you.
doesn't show the hamburger icon?
you need to add ActionBarDrawerToggle object
public class DrawerActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private void setupDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
So, I've this little activity and I want to add a back button (the standard arrow one) I search for the answers here and I found this little code:
public class PozosFluyentes extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pozos_fluyentes);
ActionBar actionBar = getSupportActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
}
This crash my app, so anyone could help me with this please! I know is something pretty usual but I new to android development. Thanks in advance.
It's probably you add an android.support.v7.widget.Toolbar and not using a Theme that includes NoActionBar. Theme.AppCompat.Light.NoActionBar like theme will fix the issue. But if it does not, share your xml, styles.xml and logcat to check error. With little info the first thing to look is this. Also you can check here and see you need to disable theme provided ActionBar
Replcae your code with below.
ActionBar actionBar = getSupportActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (actionBar!= null) { // you have to use actionBar (ActionBar Object) instead.
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
let me know it working or not.....Hope this will helps you...(: