Parth Dave
Parth Dave

Reputation: 2126

android activity not showing in only main activity and not showing properly

action bar not showing in main activity while the same code present for another activity and another thing action bar overflow not showing by list in right corner

  1. image 1 : main activity with menu key pressed

enter image description here

  1. image 2 : show message activity

enter image description here

code for main activity:

public class MainActivity extends ActionBarActivity {
EditText t;
public final static String sendMessageextra="com.example.example2.textmessage";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t=(EditText) findViewById(R.id.textmessage);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.activity_main_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

public void sendMessage(View v){
    Intent sendmessageintent=new Intent(this,showMessage.class);
    String messagestring=t.getText().toString();
    sendmessageintent.putExtra(sendMessageextra, messagestring);
    startActivity(sendmessageintent);
}

}

show message activity:

public class showMessage extends Activity{
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sendmessage);
    t=(TextView) findViewById(R.id.showmessage);
    showmessage();
}


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


public void showmessage(){
    Intent intent=getIntent();
    String message=intent.getStringExtra(MainActivity.sendMessageextra);
    t.setText(message);
    Log.i("Set New Text Box", "Text BOx");
}

}

activity main actions.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search Widget -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom"
      android:actionViewClass="android.widget.SearchView"/>

<!-- Location Found -->
<item android:id="@+id/action_location_found"
      android:icon="@drawable/ic_action_location_found"
      android:title="@string/action_location_found"
      android:showAsAction="never" />
<!-- Help -->
<item android:id="@+id/action_help"
      android:icon="@drawable/ic_action_help"
      android:title="@string/action_help" 
      android:showAsAction="never"/>
</menu>

android menifest file: Activity Manifest File:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example2"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.example2.showMessage"
            android:label="Show Message" android:parentActivityName="com.example.example2.MainActivity"/>
    </application>
</manifest>

Upvotes: 1

Views: 992

Answers (2)

KCF
KCF

Reputation: 26

If you want to show the search icon in the right of the actionbar in every activity then just make a change in the actions.xml instead of android:showAsAction="ifRoom" change it to android:showAsAction="always"

Upvotes: 1

Rohit
Rohit

Reputation: 2681

  1. ActionBar not showing

Do this in your oncreate()

ActionBar actionBar = getActionBar();
actionBar.show();
  1. ActionBar overflow there you can see your menu

To display menu, In your activity main actions.xml,

<menu xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_search"
        android:showAsAction="always"
        android:title="@string/action_search"
        android:icon="@android:drawable/ic_action_search"
        android:actionViewClass="android.widget.SearchView"/>

</menu>

Like this you need to do changes in main actions file

Upvotes: 0

Related Questions