Share Action provider not working - android

I have created an Android app.
It has a main activity (with fragment) and a detailed activity (with fragment)
In the detailed activity fragment, I want to add a share button which will share some fixed text (say - "Hello").
The share button appears in the app but nothing happens when I click it.
I want to share plain fixed text.
Here is the menu_detail.xml file:
<?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_share"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android:support.v7.widget.ShareActionProvider" />
</menu>
And here is the DetailActivityFragment.java file:
package com.blogspot.amangoeliitb.amansblog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
public class DetailActivityFragment extends Fragment {
public static final String LOG_TAG = DetailActivityFragment.class.getSimpleName();
public DetailActivityFragment() {
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_detail, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if(mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
}
else
Log.d(LOG_TAG, "Share action provider is null");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
String title = extras.getString("TITLE") ;
String content = extras.getString("CONTENT") ;
((TextView) rootView.findViewById(R.id.post_title)).setText(title);
((WebView) rootView.findViewById(R.id.post_content)).loadData(content, "text/html", "UTF-8");
return rootView;
}
private Intent createShareForecastIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
return shareIntent;
}
}

I was also working on the same problem.
There is a minute error in your code in menu_detail.xml file. In this line
app:actionProviderClass="android:support.v7.widget.ShareActionProvider"
Remove the ":" after android and replace with a full stop. So it should be
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
Everything else is good. And it is working for me.

Related

Search Bar for a seperate fragment

I have made different fragments inside one Activity (Home.java) on which toolbar and navigation drawer is there.
Right now I thought to put searchview inside fragment.We usually write all code related to searchview inside oncreateoption menu. I have done the same thing but oncreateoption is made inside fragment.
I'm not getting any error but searchbar is not be shown over the toolbar.
package com.example.foody;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class fitem extends Fragment implements SearchView.OnQueryTextListener
{
RecyclerView rv1;
fitemAdapter adapter;
ArrayList<All_Data> item;
Cursor cr;
Storage sob;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.fragment_fitem, container, false);
sob=new Storage(getContext());
Bundle bundle = this.getArguments();
if (bundle == null)
cr=sob.allData(); //if called from home page
else
cr=sob.categorisedData(bundle.getString("category")); // if called from category page
inData();
rv1=v.findViewById(R.id.rv1);
rv1.setHasFixedSize(true);
rv1.setLayoutManager(new LinearLayoutManager(getContext()));
adapter=new fitemAdapter(getContext(),item);
rv1.setAdapter(adapter);
return v;
}
void inData()
{
item=new ArrayList<>();
while(cr.moveToNext())
{
item.add(new All_Data(cr.getString(0),cr.getString(2),cr.getString(3)));
}
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.tool_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search1);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(fitem.this);
searchView.setQueryHint("Search");
}
#Override
public boolean onQueryTextChange(String s)
{
String userInput=s.toLowerCase();
ArrayList<All_Data> filterlist=new ArrayList<>();
for(int i=0;i<item.size();i++)
{
String st=item.get(i).getName().toLowerCase();
if(st.contains(userInput))
filterlist.add(item.get(i));
}
adapter=new fitemAdapter(getContext(),filterlist);
rv1.setAdapter(adapter);
return true;
}
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
}
Inside your fragment's onCreate()/onCreateView() call setHasOptionsMenu(true);
This will allow your fragment to show the menu items you inflate inside the fragment's onCreateOptionsMenu(); otherwise it uses the host Activity's menu.

Easy share action is not working in WebView Android app

I have been trying almost everything to get this share button to work. The button is placed right but it's not getting clicked and the share window is not showing.
Secondly, I want it to share Web URL of the page when clicked and I am also unable to understand how to do that.
Moreover, I do not want the button to be hidden in the Action settings. I want it to be displayed on the top bar, that's what's my main problem is.
MainActivity.java
package com.yoalfaaz.yoalfaaz;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.support.v4.view.MenuItemCompat;
public class MainActivity extends AppCompatActivity {
private WebView YoWeb;
private ShareActionProvider mShareActionProvider;
SwipeRefreshLayout swipe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb() {
YoWeb = (WebView)findViewById(R.id.webview);
WebSettings webSettings = YoWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
YoWeb.loadUrl("http://www.yoalfaaz.com");
swipe.setRefreshing(true);
YoWeb.setWebViewClient(new WebViewClient() {
//onPageFinished Method
public void onPageFinished(WebView view, String url) {
//Hide the SwipeRefreshLayout
swipe.setRefreshing(false);
}
});
}
#Override
public void onBackPressed() {
if (YoWeb.canGoBack()) {
YoWeb.goBack();
} else {
super.onBackPressed();
}
}
#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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
private Intent createShareIntent() {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
shareIntent.setType("text/plain");
startActivity(shareIntent);
return shareIntent;
}
}
menu_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.yoalfaaz.yoalfaaz.MainActivity">
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
First, the button is not clickable.
Second, how to share the current page URL?
These are the two queries which I am unable to solve.

ShareActionProvider not doing anything

I have been following the Android Development course on Udacity, and got to a lesson where we were to implement a ShareActionProvider. After attempting it myself, and then following what they did--I'm certain it's not working properly.
I'm putting the code in a Fragment; the correct item is present in the overflow menu, but when I click "Share", nothing happens. I noticed when I open the DetailsActivity, that the following appears in the logs:
17493-17493/com.dummy.sunshine.app W/MenuItemCompat﹕ setActionProvider: item does not implement SupportMenuItem; ignoring
I'm not sure what that means, but I couldn't find anything about it online. I would assume that clicking it is supposed to do something--however, they never go over what's supposed to happen after that lesson--so I'm a tad confused.
DetailActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class DetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_detail, 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();
switch (id) {
case R.id.action_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
DetailFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A placeholder fragment containing a simple view.
*/
public class DetailFragment extends Fragment {
private static String mForecastStr;
private final String TAG = DetailActivity.class.getSimpleName();
public DetailFragment() {
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
Intent intent = getActivity().getIntent(); //allows us to get data from the screen that sent us to this activity
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT); //we get the EXTRA_TEXT that the ForecastFragment sent us
((TextView) rootView.findViewById(R.id.detail_text))
.setText(mForecastStr);
}
return rootView;
}
private Intent shareDetailForecast() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, mForecastStr + "#SunshineApp");
return sendIntent;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_detail_fragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = new ShareActionProvider(getActivity());
mShareActionProvider.setShareIntent(shareDetailForecast());
MenuItemCompat.setActionProvider(menuItem, mShareActionProvider);
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareDetailForecast());
} else {
Log.d(TAG, "mShareActionProvider is null...");
}
}
}
menu_detail_fragment.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_share"
android:title="#string/action_share"
android:orderInCategory="5"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
menu_detail.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.nxt3.sunshine.app.DetailActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
This is what the app looks like on the DetailActivity page:
I believe your problem is that on your DetailActivity you are extending a FragmentActivity while you should be extending an AppCompatActivity (which is a subclass of the FragmentActivity), as you are using an action bar on your activity and using the support library.
so you should replace this:
public class DetailActivity extends FragmentActivity {
by this:
public class DetailActivity extends AppCompatActivity {
and import the support library:
import android.support.v7.app.AppCompatActivity;
I don't know if you already have it set, but you also need to set your AppTheme to be Theme.AppCompat or something similar on your AndroidManifest.xml. Something like below:
</manifest>
<application
...
android:theme=""Theme.AppCompat.Light" >
...
</application>
</manifest>
I'm doing the same exercise in the Udacity and that worked for me, I hope it helps.
I assume you want the share view appears beside the toolbar menu!
If yes, here is the right solution for your issue :
DetailActivity.java
package com.example.android.sunshine.app;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new DetailFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_detail, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
DetailFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public static class DetailFragment extends Fragment {
private static final String LOG_TAG = DetailFragment.class.getSimpleName();
private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp";
private String mForecastStr;
public DetailFragment() {
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
// The detail Activity called via intent. Inspect the intent for forecast data.
Intent intent = getActivity().getIntent();
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
((TextView) rootView.findViewById(R.id.detail_text))
.setText(mForecastStr);
}
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_detail_fragment, menu);
// Retrieve the share menu item
MenuItem menuItem = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
ShareActionProvider mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
// Attach an intent to this ShareActionProvider. You can update this at any time,
// like when the user selects a new piece of data they might like to share.
if (mShareActionProvider != null ) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
private Intent createShareForecastIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
mForecastStr + FORECAST_SHARE_HASHTAG);
return shareIntent;
}
}
menu_detail_fragment.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_share"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
menu_detail.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.example.android.sunshine.app.DetailActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

Search view widget and Icon not showing up in the action bar

I am a beginner to Android. Trying to add a search a widget in the action bar.
Here is my menu file code ("main_acitivity.xml"):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
My MainActivity class code is:
package com.example.fragment;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.fragment.MESSAGE";
#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.
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.main_activity,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view){
Intent intent= new Intent(this,DisplayMessageActivity.class);
EditText editText= (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I have tried "android.widget.SearchView" as well but it is not even showing the icon. Could you please advise what I am doing wrong ?
you missed these:
xmlns:yourappname="http://schemas.android.com/apk/res-auto"
and
yourappname:showAsAction="always"
yourappname:actionViewClass="android.support.v7.widget.SearchView"/>

Menu items won't show up in the actiobar

UPDATE:
It works on the tab, but on the phone it's overlayed for some reason
I have a question regarding the menu items android uses for the ActionBar, I have a custom background for my actiobar (a picture) and set it in the code here. That piece of code works fine. But when I try to add a menu item, it fails.
What I want is fairly simple, I want a button that says "save product as favorite" at the bottom of the screen, like the bottom-actionbar. But when I try to add the button as menu item it won't show up, nothing changes.
The log tells me the onCreateOptionsMenu() method is in fact executed. Any ideas?
This is the activity I want the menu for:
package com.x;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.x.R;
import com.x.products.fragments.*;
import com.x.tasks.ZoekQueryTask;
import com.x.util.Helper;
import com.x.util.UnscaledBitmapLoader;
public class ProductActivity extends Activity {
private Bundle save;
/** Called when the activity is first created. */
#SuppressWarnings("rawtypes")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product);
#SuppressWarnings("deprecation")
LocalActivityManager manager = new LocalActivityManager(this, false);
Helper helper = new Helper();
save = null;
String id = (String) getIntent().getExtras().get("id");
String score = (String) getIntent().getExtras().get("score");
String profileId = (String) getIntent().getExtras().get("profileId");
// String id = "138946";
// String profileId = "28";
// String score = "100%";
String query = "SELECT * FROM products WHERE id = "+id;
ArrayList<HashMap> result = null;
try {
ZoekQueryTask zoekQueryTask = new ZoekQueryTask(query);
result = zoekQueryTask.getResults();
}
catch(Exception e) {
Log.e("Afgevangen error", e.getMessage());
}
TabHost tb = (TabHost) findViewById(R.id.tabs);
manager.dispatchCreate(savedInstanceState);
tb.setup(manager);
Intent info = new Intent();
info.setClass(this, InfoFragment.class);
info.putExtra("result", result);
info.putExtra("profileId", profileId);
TabSpec tsInfo = tb.newTabSpec("info");
tsInfo.setContent(info);
tsInfo.setIndicator("info");
Intent specs = new Intent();
specs.setClass(this, SpecsFragment.class);
specs.putExtra("result", result);
TabSpec tsSpecs = tb.newTabSpec("specs");
tsSpecs.setContent(specs);
tsSpecs.setIndicator("specs");
Intent prijs = new Intent();
prijs.setClass(this, PrijsFragment.class);
prijs.putExtra("result", result);
TabSpec tsPrijs = tb.newTabSpec("Prijs");
tsPrijs.setContent(prijs);
tsPrijs.setIndicator("Prijs");
TabSpec tsFotos = tb.newTabSpec("Fotos");
tsFotos.setContent(info);
tsFotos.setIndicator("Fotos");
tb.addTab(tsInfo);
tb.addTab(tsSpecs);
tb.addTab(tsPrijs);
tb.addTab(tsFotos);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
Bitmap bm = UnscaledBitmapLoader.loadFromResource(getResources(), R.drawable.logo, null);
BitmapDrawable background = new BitmapDrawable(this.getResources(), bm);
background.setTileModeX(android.graphics.Shader.TileMode.CLAMP);
actionBar.setBackgroundDrawable(background);
TextView nameText = (TextView) findViewById(R.id.name);
TextView scoreText = (TextView) findViewById(R.id.score);
nameText.setText(result.get(0).get("name").toString());
scoreText.setText(score);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
Log.i("hallo", "hoi");
inflater.inflate(R.layout.product_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.save:
Log.i("menu", "werkt");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
This is the menu XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/save"
android:title="Opslaan in favorieten" >
</item>
</menu>
I think you need to fix this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
Log.i("hallo", "hoi");
inflater.inflate(R.layout.product_menu, menu); // <- this is wrong
return true;
}
Instead of R.layout.product_menu, it should refer to R.menu.your_desired_menu
If you want to show your menu item in the action bar, set the android:showAsAction property of your item in product_menu.xml;
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item1"
android:title="Save"
android:showAsAction="always" />
</menu>
You can set this property to:
ifRoom
never
withText
always
collapseActionView
I hope this helps you!
Steffen

Categories

Resources