Share Button Not Working On App
Solution 1:
You are using AppCompatActivity
. This means that you have a variety of bugs.
First, you need to be using android.support.v7.widget.ShareActionProvider
, not android.widget.ShareActionProvider
.
Second, android:showAsAction
and android:actionProviderClass
need to be switched to app:showAsAction
and app:actionProviderClass
, as shown in the documentation for android.support.v7.widget.ShareActionProvider
. You will also need to add xmlns:app="http://schemas.android.com/apk/res-auto"
to your root <menu>
element.
Third, use MenuItemCompat
to get the ShareActionProvider
in your code, after changing the import to be android.support.v7.widget.ShareActionProvider
.
This sample project demonstrates using appcompat-v7
and its edition of ShareActionProvider
. My menu resource is:
<item
android:id="@+id/share"
android:title="Share!"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
and the Java code uses MenuItemCompat
:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.sap;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
publicclassMainActivityextendsAppCompatActivityimplementsShareActionProvider.OnShareTargetSelectedListener, TextWatcher {
privateShareActionProvider share=null;
privateIntent shareIntent=newIntent(Intent.ACTION_SEND);
privateEditText editor=null;
@OverridepublicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
shareIntent.setType("text/plain");
editor=(EditText)findViewById(R.id.editor);
editor.addTextChangedListener(this);
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actions, menu);
MenuItem item=menu.findItem(R.id.share);
share=(ShareActionProvider)MenuItemCompat.getActionProvider(item);
share.setOnShareTargetSelectedListener(this);
return(super.onCreateOptionsMenu(menu));
}
@OverridepublicbooleanonShareTargetSelected(ShareActionProvider source,
Intent intent) {
Toast.makeText(this, intent.getComponent().toString(),
Toast.LENGTH_LONG).show();
return(false);
}
@OverridepublicvoidafterTextChanged(Editable s) {
shareIntent.putExtra(Intent.EXTRA_TEXT, s.toString());
share.setShareIntent(shareIntent);
}
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
int after) {
// ignored
}
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before,
int count) {
// ignored
}
}
</menu>
Post a Comment for "Share Button Not Working On App"