3

I am quite new to android programming. As I follow the tutorials here,https://developer.android.com/training/basics/actionbar/overlaying.html.. I have a problem. When I enable the action bar overlay mode,a textView object I create during runtime is covered by the action bar.

   TextView textView =TextView(this);
   textView.setText(message);//message is given from previous activity
   setContentView(textView);

Is there anyway to fix this? I was thinking getting the height of the action bar and set the margin but I couldn't find a way to get the height programmatically

here is the layout

  <LinearLayout
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"
android:layout_paddingTop="?attr/actionBarSize"
tools:context="com.example.galaxy.test.DisplayMessageActivity"
>

and the style.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat" >

        <item name="android:windowActionBarOverlay">true</item>

        <!-- Support library compatibility -->
        <item name="windowActionBarOverlay">true</item>

</style>

Cris
  • 2,002
  • 4
  • 30
  • 51
GalaxyVintage
  • 656
  • 1
  • 11
  • 20

2 Answers2

4

If you want to make sure there aren't Views under your Action Bar, you can add a top margin to it in the XML.

<YourView
    ...
    android:layout_paddingTop="?android:attr/actionBarSize" />

as described here.

Probably you should also insert the attribute android:orientation:"vertical" since you are using a LinearLayout.

Create a TextView in your xml (not programmatically) activity_display_message.xml:

<LinearLayout android:id="@+id/display_message"     
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" 
  android:orientation="vertical"
  android:layout_width="match_parent" android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"   
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="?android:attr/actionBarSize"     
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context="com.example.galaxy.test.DisplayMessageActivity">

<TextView android:id="@+id/message"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/message" 
    android:layout_marginTop="?attr/actionBarSize"/>

</LinearLayout>

in onCreate() method you can retrieve it this way:

setContentView(R.layout.activity_display_message);
TextView message = (TextView) findViewById(R.id.message);
message.setText(yourMessageString);
Cris
  • 2,002
  • 4
  • 30
  • 51
  • For some reason the textView I create during the activity is still under the action bar even with the `android:layout_marginTop=...` – GalaxyVintage May 18 '15 at 23:56
  • Try: `android:paddingTop="?android:attr/actionBarSize">`, are you using the Support Library? Did you create the `TextView` in the xml, not programmatically? If doesn't work post more code – Cris May 19 '15 at 00:04
  • I added the code for layout..Unfortunately neither of them worked...my viewText is still covered by the action bar...I am currently using the Support Library and I created the viewText in the java file... – GalaxyVintage May 19 '15 at 00:21
  • More code please. Are you using the Support Library? How is your file `style. xml`? Are you creating the `TextView` programmatically or statically? – Cris May 19 '15 at 00:22
  • I was basically following the tutorial to set up the style and the textView is created in the activity by using the first block of code inside the onCreate function. – GalaxyVintage May 19 '15 at 00:34
  • Create the `TextView` in your xml file. Then, probably you should also insert the attribute `android:orientation:"vertical"` since you are using a `LinearLayout`. Otherwise the `TextView` might be added horizontally. Let us know what the results are. – Cris May 19 '15 at 10:30
  • 1
    Thanks a lot. I have never thought of that..This should work if the textview is created in the Xml. I also follow the "how to get the actionbar height" and use setpadding . Both work. – GalaxyVintage May 19 '15 at 16:44
1

get the action bar size

How to get the ActionBar height?

create a layout that have to top padding equals to action bar height

and than add your view

Community
  • 1
  • 1