Unable to get the ActionBar ID using AppCompat Activity - android

Unable to get the id with the below code when updated to AppCompatActivity.
int abContainerViewID = context.getResources().getIdentifier("action_bar_container", "id", "android");
return (FrameLayout) ((AppCompatActivity) context).findViewById(abContainerViewID);
The above method is returning Null now.
This is my App Theme
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
</style>

You should get ActionBar like this:
ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();

Related

Cannot hide/remove an Activity's title

I cannot hide the title's view from an Activity that I'm styling as a Dialog.
android:windowNoTitle is set to false in the reference style, and RequestWindowFeature(WindowFeatures.NoTitle) is called from OnCreate.
Is there another setting for completely removing the title's view from an Activity?
styles.xml
<style name="Theme.AppCompat.Light.Dialog_Configuration" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
ConfigurationView
[Activity(
Label = "Configuration",
MainLauncher = false,
Theme = "#style/Theme.AppCompat.Light.Dialog_Configuration",
ExcludeFromRecents = true
)]
public class ConfigurationView : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RequestWindowFeature(WindowFeatures.NoTitle);
SetContentView(Resource.Layout.Configuration);
}
}
Are you using toolbar? You can try this
getSupportActionBar().setDisplayShowTitleEnabled(false);
or
requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title.
getSupportActionBar().hide(); //hide the title bar.
While you have removed the title, you also need to remove the title bar as well. Try adding the following line to your onCreate method:
getSupportActionBar().hide();
Write this in the OnCreate method just after SetContentView:
ActionBar.Hide();
Alternatively, you could try this
NavigationPage.SetHasNavigationBar(this, false);

ToolBar AppCompat & Action Bar backStack

I currently have an issue with the AppCompat Toolbar.
If I set the Theme to NoActionBar, no action is shown.
If I set the Theme normally, I can't set the action bar like :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
Because you have no right to set the action bar if one is already set.
I simply would like to get the back arrow on my Fragments navigation like :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I can't figure out how to do that :/
try below code:
getSupportActionBar().setHomeButtonEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_back);
Provide a Parent Activity in the Manifest.
See http://developer.android.com/training/implementing-navigation/ancestral.html
You have to set this attribute in your style.
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
</style>
Then in your Activity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mActionBarToolbar != null) {
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
}
}
}

Set actionbar icon for activity programatically?

I want to set the actionbar icon and title to be visible for an activity.
My app theme turns them off:
// styles.xml
<item name="android:displayOptions"></item>
In my activity:
public void onCreate() {
...
ActionBar actionBar = activity.getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setIcon(R.drawable.ic_launcher);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
The title and up arrow appear. But I can't get the icon to appear. What am I doing wrong?
You are missing setDisplayShowHomeEnabled().

Android - Using AppCompat/ActionBar for older devices(java.lang.NullPointerException)

I have an application which currently works on Android versions 4.x and more.
I am trying to change it so it can be usable by older devices such as 2.x and more.
Everything is working fine except the action bar. I am getting a null pointer exception every time I am trying to getSupportActionBar. This is am issue that have been discussed a lot in forums but I still cannot solve it.
The application uses the appCompat library but it does not seem to work on older devices.
styles.xml file
<style name="AppBaseTheme" parent="Theme.MyApp">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerItem.DropDownItem</item>
<item name="android:windowNoTitle">true</item>
</style>
MyApp.xml file
<style name="Theme.MyApp" parent="#style/Theme.AppCompat">
<item name="actionBarItemBackground">#drawable/selectable_background</item>
<item name="popupMenuStyle">#style/PopupMenu.MyApp</item>
<item name="dropDownListViewStyle">#style/DropDownListView.MyApp</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.MyApp</item>
<item name="actionDropDownStyle">#style/DropDownNav.MyApp</item>
<item name="actionBarStyle">#style/ActionBar.Transparent.MyApp</item>
<item name="actionModeBackground">#drawable/cab_background_top</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom</item>
<item name="actionModeCloseButtonStyle">#style/ActionButton.CloseMode.MyApp</item>
Activity trying to get the actionbar
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auction_list);
if (android.os.Build.VERSION.SDK_INT >=11)
{
getActionBar().setIcon(R.drawable.gem);
getActionBar().setDisplayHomeAsUpEnabled(false);
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar_title, null);
((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
}
else
{
//This calls a private class inside the same class to get a supported action bar
notActionBar nab = new notActionBar();
nab.getNotActionBar(); //THIS IS THE ERROR LINE
}
Private class that gets the supported action bar. I have used a private class instead of directly getting the actionbar because i could not extend the ActionBarActivity because the activity all ready extends fragments.
private class notActionBar extends ActionBarActivity {
public void getNotActionBar(){
ActionBar actionBar = getSupportActionBar(); //NULL POINTER EXCEPTION
actionBar.setIcon(R.drawable.gem);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowCustomEnabled(true);
this.getSupportActionBar().setDisplayShowCustomEnabled(true);
this.getSupportActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar_title, null);
((CFTextView)v.findViewById(R.id.actionbarTitle)).setText(this.getTitle());
//assign the view to the actionbar
this.getSupportActionBar().setCustomView(v);
}
public void setTitleActionBar(String a){
getSupportActionBar().setTitle(a);
}
}
you should not try to instantiate Activities like this in Android:
notActionBar nab = new notActionBar();
the way to start Activities in Android is with startActivity(Intent intent) or startActivityForResult method.
But anyhow, The solution for your Problem should be extending ActionBarActivity instead FragmentActivity in your main Activity.
You can do this also if you use Fragments! Because ActionBarActivity extends FragmentActivity.
see related answer here
You should also consider using only getSupportActionBar() or getActionBar() either one of them. Since you wanna target earlier version, use only getSupportActionBar() and remove all the refernces to getActionBar() from your code.
A complete guide to Action Bar is here.

Android: Apply different themes to fragments of one activity

What I want to do:
I want each Fragment of my MainActivity to use a different theme, so that the ActionBar has different background-colors, depending on the visible Fragment.
The Situation:
I created a MainActivity which uses the Tabs + Swipe Navigation. I Added 7 Tabs (=7 Fragments). I created one Theme which should be applied only to the first Fragment (fragment_main_1).
Here the Theme:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Blue" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/Blue.ActionBarStyle</item>
</style>
<style name="Blue.ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">#style/Blue.ActionBar.TitleTextStyle</item>
<item name="android:background">#33B5E5</item>
</style>
<style name="Blue.ActionBar.TitleTextStyle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
After creating 6 more Themes it should be possible to swipe through the Tabs while the ActionBar changes its background-color automatically.
What didn't work:
Adding those lines (which I found here on stackoverflow) to the Fragment1.java:
// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.Blue);
// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.fragment_main_1,container, false);
I hope you can help me:) Thank you.
Many fragments (such as PreferenceFragment) read the styled attributes directly from the Context returned by Fragment.getContext() method, so you might need to override that too:
private var themedContext: Context? = null
override fun onAttach(context: Context) {
super.onAttach(context).also {
themedContext = ContextThemeWrapper(context, R.style.ThemeForThisFragment)
// if you want to apply a theme overlay:
// themedContext.theme.applyStyle(R.style.MyThemeOverlay, true)
}
}
override fun onDetach() {
super.onDetach()
themedContext = null
}
override fun getContext(): Context? {
return themedContext ?: super.getContext()
}
Try LayoutInflater localInflater = inflater.from(contextThemeWrapper); instead.

Categories

Resources