Chris Yates
Chris Yates

Reputation: 91

How to include ActionBar in a project compiled with Apache Ant

I'd like to know how to add ActionBar in a project compiled with Apache Ant(i must use it). Using android with Apache Ant always generates a class that extends Activity. However with that i don't get ActionBar. Here is what i tried so far:

  1. I did a bit of search and i found out that i need to extend AppCompatActivity. I imported android.support.v7.app.AppCompatActivity but i always get package android.support.v7.app doest not exist as an error when compiling.
  2. I copied /android/sdk/extras/android/m2repository/com/android/support/appcompat-v7 into libs folder in my project and then did the following command in root of project(c:\users\pc\desktop\temp):

android update project --path . --target 3 --library libs\appcompat-v7

I got the following as output:

`Error: c:\users\pc\desktop\temp\libs\appcompat-v7 is not a valid project (AndroidManifest.xml not found). It seems that there are sub-projects. If you want to update them please use the --subproject parameter.`

What do i need to do in order to include libraries needed to use ActionBar and then be able to compile the project with Apache Ant?

Upvotes: 2

Views: 76

Answers (1)

Chris Yates
Chris Yates

Reputation: 91

The first suggestion to use Theme.Holo did solve my problem.(Should have started with that one!). Here is what i did:

In AndroidManifest.xml:

<application android:theme="@style/AppTheme">

In strings.xml add:

<style name="AppTheme" parent="android:Theme.Holo.Light">

This is enough to get ActionBar. Now here is rest of code to get items on it. menu_main.xml:

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<item
    android:id="@+id/share_ic"
    android:layout_width="wrap_content"
    android:title="share"
    android:showAsAction="always"
    android:icon="@drawable/ic_menu_share" />

<item
    android:id="@+id/search_ic"
    android:layout_width="wrap_content"
    android:title="search"
    android:showAsAction="always"
    android:icon="@drawable/ic_menu_search" />

<item
    android:id="@+id/moreoverflow_ic"
    android:layout_width="wrap_content"
    android:title="moreOverflow"
    android:showAsAction="always"
    android:icon="@drawable/ic_menu_moreoverflow" />

</menu>

and finally MainActivity.java

package com.example.android;

import android.app.Activity;
import android.os.Bundle; 
import android.view.MenuInflater;
import android.view.Menu;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
}
}

Upvotes: 1

Related Questions