can't add search button on action bar - java

So i'm new to android programming
im going to add two action bar , but when i add the second action bar , it fills the old one , not making a new one .
i already use the holo light theme .
this is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orionpreneur"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow">
<!-- Splash Screen Activity -->
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name=".LoginActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/about_us"
android:parentActivityName=".LoginActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.orionpreneur.MainActivity" />
</activity>
<activity
android:name=".About"
android:label="#string/title_activity_about"
android:parentActivityName=".LoginActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.orionpreneur.MainActivity" />
</activity>
</application>
</manifest>
this is my main_activity_menu.xml code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/about"
android:title="#string/about"
android:showAsAction="always"/>
</menu>
this is my MainActivity.java code
package com.example.orionpreneur;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class LoginActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#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.
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "you've pressed the search button !", Toast.LENGTH_SHORT).show();
return true;
case R.id.about:
Intent in = new Intent(this, About.class );
startActivity(in);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
i wanted the search button to be added in separated bar like this
https://developer.android.com/images/training/basics/actionbar-actions.png
i'm desperate , sorry if my question is already asked .
thanks :)))

*remove this *
android:uiOptions="splitActionBarWhenNarrow"
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen

Related

Menu does not show up in Android studio

This is my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="k.coursera.mylocation">
<uses-permission android:name="android.permission.INTERNET" />
<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>
<activity android:name=".test"></activity>
</application>
</manifest>
When I tried to change the theme to:
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
The menu will show up but the other function of my act wouldn't work (such as start a new activity).
And here is the piece of code for my menu XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/deleteCountry" android:title="delete" />
</menu>
And my java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.deleteCountry:
mAdapter.removeAll();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

ActionBar not working when extending ListActivity

I have a confusing problem. I have a MainActivity with 2 actions : Update and Logout. The problem is when I run the activity that extends ListActivity the action bar doesn't appear.
Below I have 2 images with 2 different extend types in MainActivity
Extending ActionBarActivity example
public class MainActivity extends ActionBarActivity
By extends ListActivity the result is the same as in the picture below. Basically I want to make the main activity with a ListView and an action bar so that the user is able to update and logout using the action bar. But it seems it doesn't work and i need your help. I tried searching on the web i couldn't find anything that helped.
public class MainActivity extends ListActivity
Here you can see my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.florin.statusapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="11"
android:targetSdkVersion="21"/>
<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=".RegisterActivity"
android:label="#string/title_activity_register" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".UpdateStatusActivity"
android:label="#string/title_activity_update_status" >
</activity>
</application>
</manifest>
My MainActivity.java
public class MainActivity extends ListActivity{
private List<ParseObject> mStatusObjects;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enable Local Datastore.
Parse.initialize(this, "foo", "bar");
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
} else {
// show the login screen
Intent toLoginActivity = new Intent(MainActivity.this, LoginActivity.class);
startActivity(toLoginActivity);
}
}
#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;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#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
switch (id) {
case R.id.updateStatus:
// take user to update activity
Intent toMainActivityIntent = new Intent(MainActivity.this, UpdateStatusActivity.class);
startActivity(toMainActivityIntent);
break;
case R.id.LogoutUser:
//Log out user
ParseUser.logOut();
// take user to login activity
Intent toLoginActivityIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(toLoginActivityIntent);
break;
}
return super.onOptionsItemSelected(item);
}
and the menu_main.xml for the action bar:
<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="com.example.florin.statusapp.MainActivity">
<item android:id="#+id/updateStatus"
android:title="Update"
app:showAsAction="always" />
<item
android:id="#+id/LogoutUser"
android:title="Logout"
app:showAsAction="never"
/>
</menu>
This should be related to your theme. Action bars are only supported on themes after holo.
http://developer.android.com/guide/topics/ui/actionbar.html#Adding
Your styles.xml probably has something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
You can change it to this to use the holo theme:
<style name="AppTheme" parent="android:Theme.Holo">
As Tachyonflux said, on API 11 and higher, the action bar is included in all activities that use Theme.Holo or one of it's descendants
Try adding the following to your AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo">
Or another Theme or your choosing. Go to the link Tachyonflux has and look at the available options. There are various default options, but you can also create your own.

Actionbar not rendering using nbandroid

Cannot get the Actionbar to load. It works using Eclipse and Android Studio but I prefer to work in Netbeans if at all possible. New to Android development
MainActivity.java
package action.bar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
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.actionbarmenu, menu);
return super.onCreateOptionsMenu(menu);
}
}
actionbarmenu.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/contacts"
android:title="Private Contacts"
/>
<!-- android:icon="#drawable/ic_action_person" -->
</menu>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="action.bar"
android:versionCode="15"
android:versionName="15.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<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>
</application>
</manifest>

Intent using thread is not working

I know I already asked this, but I didn't get a sufficient answer. Im trying to start an activity, but the emulator stays on the first activity. Ive tried all ways to do it but it never works. The youtube videos show that it should work but it never does. Is there something missing or is there anything wrong with the following code?
//First Activity:
package com.mtprogramming.magicsquaresgame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
public class Opening extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opening);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
Intent open = new Intent("com.mtprogramming.magicsquaresgame.MENU");
startActivity(open);
}
}
};
timer.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.opening, menu);
return true;
}
}
//Second Activity:
package com.mtprogramming.magicsquaresgame;
import android.app.Activity;
import android.os.Bundle;
//Created by suprav on 7/11/13.
public class Menu extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
}
//Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mtprogramming.magicsquaresgame"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.mtprogramming.magicsquaresgame.Opening"
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.mtprogramming.magicsquaresgame.Menu"
android:label="#string/title_activity_menu"
android:parentActivityName="Opening" >
<intent-filter>
<action android:name="com.mtprogramming.magicsquaresgame.MENU"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="Opening" />
</activity>
</application>
</manifest>
Try this:
Intent open = new Intent(Opening.this, Menu.class);
startActivity(open);
I think i found the issue, seems like metadata property is not being properly used, hences activity its not being started, this is the proper way to use the property:
<activity android:name=".TestActivity" >
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".TestParentActivity">
</meta-data>
</activity>
So, seems like you are missing a dot in "android:value="Opening"
Regards!

Android Menu Inflater error

Im trying to get my menu inflater to link to my layout file, but when app launches and I hit menu, then select menu option it crashes program. It says it cannot find activity to launch:
error in eclipse LogCat:
01-01 10:24:38.799: E/AndroidRuntime(30359): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.menu/com.menu.AboutUs}: java.lang.ClassNotFoundException: com.menu.AboutUs in loader dalvik.system.PathClassLoader[/data/app/com.menu-1.apk]
Main Code:
package menu;
import com.menu.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
public class testActivity 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(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.new_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.aboutUs:
Intent i = new Intent("menu.ABOUT");
startActivity(i);
break;
case R.id.settings:
break;
}
return false;
}
Manifest code:
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name="menu.testActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AboutUs"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog">
<intent-filter >
<action android:name="menu.ABOUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
about.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:text="Why dosent this work??????????????????????? "
android:title="About Us"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
I would gratefully appreciate help... thank you
If you want to launch say Activity B from Activity A, you should make sure that you have declared Activity B in your manifest file.Then in your onOptionsItemSelected(), you can say,
Intent i = new Intent(this,B.class);
startActivity(i);
If you want to start activity using Action parameter for intent you can use the following in the Activity tag in the mafifest.
This is considering activity B is in the application package directly
<activity android:name=".B">
<intent-filter>
<action android:name="menu.ABOUT" />
</intent-filter>
</activity>
You got Class not found exception for com.menu.AboutUs - pretty straightforward

Categories