Android Google +1 Button Issues - java

I have implemented the Google+ one button as outlined here https://developers.google.com/+/mobile/android/recommend.
However clicking the +1 button does not increment the counter and sharing shares an empty post. I have no idea why this is happening, I have tried various solutions.
Here's my code for implementing the button:
private static final int PLUS_ONE_REQUEST_CODE = 0;
//In onCreate():
mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
#Override
protected void onResume() {
super.onResume();
// Refresh the state of the +1 button each time the activity receives focus.
mPlusOneButton.initialize(url, PLUS_ONE_REQUEST_CODE);
}
Here's my XML for the button:
<com.google.android.gms.plus.PlusOneButton
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="#+id/plus_one_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
plus:size="standard"
plus:annotation="inline" />

Reason of this issue was
android:launchMode="singleInstance"
in Manifest for current activity.
Could you please share your code maybe I can help.
In my case I change code to
android:launchMode="singleTask"
Regards

Related

App keeps crashing after button click

I'm making a simple app and when I press a button that has an onClick() it crashes. I'm trying to open another activity on the button click.
Here's my onClick method from MainActivity.java:
public void gameActivity(View view){
Intent intent = new Intent(MainActivity.this, GameActivity.class);
MainActivity.this.startActivity(intent);
}
Here's my Button code from activity_main.xml:
<Button
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f01c00"
android:onClick="gameActivity"
android:padding="16dp"
android:text="Play"
android:textColor="#FFF" />
Let me know what you think's the problem. Thanks
Well, I tried different things like moving around the startActivity(), but nothing happened. I deleted the activity I wanted to start and made a new one. That worked. I don't know what happened, but it's solved.
GameActivity must be registered in the AndroidManifest.

Receive data from check boxes in different Activity

I have several checkboxes in my Activity_Main.XML similar to as follows
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Video"
android:id="#+id/Video"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/VideoCheck"
android:onClick="onCheckboxClicked"/>
Now on a different activity I want the state of this checkbox to be displayed, for ease of use I have set up the code so it will change the text or a text label. Code in my MenuActivity.java is as follows
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
switch (view.getId())
{
case R.id.VideoCheck:
if (checked) {
TextView MyTextLabel = (TextView)findViewById(R.id.Test1);
MyTextLabel.setText("Video");
}
else
{
TextView MyTextLabel = (TextView)findViewById(R.id.Test1);
MyTextLabel.setText("No Video");
}
break;
}
}
Worth saying the text label on the XML display is called MyTextLabel. I think the problem is because the Checkboxes are set up to call "onCheckboxClicked" but that checkbox clicked part is not in the Activity native to that set up.
Essentially the first page(activity_main.xml, MainActivity.Java) of my app has the checkboxes, then a button takes the user to the second page (activity_menu.xml, MenuActivity.java) has the Label set to change depending on the state of the checkboxes on the first page.
I appreciate this is explained horrendously but please ask any question you may have.
You need to send the state of the checkbox in the Intent you use to start the second activity. See Build an Intent for details. Then in the second activity, you need to extract the checkbox state from the received intent. See Receive an Intent.
Note that I am assuming that you already know how to create an Intent to start an Activity. If not, you should read Starting Another Activity.

Make a button change value of a textview

So, my friend got me to mess with android programming in android studio today. I started making a beginner application and have had some good results. But right now I am trying to get a button to change the value of a textview.
Here is the code for the button and the textview
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one"
android:id="#+id/button3"
android:layout_alignBottom="#+id/button2"
android:layout_alignRight="#+id/textView"
android:layout_toRightOf="#+id/button"
android:onClick="addone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/counter"
android:textSize="40dp"
android:layout_alignTop="#+id/button3"
android:layout_centerHorizontal="true"/>
and here is the code that is attempting to change the value of the text view.
public void addone(){
numtest+=1;
TextView t = (TextView) findViewById(R.id.counter);
t.setText(numtest);
}
THe program compiles and opens, but it crashes whenever i press the button. I believe I have narrowed down the culprit to this line, but im not sure why it isnt working
TextView t = (TextView) findViewById(R.id.counter);
Any ideas?
An overloaded method is your problem. It is subtle, a mistake even a seasoned programmer could make. I've made it myself before.
You are calling TextView.setText(Integer). This attempts to load a string having a resource id of numtest, which does not exist.
Try this instead:
public void addone(View v){
numtest+=1;
TextView t = (TextView) findViewById(R.id.counter);
t.setText(numtest+"");
}
This will call TextView.setText(CharSequence) instead, which is what you're really trying to do.
Also, as Osmium USA demonstrated, your method signature is incorrect. The button pressed callback method must accept a view, and must be public.
When you give a layout element a function to execute, it looks for that name accepting a View (so you know what was clicked if you assign multiple elements the same click behavior in the XML).
So addone() must be addone(View v)
From the Button Docs
In order for this to work, the method must be public and accept a View as its only parameter
William Morrison also has a good point. You should make that number a String either by what he did or use the Integer Class:
t.setText(Integer(numtest).toString());
or
t.setText(numtest+"");
this should work:
public void addone(View view){
switch(view.getId())
{
case R.id.button3:
numtest+=1;
TextView t = (TextView) findViewById(R.id.counter);
t.setText(Integer.toString(numtest));
break;
}
}

Trying to call an activity with a button onClick

I've been racking my brain for the past two days trying to get a button to work.
Basically I want my button to call 'add a password function'.
If you like to view my entire code, its at github.com/servingbaby/UPM-Epassafe .
Here is my main.xml (The button shows up succesfully however when pushing it nothing happens)
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:gravity="center"
android:text="#string/no_accounts" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center"
android:text="#string/add_account" android:id="#+id/add" android:minWidth="125dip"></Button>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Here is my AndroidManifest slimmed down to the parts I believe are where I am going wrong.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.epassafe.upm"
android:versionCode="3"
android:versionName="1.2">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="16"/>
<application android:icon="#drawable/upm"
....>
<activity android:name=".AppEntryActivity">
</activity>
<activity android:name=".FullAccountList">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResults" />
</activity>
<activity android:name=".AddEditAccount">
</activity>
</application>
</manifest>
Here is my MainActivity.Java, where I believe I should be calling the correct code.
public abstract class MainActivity extends Activity implements OnClickListener{`
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button countButton = (Button) findViewById(R.id.add);
countButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
MainActivity.this.startActivity(intent);
}
});
}
}
But pushing the button nothing happens like I expect. No errors or force close, so Im really at a loss at the moment. Thankyou!
**Edit
Thank you all how attempt to help me. I have tried many of the suggested solutions and still haven't figured it out just yet. I have uploaded my files to github with some of the attempted edits I have made as commented. Basically I am just trying to figure out how to add a button and get it to run the necessary code to add an account. (The original code works well, but I would like to improve upon it. Sorry my competence level is still low, but trying to learn my best :) I would really appreciate if anyone could help me with this! Thank you^^
**Edit 2
Again Thankyou all for the advice and suggestions. I figured out a simple solution. I ended up using a Menu Wrapper to create a button click event, and also it turned out I was editing the wrong Java document, when I was suppose to be trying to add the correct code to another one that seem to almost be doing the same thing. Learn something new everyday.
I don't understand why you're implementing OnClickListener to your activity. Remove the implements OnClickListener from your activity. It should work fine.
But since you're doing this for don't-know-what reason, you should make your button clickable like this.
countButton.setOnClickListener(this);
public void onClick(View view)
{
Intent intent = new Intent(MainActivity.this,AddEditAccount.class);
MainActivity.this.startActivity(intent);
}
First of all you dont need to implement OnClickListener to set a a listner in your button. But maybe you have it for another reason, if not, remove it.
You are declaring wrong the names of the id of your xml i think, take a look at this:
Difference between "#id/" and "#+id/" in Android
May be because of the #android:id instead of #+id.
use this way call Intent
Intent intent = new Intent(MainActivity.this, AddEditAccount.class);
startActivity(intent);
look at your list view
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
you have given width and height as fill parent so your ListView is on your button so on the screen when you click on the button you are not actually clicking on the button. when you click a button the button color changes for a sec that is not happening in your case see it for your self. so remove the listview or set the height below the button and then check your button click will work

how to do haptic feedback when an ImageView is not working

I created an Activity displays an ImageView on the screen. I want get haptic feedback when the image is clicked.
In the main layout main.xml I added the next ImageView tag:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:src="#drawable/dog"
android:onClick="doBark"
android:hapticFeedbackEnabled="true"/>
Then, in the Activity code I add this method:
public void doBark(View v) {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
Log.d("BarkingDog", "is hapticFeedbackEnabled: " + v.isHapticFeedbackEnabled());
}
When I click on the image I can see that doBark() is called and the output of the Logcat says "is hapticFeedbackEnabled: true", but I can't feel anything. I've also tried with the other two HapticFeedback constants, and no luck.
I know that HapticFeedback is enabled because each time I press the menu button, the device vibrates.
Any ideas? Suggestions?
PS: I don't want to use the Vibrator object. By using it, I can make the device vibrate, but I don't think it's the right way to do it.
Take a look at this: http://groups.google.com/group/android-developers/browse_thread/thread/de588e3d15cb9055?pli=1
Do note that it is old though, but the last time I had to use haptic feedback, I followed what Dianne had to say here

Categories