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
Related
I have a bottom navigation bar as you can see below:
As you can see, I have a profile icon on the top, and when you press that, the 'Settings' item, at the last of the bottom navigation menu, should open up.
This is my java code:
public void openSettings(View v) {
openFragment(com.Notely.Notes.fragments.NavigationSettings.newInstance("", ""));
}
This is my profile icon code:
<Button
android:id="#+id/button"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="290dp"
android:layout_marginTop="20dp"
android:background="#drawable/profile_btn"
android:onClick="openSettings"
app:layout_constraintStart_toStartOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="#+id/linearLayout" />
In the above java code, when a person presses the profile icon, it should open the fragment Settings, but I want the item Settings also to be activated like the below picture:
Thanks in advance.
I think the best way to do that it is using BottomNavigationView there is a method call setSelectedId, have a look at this question:
Set selected item in Android BottomNavigationView
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.
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" />
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've got a ListActivity and ListView and I've bound some data to it. The data shows up fine, and I've also registered a context menu for the view. When I display the list items as just a simple TextView, it works fine:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
However when I try something a bit more complex, like show the name and a CheckBox, the menu never shows up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/namecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Can long-presses work on more complex elements? I'm building on 2.1.
(edit)
Registering with this on the ListActivity:
registerForContextMenu(getListView());
The code I posted is the item template for the list.
Your CheckBox may be interfering with matters. Consider using a CheckedTextView instead of a LinearLayout, CheckBox, and TextView combination, since CheckedTextView is what Android expects for a CHOICE_MODE_MULTIPLE list.
Check out $ANDROID_HOME/platforms/$VERSION/data/res/layout/simple_list_item_multiple_choice.xml, where $ANDROID_HOME is wherever you installed the SDK and $VERSION is some Android version (e.g., android-2.1). This resource is the standard resource you should use for CHOICE_MODE_MULTIPLE lists. Feel free to copy it into your project and adjust the styling of the CheckedTextView as needed.
set checkbox property
focusable = false;
and run project again..
Found at this place: http://www.anddev.org/view-layout-resource-problems-f27/custom-list-view-row-item-and-context-menu-t52431.html
Setting the checkbox to not be focusable fixes the problem.
Not sure if it would cause issues when navigating the UI with something else than a touchscreen (with a wheel or arrow keys), but it fixed my problem (my layout was a bit more complicated than just a TextView and a Checkbox...)
Context menu's can only be registered to subclasses of View. I don't know how you registered the LinearLayout with a context menu, did you package it in some type of View? if so, you should post that code.
Anyways why not just register the TextView of each list item? Who would long press a checkbox...
This should from a regular ListView as well. But if you're starting from scratch on a new list I would consider using the CheckedTextView:
checkBox.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// return false to let list's context menu show
return false;
}
});