Asked  6 Months ago    Answers:  5   Viewed   54 times

How can I have one layout for landscape and one for portrait? I want to assume extra width and conserve vertical space when the user rotates the phone over sideways.

 Answers

32

By default, the layouts in /res/layout are applied to both portrait and landscape.

If you have for example

/res/layout/main.xml

you can add a new folder /res/layout-land, copy main.xml into it and make the needed adjustments.

orientation

See also http://www.androidpeople.com/android-portrait-amp-landscape-differeent-layouts and http://www.devx.com/wireless/Article/40792/1954 for some more options.

Tuesday, June 1, 2021
 
JackTheKnife
answered 6 Months ago
89

The inflate exception is not actually the problem but really comes from another deeper issue in your layout that is then wrapped in an InflateException. A common issue is an out of memory exception when trying to inflate an ImageView loading a drawable resource. If one of these resources has a high pixel resolution it would take a lot of memory causing then an inflate exception.

So basically verify that the pixel resolution in all your image drawables is just the minimum necessary for your layout.

Tuesday, June 1, 2021
 
Juriy
answered 6 Months ago
48

Don't worry . . .

I got the same problem before .Use my way to get drawable

 Drawable image = c.getResources().getDrawable(R.drawable.name_of_image_in_drawableFolder);

example :

Drawable image = c.getResources().getDrawable(R.drawable.ic_launcher);

This will definetely solove your problem.

But, If you dont have images in drawable folder There might be two posibilities

- Getting Drawable from Phone(SDCARD)
- Getting Drawable from url

In First Case

imageCity.setImageBitmap(BitmapFactory.decodeFile(Environment.getExternalStorage‌​Directory()+"image.png"));

In Second case

imageCity.setImageDrawable(drawableFromUrl("url")); 
    public Drawable drawableFromUrl(String url) throws IOException {
        Bitmap x;
        HttpURLConnection connection = (HttpURLConnection) new URL(url)
                .openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        x = BitmapFactory.decodeStream(input);
        return new BitmapDrawable(x);
    }

Reference :Android Drawable Images from URL

If any problem persists you can contact me megaganpreet0@gmail.com .

Monday, August 16, 2021
 
Chris Herrera
answered 4 Months ago
93

It's possible now with the Data Binding Library.

First, you have to define a variable for the BuildConfig object:

<data>
  <import type="android.view.View" />

  <variable
    name="buildConfig"
    type="your.app.domain.BuildConfig"/>
</data>

Then just use it like this:

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:visibility="@{buildConfig.DEBUG ? View.VISIBLE : View.GONE}">
Saturday, October 16, 2021
 
MM1
answered 2 Months ago
MM1
72

You can use a SuperClass for activities and implement this https://stackoverflow.com/a/9629127/710162 on, for example, onCreate().

Wednesday, November 3, 2021
 
Cornstalks
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share