Search code examples
javaandroidandroid-relativelayout

How to align views in a relative layout to far left, center, and far right programmatically?


I have a relative layout that has two buttons and one textview. What i'm trying to have happen is having one button on the far left, the textview in the center, and the other button on the far right. Trying to do this without XML.

Here's my code:

        RelativeLayout fm = new RelativeLayout(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        fm.setLayoutParams(lp);
        fm.setBackgroundColor(Color.CYAN);

        Button done = new Button(this);
        done.setId(10);
        done.setText("Done");

        Button save = new Button(this);
        save.setId(12);
        save.setText("Save");

        TextView formManager = new TextView(this);
        formManager.setId(11);
        formManager.setText("Form Manager");

        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        fm.addView(formManager, lp);

        lp.removeRule(RelativeLayout.CENTER_IN_PARENT);

        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        fm.addView(done, lp);

        lp.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);

        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        fm.addView(save, lp);

        lp.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        mainLayout.addView(fm);

Problem is...is that the Save button stretches and takes up the whole layout along with being very thin. Basically with this code nothing is happening like I thought. Any ideas on how to achieve this goal?


Solution

  • try this way

      LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
        RelativeLayout fm = new RelativeLayout(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        fm.setLayoutParams(lp);
        fm.setBackgroundColor(Color.CYAN);
    
        Button done = new Button(this);
        done.setId(10);
        done.setText("Done");
    
        Button save = new Button(this);
        save.setId(12);
        save.setText("Save");
    
        TextView formManager = new TextView(this);
        formManager.setId(11);
        formManager.setText("Form Manager");
    
        RelativeLayout.LayoutParams lpp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        formManager.setLayoutParams(lpp);
        lpp.addRule(RelativeLayout.CENTER_IN_PARENT);
        fm.addView(formManager, lpp);
    
        RelativeLayout.LayoutParams doneLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        done.setLayoutParams(doneLayoutParams);
        doneLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        fm.addView(done, doneLayoutParams);
    
        RelativeLayout.LayoutParams saveLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        save.setLayoutParams(saveLayoutParams);
        saveLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        fm.addView(save, saveLayoutParams);
    
    
        mainLayout.addView(fm);