0

I have a Class that used in it below code

this.guageBack= (AbsoluteLayout) findViewById(R.id.gaugeFrame);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);
gaugeNeedle.setLayoutParams(params);

AbsoluteLayout deprecated so When I want to use RelativeLayout I have not replcement for

AbsoluteLayout.LayoutParams(this.needleWidth,this.needleHeight,this.needleX,this.needleY);

Because it accept two args like below

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);

What should I do?

user6022289
  • 65
  • 1
  • 7
  • check this http://stackoverflow.com/questions/8477164/position-view-in-relativelayout-runtime and read relative layout documentation. it is meant when you want to place one view relative to another – Raghunandan Apr 16 '16 at 06:18

3 Answers3

1

Just add this code instead of yours

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
        params.topMargin = needleY;
        params.leftMargin = needleX;
Ali
  • 508
  • 1
  • 5
  • 16
0

Do it like this

public class CodeLayout extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Creating a new RelativeLayout
        RelativeLayout relativeLayout = new RelativeLayout(this);

        // Defining the RelativeLayout layout parameters.
        // In this case I want to fill its parent
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);

        // Creating a new TextView
        TextView tv = new TextView(this);
        tv.setText("Test");

        // Defining the layout parameters of the TextView
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);

        // Setting the parameters on the TextView
        tv.setLayoutParams(lp);

        // Adding the TextView to the RelativeLayout as a child
        relativeLayout.addView(tv);

        // Setting the RelativeLayout as our content view
        setContentView(relativeLayout, rlp);
    }
} 
user1919
  • 309
  • 5
  • 15
0

try this code,

AbsoluteLayout absoluteLayout = //get absolute layout

Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;

absoluteLayout.addView(button, params);
bhadresh
  • 445
  • 5
  • 15