How to change the default icon of OptionsMenu in android? - android

I coded this in order to get a Filter icon at OptionsMenu.I was using an empty meny with drawable image and the onClick of that was showing a DialogFragment. But this doesn't work fine as an empty title item appears on the OptionsMenu clicking which my DialogFragment gets open.
I appreciate any help.
Thanks.
menu file for OptionsMenu :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/filter"
android:orderInCategory="100"
android:icon="#drawable/filter_icon"
android:showAsAction="ifRoom|withText"
android:title="hi" />
</menu>
This is myactivity :
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
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.
String[] values = new String[]{ "Veg.",
"Non Veg.",
"Veg & Non veg."
};
int id = item.getItemId();
if (id == R.id.filter) {
RestaurantListingFragment restaurant_categories_dialog = new RestaurantListingFragment();
android.app.FragmentManager fm = getFragmentManager();
Bundle args = new Bundle();
args.putStringArray("restaurantCategories",values);
restaurant_categories_dialog.setArguments(args);
// Show DialogFragment
restaurant_categories_dialog.show(fm, "hi");
return true;
}
return super.onOptionsItemSelected(item);
}
This is my fragment class:
public class RestaurantListingFragment extends DialogFragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dialog_restaurant_categories, container,false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
String[] restaurantCategories = getArguments().getStringArray("restaurantCategories");
if(restaurantCategories != null)
{
ListView lv=(ListView) rootView.findViewById(R.id.fd_lv);
ArrayAdapter arrayAdapter =new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,restaurantCategories);
lv.setAdapter(arrayAdapter);
}
return rootView;
}
}
Manifests :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cresol.demo.drestodemo">
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:supportsRtl="true">
<activity
android:name=".Home"
android:theme="#style/NoTitle">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".signup"
android:theme="#style/NoTitle" />
<activity
android:name=".RestaurantListing"
android:theme="#style/AppTheme" />
<activity android:name=".DishListing"
android:theme="#style/AppTheme"></activity>
</application>
</manifest>

In your menu file change this line
android:showAsAction="ifRoom|withText"
with this one
app:showAsAction="always"

Related

How to maintaining listView (java/android)

I have a listview in the SubActivity of my app.. but when I go from the subActivity to the main Activity and i try to return to the subActivity, the item of the listView disappear.. how to fix this problem?
also if I close my app and i restart it, my listview is empty... why?
my MainActivity:
final ArrayList<String> listCron = new ArrayList<String>();
....
#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();
switch(id) {
case R.id.history:
Intent history = new Intent(getApplicationContext(), HistoryClass.class);
history.putStringArrayListExtra("history", listCron);
startActivity(history);
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
my SubActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
ArrayList<String> cronologia = i.getStringArrayListExtra("history");
ListView lv = (ListView) findViewById(R.id.viewCron);
if (cronologia.isEmpty()) {
cronologia.add("No operations");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_layout, cronologia);
lv.setAdapter(adapter);
}
}
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.VIBRATE"/>
<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"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HistoryClass"
android:label="#string/history">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>

Icon does not show up on the action bar - Android

I'm trying to show a camera icon on the action bar, but it does not work. The menu shows up and works fine but the camera icon does not show up. Can you please help me?
Thank you in advance.
The menu code:
<?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/action_camera"
android:orderInCategory="0"
android:icon="#drawable/ic_action_camera"
android:title="#string/action_camera"
app:showAsAction="always" />
<item android:id="#+id/action_delete"
android:title="#string/action_delete"
app:showAsAction="never" />
</menu>
The manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.cfb.daily_selfie" >
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_action_camera"
android:label="#string/app_name"
android:theme="#style/AppBaseTheme" >
<activity
android:name=".DailySelfieActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayPictureActivity"
android:label="#string/title_activity_display_picture"
android:parentActivityName=".DailySelfieActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="br.com.cfb.daily_selfie.DailySelfieActivity" />
</activity>
<receiver android:name=".AlarmNotificationReceiver" >
</receiver>
</application>
</manifest>
Bellow onCreate()
public class DailySelfieActivity extends ListActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
public final static String EXTRA_MESSAGE = "br.com.cfb.daily_selfie.MESSAGE";
private static final String TAG = "DS-DailySelfieActivity";
private final static int INTENT_ID = 322;
private SelfieViewAdapter mAdapter;
private SelfieRecord mSelfieRecord;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView selfieListView = getListView();
final View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
selfieListView.addFooterView(footerView);
mAdapter = new SelfieViewAdapter(this.getApplicationContext());
loadListAdapter();
setListAdapter(mAdapter);
// Create Alarm to take a selfie
Alarm mAlarm = new Alarm(getApplicationContext());
// Enable filtering when the user types in the virtual keyboard
// selfieListView.setTextFilterEnabled(true);
// Set an setOnItemClickListener on the ListView
selfieListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Display a Toast message indicting the selected item
Toast.makeText(getApplicationContext(), "position = " +position + " id = " +id
, Toast.LENGTH_LONG).show();
mSelfieRecord = (SelfieRecord) mAdapter.getItem(position);
Toast.makeText(getApplicationContext(), "Date = " +mSelfieRecord.getDate()
, Toast.LENGTH_LONG).show();
// Display Picture
Intent intent = new Intent(DailySelfieActivity.this, DisplayPictureActivity.class);
Bitmap mPicture = mSelfieRecord.getPicture();
intent.putExtra(EXTRA_MESSAGE, mPicture);
startActivity(intent);
}
});
}
Bellow OnCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_daily_selfie, menu);
return true;
}
What about ic_menu_camera? Although I do see references to ic_action_camera elsewhere, that is not coming up for me. I do have access to android:icon="#android:drawable/ic_menu_camera" though (note the addition of the #android:drawable)
Or, as #acostela indicated, make sure it's in your drawable folder to use just the
android:icon="#drawable/ic_action_camera".
my original Activity was extending a ListView, so I changed and extended the ActionBar and it worked well.
Thank you,
Carlos

Android - Searchview in fragment is not working as expected

I’m facing some problems with fragments.
My MainActivity contains a DrawerLayout and a FrameLayout like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!—- Main view (fragments) -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/cor_view"/>
<!-- Navigation drawer -->
<ListView android:id="#+id/lista"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/cor_divider_drawer"
android:dividerHeight="1dp"
android:background="#color/background_drawer"
android:textColor="#color/cor_texto_drawer"/>
</android.support.v4.widget.DrawerLayout>
According to each item from my navigation drawer, a specific fragment will be shown at the id/content_frame. This is working fine, but the problem is that one specific fragment has a search option at the action bar and when I submit the query, the result is not shown at the same “place” of this fragment. I mean, the result of the search don’t fill the framelayout (id/content_frame).
How could I do that ? A fragment replacement ? Does this work only using activities ?
This is my MainActivity:
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navigation.NavList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
{
// According to each item from menu, I’ll choose which fragment will be shown.
navigation.showFragment(position, MainActivity.this);
}
});
// When the app starts, by default, the home will be shown.
navigation.showFragment(0, MainActivity.this);
}
}
Inside the method showFragment I creat the fragment object like this
(in this example, the position will be set to point to ABCFragment):
Fragment fragment = new ABCFragment();
FragmentManager fragmentManager = activity.getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
So this is my fragment that has the searchview:
public class ABCFragment extends Fragment implements OnQueryTextListener
{
/*
onCreateView …
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
// Action bar.
inflater.inflate(R.menu.activity_main_actions, menu);
searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.
setSearchableInfo(searchManager.getSearchableInfo(getActivity()
.getComponentName()));
}
#Override
public boolean onQueryTextSubmit(String query)
{
searchView.clearFocus();
return false;
}
#Override
public boolean onQueryTextChange(String newText)
{
return false;
}
}
My action bar in menu folder:
<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"/>
<item android:id="#+id/action_new"
android:icon="#drawable/ic_action_new_event"
android:title="#string/action_new_event"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:title="#string/action_refresh"
android:showAsAction="ifRoom" />
My searchable file in XML folder:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/search_hint"
android:inputType="number"
android:label="#string/app_name" />
My Android Manifest:
<!-- Main Activity -->
<activity
android:name="com.test.MainActivity"
android:label="#string/app_name">
</activity>
<!-- ABC Fragment -->
<activity
android:name="com.test.ABCFragment"
android:label="#string/app_name">
<meta-data
android:name="android.app.default_searchable"
android:value="com.test.SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Search results activity -->
<activity android:name="com.test.SearchResultsActivity"
android:parentActivityName="com.test.ABCFragment" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
i'm using the searchview through the custum action bar directly in the fragment
Here we can not find the activity but using the getActivity can get all widgets
searchView = (SearchView) getActivity().findViewById(R.id.searchView1);

Action bar App title name wasn't displayed in my applicaton

I didn't know why my action bar title name was suddenly disappears.But icon was displayed at the top of the screeen.But Application name wasn't displayed at the top of the screen.
Below I am posted codes related to that.
HomeActivity.java:
package com.sit.loco.activity;
public class HomeActivity extends FragmentActivity
implements
VideoListFragment.OnVideoSelectedListener,ActionBar.OnNavigationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add channel list array to actionbar spinner
Context context = getActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.channel_name, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
// remove actionbar title and add spinner to actionbar
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActionBar().setListNavigationCallbacks(list, this);
// position = getIntent().getExtras().getInt("position");
Log.v("position", position + "");
appData = ((GemsApplication) this.getApplication()).getAppData();
AppPreferences.setAppPreferences(getApplicationContext());
actionabar = getActionBar();
actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// actionabar.setDisplayHomeAsUpEnabled(true);
viewpager = (ViewPager) findViewById(R.id.pager);
fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
actionabar.setSelectedNavigationItem(position);
}
};
}
Manifest:
<application
android:name="com.sit.loco.app.GemsApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.sit.loco.activity.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<application>
In res/menu/home.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menuShare"
android:title="#string/share_it"
android:icon="#drawable/ic_action_share"
android:showAsAction="always|withText"/>
<item
android:id="#+id/menuRate"
android:title="#string/rate_it"
android:icon="#drawable/ic_action_good"
android:showAsAction="always|withText"/>
<item
android:id="#+id/menuAbout"
android:title="#string/about"
android:icon="#drawable/ic_action_info"
android:showAsAction="always|withText"/>
</menu>
I didn't know how to solve this.Anybody can help me with this.Thank you.
You have this line:
getActionBar().setDisplayShowTitleEnabled(false);
It removes the title from the action bar. Either change it to:
getActionBar().setDisplayShowTitleEnabled(true);
or simply remove it completely.

None of the action buttons are showing up on action bar

I am following the guild on developer.android.com and have made it to the part to adding action button to the bar. None of them, however, are wanting to show up. I have looked through the code, did research, and even redid the whole project just the make sure I was following everything right. Nothing I do seems to work so I thought I would put it here and let a fresh (and more experience) set of eyes look at it and let me where I am going wrong.
main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myfirstapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
myfirstapp:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
myfirstapp:showAsAction="never" />
</menu>
DisplayMessageActivity.java
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
setContentView(R.layout.activity_main);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
//openSearch();
return true;
case R.id.action_settings:
//openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<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=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
screenshot
Just remove the setContentView(R.layout.activity_main);inside
#Override
public boolean onCreateOptionsMenu(Menu menu) {
setContentView(R.layout.activity_main);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
and also add return true instead of return super.onCreateOptionsMenu(menu);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;
}
try this. Its working for me.

Categories

Resources