user6022289
user6022289

Reputation: 65

How to replcae AbsoluteLayout with RelativeLayout

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?

Upvotes: 0

Views: 184

Answers (3)

Ali
Ali

Reputation: 508

Just add this code instead of yours

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(needleWidth, needleHeight);
        params.topMargin = needleY;
        params.leftMargin = needleX;

Upvotes: 1

bhadresh
bhadresh

Reputation: 443

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);

Upvotes: 0

user1919
user1919

Reputation: 309

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);
    }
} 

Upvotes: 0

Related Questions