Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Android button background method #1296

Closed
bradmartin opened this issue Dec 26, 2015 · 14 comments
Closed

Change Android button background method #1296

bradmartin opened this issue Dec 26, 2015 · 14 comments

Comments

@bradmartin
Copy link
Contributor

Change the button background styling from

      setBackground()

to

     setBackgroundTintList()


       btn.setBackgroundTintList(android.content.res.ColorStateList.valueOf(tColor.android));

I've done this programatically by creating a new android.widget.Button() and found no issues.

If this is possible without collision in other areas it would give the buttons their material design ripple effect, whereas setBackground removes the ripple effect. Will this open other issues. I didn't want to make a PR without knowing if there would be other issues or conflicts.

@x4080
Copy link

x4080 commented Dec 28, 2015

Good idea

@vakrilov vakrilov added this to the 1.6.0 (Under Review) milestone Jan 4, 2016
@vakrilov
Copy link
Contributor

vakrilov commented Jan 4, 2016

Nice one @bradmartin - it is good to be able to change the color of the button without loosing the material design.

@enchev
Copy link
Contributor

enchev commented Jan 11, 2016

We can use simply:

var nativeColor = v.style._getValue(style.backgroundColorProperty).android;
nativeView.getBackground().setColorFilter(nativeColor, android.graphics.PorterDuff.Mode.SRC_IN);

if only backgroundColor is set. In this way we can preserve other styles.

@talvasconcelos
Copy link

Hi @enchev i'm sorry, how do i implement that code you showed. I have this issue where the button background-color is used as a tint, or has transparency... i want to remove that.

i pasted that snippet in my app.js and it fails to compile saying "v" is not defined... i missed a require somewhere.

@spstratis
Copy link

@enchev I have the same question as @talvasconcelos, what is the "v" object referencing here?

@bradmartin
Copy link
Contributor Author

Think it's the view instance so the button. Not at computer to check the NativeScript view api but i think that's it

@spstratis
Copy link

@bradmartin Got it, that would make sense.

@spstratis
Copy link

spstratis commented Oct 6, 2016

In one of my views where I'm experiencing this problem I've tried the following:

exports.viewLoaded = function (args) {
    var page = args.object;
    var createBtn = page.getViewById('create-btn');
    var nativeColor = createBtn.style._getValue(style.backgroundColorProperty).android;    
    page.getBackground().setColorFilter(nativeColor, android.graphics.PorterDuff.Mode.SRC_IN);
    page.bindingContext = eulaViewModel;    
};

Yet when I load that view I get the following error:

TypeError: Cannot read property 'id' of undefined
File: "/data/data/org.nativescript.LibreLinkUp/files/app/tns_modules/ui/core/dependency-observable.js, line: 148, column: 50

Should I be referencing a different "view" when calling getBackground()? I have tried a couple different things but none of them are working out.

It seems if I set the border-width of the button with ID "create-btn" then that fixes the issue in Android 4.4 but then I lose the material design in all other versions.

@calebeaires
Copy link

@enchev - Is this issue still on NativeScript team radar?

@enchev
Copy link
Contributor

enchev commented Nov 10, 2016

Hey @calebeaires,

This was fixed long time ago. Right now if you only want to set the color we will not override the default drawable.

@calebeaires
Copy link

calebeaires commented Nov 10, 2016

@enchev - Thanks for your help.

May you just me give some more info? If I choose to use border-width, I will loose ripple effect on Android 4.2 to Android 4.4?

@bradmartin
Copy link
Contributor Author

Those android API versions don't have material/ripple :)

On Thu, Nov 10, 2016, 7:32 AM Calebe Aires notifications@github.com wrote:

Thanks for your help.

May you just me give some more info? If I choose to use border-width, I
will loose ripple effect on Android 4.2 to Android 4.4?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#1296 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFulhMpKg-nu3KAIgJfJUE6wcg6RVU17ks5q8xzrgaJpZM4G7fSb
.

@enchev
Copy link
Contributor

enchev commented Nov 10, 2016

Yep, @bradmartin is correct. You can only set the color if you want to keep the original drawable.

@lock
Copy link

lock bot commented Aug 29, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Aug 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants