0

I am trying to develop a simple weather forecast app. Android Studio v2.2 Minimum SDK set to API10:Gingerbread.

Problem - I need a Search City option on my 1st page.So I simply edited the menu_main.xml file and added that option. My problem is that the Search City option appears in 'design' and 'preview' section of menu_main.xml but does not appear when I check the 'design'or 'preview' section of my activity_main.xml .

I have set theme to @style/AppTheme in manifests also.

In that red highlighted part I want my menu to appear..

My menu file is-

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    tools:context=".MainActivity" >

    <item

    android:id="@+id/change_city"
    android:orderInCategory="100"
    android:title="@string/change_city"

    app:showAsAction="always"/>

    </menu>

My mainactivity.xml 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ff1ba1ee"``
    tools:context="com.example.hp.theweatherapp.MainActivity">

I have not added whole xml as it just contains simple TextViews.

My MainActivity.java is-

    package com.example.hp.theweatherapp;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    }

Manifests.xml is-

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.hp.theweatherapp">

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
        </activity>
      </application>
    </manifest>

any help is appreciated..thanks..

saurabh169
  • 499
  • 1
  • 4
  • 12

1 Answers1

1

I think that the problem is that you're not inflating menu in your activity. Add to your MainActivity.java this code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.change_city) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

For creating ActionBar button, check: Creating a button in Android Toolbar

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94