android how to show progress dialog on snackBar? - android-asynctask

Is there way to show progress dialog on snackBar while getting data from server for pagination inside AsyncTask.If is this possible then how to do this.Please give suggestions.

For new design library:
if you want to show progressBar in left side:
Snackbar bar = Snackbar.make(root, R.string.data_updating, Snackbar.LENGTH_INDEFINITE);
ViewGroup contentLay = (ViewGroup) bar.getView().findViewById(android.support.design.R.id.snackbar_text).getParent();
ProgressBar item = new ProgressBar(context);
contentLay.addView(item,0);
bar.show();
or in right side:
Snackbar bar = Snackbar.make(root, R.string.data_updating, Snackbar.LENGTH_INDEFINITE);
ViewGroup contentLay = (ViewGroup) bar.getView().findViewById(android.support.design.R.id.snackbar_text).getParent();
ProgressBar item = new ProgressBar(context);
contentLay.addView(item);
bar.show();

the easiest and the best approach would be this:
Snackbar bar = Snackbar.make(layout, "Somthing", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout snack_view = bar.getView();
snack_view.addView(new ProgressBar(context));
bar.show();
SnackbarLayout extends LinearLayout, so you can add view as a child or customize it the way you want.
for example to change the style of the TextView inside SnackBar you can do this:
TextView tv = (TextView) bar.getView().findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
tv.setTextSize(0, dimen[1] / 40);
tv.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/font.otf"));

I've written a library that do just that. It also includes queue system. Try it out https://github.com/tingyik90/snackprogressbar

Kotlin 2021:
fun showLoadingSnackbar(){
val snackbar: Snackbar = Snackbar.make(findViewById(R.id.rootlayoutid), "TEXT or RES", Snackbar.LENGTH_INDEFINITE)
val viewGroup = snackbar.view.findViewById<View>(com.google.android.material.R.id.snackbar_text).parent as ViewGroup
viewGroup.addView(ProgressBar(this))// Or Context if not in Activity
snackbar.show()
}
If you want it on the left simply call addView(ProgressBar(this),0) instead as mentioned in the other answer
Edit: The answer has the progressbar at the top if the text is too long, so for it to be centered you can do this:
val snackbar: Snackbar = Snackbar.make(this, text, duration)
val viewGroup = snackbar.view.findViewById<View>(com.google.android.material.R.id.snackbar_text).parent as ViewGroup
viewGroup.addView(ProgressBar(context).also {
it.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
it.foregroundGravity = Gravity.CENTER_VERTICAL
})
snackbar.show()

I think you could style a custom progress dialog to look like a Snackbar, since snackbar doesn't prevent user interaction with GUI while loading.
Update:
Try this:
Create anim res file name it slide_in.xml and paste this:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="50%p" android:toYDelta="0%p"
android:duration="#android:integer/config_longAnimTime"
/>
create another anim res file name it slide_out.xml:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="#android:integer/config_longAnimTime"
android:fromYDelta="0%p"
android:toYDelta="100%p" />
now place process dialog style in style res:
<style name="CustomDialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
<item name="android:background">#323232</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:alertDialogStyle">#style/customDialogStyle</item>
<item name="android:windowAnimationStyle">#style/DialogAnimation</item>
</style>
<style name="customDialogStyle">
<item name="android:bottomBright">#android:color/transparent</item>
<item name="android:bottomDark">#android:color/transparent</item>
<item name="android:bottomMedium">#android:color/transparent</item>
<item name="android:centerBright">#android:color/transparent</item>
<item name="android:centerDark">#android:color/transparent</item>
<item name="android:centerMedium">#android:color/transparent</item>
<item name="android:fullBright">#android:color/transparent</item>
<item name="android:fullDark">#android:color/transparent</item>
<item name="android:topBright">#android:color/transparent</item>
<item name="android:topDark">#android:color/transparent</item>
Place this for animation:
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_in</item>
<item name="android:windowExitAnimation">#anim/slide_out</item>
</style>
Now add you method:
public static ProgressDialog processSnackbar(Context context,String s){
ProgressDialog pSnackbar = new ProgressDialog(context, R.style.CustomDialog) {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressBar progress = (ProgressBar) findViewById(android.R.id.progress);
LinearLayout bodyLayout = (LinearLayout) progress.getParent();
TextView messageView = (TextView) bodyLayout.getChildAt(1);
messageView.setPadding(20,0,0,0);
LinearLayout.LayoutParams llp =
(LinearLayout.LayoutParams) messageView.getLayoutParams();
llp.width = 0;
llp.weight = 1;
bodyLayout.removeAllViews();
bodyLayout.addView(messageView, llp);
bodyLayout.addView(progress);
}
};
pSnackbar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pSnackbar.setMessage(s);
pSnackbar.getWindow().setGravity(Gravity.BOTTOM);
pSnackbar.setCancelable(false);
return pSnackbar;
}
Method Credit returns to #mike-m reference Is it possible to alter progress dialog's message or spinner position?
I am using this as well in my application take a look:

Related

Xamarin: How to change Stepper color in Android to look like in iOS?

I have Xamarin PCL app which uses Stepper. I wanted to change the color of my Stepper button and now using the following renderer.
In PCL:
public class MyStepper : Stepper
{
public static readonly BindableProperty ColorProperty =
BindableProperty.Create(nameof(Color), typeof(Color), typeof(MyStepper), Color.Default);
public Color MyColor
{
get { return (Color)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
}
In iOS:
public class MyStepperRenderer : StepperRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
{
base.OnElementChanged(e);
MyStepper s = Element as MyStepper;
if (Control != null)
Control.TintColor = s.MyColor.ToUIColor();
}
}
In Android:
protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
{
base.OnElementChanged(e);
MyStepper s = Element as MyStepper;
if (Control != null)
{
Control.GetChildAt(0).Background.SetColorFilter(s.MyColor.ToAndroid(), PorterDuff.Mode.Multiply);
Control.GetChildAt(1).Background.SetColorFilter(s.MyColor.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
In XAML:
<local:MyStepper MyColor="Red" Maximum="10" Minimum="0" ....... />
As expected it would work like the images attached. But is it possible to make the Android color to change just the "outline" of the button like what it looks like in iOS?
How about changing the colors of the - and + also to red. Is there a way to do that also?
Based on #Sven-Michael Stübe's answer, I add some code to change the "-" and "+" color:
protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
{
base.OnElementChanged(e);
MyStepper s = Element as MyStepper;
if (Control != null)
{
var button = Control.GetChildAt(0) as Android.Widget.Button;
button.SetTextColor(s.MyColor.ToAndroid());
button.SetBackground(ResourcesCompat.GetDrawable(Resources, Resource.Drawable.button_selector, null));
button.Background.SetColorFilter(s.MyColor.ToAndroid(), PorterDuff.Mode.Multiply);
var button2 = Control.GetChildAt(1) as Android.Widget.Button;
button2.SetTextColor(s.MyColor.ToAndroid());
button2.SetBackground(ResourcesCompat.GetDrawable(Resources, Resource.Drawable.button_selector, null));
button2.Background.SetColorFilter(s.MyColor.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
Effectt.
go and create a the drawable with the states and set them in your Renderer instead of setting a single color.
Sven-Michael Stübe wanna say that if you set a single color as a background, then your Button will have no click effect. You could refer to: Material effect on button with background color
for more detail information.
For example:
button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorAccent" android:state_pressed="true"/>
<item android:drawable="#color/colorPrimaryDark" android:state_focused="true"/>
<item android:drawable="#drawable/button_border"/>
</selector>
button_border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00FFFFFF" />
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#FFFFFF" />
</shape>
You can do it if you create a custom vector drawable
button_border.xml
Place this file in your Android Resources\drawable folder and ensure the build action is set to AndroidResource
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00FFFFFF" />
<corners android:radius="5dp" />
<stroke
android:width="2dp"
android:color="#FFFFFF" />
</shape>
MyStepperRenderer.cs
Then you just need to set it as background of your two buttons.
protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
{
base.OnElementChanged(e);
var s = Element as MyStepper;
if (Control != null)
{
Control.GetChildAt(0).SetBackground(Resources.GetDrawable(Resource.Drawable.button_border));
Control.GetChildAt(1).SetBackground(Resources.GetDrawable(Resource.Drawable.button_border));
Control.GetChildAt(0).Background.SetColorFilter(s.MyColor.ToAndroid(), PorterDuff.Mode.Multiply);
Control.GetChildAt(1).Background.SetColorFilter(s.MyColor.ToAndroid(), PorterDuff.Mode.Multiply);
}
}
This will look like:
discussion
You loose states like pressed, disabled, ... (see https://developer.android.com/guide/topics/resources/color-list-resource.html)
If you don't have many different colors, go and create a the drawable with the states and set them in your Renderer instead of setting a single color. So your user will get a better feedback.

SlidingPanel for Xamarin.Android

I'm creating a slide view with 2 tabs, but I don't know why the part of tabs are "transparent", they take the background color and not the AppBar color..anyone knows why?
MainActivity:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
namespace M_v1
{
[Activity(Label = "M_v1", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
SetContentView(Resource.Layout.Main);
TabLayout tabLayout = (TabLayout)FindViewById(Resource.Id.tablayout_navigation);
ViewPager viewPager = (ViewPager)FindViewById(Resource.Id.pager);
SetupviewPager(viewPager);
tabLayout.SetupWithViewPager(viewPager);
}
private void SetupviewPager(ViewPager viewPager)
{
viewPager.OffscreenPageLimit = 2;
PageAdapter adapter = new PageAdapter(SupportFragmentManager);
adapter.AddFragment(new Fragment1(), "One");
adapter.AddFragment(new Fragment2(), "Two");
viewPager.Adapter = adapter;
}
}
}
Here the theme:
<style name = "AppTheme" parent = "Theme.AppCompat.Light.NoActionBar">
<item name = "windowActionBar">true</item>
<item name ="windowNoTitle">false</item>
<item name ="colorPrimary">#2B5C56</item>
<item name ="colorPrimaryDark">#000000</item>
<item name ="colorAccent">#FFEE58</item>
</style>
Having the problem that the new TabLayout uses the indicator colour from the value colorAccent, I decided to dig into the android.support.design.widget.TabLayout implementation, finding that there are no public methods to customize this. However, I found this style specification of the TabLayout:
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabMaxWidth">#dimen/tab_max_width</item>
<item name="tabIndicatorColor">?attr/colorAccent</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabPaddingStart">12dp</item>
<item name="tabPaddingEnd">12dp</item>
<item name="tabBackground">?attr/selectableItemBackground</item>
<item name="tabTextAppearance">#style/TextAppearance.Design.Tab</item>
<item name="tabSelectedTextColor">?android:textColorPrimary</item>
Having this style specification, now we can customize the TabLayout like this:
<android.support.design.widget.TabLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#id/pages_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="#android:color/white"
app:tabIndicatorHeight="4dp"/>

Keyboard scroll up and hide Toolbar and Status bar on Android

I want to keep status bar and toolbar still visible on top while displaying soft keyboard. I tried many ways but cannot done. Tried windowSoftInputMode and set/clear flag of windows, but all failed.
Could anyone help me, please ?
Please click here to see gif file!
This is a Xamarin bug. See here.
There are some workaronds. You have to do the following in your MainActivity:
protected override void OnCreate(Bundle bundle)
{
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle);
//Remove the status bar underlay in API 21+
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window.DecorView.SystemUiVisibility = 0;
var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
statusBarHeightInfo.SetValue(this, 0);
Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
}
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}
If you use FormsApplicationActivity instead of FormsAppCompatActivity, then you have to remove the following lines:
var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
statusBarHeightInfo.SetValue(this, 0);
You can see more details with Jimmy Garrido here.
But to hide ActionBar, if you use the following code in your MainActivity, so only the above solution will not work:
Xamarin.Forms.Forms.SetTitleBarVisibility(Xamarin.Forms.AndroidTitleBarVisibility.Never);
To fix the problem, what worked for me was to delete this line above and create a specific theme to make the ActionBar disappear.
Under Resources / values / Styles.xml I created a new theme (you can change yours if you already have one):
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarSize">0dp</item>
</style>
</resources>
And don't forget to change your theme in MainActivity, like:
[Activity(WindowSoftInputMode = SoftInput.AdjustPan, Label = "Test", Icon = "#drawable/icon", Theme = "#style/MyTheme", MainLauncher = true, NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
I hope this can help you.

How to change the color of statusbar in iOS and Android with NativeScript

I'm trying to change the color of the status bar to match the rest of the Page background-color and it seems to be not working.
Below is what my home.css looks like
Page {
background-color: #5497CB;
color:#ecf0f1;
}
Here is what my home.js looks like
var frameModule = require("ui/frame");
exports.loaded = function(args) {
var page = args.object;
var iosFrame = frameModule.topmost().ios;
if (iosFrame) {
iosFrame.navBarVisibility = 'never';
}
};
And lastly here is what my home.xml
<Page loaded="loaded">
<StackLayout orientation="vertical">
<TextField id="email_address" text="{{ email }}" hint="Email Address" keyboardType="email" />
<TextField secure="true" text="{{ password }}" hint="Password" />
<Button text="Sign in" tap="signIn"/>
<Button text="Sign up for Groceries" cssClass="link" tap="register"/>
</StackLayout>
</Page>
As you can see nothing over complicated, just trying to get the status bar color to have a white font and same background color!
Any pointers?
You're not using an action bar, so you need to set the backgroundSpanUnderStatusBar on the Page element to true.
<page backgroundSpanUnderStatusBar="true">
The text will still be black, but you can change that by changing the status bar to light for the whole application.
You can use the StatusBar Plugin for more control over the status bar in NativeScript.
The Status Bar (time, battery indicator) inherits its background color from the Navigation Bar (back, page title, etc). But if you hide the Navigation Bar the Status Bar inherits the color from the Window background color instead. So if you want to change the color of the Status Bar with the Navigation Bar hidden you have to set the Window background color.
Here's an example of hiding the Nav bar and setting the status bar background to a gray-ish color.
var iosFrame = frameModule.topmost().ios;
if (iosFrame) {
iosFrame.navBarVisibility = 'never';
iosFrame.controller.view.window.backgroundColor = UIColor.colorWithRedGreenBlueAlpha(0.945, 0.953, 0.953, 1);
}
(colorWithRedGreenBlueAlpha wants a number between 0 and 1 where 1 is the same as 255 in a rgb(255, 255, 255) pattern. So basically divide with 255)
This only works for android.
You can change the primary and primaryDark colors:
<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarStyle">#style/NativeScriptToolbarStyle</item>
<item name="colorPrimary">#color/ns_primary</item> <- Here
<item name="colorPrimaryDark">#color/ns_primaryDark</item> <- And here
<item name="colorAccent">#color/ns_accent</item>
</style>
This answer solved my problems.
To change the Background Color for Android use:
import { Page, Color } from "tns-core-modules/ui/page/page";
#Component({...})
export class YourAnyComponent {
public constructor(private page: Page) {}
ngOnInit() {
this.page.androidStatusBarBackground = new Color("#0077cc");
}
}
import { Page } from "ui/page";
#Component({...})
export class YourAnyComponent {
public constructor(private page: Page) {}
ngOnInit() {
this.page.backgroundColor = '#2c303a';
this.page.backgroundSpanUnderStatusBar = true;
this.page.actionBarHidden = false; // if you want to show only statusbar or - true if you want to show both stutus bar and action bar
}
}
For Android, It's as simple as that. Just copy paste below lines in E:\projects\appname\app\App_Resources\Android\src\main\res\values\styles.xml
<item name="android:statusBarColor">#color/ns_primaryDark</item>
<item name="android:windowLightStatusBar">true</item>
put it under any theme you want, Usually I put this under AppThemeBase

Android: Make custom seekbar opaque

Hi can anyone tell me why is my Seekbar's background and progress always have some opacity and just can't be removed even I have set the alpha to 1.0. Below is my progress_drawable.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:alpha="1.0"
android:startColor="#000000"
android:endColor="#000000"/>
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:alpha="1.0"
android:startColor="#000000"
android:endColor="#000000" />
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:alpha="1.0"
android:endColor="#2e2dff"
android:startColor="#2e2dff" />
<solid android:color="#2e2dff" />
</shape>
</clip>
</item>
And this is the SeekBar tag in activity xml:
<SeekBar
android:padding="0dp"
android:id="#+id/normalSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imgPenguin"
android:alpha="1"
android:progress="0"
android:progressDrawable="#drawable/progress_drawable"
android:secondaryProgress="0" />
I got this result but I want the bars to be opaque, please advise and thanks in advanced.
Just found the issue:
normalSB.setEnabled(false);
I shouldn't use setEnabled(false) to avoid user drag on thumb, instead, I have done this to reset progress when fromUser == true:
normalSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int originalProgress;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float seekBarStarting = seekBar.getX();
float seekBarWidth = seekBar.getWidth();
float seekBarOffset = seekBar.getThumbOffset();
float ratio = (float) progress / seekBar.getMax();
float scaledProgress = ratio * seekBarWidth;
if (scaledProgress > imgPenguin.getWidth()) {
imgPenguin.setX(seekBarStarting - imgPenguin.getWidth() + scaledProgress);
}
if (fromUser == true) {
seekBar.setProgress(originalProgress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
originalProgress = seekBar.getProgress();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Resources