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.
Related
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"
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
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);
On android device 4.0.1 I am trying to build application with ActionBar but getting the NullPointException. I have tried the following solutions:
Adding Theme Theme.Holo.Light to the application theme.
In OnCreate of Activity, setting ActionBar feature before setContentView as requestWindowFeature(Window.FEATURE_ACTION_BAR).
Used SherlockActivity and called getSupportActionBar()
But no luck. In all methods, I am getting null ActionBar. Can anyone please point me out what is the issue. I am pasting Activity and AndroidManifest.xml here.
AndroidManifest.xml
<code>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tzoomers.birthdaysdiary"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light">
<activity
android:name="com.tzoomers.birthdaysdiary.BirthdaysDiary"
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=".ContentActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SyncActivity">
</activity>
</application>
</manifest>
</code>
SyncActivity.java
<code>
public class SyncActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.layout_sync_activity);
ActionBar actionBar = getActionBar();
if(actionBar != null)
{
getActionBar().setDisplayHomeAsUpEnabled(false);
}
else
{
Toast.makeText(this, "Action bar is null", Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
Please help what can be exact issue instead of pointing urls. I have tried all the solutions. If I am missing something in XML or JAVA files please point that.
Thanks in advance.
I JUST solved this problem myself, with the new updates its a bit tricky and some of the old fixes don't work as well anymore. Try this:
set your MainActivity Java code to extend ActionBarActivity
use getSupportAcionBar(); call to retrieve your action bar
Be sure your (custom) TabListener extends android.support.v7.app.ActionBar.TabListener
you can use FragmentManager to add and remove tab Fragments when tabs are selected and unselected.
Here's some of my code snippets to help illustrate, hope it works for you too :)
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) //Overrode Default Constructor
{
super.onCreate(savedInstanceState);
android.support.v7.app.ActionBar tabsActionBar = getSupportActionBar();
/***following changes ActionBar to a Tabbed ActionBar***/
tabsActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);`
android.support.v7.app.ActionBar.Tab tabArray = tabsActionBar.newTab();to later contain 'inflated' digitalClock (tabbed) UI
tabArray.setText(R.string.tab_one);
/***following sets clockTabListener private class as Listener for
* this object***/
tabArray.setTabListener( new clockTabListener( this, digitalClockFragment.class.getName() ) );//actual call to digitalClockFragment
tabsActionBar.addTab(tabArray);//adds TabArray to Action Bar Tab(s)
/******Second call for setting New Tab to AnalogClockFragment******/
tabArray = tabsActionBar.newTab();
tabArray.setText(R.string.tab_two);
tabArray.setTabListener( new clockTabListener( this, analogClockFragment.class.getName() ) );//actual call to analogClockFragment
tabsActionBar.addTab(tabArray);
private class clockTabListener implements android.support.v7.app.ActionBar.TabListener
{
private final Activity currentActivity;
private final String currentFragment;
private Fragment launchFragment;
private android.app.FragmentManager frgManager;
public clockTabListener(Activity activityName, String fragmentName)
{
currentActivity = activityName;
currentFragment = fragmentName;
frgManager = getFragmentManager();
}
/******************************************************************/
/******************************************************************/
/******************************************************************/
#Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab arg0,
android.support.v4.app.FragmentTransaction arg1)
{
launchFragment = Fragment.instantiate(currentActivity, currentFragment);
frgManager.beginTransaction().replace(android.R.id.content, launchFragment).commit();
}
I have just started working on a new tabbed App.
For some reason, I can't seem to be able to get rid for a settings action item with its own bar that goes below my tab bar.
Anyone know what this is all about?
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new Fragment();
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
ActionBar.Tab mapTab = actionBar.newTab().setText("TEST1");
ActionBar.Tab infoTab = actionBar.newTab().setText("TEST2");
ActionBar.Tab settingsTab = actionBar.newTab().setText("TEST3");
mapTab.setTabListener(new TabListener(fragment));
infoTab.setTabListener(new TabListener(fragment));
settingsTab.setTabListener(new TabListener(fragment));
actionBar.addTab(mapTab, 0, false);
actionBar.addTab(infoTab, 1, false);
actionBar.addTab(settingsTab, 2, false);
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfcproducttracing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.nfcproducttracing.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>
Presumably, you are adding that action item in onCreateOptionsMenu() of your activity. If you do not want that action item, remove it.