FlaMM3R
FlaMM3R

Reputation: 67

How to retrieve button information in xml

I am new to android and I want to retrieve button information which is in the XML file. (specifically margin_top and margin_left). After that, i want to dynamically give another margins to that specific button in cording. I can create new button with new margin but i cant retrieve current button margins..

Any help would be appreciated.

android.view.ViewGroup.LayoutParams lay = btn1.getLayoutParams();

this lay object hold all the information but i cant get margins from that.

i created my button using following code.

RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeTest);
        Button btn3 = new Button(TestActivity.this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(200, 200, 200, 200);
        layout.addView(btn3, params);

Upvotes: 0

Views: 205

Answers (2)

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Hi FlaMM3R

My layout looks like below and you have to use same idea according to your layout.

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:text="@string/hello"
        android:id="@+id/tv" />
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/btn"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="36dp"
            android:text="Button" />    
        <Button android:id="@+id/btn2"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginLeft="148dp"
            android:layout_marginTop="62dp"
            android:text="Button"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"/>
    </RelativeLayout> 
</LinearLayout>

Java file

public class HelloActivity extends Activity {
    TextView tv;
    Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv =(TextView)findViewById(R.id.tv);
        btn = (Button)findViewById(R.id.btn);        
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) btn.getLayoutParams();       
        tv.setText("left : "+params.leftMargin+"\ntop : "+params.topMargin);
        params.setMargins(10, 50, 0, 0); // setting new margins for btn
    }
}

Use same idea in your code and let me know what happened. I hope it will be the answer you are looking.

Upvotes: 1

Yugandhar Babu
Yugandhar Babu

Reputation: 10349

Give an ID to the button in XML file and use findViewById to get that button as you did for RelativeLayout. And then you can get all information about that button. You can modify the properties of button also. try it.

Upvotes: 1

Related Questions