Code is here; https://github.com/googlesamples/android-play-location/blob/master/LocationUpdates/app/src/main/java/com/google/android/gms/location/sample/locationupdates/MainActivity.java
Hello, I'm testing out this code, but at startUpdatesButtonHandler() and stopUpdatesButtonHandler() I get the message that these are never used. Where should I use these, have I forgotten something? I used the code linked above.
The lat lon are not being updated, is this because of the start and stop button not being used? I have to refresh the entire activity for it to be updated.
// Locate the UI widgets.
mStartUpdatesButton = (Button) findViewById(R.id.start_updates_button);
mStopUpdatesButton = (Button) findViewById(R.id.stop_updates_button);
You need to set onClickListener on those two buttons.
Check the main_activity.xml file, it could be that onClick is already set there. However it says in the comments, and you can see it in the code that startUpdatesButtonHandler(View view) will do nothing if location changes is already been made.
Just adding the accepted comment as an answer. You need to add set them in your XML.
e.g.
<Button
...
android:onClick="startUpdatesButtonHandler"
android:text="#string/start_update" />
<Button
...
android:onClick="stopUpdatesButtonHandler"
android:text="#string/stop_update" />
Related
I implemented the interface View.OnclickListener and Override the method onClick.
Problem is, all other buttons and view response on click, but a particular button doesn't work.
Java code for that button is:
if(v.getId() == R.id.btnResetPass){
Log.i("test", "In ResetButton");
resetPassword();
}
and xml file code for this button is:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnResetPass"
android:text="Reset"/>
Nothing happens when I click on this button. But all other Views and Buttons in this onClick method
work properly accept this one. Why isn't it working then?
Have you set the Listener
button.setOnClickListener(this);
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.
I am developing an Fitness App. My app will have different exercises like Push Ups and Sit Ups. Every exercise need to have an image to show to the user.
I have been thinking a while on what is a good way to solve this problem. But I don't think my solution below is good. Have you worked with images on Android for displaying images? Give me your solution on how you did it.
An exercise have a name but also images. The purpose is to display a specific Exercise with the images and exercise name.
My Exercise class looks like this right now:
I have thought of having the path to the image saved which I can have access when I need to show the image. I am uploading the images on the assets folder.
public class Exercise {
private String exerciseName;
private String exerciseSmallImagePath;
private String exerciseLargeImagePath;
public Exercise(String exerciseName, String exerciseSmallImagePath, String exerciseLargeImagePath){
this.exerciseName = exerciseName;
this.exerciseSmallImagePath = exerciseSmallImagePath;
this.exerciseLargeImagePath = exerciseLargeImagePath;
}
}
saving the path to the image-source is definitly a good approach. Have a look at ImageViews in order to display your image. There are two approaches to implement such an ImageView:
1: define it in your XML and set the image-source afterwards in your oncreate-method:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Some description" />
2: define your ImageView programmatically in your Activity:
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(fetch your ID here);
someLinearLayout.addView(imageView);
I suppose you are familiar with how to create simple application in Android? If not, then you should get started with samples and reading up on Android developers guide
This article can give you good start in how to work with Images.
As for how you should accomplish this, I can only suggest one approach, the rest is upto your imagination.
Start with creating a fragment that has an image and a text below (or above if you like) it. You can then put this fragment where you want with new image and text.
Here is a rough idea
<LinearLayout (vertical orientation>
<Image ... />
<Text ... />
</LinearLayout>
Once the layout is in place you can set the image source at runtime.
A good way to display this information is to have steps which user can navigate using swiping (view pager).
Every page can have the above mentioned fragment that will show one step. This will result in cleaner, slide screen style guide.
I have this problem accessing TextView inside Relative Layout inside LinearLayout
Let's say I have these views in the file row.xml
<LinearLayout>
<TextView android:id="#+id/title" />
<RelativeLayout android:id="#+id/a_parent">
<TextView android:id="#+id/a">
</RelativeLayout>
<RelativeLayout android:id="#+id/b_parent">
<TextView android:id="#+id/b">
</RelativeLayout>
</LinearLayout>
Now I want to change the value of TextViews from an activity
LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout temp=(LinearLayout) li.inflate(R.layout.row, null);
((TextView)temp.findViewById(R.id.title)).setText("This is title");
RelativeLayout rl=(RelativeLayout)temp.findViewById(R.id.a_parent);
((TextView)rl.findViewById(R.id.a)).setText("an this is the content of a");
I can successfully set the TextView of id 'title', but an error appears when approaching the last line. It says that the error was caused by android.content.res.Resources$NotFoundException: String resource ID #0x1
Can anyone tell me what is wrong, how to fix it, and why does the code not work as I expected?
Thanks
Your Exception is a ResourcesNotFoundException for a String. Are you setting the value of the TextView using getString? And if so, are you sure that this string exists in your strings.xml file? If not, did you maybe reference one of your views with R.string.name instead of R.id.name? It looks like you modified your code for posting, it may be easier to help if you posted exactly what you're doing in the original, as well as the full stack trace.
Sometimes problems with Resources occur because the R file isn't up to date with new code. I would also try Rebuilding/Cleaning your project and see if that helps.
Usually this error comes when you are trying to set the integer value in Textview
like below :
textview.setText(1);
In this case the android will think that the integer value as string resource id and checks for
the string the strings.xml and throws Resources$NotFoundException.
Please check your code. If not post your necessary code and logcat output.
LinearLayout temp=(LinearLayout) li.inflate(R.layout.row....
in Your XML file, LinearLayout has not "android:id" property is declared.
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