Cannot hide/remove an Activity's title - android

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);

Related

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.

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.

How to set the title of DialogFragment?

This should be a simple task, but for some reason I can find a way to set the title of a DialogFragment. (I am setting the dialog contents using onCreateView overload).
The default style leaves a place for the title, but I can't find any method on the DialogFragment class to set it.
The title is somehow magically set when the onCreateDialog method is used to set the contents, so I wonder if this is by design, or there is a special trick to set it when using the onCreateView overload.
You can use getDialog().setTitle("My Dialog Title")
Just like this:
public static class MyDialogFragment extends DialogFragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set title for this dialog
getDialog().setTitle("My Dialog Title");
View v = inflater.inflate(R.layout.mydialog, container, false);
// ...
return v;
}
// ...
}
Does overriding onCreateDialog and setting the title directly on the Dialog work? Like this:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("My Title");
return dialog;
}
Jason's answer used to work for me, but now it needs the following additions to get the title to show.
Firstly, in your MyDialogFragment's onCreate() method, add:
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogFragmentStyle);
Then, in your styles.xml file, add:
<style name="MyDialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
After hours of trying different things, this is the only one that has done the trick for me.
NB - You may need to change the Theme.AppCompat.Light.Dialog.Alert to something else in order to match the style of your theme.
DialogFragment could be represented as dialog and as Activity. Use code below that would work properly for both
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getShowsDialog()) {
getDialog().setTitle(marketName);
} else {
getActivity().setTitle(marketName);
}
}
You can take a look at the official docs.
The way i did is like this:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("My Title");
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_layout, null);
builder.setView(view);
return builder.create();
}
Similar to Ban Geoengineering's answer, but with a few modifications, so instead of coding what specific theme to use in the DialogFragment, I override the default style used by DialogFragments in my styles.xml.
set the title in the androidx.fragment.app.DialogFragment.
class EditBatteryLevelFragment:DialogFragment(),SelfClosingFragment.Host
{
override fun onCreateView(
inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?
):View
{
// set dialog title
requireDialog().setTitle(R.string.edit_battery_level__title)
// .....
return someView
}
}
in your app theme in styles.xml, override android:dialogTheme, which is the default style used by DialogFragment instances.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents">
<!-- BONUS READING: override material colors here, too https://material.io/develop/android/theming/color -->
<!-- override DialogFragment theme of those spawned by activities with this theme -->
<item name="android:dialogTheme">#style/AppTheme.Dialog</item>
</style>
<!-- ... -->
also in styles.xml, declare the dialog theme that will be used by DialogFragment instances. it's important for this style to inherit from ThemeOverlay so that it will preserve your app's theme colors.
<!-- ... -->
<!-- define the style for your dialog -->
<style name="AppTheme.Dialog" parent="ThemeOverlay.MaterialComponents.Dialog">
<!-- add a minimun width to the dialog, so it's not too narrow -->
<item name="android:windowMinWidthMajor">#dimen/abc_dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#dimen/abc_dialog_min_width_minor</item>
<!-- display the title for dialogs -->
<item name="android:windowNoTitle">false</item>
</style>
</resources>
make sure that the activity that is spawning the DialogFragment is using the defined AppTheme.
If you are using view binding:
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
binding = YourDialogXmlBinding.inflate(getLayoutInflater());
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle("Your title here")
.setView(binding.getRoot());
return builder.create();
}

Categories

Resources