1

I am trying to create a layout programmatically where there is one ImageView with two buttons. Now the ImageView will be in the center, and the buttons will be at the bottom of the layout. But I am not able to do it. Here is my code

public class StackView extends RelativeLayout {

    ....................

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM|Gravity.LEFT);
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM|Gravity.RIGHT);


    addView(imageview, 0, params);
    Button button=new Button(context);
    button.setText("left");
    button.setGravity(Gravity.START);
    addView(button, 1, params1);
    Button button1=new Button(context);
    button1.setText("right");
    button.setGravity(Gravity.END);
    addView(button1, 2,params2);
}
3
  • 3
    and why dont you want to inflate it from xml? Commented Jul 15, 2015 at 11:00
  • It's just a basic research for me,no other issue.@pskink Commented Jul 15, 2015 at 11:08
  • Have you tried changing LayoutParams ? Commented Jul 15, 2015 at 11:43

2 Answers 2

1

I got the answer.Here how I do it.

            Button button=new Button(context);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            params1.addRule(BELOW,card.getId());
            RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
           // params2.addRule(BELOW,card.getId());
            params2.addRule(ALIGN_PARENT_RIGHT,button.getId());

            addView(imageview, 0, params);

            button.setText("left");
            button.setGravity(Gravity.START);
            addView(button, 1, params1);
            Button button1=new Button(context);
            button1.setText("right");
            //button.setGravity(Gravity.END);
            addView(button1, 2,params2);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

activity_main.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.demo.MainActivity"
    android:id="@+id/layout" >

</RelativeLayout>
public class MainActivity extends Activity {

    private RelativeLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layout = (RelativeLayout) findViewById(R.id.layout);

        //for imageview
        RelativeLayout.LayoutParams paramBtn = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT);        
         paramBtn.addRule(RelativeLayout.CENTER_IN_PARENT);

        ImageView imgView = new ImageView(getApplicationContext());
        imgView.setId(1);
        imgView.setBackgroundResource(R.drawable.ic_launcher);
        imgView.setLayoutParams(paramBtn);
        layout.addView(imgView);

        RelativeLayout.LayoutParams param1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        param1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        Button btn1 = new Button(getApplicationContext());
        btn1.setId(2);
        btn1.setText("Submit");
        btn1.setLayoutParams(param1);
        layout.addView(btn1);


        RelativeLayout.LayoutParams param2 = new    
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
        param2.addRule(RelativeLayout.RIGHT_OF,btn1.getId());
        param2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        Button btn2 = new Button(getApplicationContext());
        btn2.setId(3);
        btn2.setText("Submit-2");
        btn2.setLayoutParams(param2);
        layout.addView(btn2);

      }
  }

1 Comment

Thanks for your answer,but according to my research I don't want to use any xml file.Anyway I have got my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.