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

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
image 1 : main activity with menu key pressed
image 2 : show message activity
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>

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"

ActionBar not showing
Do this in your oncreate()
ActionBar actionBar = getActionBar();
actionBar.show();
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

Related

How to make the actionbar disappear in Android Studio?

I need to make the action bar disappear from the screen, I've tried everything I've been able to find online, but nothing seems to work. I've tried creating my own style, which doesn't do anything, getActionbar().hide(), which gives me an error and doesn't compile, and requestWindowFeature(Window.FEATURE_NO_TITLE);, which also doesn't do anything. I really need to get rid of that action bar. I'm using a sensorLandscape orientation, but I've already tried changing it and it doesn't change anything.
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tanques">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen>
<activity android:name=".MainActivity" android:screenOrientation="sensorLandscape" android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My own style:
<resources>
<!-- Base application theme. -->
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
MainActivity.java
package com.example.tanques;
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements JoystickView.JoystickListener, JoystickHorizontal.JoystickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button ShootLeft= findViewById(R.id.ShootLeft);
Button ShootRight= findViewById(R.id.ShootRight);
ShootLeft.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootLeft");
}});
ShootRight.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootRight");
}});
}
#Override
public void onJoystickMoved(float Percent, int source) {
switch(source){
case R.id.JoystickLeft:
Log.d("Joystick", "JoystickLeft" + " Percentage: " +Math.round(Percent));
break;
case R.id.JoystickRight:
Log.d("Joystick", "JoystickRight" + " Percentage: " +Math.round(Percent));
break;
case R.id.JoystickTurret:
Log.d("Joystick", "JoystickTurret" + " Percentage: " +Math.round(Percent));
break;
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tanques">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.NoActionBar"/>
</resources>
Put this inside your onCreate() in MainActivity.java before setContentView()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//add this two lines to your code to hide the system bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
//this hides the navigation bar at the bottom of your device
hideNavigationBar();
final Button ShootLeft= findViewById(R.id.ShootLeft);
Button ShootRight= findViewById(R.id.ShootRight);
ShootLeft.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootLeft");
}});
ShootRight.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v){
Log.d("Button","ShootRight");
}});
}
create hideNavigationBar() method and call it in the onCreate()
private void hideNavigationBar(){
getWindow().getDecorView()
.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
);
}

Android settings page not showing any preferences

Background
In a rather simple Android App a Settings Activity is used for just one ListPreference. When clicking the menu item in the header a new screen shows up without any preference or header.
compiledSdkVersion & targetSdkVersion = 25
minSdkVersion = 10
Code
Here is the code that I currently have.
SettingsActivity.java: Here the onCreate method is never called.
public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {
/**
* {#link PreferenceActivity#onCreate(Bundle)}
*/
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
addPreferencesFromResource(R.xml.pref_movies);
}
/**
* {#link android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange(Preference, Object)}
*/
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
return false;
}
}
pref_movies.xml: Is located in res/xml.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:title="#string/setting_sort_label"
android:key="#string/setting_sort_key"
android:entries="#array/movies_sort"
android:entryValues="#array/movies_sort_values" />
</PreferenceScreen>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.brainchest.udacity.popularmovies">
<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=".SettingsActivity"
android:label="#string/general_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="net.brainchest.udacity.popularmovies.MainActivity"></meta-data>
</activity>
</application>
</manifest>
onOptionsItemSelected in my Fragment: Here the intent is created and startActivity is called. The debugger runs through it, no exceptions.
/**
* {#link Fragment#onOptionsItemSelected(MenuItem)}
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// open the settings
if (item.getItemId() == R.id.action_settings) {
Intent settingsIntent = new Intent(getContext(), SettingsActivity.class);
startActivity(settingsIntent);
}
return super.onOptionsItemSelected(item);
}
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="movies_sort">
<item>Most popular</item>
<item>Highest rated</item>
</string-array>
<string-array name="movies_sort_values">
<item>0</item>
<item>1</item>
</string-array>
</resources>
something else I need to show here?
What I tried so far
Searching the web I found some common mistakes that refer either to a missing AndroidManifest.xml entry or mixing up some resource IDs. I double checked this to my best knowledge but still it doesn't work.
What did I miss?
Try wrapping the ListPreference in a PreferenceCategory like this:
<PreferenceCategory
android:key="<YOUR_KEY>"
android:title="<YOUR_TITILE>" >
<ListPreference
android:title="#string/setting_sort_label"
android:key="#string/setting_sort_key"
android:entries="#array/movies_sort"
android:entryValues="#array/movies_sort_values" />
</PreferenceCategory>
Finally found out what the issue is. I used the wrong overload. Instead of
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
addPreferencesFromResource(R.xml.pref_movies);
}
which is not called, I had to use
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_movies);
}
Now the settings are shown.

Action SEARCH doesn't start new Activity

I have two classes in two different files, one is MainActivity and the other one is SearchResultsActivity.
For some reason when i press the search button it doesn't start SearchResultsActivity, I'm assuming this because i put several breakpoints (even some at the beginning of the class) and the debugger doesn't seem to reach them.
MainActivity.Java
package myapp.myapp;
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
ProgressBar theProgressBar;
TextView stInstTxt;
public void sendMessage(View view) {
Intent intent = new Intent(this, MainActivity.class);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_element).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item){
// Handle item selection
switch (item.getItemId()) {
case R.id.search_element:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//text view
stInstTxt = (TextView) findViewById(R.id.textView01);
//progress bar
theProgressBar = (ProgressBar) findViewById(R.id.progressBar01);
theProgressBar.setVisibility(View.INVISIBLE);
//toolbar
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
}
SearchResultsActivity.Java
package myapp.myapp;
public class SearchResultsActivity extends Activity{
TextView stInstTxt;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
searchItems(query);
}
}
public void searchItems(String query) {
stInstTxt.append(query);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp.myapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/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="myapp.myapp.SearchResultsActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value="myapp.myapp.SearchResultsActivity"/>
</activity>
</application>
</manifest>
activity_main.xml
<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"
tools:context=".MainActivity"
android:background="#FFF9C4">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:paddingTop="70dp"
android:layout_height="wrap_content">
<TextView android:text="#string/help_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView01"
android:scrollbars="vertical"
android:visibility="visible"/>
</RelativeLayout>
</RelativeLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_element"
android:orderInCategory="200"
android:title="#string/search_hint"
android:icon="#drawable/ic_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
Could there be something wrong with AndroidManifest.xml?
After almost 6 hours of trying to implement this (starting with the android documentation as a guide), and trying dozens of different ways to do it, i feel i maybe could help someone else with a similar problem save some time, I came up to this tutorial
I read it all but what I ended up doing was downloading the code and comparing it with mine until i found that I was missing a couple of important things. The official android documentation is incomplete, and pretty much there was no way i could have came up with a solution without seeing a real and complete working example.

SearchView doesn't start Search Activity but searchbar opens

I'm having trouble creating the search feature for my application. I have followed various tutorials, followed the Android Docs, and other Stack Overflow answers with no success. I have the icon for the each in one activity (ContinentActivity.java) and when clicked the toolbar opens up a search window. However typing data does not register in the SearchResultsActivity. It is never created. Could this be because I am using a toolbar and a menu?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_swell_alert_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity"/>
<activity
android:name=".SearchResultsActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<!-- to identify this activity as "searchable" -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.appcompat.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".ui.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=".ui.locations.LocationSelectionActivity"
android:label="#string/title_activity_location_selection"
android:noHistory="true"
android:parentActivityName=".ui.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
<activity
android:name=".ui.locations.ContinentActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_continent"
android:parentActivityName=".ui.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
<!-- Children of Continent Activity. Each have search capabilities-->
<activity
android:name=".ui.locations.CountryActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_country"
android:parentActivityName=".ui.locations.ContinentActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.locations.ContinentActivity" />
</activity>
<activity
android:name=".ui.locations.StateActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_state"
android:parentActivityName=".ui.locations.CountryActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.locations.CountryActivity" />
</activity>
<activity
android:name=".ui.locations.SurfSpotActivity"
android:theme="#style/NoAnimationTheme"
android:label="#string/title_activity_surf_spot"
android:parentActivityName=".ui.locations.StateActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.locations.StateActivity" />
</activity>
</application>
SearchResultsActivity.java
public class SearchResultsActivity extends AppCompatActivity {
public static final String TAG = "SEARCH_RESULTS_ACTIVITY";
SearchResultsAdapter mSearchResultsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate");
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.v(TAG, "onNewIntent");
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Log.v(TAG, "HANDLE INTENT");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
// Query your data set and show results
// ...
Log.v(TAG, "Searching for " + query + "...");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.v(TAG, "onCreateOptionsMenu");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_location, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );
return true;
}
This activities menu has the search icon
ContinentActivity.java
public class ContinentActivity extends AppCompatActivity {
private ArrayList<Continent> mContinents;
#Bind(R.id.recyclerView) RecyclerView mRecyclerView;
#Bind(R.id.toolBar) Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_selection);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
LocationDataSource dataSource = new LocationDataSource(this);
dataSource.test();
mContinents = dataSource.readContinents();
ContinentAdapter adapter = new ContinentAdapter(this, mContinents);
mRecyclerView.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_location, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
menu_location.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:orderInCategory="200"
android:title="#string/action_settings"
android:icon="#drawable/ic_search_white_24dp"
appcompat:showAsAction="always"
appcompat:actionViewClass="android.widget.SearchView"/>
</menu>
tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="4dp"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
android:titleTextColor="#color/ColorText">
</android.support.v7.widget.Toolbar>
Seems you have missing call to associate SearchView with searchable info within the onCreateOptionsMenu() method? Quote from the training link http://developer.android.com/guide/topics/search/search-dialog.html
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
But since you are not using the same activity for search result, you cannot simply use getComponentName(), but to use new ComponentName(this, SearchResultsActivity.class)

Android app icon doesn't show on actionbar

I am creating an android app using eclipse and I have a problem. the app icon doesn't show on actionbar. can anybody tell me what's wrong? here is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gowemto.gnoulashe"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light" >
<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=".CalcResultActivity"
android:label="#string/title_activity_calc_result"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gowemto.gnoulashe.MainActivity" />
</activity>
</application>
</manifest>
and here's the main activity code:
package com.gowemto.gnoulashe;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void intent1(View view) {
Intent intent = new Intent(this, CalcResultActivity.class);
startActivity(intent);
}
}
and here's the xml file of main activity:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="#string/welcome_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_the_dates"
android:onClick="intent1" />
</LinearLayout>
and this is what the actionbar look like when I run the app: http://i.imgur.com/jADDz7b.png
The app doesn't show any errors, and the icon is put in the right way in the directories. so can anyone tell me what's the problem and how can I fix it?
The problem with #ZsoltBoldizsár's answer is that you are using ActionBarActivity. Change getActionBar() to getSupportActionBar()
Add the following code in onCreate(Bundle) after you call super.onCreate(Bundle):
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
Appcompat-v7 automatically disables setDisplayShowHomeEnabled so you must set it to true to show an icon.
You need to configure the actionbar. Read the documentation, but the line below might help you.
getActionBar().setHomeButtonEnabled(true);
getActionBar().setIcon(here comes your drawable id) (eg. R.drawable.ic_launcher)
Try with this:
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
ab.setTitle(Html.fromHtml("your text"));
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor(your_colorCode));
ab.show();

Categories

Resources