0

I want to move button up and textview down. Button is moving up but it's comin on textview. Textview is not coming down.

Button b;
TextView t;

public void btnclick(View v){
    b= (Button) v;
    t= (TextView) findViewById(R.id.tv);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) t
            .getLayoutParams();
    params.addRule(RelativeLayout.BELOW, b.getId());
    b.setLayoutParams(params);

}
Darek Kay
  • 15,827
  • 7
  • 64
  • 61
Siddharth Gharge
  • 112
  • 3
  • 13

2 Answers2

0

Seem you are trying to apply the rule on Button b, instead try applying it on TextView t.

TextView t = (TextView) findViewById(R.id.tv);

public void btnclick(View v){
    b = (Button) v;
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) t.getLayoutParams();
    params.addRule(RelativeLayout.BELOW, b.getId());
    t.setLayoutParams(params); // Changed this
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

basically, textview and button same dimension in RelativeLayout.

try this code

activity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mButton;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButton = (Button) findViewById(R.id.btn);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                mTextView = (TextView) findViewById(R.id.textview);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mTextView.getLayoutParams();
                params.addRule(RelativeLayout.BELOW, v.getId());
                mTextView.setLayoutParams(params);
                break;
        }
    }
}

layout:

<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=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"/>

</RelativeLayout>
LEETAEJUN
  • 186
  • 9