WildBill
WildBill

Reputation: 9291

Getting RelativeLayout to work correctly at runtime

I'm trying to use a RelativeLayout to produce two rows of items, one row of three buttons up top with a scrollview below them. I want the scrollView to occupy as much of the space as possible.

I believe a RelativeLayout is the best approach here. I'm trying to do this at runtime and not via an xml layout file. My code is as follows:

            RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT); 


            exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  

            RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT); 
            RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT); 
            RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
            scrollparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM): 



            // Exit button
            Button exitbutton = new Button(this);
            exitbutton.setText("Exit");
            exitbutton.setGravity(Gravity.CENTER);
            exitbutton.setWidth(200);
            exitbutton.setLayoutParams(exitparams);


            // Zoom In button
            Button zoomOutButton = new Button(this);
            zoomOutButton.setText("Zoom Out");
            zoomOutButton.setGravity(Gravity.CENTER);
            zoomOutButton.setWidth(200);
            zoomOutButton.setLayoutParams(zimparams);


            // Zoom In button
            Button zoomInButton = new Button(this);
            zoomInButton.setText("Zoom In");
            zoomInButton.setGravity(Gravity.CENTER);
            zoomInButton.setWidth(200);
            zoutparams.addRule(RelativeLayout.LEFT_OF, zoomInButton.getId());  

            zoomInButton.setLayoutParams(zoutparams);

            RelativeLayout layout1 = new RelativeLayout(this);  
            layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));  
            layout1.addView(exitbutton);  
            layout1.addView(zoomInButton);  
            layout1.addView(zoomOutButton);  





            exitbutton.setOnClickListener (new View.OnClickListener() {
                public void onClick(View v) {
                   finish();
                }
            });

            zoomInButton.setOnClickListener (new View.OnClickListener() {
                public void onClick(View v) {
                    increaseZoom();
                    onResume();
                   }
            });

            zoomOutButton.setOnClickListener (new View.OnClickListener() {
                public void onClick(View v) {
                   reduceZoom();
                   onResume();
                   }
            });

            drawView = new DrawView(this, height, width, SMD, zoomLevel);
            drawView.setBackgroundColor(Color.WHITE);
            ScrollView scrollView = new ScrollView(this);
            scrollView.addView(drawView);
            scrollView.setLayoutParams(scrollparams);

            layout1.addView(scrollView);
            setContentView(layout1);

Yet the only thing that shows up is the scrollView. I'm guessing I'm missing something small here... I read the documentation and I believe I did this correctly but the fact that it is not working suggests I either did something wrong or cannot accomplish what I want to do.

A visual of how I want this to look is as follows:

layout

Upvotes: 0

Views: 512

Answers (1)

nedaRM
nedaRM

Reputation: 1827

You need to call setContentView(layout1) at the end to actually add the layout.

EDIT:

Okay so Iv'e fixed you code up and it's working on my phone. However I couldn't' work with DrawView obviously so if there is something wrong with that I can't help you. Give this a go.

     RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT); 


     exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  

     RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT); 
     RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT); 
     RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT);

     zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 




     // Exit button
     Button exitbutton = new Button(this);
     exitbutton.setText("Exit");
     exitbutton.setGravity(Gravity.CENTER);
     exitbutton.setWidth(200);
     exitbutton.setLayoutParams(exitparams);
     exitbutton.setId(2);


     // Zoom In button
     Button zoomOutButton = new Button(this);
     zoomOutButton.setText("Zoom Out");
     zoomOutButton.setGravity(Gravity.CENTER);
     zoomOutButton.setWidth(200);
     zoomOutButton.setLayoutParams(zimparams);
     zoomOutButton.setId(1);

     // Zoom In button
     Button zoomInButton = new Button(this);
     zoomInButton.setText("Zoom In");
     zoomInButton.setGravity(Gravity.CENTER);
     zoomInButton.setWidth(200);
     zoutparams.addRule(RelativeLayout.LEFT_OF, 1);  
     zoutparams.addRule(RelativeLayout.RIGHT_OF, 2);  

     zoomInButton.setLayoutParams(zoutparams);

     RelativeLayout layout1 = new RelativeLayout(this);  
     layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));  
     layout1.addView(exitbutton);  
     layout1.addView(zoomInButton);  
     layout1.addView(zoomOutButton);  





     exitbutton.setOnClickListener (new View.OnClickListener() {
         public void onClick(View v) {
            finish();
         }
     });

     zoomInButton.setOnClickListener (new View.OnClickListener() {
         public void onClick(View v) {
             //increaseZoom();
             onResume();
            }
     });

     zoomOutButton.setOnClickListener (new View.OnClickListener() {
         public void onClick(View v) {
            //reduceZoom();
            onResume();
            }
     });

     //drawView = new DrawView(this, height, width, SMD, zoomLevel);
    // drawView.setBackgroundColor(Color.WHITE);
     ScrollView scrollView = new ScrollView(this);
     //scrollView.addView(drawView);
     scrollparams.addRule(RelativeLayout.BELOW,1);
     scrollView.setLayoutParams(scrollparams);

     layout1.addView(scrollView);
     setContentView(layout1);

Upvotes: 1

Related Questions