How do I create a textView below the action bar?
I am new to android programming. As I am following the instructions here, https://developer.android.com/training/basics/actionbar/overlaying.html .. I have a problem. When I turn on the Blend Mode of the Action Bar, the textView object I create at runtime is covered by the Action Bar.
TextView textView =TextView(this);
textView.setText(message);//message is given from previous activity
setContentView(textView);
Is there a way to fix this? I was thinking about getting the height of the action bar and setting the border, 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 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>
source to share
If you want to make sure your action bar has no views, you can add a top marker to it in XML.
<YourView
...
android:layout_paddingTop="?android:attr/actionBarSize" />
as described here .
You should probably also insert an attribute android:orientation:"vertical"
, since you are using LinearLayout
.
Create TextView in 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()
you can restore it like this:
setContentView(R.layout.activity_display_message);
TextView message = (TextView) findViewById(R.id.message);
message.setText(yourMessageString);
source to share