0

I want to fix some buttons and editTexts width in the same width of the portrait in landscape. How to do that This is my xml source code:

<TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="17dp"
            android:layout_marginBottom="17dp"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:background="@drawable/dr_t"
            android:gravity="center|bottom"
            android:padding="15dp"
            android:text="Click here"
            android:textAllCaps="true"
            android:textColor="@color/White"
            android:textSize="14sp" />
Rodrigo
  • 327
  • 6
  • 25

3 Answers3

0

Add id to XML Textviews, Buttons or EditTexts, for examp!e:

android:id="@+id/tvx"

Then in MainActivity add this code to listen for changes in orientation.

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

//Get TV
TextView TVExample = (TextView) findViewById(R.id.tvx);
int TVWidth= 0;


//Set width on orientation change
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

    TVExample.getLayoutParams().width = TVWidth;

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    TVWidth= TVExample.getMeasuredWidth();
}
}

Helpful related SO Post

Lew Perren
  • 1,209
  • 1
  • 10
  • 14
  • Thanks for this answer, can you explain :) – Rodrigo Jul 14 '17 at 09:15
  • public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); listens for orientation change and if statement either copies width if portrait or sets if landscape. – Lew Perren Jul 14 '17 at 09:17
  • @BusinessPalenQuickBuilder so i should override this method ineach activity so that i can dectect device orientation – Rodrigo Jul 14 '17 at 09:26
  • Yes, that wou!d make sense. I think it will work, but perhaps not that elegant. Good thing is when implemented it gives you lots of fine level control as you can add or subtract from thne wdith and also use for other adjustments on orientation change. – Lew Perren Jul 14 '17 at 09:30
  • It's too stressed and inappropriate way as I have many widgets. Thanks @BusinessPlanQuickBuilder for the help – Rodrigo Jul 14 '17 at 09:32
  • You are welcome, you cou!d use reflection to automate I guess. – Lew Perren Jul 14 '17 at 09:33
0

Create folder in res as layout-land or layout-port

layout-port: layout for widget that must change for portrait orientation

layout-land: layout for widget that must change for landscape orientation

For more information please check this

Shreeya Chhatrala
  • 1,441
  • 18
  • 33
0

Another way to achieve this in XML would be to fix the width to the portrait size for different screen sizes using a dimens resources, so layout XML becomes:

<TextView
        android:layout_width="@dimen/TVWidth"

in sub-directories,res/values-large, res/values-normal, es/values-small... dimens.xml file has the TVWidth resource:

<resources>
<dimen name="TVWidth">200dp</dimen>
</resources>

So XML elements have fixed dp width for different screen sizes.

Lew Perren
  • 1,209
  • 1
  • 10
  • 14