Android Databinding default variable value












0














I have this layout.



<data>
<import type="android.view.View" />
<variable
name="shouldBeVisible"
type="java.lang.Boolean" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@={shouldBeVisible}" />

<LinearLayout
android:layout_width="40dp"
android:layout_height="30dp"
android:background="#dd0"
android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE}" />

</LinearLayout>


shouldBeVisible is by default false. Is there a way to make shouldBeVisible true? I want to have LinearLayout visible in this one case.



So far I'm using binding.shouldBeVisible=true










share|improve this question





























    0














    I have this layout.



    <data>
    <import type="android.view.View" />
    <variable
    name="shouldBeVisible"
    type="java.lang.Boolean" />
    </data>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="@={shouldBeVisible}" />

    <LinearLayout
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:background="#dd0"
    android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE}" />

    </LinearLayout>


    shouldBeVisible is by default false. Is there a way to make shouldBeVisible true? I want to have LinearLayout visible in this one case.



    So far I'm using binding.shouldBeVisible=true










    share|improve this question



























      0












      0








      0







      I have this layout.



      <data>
      <import type="android.view.View" />
      <variable
      name="shouldBeVisible"
      type="java.lang.Boolean" />
      </data>

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <CheckBox
      android:id="@+id/checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="@={shouldBeVisible}" />

      <LinearLayout
      android:layout_width="40dp"
      android:layout_height="30dp"
      android:background="#dd0"
      android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE}" />

      </LinearLayout>


      shouldBeVisible is by default false. Is there a way to make shouldBeVisible true? I want to have LinearLayout visible in this one case.



      So far I'm using binding.shouldBeVisible=true










      share|improve this question















      I have this layout.



      <data>
      <import type="android.view.View" />
      <variable
      name="shouldBeVisible"
      type="java.lang.Boolean" />
      </data>

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <CheckBox
      android:id="@+id/checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="@={shouldBeVisible}" />

      <LinearLayout
      android:layout_width="40dp"
      android:layout_height="30dp"
      android:background="#dd0"
      android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE}" />

      </LinearLayout>


      shouldBeVisible is by default false. Is there a way to make shouldBeVisible true? I want to have LinearLayout visible in this one case.



      So far I'm using binding.shouldBeVisible=true







      android android-databinding






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 21:17







      svkaka

















      asked Nov 12 '18 at 21:06









      svkakasvkaka

      579320




      579320
























          1 Answer
          1






          active

          oldest

          votes


















          1














          To solve that error you can use several ways:
          1) You can use primitive boolean type with inverted logic (shouldBeVisible -> shouldBeHidden)



          <variable name="shouldBeHidden" type="boolean" />

          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeHidden ? View.GONE : View.VISIBLE}" />


          2) Second way is to use boxed Boolean type (as you have now) and set default value in expressions



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{(shouldBeVisible ?? true) ? View.VISIBLE : View.GONE}" />


          3) Third way - manually set it after inflating of binding (as you already do now)



          binding.shouldBeVisible=true


          4) Use default keyword in databining value



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE, default=View.GONE}" />


          Choose the one best fits your needs.






          share|improve this answer























          • 3rd way did the trick
            – svkaka
            Nov 28 '18 at 11:58










          • Try to use 4-th approach
            – ConstOrVar
            Nov 28 '18 at 13:18











          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',
          autoActivateHeartbeat: false,
          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%2f53270107%2fandroid-databinding-default-variable-value%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









          1














          To solve that error you can use several ways:
          1) You can use primitive boolean type with inverted logic (shouldBeVisible -> shouldBeHidden)



          <variable name="shouldBeHidden" type="boolean" />

          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeHidden ? View.GONE : View.VISIBLE}" />


          2) Second way is to use boxed Boolean type (as you have now) and set default value in expressions



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{(shouldBeVisible ?? true) ? View.VISIBLE : View.GONE}" />


          3) Third way - manually set it after inflating of binding (as you already do now)



          binding.shouldBeVisible=true


          4) Use default keyword in databining value



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE, default=View.GONE}" />


          Choose the one best fits your needs.






          share|improve this answer























          • 3rd way did the trick
            – svkaka
            Nov 28 '18 at 11:58










          • Try to use 4-th approach
            – ConstOrVar
            Nov 28 '18 at 13:18
















          1














          To solve that error you can use several ways:
          1) You can use primitive boolean type with inverted logic (shouldBeVisible -> shouldBeHidden)



          <variable name="shouldBeHidden" type="boolean" />

          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeHidden ? View.GONE : View.VISIBLE}" />


          2) Second way is to use boxed Boolean type (as you have now) and set default value in expressions



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{(shouldBeVisible ?? true) ? View.VISIBLE : View.GONE}" />


          3) Third way - manually set it after inflating of binding (as you already do now)



          binding.shouldBeVisible=true


          4) Use default keyword in databining value



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE, default=View.GONE}" />


          Choose the one best fits your needs.






          share|improve this answer























          • 3rd way did the trick
            – svkaka
            Nov 28 '18 at 11:58










          • Try to use 4-th approach
            – ConstOrVar
            Nov 28 '18 at 13:18














          1












          1








          1






          To solve that error you can use several ways:
          1) You can use primitive boolean type with inverted logic (shouldBeVisible -> shouldBeHidden)



          <variable name="shouldBeHidden" type="boolean" />

          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeHidden ? View.GONE : View.VISIBLE}" />


          2) Second way is to use boxed Boolean type (as you have now) and set default value in expressions



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{(shouldBeVisible ?? true) ? View.VISIBLE : View.GONE}" />


          3) Third way - manually set it after inflating of binding (as you already do now)



          binding.shouldBeVisible=true


          4) Use default keyword in databining value



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE, default=View.GONE}" />


          Choose the one best fits your needs.






          share|improve this answer














          To solve that error you can use several ways:
          1) You can use primitive boolean type with inverted logic (shouldBeVisible -> shouldBeHidden)



          <variable name="shouldBeHidden" type="boolean" />

          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeHidden ? View.GONE : View.VISIBLE}" />


          2) Second way is to use boxed Boolean type (as you have now) and set default value in expressions



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{(shouldBeVisible ?? true) ? View.VISIBLE : View.GONE}" />


          3) Third way - manually set it after inflating of binding (as you already do now)



          binding.shouldBeVisible=true


          4) Use default keyword in databining value



          <LinearLayout
          android:layout_width="40dp"
          android:layout_height="30dp"
          android:background="#dd0"
          android:visibility="@{shouldBeVisible ? View.VISIBLE : View.GONE, default=View.GONE}" />


          Choose the one best fits your needs.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 13:18

























          answered Nov 13 '18 at 11:14









          ConstOrVarConstOrVar

          1,034149




          1,034149












          • 3rd way did the trick
            – svkaka
            Nov 28 '18 at 11:58










          • Try to use 4-th approach
            – ConstOrVar
            Nov 28 '18 at 13:18


















          • 3rd way did the trick
            – svkaka
            Nov 28 '18 at 11:58










          • Try to use 4-th approach
            – ConstOrVar
            Nov 28 '18 at 13:18
















          3rd way did the trick
          – svkaka
          Nov 28 '18 at 11:58




          3rd way did the trick
          – svkaka
          Nov 28 '18 at 11:58












          Try to use 4-th approach
          – ConstOrVar
          Nov 28 '18 at 13:18




          Try to use 4-th approach
          – ConstOrVar
          Nov 28 '18 at 13:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270107%2fandroid-databinding-default-variable-value%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma