1

For example I'll use Button element, with layout params such margins and alignment. Depend on the run logic this Button can be in 1 of 2 states:

<!--State 1: Right orientation-->
<Button
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp"/>

<!--State 2: Left orientation-->
<Button
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="5dp"/>
  1. Trivial Implementation: Put them both on the view and control via View.Visibility.
  2. Programmatic Implementation:

//change to right orientation - state 1

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)myButton.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);  //instead of remove rule which needs api>17...
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
myButton.setLayoutParams(params);
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams)myButton.getLayoutParams();
marginParams.setMargins(0, 0, 5, 0);  //(left, top, right, bottom)
myButton.requestLayout();

The problem: Too much code, and the setMargins(l,t,r,b) is on pixels and not dp - It's a bug... EDIT: There is a solution for this.

I wonder if there are implementations using dynamic attach of custom layouts to only 1 code object of Button? Is it possible/logic here to use selector here?

EDIT- What I'm looking for is something like:

In activity xml there is a button view whom layout params set on some other xml and non "inline". And the button view has this xml as source for it's layout params. However this button has 2 states of layouts (left/right...) and it depends on some logic (not android: ... e.g. state_pressed, some code flow situation). And when this happens, I want to switch layouts. Better if it will be 1 extra xml with selector.

Thanks,

Community
  • 1
  • 1
michael
  • 3,835
  • 14
  • 53
  • 90
  • 1
    `setMargins` is in pixels not `dp` is not a bug it is designed as it is. But you can convert your values to dp and apply to layout params http://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android – Jibran Khan Oct 09 '15 at 06:33
  • You right, but still I'm looking for more elegant solution as I described, sure something must be... – michael Oct 09 '15 at 06:35
  • well you stated method 2 to be lengthy, whats wrong with 1st method of Trivial Implementation ? – Jibran Khan Oct 09 '15 at 06:38
  • I want to learn how to use selection of custom layouts on the run – michael Oct 09 '15 at 06:39
  • 1
    see here for switching layouts on orientation change http://stackoverflow.com/questions/13848500/replace-layout-on-orientation-change – Jibran Khan Oct 09 '15 at 06:44
  • thanks, i'm looking for more like define button with custom xml layout and use selctor – michael Oct 09 '15 at 06:48

1 Answers1

1

Here is one implementation of custom Button layout using selector

Button btn = new Button(this);
btn.setBackgroundResource(R.layout.btn2);

Here is btn2.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true"  android:drawable="@drawable/btn_pressed" />   
  <item android:drawable="@drawable/btn_normal"/> 

</selector>

I still don't know if I reached your question

EDIT styles.xml

<style name="ButtonStyleRight">
    <item name="android:layout_marginRight">5dp</item>
</style>
<style name="ButtonStyleLeft">
    <item name="android:layout_marginLeft">5dp</item>
</style>

Implementing in xml

<Button
    style="@style/ButtonStyleRight" />

Programmatically you can apply/load styles like mentioned in this thread

Community
  • 1
  • 1
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • I mean: In activity xml there is a button view whom layout params set on some other xml and non "inline". And the button view has this xml as source for it's layout params. However this button has 2 states of layouts (left/right...) and it depends on some logic (not android: ... e.g. state_pressed, some code flow situation). And when When this happens, I want to switch layouts. Better if it will be 1 extra xml with selector. – michael Oct 09 '15 at 07:10