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
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 have an activity with a TabLayout that's hooked up to a FragmentPagerAdapter. Whenever the user changes tabs, another fragment is loaded, but the Activity stays the same.
The activity also contains a search function, i.e. autocomplete suggestions are fetched from a web server. I want to perform a different search action - basically, query another URL - depending on which fragment is currently loaded in the FragmentPager/TabLayout.
So far, I haven't found any way to do this. Here's what I have tried:
currently, the search function is connected to a content provider (via manifest and searchable.xml). However, I haven't found a way to pass data to this ContentProvider to let it know which tab is selected
using multiple content providers doesn't seem to be possible because there can only be one for any given Activity, and the Activity isn't supposed to change when selecting another tab
manipulating the Activity's onQueryText* methods isn't feasible because I don't want to block the UI thread fetching the data
Here's a quick sketch of the situation: Sketch
Here are the relevant code fragments:
MainActivity.class
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mViewPager.setAdapter(new MainFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
TabLayout tabs = (TabLayout) findViewById(R.id.sliding_tabs);
tabs.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, MainActivity.class)));
searchView.setIconifiedByDefault(false);
return true;
}
/* ... */
}
menu/action_search.xml
<?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_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="hint"
android:label="#string/app_name"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:searchSuggestAuthority="xyz.demo.search.datasuggestion"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://xyz.demo.search.data"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xyz.demo.app">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<activity android:name=".MainActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<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>
<provider
android:name=".SuggestionProvider"
android:authorities="xyz.demo.search.datasuggestion"
android:enabled="true"
android:exported="true"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
SuggestionProvider.java
public class SuggestionProvider extends ContentProvider {
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(".../suggestions")
.build();
try {
Response response = client.newCall(request).execute();
String jsonString = response.body().string();
JSONArray jsonArray = new JSONArray(jsonString);
/* ... */
} catch (Exception e) {
e.printStackTrace();
}
/* ... eventually returns a MatrixCursor */
}
/* ... */
}
Ah okay I see this is one of those things where Android is calling your provider and you can't control how it's going to call it.
I don't see a way to do this cleanly either. The searchable is linked to the activity. You either base your screens on the activity not the fragment (changing your design), or do something terrible like set a preference when your fragment resumes so the content provider knows which fragment is "active".
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.
In virtual mobile menu is displaying.But it is not displaying in my personal mobile.I have kept mainactivity.java ,main.xml,androidmainfest.xml files here.Any answers please...
//mainactivity.java
public class MainActivity extends Activity{
String username,password; ResultSet rs =null;
boolean temcfag=false;
static boolean temqfag=true;
public static String tag="Lifecycle activity";
EditText user,pass;
AlertDialog.Builder dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog=new AlertDialog.Builder(this);
dialog.setNeutralButton("OK", null);
user=(EditText)findViewById(R.id.editText1);
pass=(EditText)findViewById(R.id.editText2);
final Button click=(Button)findViewById(R.id.button2);
click.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent launchactivity= new Intent(MainActivity.this,com.example.desisquarea.LoginActivity.class);
startActivity(launchactivity);
}
});
}
#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;
}
}
//menu/main.xml
<?xml version="1.0" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/login" android:title="SignUP" />
<item android:id="#+id/sign" android:title="Login" />
<item android:id="#+id/home" android:title="Home" />
</menu>
//androidmainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.desisquarea"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="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_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.desisquarea.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>
LayoutParams lp = dialog.getWindow().getAttributes();
lp.gravity = Gravity.TOP | Gravity.RIGHT;
myDialog.getWindow().setAttributes(lp);
dialog.show();
I have 2 projects, and I want to embed a project in other. I have created 2 projects and have made a project as library file and inserted in other but I am still unable to get it working. I have taken a simple activity and want to display a toast message, using Intent I have given the address of the next project library. This is the code of the main (first) project.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.sec_pro";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "First Activity", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, com.example.sec_pro.MainActivity.class);
EditText editText = (EditText) findViewById(R.id.editText1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
#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;
}
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.integrate.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.sec_pro.MainActivity" >
</activity>
</application>
Now this "sec_pro" is the library file of my next project which I have inserted in the first project.
Please help.
Thanks in advance.
I think you have missed to declare your library activity in Manifest.
try this code to declare in to manifest under application tag:
<activity android:name="PackageName.YourLibraryActivity" />
Please check onClick method again, I think you missed a statement:
startActivity(intent);