<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="#string/game_title">
I've a preference screen for my app for which I've added the title. When the app gets opened in split screen and resized to different size in window, the title is disappearing when the width present is smaller.
Tried adding,
android:layout_height="wrap_content"
android:layout_width="wrap_content"
property to the PreferenceScreen but it didn't help. Some help would be appreciated.
Like in this answer, you can create a custom layout for the PreferenceScreen and apply it with android:layout attribute
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout="#layout/preference_title"
android:title="#string/game_title">
preference_title.xml layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/preference_title"/>
And create different versions of res/values/dimen.xml file to have variable value of the title text size (#dimen/preference_title) that are proportional to possible screen widths .. typically you'll have different versions values directory, for instance for a screen width of w600dp, you'll have values-w600dp directory.
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
So, I want to create a layout with listview and Linearlayout clearly specified with the image.
The padding i.e. required from all the edges of the device is important for me and this should work in a responsive manner for different devices it should be padded from various sides.
Could I Use Nine patch in this case , if yes how?
How could I achieve this , any skeleton layout will solve my problem.
To get that spacing you should use margin, not padding.
Here's a solution:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/mylistview"
android:layout_width="match_parent"
android:layout_height="#dimen/listview_height"
android:layout_margin="#dimen/standard_margin"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/mylistview"
android:layout_margin="#dimen/standard_margin>
</LinearLayout>
</RelativeLayout>
This sets a fixed value for the ListView's height, and makes the LinearLayout automatically resize to fit the remaining space (because of layout_below).
Then create a dimensions file in res/values:
E.g.
dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="listview_height">200dp</dimen>
<dimen name="standard_margin">30dp</dimen>
</resource>
To support different screen sizes you can have different dimensions in other folders such as /res/values-large, /res/values-xlarge etc.
I have two android devices for testing. One is with resolution 480x320 and other is with 800x480. I define different layouts in layout-normal and layout directories. I also tried it with layout-hdpi, layout-mdpi etc different combinations.
Is there a way to know from the log of somewhere that in which layout category a device falls just for debugging purposes. I would like to know layout file from which directory is used at runtime. If not then could someone tell me the right combination of layout directories for two devices with the pre-mentioned resolution.
Thanks in advance.
To find which layout (from layout-ldpi, layout-mdpi folder etc...) is used during runtime. You can use the tag attribute on your layout. For example let's say you have defined two layouts for different screens, the one in layout-mdpi folder and the other in layout-hdpi folder. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<!--Layout defined in layout-mdi folder-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="mdpi"
android:orientation="horizontal" >
<!-- View and layouts definition-->
<!LinearLayout>
And:
<?xml version="1.0" encoding="utf-8"?>
<!--Corresponding Layout defined in layout-hdi folder-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="hdpi"
android:orientation="horizontal" >
<!-- View and layouts definition-->
<!LinearLayout>
To check which layout is used during runtime, you can use something like this:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.MainLayout);
if(linearLayout.getTag() != null) {
String screen_density = (String) linearLayout.getTag();
}
if(screen_density.equalsIgnoreCase("mdpi") {
//layout in layout-mdpi folder is used
} else if(screen_density.equalsIgnoreCase("hdpi") {
//layout in layout-hdpi folder is used
}
Here is an extension of #Angelo's answer that may work depending how you are using your elements: in each file, if you have the same element that you don't need to manipulate, you can give it a different ID for each layout you define (as opposed to tagging it).
For example, say I don't need to manipulate the base linear layout, I only need to manipulate the views inside it.
Here is my hdpi layout:
<?xml version="1.0" encoding="utf-8"?>
<!--Corresponding Layout defined in layout-hdpi folder-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout-hdpi"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- View and layouts definition-->
</LinearLayout>
Heres an mdpi layout:
<?xml version="1.0" encoding="utf-8"?>
<!--Corresponding Layout defined in layout-mdpi folder-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout-mdpi"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- View and layouts definition-->
</LinearLayout>
And here is my code that decides which layout it is:
if ( findViewById(R.id.layout-hdpi) != null ) {
//we are in hdpi layout
} else if ( findViewById(R.id.layout-mdpi) != null ) {
//we are in mdpi layout
}
The idea is that only one of the ids that you define for that item accross your different files will actually exist, and whichever one does is in the layout that actually loaded. The caveat is that if you actually need to manipulate that item later on, this method makes for a lot of extra work and is probably not ideal. You wouldn't want to use this technique on an item such as an EditText because then you have to check which layout you are in to decide which id to use to get that edit text.
How do i position a button in xml. Im using Eclipse with android sdk, i just used margins to put the buttons in positions. But how can i do it through the postion function?
Placing a button on the screen using xml is going to very depending on what you use for your root view group.
There isn't a position function like what you are probably thinking where you would set the X and Y position of the button and for good reason. Because of the hardware fragmentation of android devices it would be difficult to write portable UI if you were relying on hardcoded X and Y coords (regardless of portrait vs landscape).
As an example if i wanted to put a button in the bottom right of the screen and I had a relative layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:layout_alignParentBottom = "true"
android:layout_alignParentRight = "true"/>
</RelativeLayout>
This will put a button on the button right of the screen regardless of screen pixel density/resolution or even landscape VS portrait
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.