I do have a toolbar right now, but I don't really understand a lot of the things that I needed to add and copypaste from tutorials and other questions to get it to work. Also, my toolbar doesn't display an elevation even though I set android:elevation to 9dp.
Similar to the Google I/O app, I've set up a resource in /layout for my toolbar. I copied the name and named it "toolbar_actionbar_with_headerbar". Please excuse that it doesn't make any sense.
This is the content:
toolbar_actionbar_with_headerbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="128dp">
<Toolbar
android:id="#+id/toolbar"
android:elevation="9dp"
android:layout_height="128dp"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
android:gravity="bottom" />
</LinearLayout>
Now, as it's apparently supposed to be done, I am applying my toolbar to activities by including them in their /layout XML-file. Works fine.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
[...]
<include layout="#layout/toolbar_actionbar_with_headerbar" />
[...]
</RelativeLayout>
However, this is my first question. When I did that initially, my toolbar was cut off at the sides like all other content of the activity, according to the margins set at the beginning of the xml. Right now, I simply deleted those, and add margins for every text field and other things on the activity. There probably is a better way?
The second question is: Why doesn't my elevation work? I've even tried setting it through code onCreate, but that doesn't work either. And lastly, what would I have to do to properly support devices pre-Level21?
Thanks a lot.
You should read this post: appcompat-v21
Take care when you copy some code text, there are some ” that are wrong, and you should change to ".
About elevation (you can read it in the post too), it only works with lollipop.
Related
I am developing an app with Android Studio 2.2.3 and I'm facing a problem I did not find before: if I add an Activity from a template such as Basic Activity, the newly created activity will let the device status bar display
If I add instead an activity from the Empty Activity template, no status bar shows up any more.
Reading through the layout files, I see that the Basic activity uses a android.support.design.widget.CoordinatorLayout whereas the Empty Activity uses a RelativeLayout. I cannot find any other differences, neither in the manifest. I programmed some apps with Eclipse some time ago and the status bar was there, even with the RelativeLayout, as far as I remember.
Any suggestion?
Thanks in advance, regards
Edit:
Status bar does not relate with layout. It is about the theme defined in the styles.xml. Can you check which theme is applied your activity?
Thanks steve for your suggestion: the behavior is due to
Yet another question: keeping this no-actionbar theme, why is my textbox not aligned with the top of the screen, like this:
My activity layout is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="it.pgsoftware.firebasepublisher.Main4Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test string"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"/>
</RelativeLayout>
and with the Layout inspector I get the following UI structure:
The LinearLayout at the top has a 72dp top-padding which I can't find digging into the themes: any suggestion?
Thanks in advance, regards
Please check the android:appTheme="#style/AppTheme" option in the manifest file . Check the theme option in res>style.xml file . Copy the stylesheet and paste in comment to see.
I use latest Android Studio and SDK. In preview & real device i see this:
My code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myappname.view.AboutActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewAbout" />
</RelativeLayout>
How i make subtitle text color is gray? Like this:
I'm going out on a limb and assume that you're using the row layout simple_list_item_2.xml (based on the screenshot) which gives you two rows. The problem, if you may call it that, is that depending on the SDK version, the styling for this layout has changed.
On SDK 23, it looks like this:
However, on say SDK 19, it looks like this:
Why?
To understand this we first need to take a look at the xml that generates the rows from simple_list_item_2.xml, you'll see it's a pretty simple layout that uses the now deprecated view TwoLineListItem but that's just a plus on why to use your custom layout.
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd">
<TextView android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#id/text1"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
</TwoLineListItem>
The reason is because of the way the style textAppearanceListItemSecondary is resolved in each SDK version. The style is what gives the text the size, the color, etc. The evolution of the interface in Android has given birth to a huge ecosystem of themes and relying on the default styling will result in inconsistencies like the one you stumbled upon.
What to do about it?
You should use your own layout for this to allow for uniform styling across versions. To do so, please refer to any of the multiple questions covering this matter. But in short it just means creating a layout file, call it for example custom_row.xml and having the layout look exactly as you please. This also gives you total control over placement of the items, extra Views that you may need, and overhead in terms of coding is minimal compared to the SimpleAdapter or ArrayAdapter that perhaps you were using.
Note
You should consider moving your code towards RecyclerView instead of ListView if you haven't already.
You can set Textview property
android:textColor="#color/grey"
in you Adapter layout to change colour of your sub item
Hope this will help
I am developing my first Android app. Although I have 15 years of Java software development experience, Android is new to me. My desired look is a background image with other images on top of it, as well as labels and phone numbers. It seems like the best way to accomplish this is a Linear Layout for the background image, with a nested layout for the other fields on top of that. I have searched online and cannot find any sample code on how to accomplish this. Any recommendations would be greatly appreciated. Thanks in advance.
What are you using to develop? If you are you using Eclipse with the Android SDK this is super easy. Put the desired picture in the appropriate drawable folder, go to the graphical view of layout that corresponds to your activity, on the right side of the screen there is a list of all the properties, find background, then select your picture from drawables. You can also do this from the xml using android:background="#drawable/yourPic". This way you don't have to worry about having things layered on top of it.
I think you should go through followings:
1. http://phandroid.com/2011/05/11/10-tips-for-android-ui-design/
2. http://mobile.tutsplus.com/series/android-user-interface-design/
3. http://coding.smashingmagazine.com/2011/06/30/designing-for-android/
4. http://android-developers.blogspot.in/2011/09/thinking-like-web-designer.html
If I got, something like this can work:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<stuff>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<stuff>
</RelativeLayout>
<stuff>
</RelativeLayout>
I used it in an app, and made the trick.
First of all, RelativeLayout is the best layout to use. It depends on your design.
If your design says to keep all the views either vertically OR horizontally, you can use LinearLayouts in between.
My suggested way:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:orientation="Horizontal">
<View1 />
<View2 />
</LinearLayout>
</LinearLayout android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:orientation="Vertical"
android:layout_below="#id/layout1">
<View3 />
<View4 />
</LinearLayout>
</RelativeLayout>
Which will yield you something like this
I have a layout menu which contains this:
<RelativeLayout android:id="#+id/computersMenu"
android:layout_width="50dp"
android:layout_height="50dp">
<include layout="#layout/component_add_button"
android:id="#+id/imagebutton_add_client"/>
</RelativeLayout>
component_add_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_menu_add"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="center"/>
Looking at the preview (EDIT: I mean the "Graphical Layout" tab of the xml editor in eclipse which uses the same rendering engine as all devices AFAIK) of component_add_button.xml you can see the image. Great. On the page which is including it it isn't displaying (although it does give you a selectable space where the image should be).
This seems like a really simple example that "should just work". The include tag has always seemed really flaky to me - I'm wondering if I'm missing something in terms of how it actually works...?
As your answer states, the eclipse preview does not display included layouts.
The simplest way to avoid doubt or mistakes in markup when "including" layout's is to design in the original layout then right click the view you wish to extract and select Extract include... from the menu
On a device this functionality worked fine. This seems like a bug in the "Graphical Layout" tab of the layout xml editor shipped with android-sdk.
I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#EAEAEA">
<ListView
android:id="#+id/xxx"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#464C59"
android:divider="#A4C539"
android:dividerHeight="1px">
</ListView>
<ImageView
android:id="#+id/home_bottom_bar"
android:src="#drawable/bottombar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:clickable="true"/>
</FrameLayout>
The goal is to have some sort of advertising bar at the bottom of the activity (which contains a list of items). It works ok, except for one thing! There is some sort of extra space just under the bar (it's very small but it's noticeable enough). By the way, all the paddings are set to 0 so where does this space come from?
Thanks!
EDIT
After investigating the issue, it turns out that the custom background (#EAEAEA) is causing this extra space. Still don't know how to fix this though.
When you mention that it is a small extra space, it may be the tiny gradient at the top and bottom. Created by ListView, when it is made scrollable.
You may read about ListView Backgrounds, this should give you the idea on how to fix it, if it is caused by this special gradient.
This gradient line can apparently also be removed: extra line in tab host
You may want to use the merge tag since every activitys base layout is a FrameLayout.
(This may cause the padding. Im not 100% sure on this one though)
Look here.