I am trying to add an image to the status bar in xamarin forms android..
But the image is completely shifted down and coming under the normal status bar color i set in android nougat.
This happened when i updated the xamarin forms version to 2.4.0.74863
in 2.3.4.270 its working fine. Is this the xamrin forms issue or my code issue.
The code i used to draw overlay on status bar is
activity.Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
activity.Window.ClearFlags(WindowManagerFlags.TranslucentStatus);
activity.Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
ViewGroup contentView = (ViewGroup)activity.FindViewById(Android.Resource.Id.Content);
//if (contentView.ChildCount > 1)
//{
// contentView.RemoveViewAt(1);
//}
// get status bar height
int res = activity.Resources.GetIdentifier("status_bar_height", "dimen", "android");
int height = 0;
if (res != 0)
height = activity.Resources.GetDimensionPixelSize(res);
// create new imageview and set resource id
ImageView image = new ImageView(activity);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, height);
params1.Width = LinearLayout.LayoutParams.MatchParent;
image.LayoutParameters = params1;
image.SetImageResource(imageRes);
image.SetScaleType(ImageView.ScaleType.FitXy);
// add image view to content view
contentView.AddView(image);
contentView.SetFitsSystemWindows(true);
I call this as a dependedncy from the xaml page.
EDIT
After i added the flags that #york mentioned the bottom navigation bar has become translucent and the app view went down.
Just need add the following code in your Activity style :
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
Effect :
Related
I have a screen in my app that loads an image behind a transparent status bar. Sometimes these images are light enough for the status bar icons and text to get lost in the image.
How do you set the status bar to light/dark based on the colors of the image underneath it? I'm trying to keep the status bar completely transparent and not add a background.
You can use this solution to change the statusbar color based on your background :
// Set the background and text colors of a toolbar given a
// bitmap image to match
public void setToolbarColor(Bitmap bitmap) {
// Generate the palette and get the vibrant swatch
// See the createPaletteSync() method
// from the code snippet above
Palette p = createPaletteSync(bitmap);
Palette.Swatch vibrantSwatch = p.getVibrantSwatch();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Load default colors
int backgroundColor = ContextCompat.getColor(getContext(),
R.color.default_title_background);
int textColor = ContextCompat.getColor(getContext(),
R.color.default_title_color);
// Check that the Vibrant swatch is available
if(vibrantSwatch != null){
backgroundColor = vibrantSwatch.getRgb();
textColor = vibrantSwatch.getTitleTextColor();
}
// Set the toolbar background and text colors
toolbar.setBackgroundColor(backgroundColor);
toolbar.setTitleTextColor(textColor);
}
Now, simply generate bitmap from your background and then use it with this method.
For more info, visit the official developers site - Color palette API
Our design team came up with this idea... is it possible to create this loop arrow pointing to circle above dialog?
You can create full screen dialog with transparent background.
Then calculate x / y points of this loop and place arrow.
dialog example:
public void showDialog(Context context, View hourView) {
dialog = new Dialog(context, R.style.DreamDialogStyle);
dialog.setContentView(R.layout.dream_custom_add_photo_dialog);
// hourView in your case will be probably view with hour timer
// you need get for this view x and y position on your screen
// then in your dialog layout you need setX() and setY() for your loppView
// but your loopView will be full transparent background
// for position of x and y on screen You can use
// int[] array = new int[2];
// hourView.getLocationOnScreen(array);
// X = array[0] , Y = array[1]
// and in setX() setY() calculate loopSize for position loop on hourView
// And for arrow would be nice if You have image with this arrow.
// then You will need also calculate for your dilogTop and loopLeftDown
// and setAngle() in image for fill arrow line
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
dialog.show();
}
Dialog style for transparent background:
<style name="DreamDialogStyle" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
I am implementing Toolbar along and Navigation drawer.I am customized my tool bar with my own views.I have my own menu(Hamburger) icon in my toolbar to open navigation drawer and I am showing badge count on my menu icon(Hamburger).So i would like to hide default Hamburger icon.
I have tried like this:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
Please help me.Thanks in advance.
call .setDrawerIndicatorEnabled(false); on your ActionBarDrawerToggle
This works for me:
public void setLogo(String imageUrl) {
ViewGroup toolbarView = (ViewGroup) toolbar.findViewById(R.id.toolbar);
// set an arbitrary logo so the support library adds an ImageView to the toolbar.
// without this, there would be no ImageView elements in the toolbar
// and the following loop would not work
// `empty_drawable` is a simple rectangle of size 100x100 with a bg color
// matching that of the toolbar
// the size really matters because the smaller you set this image, the next logo
// you set will be of that size. If your arbitrary drawable is of size 1x1, then
// the next logo you set will be of the same size. At least that is what happens
// for me when I load an image with Glide.
// Your drawable should have a background color too. I chose the toolbar's
// background color. If you don't set a background color, your app would crash on Kitkat.
toolbar.setLogo(R.drawable.empty_drawable);
for (int i = 0; i < toolbarView.getChildCount(); i++) {
if ((toolbarView.getChildAt(i) instanceof ImageView)) {
ImageView logoView = (ImageView) toolbarView.getChildAt(i);
if (!TextUtils.isEmpty(imageUrl)) {
// this way I can download an image and set the logo that way
Glide.with(MainActivity.this).load(imageUrl).into(logoView);
// I do this because there is no space between logo and the toolbar title
logoView.setPadding(0, 0, 30, 0);
// and this is because of the `else` block
logoView.setVisibility(View.VISIBLE);
} else {
// pass null to `setLogo()` to hide the logo
logoView.setVisibility(View.GONE);
logoView.setPadding(0, 0, 0, 0);
}
break;
}
}
}
And this is my toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:animateLayoutChanges="true"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name"
tools:ignore="UnusedAttribute" />
The android:animateLayoutChanges="true" part makes all this really nice.
And tools:ignore="UnusedAttribute" is because my app's minimum SDK is set to 9.
Try this in your activity's onCreate:
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
If you're using the AppCompat, replace getActionBar with getSupportActionBar.
I tried several things before and searched the web, but no answer.
How do I get the current ActionBar size?
I styled my ActionBar like this:
<style name="MusicSlide.Theme.ActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:height">60dp</item>
<item name="android:actionBarSize">60dp</item>
<item name="android:homeAsUpIndicator">#drawable/ic_drawer</item>
<item name="android:background">#color/complete_transparent</item>
<item name="android:titleTextStyle">#style/MusicSlide.Theme.ActionBar.TitleText</item>
<item name="android:subtitleTextStyle">#style/MusicSlide.Theme.ActionBar.TitleText</item>
</style>
It is transparent.
In my app I implemented a DrawerLayout. I don't want the DrawerLayout to be underneath the ActionBar , so I set the margin of the DrawerLayout:
final TypedArray styledAttributes = mContext.getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
int actionheight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) mDrawerListRight
.getLayoutParams();
mlp.setMargins(0,(actionheight + statbarheight),0,0);
The problem is that android.R.attr.actionBarSize returns the original height of the ActionBar.
How do I get the 60dp I set in my style?
android:actionBarSize isn't an attribute of Widget.ActionBar, this is why you aren't returning the correct value. Add that line to your app's theme, wherever you define android:actionBarStyle.
My dialog is using a linearlayout for its root, and its using the following code as its custom theme:
<style name="HuskyBusDialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Is it possible to set a max width? Its fine on phones but im trying to optimize it for tablets and they are too big.
The width will be dealt with in the XML for the linear layout, not in the style that you apply to it. Use the android:layout_width tag in XML to specify how wide it could possibly be.
An old question, however no replies are suitable, but you can find some hint here: How to customize the width and height when show an Activity as a Dialog
This helps customize the width and height, but not set the max width and height! To achieve that on an activity using a dialog theme, I had to do a couple of things:
1) set a layout listener to know when the dialog layout is set.
2) Adjust the dialog layout if its size was beyond the desired limit, effectively setting max size
Here is the working snippet I'm currently using, called after setContentView():
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener()
{
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout()
{
adjustDialog();
}
});
Now the dialog size adjustment, as a percentage of actual screen size:
private void adjustDialog()
{
Window w = getWindow();
w.setGravity(Gravity.CENTER);
int current_width = w.getDecorView().getWidth();
int current_height = w.getDecorView().getHeight();
WindowManager.LayoutParams lp = w.getAttributes();
DisplayMetrics dm = getResources().getDisplayMetrics();
int max_width = (int) (dm.widthPixels * 0.90);
int max_height = (int) (dm.heightPixels * 0.90);
if (current_width > max_width)
{
lp.width = max_width;
}
if (current_height > max_height)
{
lp.height = max_height;
}
w.setAttributes(lp);
}
yeaa if u wanna do with Widht GO for ur xml
and try to do
android:layout_width="Defined in pixels//20px"
or u can try this also
android:width="Defined in pixels// 30px"
i hope it will be hepful to u