Action Bar messed up after using setTitle - android

I am trying to set the title of the actionBar in a activity. I have 3 layout views in one activity. Each View Layout shows a different state of the payment process. Now that I set the title, at one stage the actionbar background gets messed up. I wonder why.
This does not happen if I comment out the following line.
onClick() {
....
getActionBar().setTitle("Customer Payment");
....
In the onCreate of the activity I run the following to setup the actionbar background.
private void setupActionBar() {
Drawable backgroundColor;
switch (getIntent().getIntExtra(God.HOME_SCREEN_OPERATION,
God.INVALID_ID)) {
case God.OPERATION_RECHARGE:
getActionBar().setIcon(R.drawable.icon_mobile);
backgroundColor = new ColorDrawable(getResources().getColor(
R.color.RechargeBackgroundColor));
getActionBar().setBackgroundDrawable(backgroundColor);
mobileServiceSummary.setVisibility(View.VISIBLE);
serviceInfoLayout.setBackground(backgroundColor);
serviceInfoIcon.setImageResource(R.drawable.icon_mobile);
break;
case God.OPERATION_FACILITY:
getActionBar().setIcon(R.drawable.icon_facility);
backgroundColor = new ColorDrawable(getResources().getColor(
R.color.ToiletBackgroundColor));
getActionBar().setBackgroundDrawable(backgroundColor);
facilityServiceSummary.setVisibility(View.VISIBLE);
serviceInfoLayout.setBackground(backgroundColor);
serviceInfoIcon.setImageResource(R.drawable.icon_facility);
break;
case God.OPERATION_DTH:
getActionBar().setIcon(R.drawable.icon_dth);
backgroundColor = new ColorDrawable(getResources().getColor(
R.color.DthBackgroundColor));
getActionBar().setBackgroundDrawable(backgroundColor);
dthServiceSummary.setVisibility(View.VISIBLE);
serviceInfoLayout.setBackground(backgroundColor);
serviceInfoIcon.setImageResource(R.drawable.icon_dth);
break;
// case R.id.mseb_payment:
// getActionBar().setIcon(R.drawable.icon_mseb);
// msebServiceSummary.setVisibility(View.VISIBLE) ;
// break;
default:
break;
}
}
Some more code..
private void enableCustomerPayment() {
getActionBar().setTitle("Customer Payment");
getActionBar().setSubtitle(
"Pincode of customer needed for payment permission.");
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setDisplayShowCustomEnabled(false) ;
getActionBar().setDisplayUseLogoEnabled(false) ;
findViewById(R.id.next_button).setVisibility(View.GONE);
findViewById(R.id.payment_button).setVisibility(View.VISIBLE);
findViewById(R.id.done_button).setVisibility(View.GONE);
operatorLockLayout.setVisibility(View.GONE);
customerLoginAndConfirmationLayout.setVisibility(View.VISIBLE);
customerPaymentLayout.setVisibility(View.GONE);
customerConfirmLayout.setVisibility(View.VISIBLE);
// customerConfirmSpaceLayout.setVisibility(View.VISIBLE);
}
private void enablePaymentConfirmation() {
getActionBar().setTitle("Payment Confirmation");
getActionBar().setSubtitle("Thankyou for your payment.");
setupActionBar();
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(false);
findViewById(R.id.next_button).setVisibility(View.GONE);
findViewById(R.id.payment_button).setVisibility(View.GONE);
findViewById(R.id.done_button).setVisibility(View.VISIBLE);
operatorLockLayout.setVisibility(View.GONE);
customerLoginAndConfirmationLayout.setVisibility(View.VISIBLE);
customerPaymentLayout.setVisibility(View.VISIBLE);
customerConfirmLayout.setVisibility(View.GONE);
// customerConfirmSpaceLayout.setVisibility(View.GONE);
}
In styles.xml the color's are setup like this. And the colors are working fine.
<color name="NewWalletBackgroundColor">#FFD54E</color>
<color name="BalanceBackgroundColor">#FFD54E</color>
<color name="DepositBackgroundColor">#FFD54E</color>
<color name="MsebBackgroundColor">#E57272</color>
<color name="RechargeBackgroundColor">#81C784</color>
<color name="DthBackgroundColor">#AB6BAC</color>
<color name="ToiletBackgroundColor">#56C0ED</color>
The Action Bar is messed up
Here, the Action Bar background is completely blue. This is what I expect.
Edit
Seems like there is a issue with the height, it starts up with 96 and when its messed up the height is 0.
How do I fix this now ?

In my opinion the default action bar is expecting an icon for the navigation drawer icon on the right of action bar. I think there is a transparent button on the space you are talking about. Even if you set the drawer for this activity or not it will be there if you use default methods to set the action bar.First try these steps
setDrawerIndicatorEnabled(false).
actionBar.setDisplayHomeAsUpEnabled(false)
actionBar.setHomeButtonEnabled(false);
If that does not work go for a custom action bar.
Do something like this. Instead of using the default actionbar why do not you try to set a custom view (plain TextView where its width is set to match parent).
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.titleview, null);
//if you need to customize anything else about the text, do it here.
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);

I'm assuming from your question that the enablePaymentConfirmation is working code with getActionBar().setTitle("Customer Payment"); present and when it is commented out. If it too is playing up, then I will need to alter my answer.
Not seeing your xml makes it a bit more guess work, but from I can see, I would suggest the following.
1.
Call setupActionBar() from you OnCreate in your activity. This way it is set up for the duration of your activity. By not calling it in OnCreate if you call enableCustomerPayment() before enablePaymentConfirmation(), the actionbar setup will not have been called.
2.
Unless the following change in code you have not shown, these can go in your setupActionbar:
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(false);
3.
These are the two changes to the ActionBar between the two methods you show:
getActionBar().setDisplayShowCustomEnabled(false) ;
getActionBar().setDisplayUseLogoEnabled(false) ;
4.
I am not sure why you are only setting setDisplayShowCustomEnabled in your enableCustomerPayment(). I cannot see that you are using a customView.
- If not that can also go into your setup.
- Otherwise you will need to call it in all your methods with the changes.
- If you are using a custom view for your actionbar in some places and not others, this will be affecting how it is displayed. Particularly if you are not managing the bool changes.
5.
Same goes for the logo, I don't see where you are using it. So I would put it in setup. If you are using it for the others and you haven't got your xml set up correctly, it could be why there is a difference in the background color.
Hope this helps. Feel free to post more info, xml would be good.

Related

LiveData update of BadgeDrawable in ToolBar MenuItem

I want to show a badge on a toolbar action. The badge number is updated by a LiveData value.
This is how I attach the badge:
BadgeUtils.attachBadgeDrawable(inboxBadgeDrawable, toolbar, R.id.menu_inbox);
I tried different places for that call, including Activity.onCreateOptionsMenu(), Activity.onPrepareOptionsMenu() and androidx.lifgecycle.Observer.onChanged().
When anything changes (toolbar or badge content), the badge is misplaced, traveling down left. Or it is duplicated to another action.
I guess attachBadgeDrawable tries to find the container view of R.id.menu_inbox inside the toolbar, inserts the badge and updates it's offsets. If the container view of the menu item changes, the old container view still has the old badge and there is no (sensible) way to remove it. Also, application of the offsets seems to stack.
So, is there any other intended way of using the BadgeDrawable on a toolbar action icon?
I understand that this feature is still experimental. Will this issue be addressed and if yes, how long will it approximately take? (I use com.google.android.material:material:1.3.0-beta01 right now.)
This question is mainly addressed to the developers of the component because usage questions should be asked here according to https://github.com/material-components/material-components-android.
EDIT: I also created an issue (feature request) on the project's tracker: https://github.com/material-components/material-components-android/issues/1967
I'm not sure it is an official solution but this is still a workaround. I ended up with detaching the BadgeDrawable on every onPrepareOptionsMenu, in case the menu items were changed or rearranged
// This is an indicator of whether we need to show the badge or not
private var isFilterOn: Boolean = false
private var filterBadge: BadgeDrawable? = null
#SuppressLint("UnsafeExperimentalUsageError")
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
val filterItem = menu.findItem(R.id.action_filter)
val toolbar = requireActivity().findViewById<Toolbar>(R.id.toolbar)
if(filterBadge != null) {
BadgeUtils.detachBadgeDrawable(filterBadge!!, toolbar, R.id.action_filter)
filterBadge = null
}
if(isFilterOn) {
filterBadge = BadgeDrawable.create(requireContext()).also {
BadgeUtils.attachBadgeDrawable(it, toolbar, R.id.action_filter)
}
}
}

Programmatically change AppBar background color in Kotlin

I'm pretty new to Android development and completely new to Kotlin. I have an app with a navigation drawer, and am trying to change the color of the AppBarLayout based on what the user selects from the navigation drawer. I've tried a few different methods, and the closest I've come has been to change the toolbar color instead of the whole AppBar. This might be acceptable, but instead of actually setting it to the color I want, it always changes it to a dark grey.
when (item.itemId) {
R.id.nav_audit -> {
txtMain.text = "Audit"
toolbar.setBackgroundColor(R.color.colorAudit)
loadAudits()
}
R.id.nav_testing -> {
txtMain.text = "Testing"
toolbar.setBackgroundColor(R.color.colorTesting)
}
R.id.nav_workflow -> {
txtMain.text = "Workflow"
toolbar.setBackgroundColor(R.color.colorWorkflow)
}
R.id.nav_other -> {
txtMain.text = "Other"
toolbar.setBackgroundColor(R.color.colorPrimary)
}
}
I've also looked at possibly changing the theme, but it looks like it may not be easy to do that after the application has already loaded. Any thoughts are appreciated.
Besides changing the color of the Toolbar that you are already doing, one way to change the status bar in Kotlin is like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
(activity as? AppCompatActivity)?.window?.statusBarColor =
ContextCompat.getColor(context, R.color. colorTesting)
}
You could do a method that returns the color depending on itemId

How to make material design full screen dialog?

I have tried many variants to create full screen dialog but I couldn't. I need something, like this, with two buttons:
If you really want a a fullscreen dialog just extend the Dialog class and add a few tweaks. (You can also accomplish this without extending anything, but I thought you might want to keep everything in one place)
In your constructor you need to set the style (for your material look, or it can be an empty style tag):
super(context, R.style.DialogStyle);
you also need to set the view: (This is where you would define where/what those two buttons are)
setContentView(R.layout.dialog_view);
Finally, you might also need to modify the windows layout parameters:
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
I have found on the devices I have tested that setting the style is the most important.
*EDIT*
To make it a little more clear you have two options:
public class MyDialog extends Dialog {
public MyDialog(Context context) {
super(context, R.style.YourStyle);
setContentView(R.layout.dialog_view);
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); //Optional
//Any other code you want in your constructor
}
}
Then when you want to show it:
//Inside your activity
MyDialog dialog = new MyDialog(this); //Assuming you are in an activity 'this' is your context
dialog.show();
Or you can just do this:
Dialog normalDialog = new Dialog(this, R.style.YourStyle);
normalDialog.setContentView(R.layout.dialog_view);
normalDialog.show();
I agree with using a new activity. Set the HomeAsUp indicator to whatever you want, then the Save button could be a single menu item set to show ifRoom.
http://developer.android.com/reference/android/app/ActionBar.html#setHomeAsUpIndicator(int)
android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
This is what I use to display a full screen dialog without action bar:
//Display fullscreen without actionbar
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_NoActionBar_Fullscreen);
} else {
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
}

The title/action bar ID in android?

I wanted to try out this funny title bar coloring, but it doesn't work for me as
getWindow().findViewById(android.R.id.title);
returns null. So I had a look at it with Hierarchy Viewer and found out that the view is called id/action_bar instead. But there's no R.id.action_bar (autocomplete doesn't offer it and there's nothing like this is R.java).
So now I'm doubly confused:
Is android.R.id.title sort of obsolete now (I'm using version 16 in my emulator)?
Where does id/action_bar come from?
What's the recommended and simple practice w.r.t. compatibility?
Should I get ActionBarSherlock? I originally just wanted to change the title bar color... not fool around with it a lot.
Use the code below to get the ActionBar's id:
val actionBarId = resources.getIdentifier("action_bar", "id", packageName)
And use findViewById you can find the action bar.
Then find the title from actionbar's children (in normal cases):
val actionbar = findViewById<ViewGroup>(actionBarId)
for (view in actionbar.children) {
if (view is TextView) {
// this is the titleView
}
}
However, if you just want to change the title view's text, just use getSupportActionBar:
supportActionBar?.apply {
// set title text
title = "Hello"
// set colored title text
val coloredTitle = SpannableString("Hello")
coloredTitle.setSpan(ForegroundColorSpan(Color.RED), 0, coloredTitle.length, 0)
title = coloredTitle
}
I would recommend using ActionBarSherlock if you're looking for compatibility with Android versions before API level 14 / Android 4.0.
Changing the background of the ActionBar is straightforward and is most easily done via styles. See the "Background" section of http://android-developers.blogspot.com/2011/04/customizing-action-bar.html
You can also change it via code. Put this in your onCreate():
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {Color.RED, Color.GREEN});
getActionBar().setBackgroundDrawable(gradientDrawable);
Here is a screenshot of this code in action:

How to use an image rather than Activity Label in top-left corner of MainActivity in Xamarin Android (API 23)

At the top-left corner of the main activity of my Xamarin Android app (using API 23) I currently have a simple text that is set with the command below.
[Activity(Label = "Activity Name", MainLauncher = true, Icon = "#drawable/appIcon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
This line is put just before the
public class MainActivity : Activity
{
// code for main activity
}
and as a result I can see the text "Activity Name" in the top-left corner of my app.
I was wondering how can I use an image instead in that top-left corner. I have seen that the Label property of the Activity class only accept a string, so the changing the Label won't work. But I have seen apps that have some icon or image rather than the activity name, so I was wondering what is the process to achieve this?
EDIT
I have tried with the following lines in the OnCreate() method of the MainActivity:
var actionBar = this.ActionBar;
actionBar.SetIcon(Resource.Drawable.appIcon);
actionBar.SetLogo(Resource.Drawable.appIcon);
//actionBar.SetIcon(Resources.GetDrawable(Resource.Drawable.appIcon));
but none of them works. There is no error, it just doesn't show any image but only the original text I passed as Label (in my example "Activity Name").
I also tried removing the label completely cause I thought it might take the whole space where the image is supposed to be shown, but that still doesn't solve my issue. I did this by adding also:
actionBar.SetDisplayShowTitleEnabled(false);
I am sure this is the right place for the code since if I use actionBar.Subtitle I can see the subtitle is added correctly in the action bar.
Not sure if this is relevant, but I also have the following in the AssemblyInfo.cs
[assembly: Application(Theme = "#android:style/Theme.Material.Light")]
As mentioned, I am using Xamarin and API 23, to make my app comparinle,with Android 6.0+.
Try to use the following code
var actionBar = ((Activity)Context).ActionBar;
actionBar.SetIcon(Resource.Drawable.newIcon);// add the newIcon in the folder Drawable
I found the solution. I just had to add:
var actionBar = this.ActionBar;
actionBar.SetDisplayShowHomeEnabled(true);
actionBar.SetIcon(Resource.Drawable.appIcon);
SetDisplayShowHomeEnabled will force the icon to be shown.

Categories

Resources