Adding opacity to setStroke() programmatically











up vote
0
down vote

favorite












I am trying to set opacity to Border color using:



d.setStroke(5 , Color.parseColor("#4A000000"))


The result I get is a border with 50% dark color and 50% of opacity applied.



How to make that to blend like a normal background color, ColorUtils.setAlphaComponent(color, 100)?










share|improve this question
























  • Why dont you like the border opacity at 50%? Can you explain what are you trying to achieve more precisely. Cant understand why the result of your code does not fit what you want.
    – Ivan
    Nov 8 at 11:15










  • I am trying to have a border with color and a variable alpha value, so that the border itself would get a transparency blended.
    – pixelWorld
    Nov 8 at 11:26















up vote
0
down vote

favorite












I am trying to set opacity to Border color using:



d.setStroke(5 , Color.parseColor("#4A000000"))


The result I get is a border with 50% dark color and 50% of opacity applied.



How to make that to blend like a normal background color, ColorUtils.setAlphaComponent(color, 100)?










share|improve this question
























  • Why dont you like the border opacity at 50%? Can you explain what are you trying to achieve more precisely. Cant understand why the result of your code does not fit what you want.
    – Ivan
    Nov 8 at 11:15










  • I am trying to have a border with color and a variable alpha value, so that the border itself would get a transparency blended.
    – pixelWorld
    Nov 8 at 11:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to set opacity to Border color using:



d.setStroke(5 , Color.parseColor("#4A000000"))


The result I get is a border with 50% dark color and 50% of opacity applied.



How to make that to blend like a normal background color, ColorUtils.setAlphaComponent(color, 100)?










share|improve this question















I am trying to set opacity to Border color using:



d.setStroke(5 , Color.parseColor("#4A000000"))


The result I get is a border with 50% dark color and 50% of opacity applied.



How to make that to blend like a normal background color, ColorUtils.setAlphaComponent(color, 100)?







android alpha






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 14:52









André Sousa

1,012818




1,012818










asked Nov 8 at 11:05









pixelWorld

86




86












  • Why dont you like the border opacity at 50%? Can you explain what are you trying to achieve more precisely. Cant understand why the result of your code does not fit what you want.
    – Ivan
    Nov 8 at 11:15










  • I am trying to have a border with color and a variable alpha value, so that the border itself would get a transparency blended.
    – pixelWorld
    Nov 8 at 11:26


















  • Why dont you like the border opacity at 50%? Can you explain what are you trying to achieve more precisely. Cant understand why the result of your code does not fit what you want.
    – Ivan
    Nov 8 at 11:15










  • I am trying to have a border with color and a variable alpha value, so that the border itself would get a transparency blended.
    – pixelWorld
    Nov 8 at 11:26
















Why dont you like the border opacity at 50%? Can you explain what are you trying to achieve more precisely. Cant understand why the result of your code does not fit what you want.
– Ivan
Nov 8 at 11:15




Why dont you like the border opacity at 50%? Can you explain what are you trying to achieve more precisely. Cant understand why the result of your code does not fit what you want.
– Ivan
Nov 8 at 11:15












I am trying to have a border with color and a variable alpha value, so that the border itself would get a transparency blended.
– pixelWorld
Nov 8 at 11:26




I am trying to have a border with color and a variable alpha value, so that the border itself would get a transparency blended.
– pixelWorld
Nov 8 at 11:26












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You could use setStroke(),the second parameter is a ColorStateList.



And each item in a ColorStateList must define an android:color attribute like below:



<item android:state_enabled="false"
android:color="?android:attr/colorAccent"
android:alpha="0.5" /> //use this xml attribute to set opacity.


Below is a complete ColorStateList example:



<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:color="@color/sample_focused" />
<item android:state_pressed="true"
android:state_enabled="false"
android:color="@color/sample_disabled_pressed" />
<item android:state_enabled="false"
android:color="@color/sample_disabled_not_pressed" />
<item android:color="@color/sample_default"
android:alpha="0.5"/>
</selector>


After creating a ColorStateList resource xml, use it in setStroke() method.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206468%2fadding-opacity-to-setstroke-programmatically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You could use setStroke(),the second parameter is a ColorStateList.



    And each item in a ColorStateList must define an android:color attribute like below:



    <item android:state_enabled="false"
    android:color="?android:attr/colorAccent"
    android:alpha="0.5" /> //use this xml attribute to set opacity.


    Below is a complete ColorStateList example:



    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
    android:color="@color/sample_focused" />
    <item android:state_pressed="true"
    android:state_enabled="false"
    android:color="@color/sample_disabled_pressed" />
    <item android:state_enabled="false"
    android:color="@color/sample_disabled_not_pressed" />
    <item android:color="@color/sample_default"
    android:alpha="0.5"/>
    </selector>


    After creating a ColorStateList resource xml, use it in setStroke() method.






    share|improve this answer

























      up vote
      0
      down vote













      You could use setStroke(),the second parameter is a ColorStateList.



      And each item in a ColorStateList must define an android:color attribute like below:



      <item android:state_enabled="false"
      android:color="?android:attr/colorAccent"
      android:alpha="0.5" /> //use this xml attribute to set opacity.


      Below is a complete ColorStateList example:



      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_focused="true"
      android:color="@color/sample_focused" />
      <item android:state_pressed="true"
      android:state_enabled="false"
      android:color="@color/sample_disabled_pressed" />
      <item android:state_enabled="false"
      android:color="@color/sample_disabled_not_pressed" />
      <item android:color="@color/sample_default"
      android:alpha="0.5"/>
      </selector>


      After creating a ColorStateList resource xml, use it in setStroke() method.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You could use setStroke(),the second parameter is a ColorStateList.



        And each item in a ColorStateList must define an android:color attribute like below:



        <item android:state_enabled="false"
        android:color="?android:attr/colorAccent"
        android:alpha="0.5" /> //use this xml attribute to set opacity.


        Below is a complete ColorStateList example:



        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true"
        android:color="@color/sample_focused" />
        <item android:state_pressed="true"
        android:state_enabled="false"
        android:color="@color/sample_disabled_pressed" />
        <item android:state_enabled="false"
        android:color="@color/sample_disabled_not_pressed" />
        <item android:color="@color/sample_default"
        android:alpha="0.5"/>
        </selector>


        After creating a ColorStateList resource xml, use it in setStroke() method.






        share|improve this answer












        You could use setStroke(),the second parameter is a ColorStateList.



        And each item in a ColorStateList must define an android:color attribute like below:



        <item android:state_enabled="false"
        android:color="?android:attr/colorAccent"
        android:alpha="0.5" /> //use this xml attribute to set opacity.


        Below is a complete ColorStateList example:



        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true"
        android:color="@color/sample_focused" />
        <item android:state_pressed="true"
        android:state_enabled="false"
        android:color="@color/sample_disabled_pressed" />
        <item android:state_enabled="false"
        android:color="@color/sample_disabled_not_pressed" />
        <item android:color="@color/sample_default"
        android:alpha="0.5"/>
        </selector>


        After creating a ColorStateList resource xml, use it in setStroke() method.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 11:23









        navylover

        2,39421117




        2,39421117






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206468%2fadding-opacity-to-setstroke-programmatically%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check