Imageview doesn't display my icon in Actionbar? - android

I have simple image view app... That is suppose to display the image in viewpager mode. However, my problem is that it is not displaying my app icon at the top of the action bar...Any help will be appreciated.....I know that it is something really small, but I can't spot it..I woulde like to display my icon as shown in this link
Please see my following codes...
Mainactivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.activity_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
#Override
protected void onPause() {
super.onPause();
oursong.release();
}
}
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:icon="#drawable/icon" >
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="#drawable/icon" />
</RelativeLayout>
Manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.manishkpr.viewpagerimagegallery.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.manishkpr.viewpagerimagegallery.MainActivity"
android:label="#string/app_name"
android:icon="#drawable/icon">
</activity>
</application>
</manifest>

If I understood you only need edit a file in a folder called menu. Your file looks like
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
and just change never and put always
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>

Related

Searchview with back button

I'm implementing searchview without Actionbar and toolbar. Everything is fixed, just back button remains. Below is my xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"">
</SearchView>
</RelativeLayout>
I got something like this.
Below is the image what I need.
Any help would be great.
Use android.support.v7.widget.SearchView
Create a menu file like this in res/menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_search_album"
android:icon="#drawable/ic_search"
android:title="#string/str_search_album"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
</menu>
Create a searchable.xml file like this in res/xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search" >
</searchable>
now in your activity you need to Override onCreateOptionsMenu() method
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
Now in your manifest file
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
for more information android.support.v7.widget.SearchView
Also check How to create search interface
For API 21+ you can use android:searchIcon="[your drawable here]".

Unable to see menu in action bar Android

i have problem putting the menu in the upper right corner. even i checked this Link. but not worked.
i have tried with android:showAsAction="always"even tried with more then 6 SDK version but not worked .
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<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>
items.xml:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
android:showAsAction="always"
/>
<item
android:id="#+id/computer"
android:title="#string/computer"
android:icon="#drawable/computer"
/></menu>
Java:-
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) {
getMenuInflater().inflate(R.menu.items, menu);
//inflater.inflate(R.menu.items, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected Computer", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
.
please suggest . any suggestion are welcome . Thanks
Solution as below-
In build.gradle(Module:app) add appcompat dependency
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
// ... other dependencies
}
change
public class MainActivity extends Activity
to
public class MainActivity extends AppCompatActivity.
change your menu.xml file as below
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/phone"
android:title="#string/phone"
android:icon="#drawable/phone"
app:showAsAction="always" />
<item
android:id="#+id/computer"
android:title="#string/computer"
android:icon="#drawable/computer" />
</menu>

Action on ActionBar doesn't show

Yesterday I had a problem with replacing fragments, so I had to change my MainActivity to extend Acivity instead of ActionBarActivity in order to avoid compatibility issues. The problem is, that after I've done that, an actionbar item disappeared. I tried following androids official tutorials on how to set it up without using the support library, but with no avail. This is what I have:
Inside the MainActivity:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().add(R.id.frag_container, new MainMenuFragment()).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;};
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As you can see, I'm implementing the needed methods for the action bar to be populated with buttons. This is my menu's XML file:
<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.pszczyna.MainActivity" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
The manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jam.pszczyna"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jam.pszczyna.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
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>
which is the minimum SDK version required for this to work. For me everything looks like it's supposed to, whats wrong with it?
try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.pszczyna.MainActivity" >
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
android:showAsAction="always"/>
</menu>
and in your manifest remove android:theme="#style/Theme.AppCompat" and put your own style.

Search on action bar does not call search activity

I am trying to make a search bar and I followed the tutorial on http://developer.android.com/training/search/setup.html. It does not work and SearchResultsActivity does not get called, so I immediately found the following question:
Search widget on action bar doesn't trigger my search activity. However, the solutions listed there (more specifically, adding meta-data inside in AndroidManifest.xml and using references to strings in xml/searchable.xml) do not solve my problem. I also tried putting meta-data in <activity> in AndroidManifest.xml and numerous other solutions to no avail.
What am I doing wrong?
Here is my code:
main.xml:
<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.jsondataparser.app.MainActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="#+id/search_menu"
android:title="#string/app_name"
android:icon="#drawable/ic_launcher"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
xml/searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/app_name">
</searchable>
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" />
<activity
android:name="com.jsondataparser.app.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.jsondataparser.app.SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
MainActivity.java:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_menu).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
SearchResultsActivity.java:
public class SearchResultsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("SearchResultsActivity", "onCreate called");
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
Log.d("SearchResultsActivity", "onNewIntent called");
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d("SearchResultsActivity", query);
}
}
}
Put also
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
inside <activity> in your AndroidManifest.

Combining Menu and Tab

I am using this "Tab layout and Listview" from http://www.androidhive.info/2012/05/android-combining-tab-layout-and-list-view/ and "Android Menus" from http://www.androidhive.info/2011/09/how-to-create-android-menus/.
I merge them together, yet it doesn't work. The tab works, but not for the menu.
Here are my codes.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class optionMenuActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Intent myList = new Intent();
switch (item.getItemId())
{
case R.id.menu_login:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
// Toast.makeText(optionMenuActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
myList = new Intent(optionMenuActivity.this, LoginActivity.class);
startActivity(myList);
return true;
case R.id.menu_save:
Toast.makeText(optionMenuActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(optionMenuActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(optionMenuActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(optionMenuActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_quit:
// Toast.makeText(optionMenuActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
The main from "Tab layout"
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
And this is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_login"
android:icon="#drawable/icon_bookmark"
android:title="Login" />
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/menu_search"
android:icon="#drawable/icon_search"
android:title="Search" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon_share"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
<item android:id="#+id/menu_quit"
android:icon="#drawable/icon_preferences"
android:title="Quit" />
</menu>
And lastly, my manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".WorkDroid5"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Inbox Activity -->
<activity android:name=".AnnouncementActivity" />
<!-- Outbox Activity -->
<activity android:name=".HomeworkActivity" />
<!-- Profile Activity -->
<activity android:name=".TimetableActivity" />
<activity android:name=".tt_Friday" />
<activity android:name=".ttFinalDetails" />
<activity android:name=".TimetableAdapter" />
<activity android:name=".tt_Thursday" />
<activity android:name=".tt_Monday" />
<activity android:name=".tt_Tuesday" />
<activity android:name=".tt_Wednesday" />
<activity
android:name=".RegisterActivity"
android:label="Register New Account" >
</activity>
<activity
android:name=".LoginActivity"
android:label="Login to your Account" >
</activity>
<activity
android:name=".optionMenuActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.example.OPTIONMENUACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Thanks in advance for anyone's help (':
Here:
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
you pass R.layout.menu to MenuInflater.inflate(..) method, but it should be R.menu.menu. Move your menu.xml file from res/layout dir to res/menu dir.
I ran into similar problem with TabHost. By default, Android creates an options menu for the parent activity, i.e. TabHost in your case. The child tab does not receive the OnCreateOptionsMenu event. Therefore, your menu is not being created.
Put the option menu creation/handling code in the activity extending TabActivity. You can change the preference menu content by checking which tab is currently selected by checking getTabHost.getCurrentTab or getTabHost.getCurrentTabTag and inflating appropriate menu.

Categories

Resources