Inflating a view to a constraint layout from out side the constraint layouts class











up vote
1
down vote

favorite
1












I'm trying to inflate a view into a constraint layout. Which isn't a problem from with in the activity. But I also want to be able to call it from another classes method called getViews()... How can I access the constraint layout to inflate the view?



public class chromecastUI_inflater {
Button btn, btn1, fastforward, rewind, play, pause;
TextView title, user, discription, date, streamviews, chromeconnection;
ConstraintLayout constraintLayout1;
Selector selector = new Selector();
Context c;

public chromecastUI_inflater() {


****Edit****

also tried adding the layout via public constructor.
}



    public void getViews(View view) {

play = view.findViewById(R.id.play);
pause = view.findViewById(R.id.pause_button);
fastforward = view.findViewById(R.id.fastforward_button);
rewind = view.findViewById(R.id.rewind_button);
btn1 = view.findViewById(R.id.cast_connection_button);
btn = view.findViewById(R.id.cast_connection_button3);
chromeconnection = view.findViewById(R.id.tvcast);

//here is my problem layout


****Edit****
here i've tried adding



view.findViewById

constraintLayout1 = findViewById(R.id.localplayerview);


title = view.findViewById(R.id.title_text_view);
discription = view.findViewById(R.id.discription_text_view);
date = view.findViewById(R.id.date_text_view);
streamviews = view.findViewById(R.id.views_text_view);
user = view.findViewById(R.id.uploadedby_text_view);

constraintLayout1.addView(view);
}

public void inflateViews() {
LayoutInflater inflater = (LayoutInflater) c.getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout, null);
getViews(view);
}

public void setUI() {

title.setText(selector.DOCTITLE);
discription.setText(selector.ABOUT);
date.setText(selector.DATE);
streamviews.setText(selector.VIEWS);
user.setText(selector.USER);
}

public void resetUI() {

title.setText("");
discription.setText("");
date.setText("");
streamviews.setText("");
user.setText("");
}

public void Chromecast_Connected() {
chromeconnection.setText("Chromecast is connected");
btn.setVisibility(View.VISIBLE);
btn1.setVisibility(View.GONE);
}

public void Chromecast_Disconnected() {
chromeconnection.setText("Chromecast is connected");
btn.setVisibility(View.GONE);
btn1.setVisibility(View.VISIBLE);
}
}


This is where I want to call the method from my local_player activity



chromecastUI_inflater chromecastUiInflater = new chromecastUI_inflater();

@Override
public void onChromecastConnecting() {
Log.d(getClass().getSimpleName(), "onChromecastConnecting");
chromecastUiInflater.inflateViews();
}

@Override
public void onChromecastConnected(ChromecastYouTubePlayerContext chromecastYouTubePlayerContext) {
Log.d(getClass().getSimpleName(), "onChromecastConnected");
chromecastUiInflater.setUI();
chromecastUiInflater.Chromecast_Connected();
initializeCastPlayer(chromecastYouTubePlayerContext);
}


Sorry guys, I'm used to inflating the views from inside an activity. Any help please.










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    I'm trying to inflate a view into a constraint layout. Which isn't a problem from with in the activity. But I also want to be able to call it from another classes method called getViews()... How can I access the constraint layout to inflate the view?



    public class chromecastUI_inflater {
    Button btn, btn1, fastforward, rewind, play, pause;
    TextView title, user, discription, date, streamviews, chromeconnection;
    ConstraintLayout constraintLayout1;
    Selector selector = new Selector();
    Context c;

    public chromecastUI_inflater() {


    ****Edit****

    also tried adding the layout via public constructor.
    }



        public void getViews(View view) {

    play = view.findViewById(R.id.play);
    pause = view.findViewById(R.id.pause_button);
    fastforward = view.findViewById(R.id.fastforward_button);
    rewind = view.findViewById(R.id.rewind_button);
    btn1 = view.findViewById(R.id.cast_connection_button);
    btn = view.findViewById(R.id.cast_connection_button3);
    chromeconnection = view.findViewById(R.id.tvcast);

    //here is my problem layout


    ****Edit****
    here i've tried adding



    view.findViewById

    constraintLayout1 = findViewById(R.id.localplayerview);


    title = view.findViewById(R.id.title_text_view);
    discription = view.findViewById(R.id.discription_text_view);
    date = view.findViewById(R.id.date_text_view);
    streamviews = view.findViewById(R.id.views_text_view);
    user = view.findViewById(R.id.uploadedby_text_view);

    constraintLayout1.addView(view);
    }

    public void inflateViews() {
    LayoutInflater inflater = (LayoutInflater) c.getApplicationContext().getSystemService
    (Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.layout, null);
    getViews(view);
    }

    public void setUI() {

    title.setText(selector.DOCTITLE);
    discription.setText(selector.ABOUT);
    date.setText(selector.DATE);
    streamviews.setText(selector.VIEWS);
    user.setText(selector.USER);
    }

    public void resetUI() {

    title.setText("");
    discription.setText("");
    date.setText("");
    streamviews.setText("");
    user.setText("");
    }

    public void Chromecast_Connected() {
    chromeconnection.setText("Chromecast is connected");
    btn.setVisibility(View.VISIBLE);
    btn1.setVisibility(View.GONE);
    }

    public void Chromecast_Disconnected() {
    chromeconnection.setText("Chromecast is connected");
    btn.setVisibility(View.GONE);
    btn1.setVisibility(View.VISIBLE);
    }
    }


    This is where I want to call the method from my local_player activity



    chromecastUI_inflater chromecastUiInflater = new chromecastUI_inflater();

    @Override
    public void onChromecastConnecting() {
    Log.d(getClass().getSimpleName(), "onChromecastConnecting");
    chromecastUiInflater.inflateViews();
    }

    @Override
    public void onChromecastConnected(ChromecastYouTubePlayerContext chromecastYouTubePlayerContext) {
    Log.d(getClass().getSimpleName(), "onChromecastConnected");
    chromecastUiInflater.setUI();
    chromecastUiInflater.Chromecast_Connected();
    initializeCastPlayer(chromecastYouTubePlayerContext);
    }


    Sorry guys, I'm used to inflating the views from inside an activity. Any help please.










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I'm trying to inflate a view into a constraint layout. Which isn't a problem from with in the activity. But I also want to be able to call it from another classes method called getViews()... How can I access the constraint layout to inflate the view?



      public class chromecastUI_inflater {
      Button btn, btn1, fastforward, rewind, play, pause;
      TextView title, user, discription, date, streamviews, chromeconnection;
      ConstraintLayout constraintLayout1;
      Selector selector = new Selector();
      Context c;

      public chromecastUI_inflater() {


      ****Edit****

      also tried adding the layout via public constructor.
      }



          public void getViews(View view) {

      play = view.findViewById(R.id.play);
      pause = view.findViewById(R.id.pause_button);
      fastforward = view.findViewById(R.id.fastforward_button);
      rewind = view.findViewById(R.id.rewind_button);
      btn1 = view.findViewById(R.id.cast_connection_button);
      btn = view.findViewById(R.id.cast_connection_button3);
      chromeconnection = view.findViewById(R.id.tvcast);

      //here is my problem layout


      ****Edit****
      here i've tried adding



      view.findViewById

      constraintLayout1 = findViewById(R.id.localplayerview);


      title = view.findViewById(R.id.title_text_view);
      discription = view.findViewById(R.id.discription_text_view);
      date = view.findViewById(R.id.date_text_view);
      streamviews = view.findViewById(R.id.views_text_view);
      user = view.findViewById(R.id.uploadedby_text_view);

      constraintLayout1.addView(view);
      }

      public void inflateViews() {
      LayoutInflater inflater = (LayoutInflater) c.getApplicationContext().getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);
      View view = inflater.inflate(R.layout.layout, null);
      getViews(view);
      }

      public void setUI() {

      title.setText(selector.DOCTITLE);
      discription.setText(selector.ABOUT);
      date.setText(selector.DATE);
      streamviews.setText(selector.VIEWS);
      user.setText(selector.USER);
      }

      public void resetUI() {

      title.setText("");
      discription.setText("");
      date.setText("");
      streamviews.setText("");
      user.setText("");
      }

      public void Chromecast_Connected() {
      chromeconnection.setText("Chromecast is connected");
      btn.setVisibility(View.VISIBLE);
      btn1.setVisibility(View.GONE);
      }

      public void Chromecast_Disconnected() {
      chromeconnection.setText("Chromecast is connected");
      btn.setVisibility(View.GONE);
      btn1.setVisibility(View.VISIBLE);
      }
      }


      This is where I want to call the method from my local_player activity



      chromecastUI_inflater chromecastUiInflater = new chromecastUI_inflater();

      @Override
      public void onChromecastConnecting() {
      Log.d(getClass().getSimpleName(), "onChromecastConnecting");
      chromecastUiInflater.inflateViews();
      }

      @Override
      public void onChromecastConnected(ChromecastYouTubePlayerContext chromecastYouTubePlayerContext) {
      Log.d(getClass().getSimpleName(), "onChromecastConnected");
      chromecastUiInflater.setUI();
      chromecastUiInflater.Chromecast_Connected();
      initializeCastPlayer(chromecastYouTubePlayerContext);
      }


      Sorry guys, I'm used to inflating the views from inside an activity. Any help please.










      share|improve this question















      I'm trying to inflate a view into a constraint layout. Which isn't a problem from with in the activity. But I also want to be able to call it from another classes method called getViews()... How can I access the constraint layout to inflate the view?



      public class chromecastUI_inflater {
      Button btn, btn1, fastforward, rewind, play, pause;
      TextView title, user, discription, date, streamviews, chromeconnection;
      ConstraintLayout constraintLayout1;
      Selector selector = new Selector();
      Context c;

      public chromecastUI_inflater() {


      ****Edit****

      also tried adding the layout via public constructor.
      }



          public void getViews(View view) {

      play = view.findViewById(R.id.play);
      pause = view.findViewById(R.id.pause_button);
      fastforward = view.findViewById(R.id.fastforward_button);
      rewind = view.findViewById(R.id.rewind_button);
      btn1 = view.findViewById(R.id.cast_connection_button);
      btn = view.findViewById(R.id.cast_connection_button3);
      chromeconnection = view.findViewById(R.id.tvcast);

      //here is my problem layout


      ****Edit****
      here i've tried adding



      view.findViewById

      constraintLayout1 = findViewById(R.id.localplayerview);


      title = view.findViewById(R.id.title_text_view);
      discription = view.findViewById(R.id.discription_text_view);
      date = view.findViewById(R.id.date_text_view);
      streamviews = view.findViewById(R.id.views_text_view);
      user = view.findViewById(R.id.uploadedby_text_view);

      constraintLayout1.addView(view);
      }

      public void inflateViews() {
      LayoutInflater inflater = (LayoutInflater) c.getApplicationContext().getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);
      View view = inflater.inflate(R.layout.layout, null);
      getViews(view);
      }

      public void setUI() {

      title.setText(selector.DOCTITLE);
      discription.setText(selector.ABOUT);
      date.setText(selector.DATE);
      streamviews.setText(selector.VIEWS);
      user.setText(selector.USER);
      }

      public void resetUI() {

      title.setText("");
      discription.setText("");
      date.setText("");
      streamviews.setText("");
      user.setText("");
      }

      public void Chromecast_Connected() {
      chromeconnection.setText("Chromecast is connected");
      btn.setVisibility(View.VISIBLE);
      btn1.setVisibility(View.GONE);
      }

      public void Chromecast_Disconnected() {
      chromeconnection.setText("Chromecast is connected");
      btn.setVisibility(View.GONE);
      btn1.setVisibility(View.VISIBLE);
      }
      }


      This is where I want to call the method from my local_player activity



      chromecastUI_inflater chromecastUiInflater = new chromecastUI_inflater();

      @Override
      public void onChromecastConnecting() {
      Log.d(getClass().getSimpleName(), "onChromecastConnecting");
      chromecastUiInflater.inflateViews();
      }

      @Override
      public void onChromecastConnected(ChromecastYouTubePlayerContext chromecastYouTubePlayerContext) {
      Log.d(getClass().getSimpleName(), "onChromecastConnected");
      chromecastUiInflater.setUI();
      chromecastUiInflater.Chromecast_Connected();
      initializeCastPlayer(chromecastYouTubePlayerContext);
      }


      Sorry guys, I'm used to inflating the views from inside an activity. Any help please.







      java android view layout-inflater






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 12:26

























      asked Nov 8 at 11:36









      markharrop

      297113




      297113
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          so here is how i fixed it. i had to make a view called root view in my inflate view method. then from my activity call the rootview as constraint layout.



           public void inflateViews(View rootview,Context c){
          constraintLayout1 = rootview.findViewById(R.id.localplayerview);
          LayoutInflater inflater = (LayoutInflater)c.getSystemService
          (Context.LAYOUT_INFLATER_SERVICE);
          view = inflater.inflate(R.layout.layout,null);
          getViews(view);

          }


          then i had to make a getStrings method to fill my textviews from the inflated view



          public void getStrings_fromLocalPlayer(String title,String synopsis, String user, String views, String date){
          doctitle = title;
          docsynopsis = synopsis;
          docuser = user;
          docviews = views;
          docdate = date;
          }





          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%2f53206961%2finflating-a-view-to-a-constraint-layout-from-out-side-the-constraint-layouts-cla%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
            1
            down vote













            so here is how i fixed it. i had to make a view called root view in my inflate view method. then from my activity call the rootview as constraint layout.



             public void inflateViews(View rootview,Context c){
            constraintLayout1 = rootview.findViewById(R.id.localplayerview);
            LayoutInflater inflater = (LayoutInflater)c.getSystemService
            (Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.layout,null);
            getViews(view);

            }


            then i had to make a getStrings method to fill my textviews from the inflated view



            public void getStrings_fromLocalPlayer(String title,String synopsis, String user, String views, String date){
            doctitle = title;
            docsynopsis = synopsis;
            docuser = user;
            docviews = views;
            docdate = date;
            }





            share|improve this answer

























              up vote
              1
              down vote













              so here is how i fixed it. i had to make a view called root view in my inflate view method. then from my activity call the rootview as constraint layout.



               public void inflateViews(View rootview,Context c){
              constraintLayout1 = rootview.findViewById(R.id.localplayerview);
              LayoutInflater inflater = (LayoutInflater)c.getSystemService
              (Context.LAYOUT_INFLATER_SERVICE);
              view = inflater.inflate(R.layout.layout,null);
              getViews(view);

              }


              then i had to make a getStrings method to fill my textviews from the inflated view



              public void getStrings_fromLocalPlayer(String title,String synopsis, String user, String views, String date){
              doctitle = title;
              docsynopsis = synopsis;
              docuser = user;
              docviews = views;
              docdate = date;
              }





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                so here is how i fixed it. i had to make a view called root view in my inflate view method. then from my activity call the rootview as constraint layout.



                 public void inflateViews(View rootview,Context c){
                constraintLayout1 = rootview.findViewById(R.id.localplayerview);
                LayoutInflater inflater = (LayoutInflater)c.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.layout,null);
                getViews(view);

                }


                then i had to make a getStrings method to fill my textviews from the inflated view



                public void getStrings_fromLocalPlayer(String title,String synopsis, String user, String views, String date){
                doctitle = title;
                docsynopsis = synopsis;
                docuser = user;
                docviews = views;
                docdate = date;
                }





                share|improve this answer












                so here is how i fixed it. i had to make a view called root view in my inflate view method. then from my activity call the rootview as constraint layout.



                 public void inflateViews(View rootview,Context c){
                constraintLayout1 = rootview.findViewById(R.id.localplayerview);
                LayoutInflater inflater = (LayoutInflater)c.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.layout,null);
                getViews(view);

                }


                then i had to make a getStrings method to fill my textviews from the inflated view



                public void getStrings_fromLocalPlayer(String title,String synopsis, String user, String views, String date){
                doctitle = title;
                docsynopsis = synopsis;
                docuser = user;
                docviews = views;
                docdate = date;
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 16:58









                markharrop

                297113




                297113






























                    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%2f53206961%2finflating-a-view-to-a-constraint-layout-from-out-side-the-constraint-layouts-cla%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







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()