I am trying to dynamically add buttons to table rows, but need to alter the style of the buttons based on screen orientation. I have the following "game_answer_button.xml" layout file in both "layout" and "layout-land" folders.
res/layout:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
res/layout-land:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/Widget.Button.Small"
/>
The button layout is being inflated in my activity as follows:
LayoutInflater inflater = getLayoutInflater();
Button button = (Button)inflater.inflate(R.layout.game_answer_button, null);
button.setText(image.getDescription());
However, it seems the default (portrait) layout is selected every time. If the contents of the default layout are replaced with the landscape layout, then the landscape layout is finally rendered. It doesn't seem to be correctly selecting the landscape layout resource via LayoutInflater.
Any ideas on this?
did you check if the configuration of the activity, where the button is used, in the manifest is set to portrait only?
I found the problem. I was loading the dynamic buttons to an ArrayList and merely adding the buttons to my parent view on each configuration change--meaning the button style would have already been inflated when pulling the button objects from "cache". Just another Dev "doh!" moment.
Related
When I change my orientationon landscape my photo will be only half ... I want when I change my orientation to be like on facebook , to show full picture .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
You have to create another layout for horizontal layout. if you want your portrait view to be different from your landscape view. you must create another XML file for landscape.
The two XML file will be handled by the same JAVA code. although you can programmatically listen to orientation change but would prefer the different layout for Different Orientation.
create two types of layout directories to handle orientation . layout-land layout-port put the xml with the same name in both the directory. example if you have main.xml file then add have to put it in both directory.
Documentation on supporting multiple screen
Is it possible to declare a layout with a custom view layout without a wrapping root element? I've got my custom view working but it always matches the parent if I inflate the following layout.xml:
custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<com.company.ui.CustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="#dimen/custom_view_height"
android:layout_width="#dimen/custom_view_width" />
dimons.xml
<resources>
<dimen name="custom_view_height">300dp</dimen>
<dimen name="custom_view_width">400dp</dimen>
</resources>
But when the custom view loads and attaches, I see that its calculated size (from onMeasure) is the size of the device's screen.
Do I really need to declare the layout.xml as:
custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.company.ui.CustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="#dimen/custom_view_height"
android:layout_width="#dimen/custom_view_width" />
</LinearLayout>
It seems a little pointless as I actually don't care for the LinearLayout nor do I want to remember what I cast into the inflated R.layout.custom_view.
Wow, this is very interesting! Thanks to the following article:
http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
Basically, if you inflate a layout without parent specified, all layout_xxx attributes specified within the root of the layout xml are discarded. You should inflate your layouts as
inflater.inflate(R.layout.your_layout, parent, false);
Also, note that if you pass true (attach to root), the returned element will be the parent element provided. Pay attention to this as it may cause problems when you're expecting the inflated layout to be returned!
I have two layouts defined mainactivity.xml which is found in the layout folder, and another mainactivity.xml which is found in the layout-port folder. As you can probably tell one layout is for Landscape orientation and the other is for Portrait.
The issue I'm having is that in the Landscape layout I have a ScrollView, and in the Portrait layout it is a HorizontalScrollView with the same id. How do I change (in code) the ScrollView into a HorizontalScrollView when the orientation of the device changes?
You could create a bools.xml file in res/values-land (and also one in res/values-port in which is_landscape should be set to false)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">true</bool>
</resources>
Then in your code you can check
if (getResources().getBool(R.bool.is_landscape)) {
//setup horizontal scrollview
} else {
//setup vertical scrollview
}
Although perhaps a better way is to use this library (so you don't have to code for two different ui components).
https://github.com/lucasr/twoway-view
I'm using this in a current project and it works beautifully.
In your scrollview define this property
android:orientation="horizontal" for landscape
android:orientation="vertical" for portrait
So generally you define an XML layout with a single root node and all the buttons and such contained inside. But if you want to just define buttons somewhere so you can reference their IDs later on to move them around, where/how do you do that?
I'm asking this question because I'm trying to use the "android:foreground" field in the FrameLayout. So I want to point this to the ID of a button (or other widget) that I've defined. But where do I define this button (or other widget)? If I just defined it normally wouldn't it end up appearing somewhere where I don't want it to?
android:foreground adds a drawable over the layout as an overlay. you put the drawable in /res/drawable
As for the buttons, you can always code them in the java
Just define your button in my_button.xml file in res/layout folder with any attributes:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_margin="2dp"
android:layout_gravity="center"
...
style="#style/MyStyle" />
And then inflate it when needed:
getLayoutInflater().inflate(R.layout.my_button, null);
My current layout displays activity that is not full screen (that's OK).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="100dip"
android:layout_height="200dip" >
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="fill_parent" android:text="#string/hello"></TextView>
I also added android:theme="#android:style/Theme.Translucent" to manifest for my activity.
My 200x100dip activity now shows in the upper left corner. How can i specify position of my linear layout (or my activity)?
You can use either FrameLayout or RelativeLayout as outer most layout for this. Ant then use absolute position in dp or android:layout_centerInParent or similar.
I believe your Activity´s outmost layout element (LinearLayout) will be placed in a FrameLayout that is the parent given from Android. I suggest you let your outmost layout match_parent/fill_parent in layout_height and _width and then center the content inside it with gravity="center" on your outmost layout. By letting the outmost layout being transparent and not catch click element it will appear as layout in the middle where elements behind is visible. If Im correct guessing that's what you want to achieve here.
put that layout in another absolute layout in which you use android:layout_width="fill_parent" and android:layout_height="fill_parent" the other thing you can do is to use this: http://www.droiddraw.org/
you can move your elements around manually with that and it will give you the XML code that is used to do that. I found it very useful in laying out XML in android.