I am currently developing an android app. I want to show a map (based on Google Maps) and with pressing a button an overlay should be shown.
So far the map is shown and the overlay is shown, too. BUT: The problem is that the overlay is shown if I press the button twice although the overlay should be invisible after pressing the button a second time. I tried to swap the view.setVisisiblity(View.VISISBLE) and the view.setVisisiblity(View.INVISIBLE) call but the result was almost the same...I used a TextureView to solve this problem.
public void openWindowOnButtonClick() {
view = (TextureView) findViewById(R.id.textureView);
FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// check if the Overlay should be visible. If this value is true, it is not shown -> show it.
if (view.getVisibility() == View.INVISIBLE) {
view.setVisibility(View.VISIBLE);
view.bringToFront();
keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
} else {
view.setVisibility(View.INVISIBLE);
keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
}
This is my method to swap the overlay's status from visible to invisible and the other way around.
Hope anyone can help.
Thanks!
TextureView will not be available unless initializing SurfaceTexture , i don't know why textureview is available after the first click ,but you can use SurfaceView, or you can use TextureView With setting SurfaceTexture
public void openWindowOnButtonClick() {
view = (TextureView) findViewById(R.id.textureView);
view.setSurfaceTexture(new SurfaceTexture(GLES20.GL_ACTIVE_TEXTURE));
view.getSurfaceTexture().detachFromGLContext();
FloatingActionButton fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
final InputMethodManager keyboard = (InputMethodManager) getSystemService(getBaseContext().INPUT_METHOD_SERVICE);
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// check if the Overlay should be visible. If this value is true, it is not shown -> show it.
if (view.getVisibility() == View.INVISIBLE) {
view.setVisibility(View.VISIBLE);
view.bringToFront();
view.getParent().requestLayout();
((View)view.getParent()).invalidate();
keyboard.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
} else {
view.setVisibility(View.INVISIBLE);
keyboard.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
}
but it will log error in monitor but will work with you
Related
I have button with setOnClickListener function, it works well, new view/form/window is opened and I can do some changes on this. But when I click somewhere around the view then my view is closing. How to detect that I clicked outside of view?
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final DeviceItem changedDeviceItemCopy = changedDeviceItem.clone(); // copy of changedDeviceItem before authorized numbers
if (changedDeviceItem.getAuthorized_numbers() != null) {
authorized_numbers_array_list = changedDeviceItem.returnAuthorizedNumbersAsList();
}
...
}
I expect that I can detect moment when I clicked outside of view.
I have a fragment with a button that sets a background theme for the whole app. I have set up an interface so the fragment can tell the main activity to set the background or remove it depending on what button the user clicks.
The problem is that every time the app is opened the background isn't saved and needs to be toggled again. I have seen that this can be solved with SharedPreferences but implementing it here is confusing me
In my fragment This presents two buttons that send the values 1 or 2 to the main activity to toggle the background
enable = (Button) rootView.findViewById(R.id.enable);
enable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.themechanged(2);
enable.setVisibility(View.GONE);
disable.setVisibility(View.VISIBLE);
}
});
disable = (Button) rootView.findViewById(R.id.disable);
disable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.themechanged(1);
disable.setVisibility(View.GONE);
enable.setVisibility(View.VISIBLE);
}
});
In my Main Activity This takes the value from the listener and toggles the background depending on what the value is
#Override
public void themechanged(int value) {
if(value==2) {
if (isDarkTheme) {
appbackground.setVisibility(View.GONE);
shade.setVisibility(View.GONE);
} else {
appbackground.setVisibility(View.VISIBLE);
shade.setVisibility(View.VISIBLE);
}
}else if(value!=2||value==1){
appbackground.setVisibility(View.GONE);
shade.setVisibility(View.GONE);
}
}
Use SharedPrefence to store the value for theme like-:
Global Variable
SharedPreferences pref;
SharedPreferences.Editor editor;
In OnCreateView()
pref = getActivity().getSharedPreferences("Theme", Context.MODE_PRIVATE);
editor = pref.edit();
Now, store preferences on Button click
enable = (Button) rootView.findViewById(R.id.enable);
enable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editor.putInt("yourTheme", 2);
editor.commit();
listener.themechanged(2);
enable.setVisibility(View.GONE);
disable.setVisibility(View.VISIBLE);
}
});
disable = (Button) rootView.findViewById(R.id.disable);
disable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editor.putInt("yourTheme", 1);
editor.commit();
listener.themechanged(1);
disable.setVisibility(View.GONE);
enable.setVisibility(View.VISIBLE);
}
});
and then, In OnCreate() of MainActivity you can check like
SharedPreferences pref = getSharedPreferences("Theme", MODE_PRIVATE);
value= pref.getInt("yourTheme", 1);//1 is default value
if(value==2) {
if (isDarkTheme) {
appbackground.setVisibility(View.GONE);
shade.setVisibility(View.GONE);
} else {
appbackground.setVisibility(View.VISIBLE);
shade.setVisibility(View.VISIBLE);
}
}else if(value==1){
appbackground.setVisibility(View.GONE);
shade.setVisibility(View.GONE);
}
Done, it may be helpful
In the onClick() you should do 2 things:
Sent the value to the listener (you're already doing this)
Save this value to the preferences (already posted how to do that here)
Then, in the onCreate() of your MainActivity you should check for that preference and do the same you are doing on themechanged(int)
Actually, you could use only one onClickListener(), this way:
// Not need to cast to `Button`, since all views can have an onClickListener
rootView.findViewById(R.id.enable).setOnClickListener(clickListener)
rootView.findViewById(R.id.enable).setOnClickListener(clickListener)
// Put this as a member of your Fragment class.
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == R.id.enable) {
// Save your preference here
// ...
listener.themechanged(2);
enable.setVisibility(View.GONE);
disable.setVisibility(View.VISIBLE);
}
if (v.getId() == R.id.R.id.disable) {
// Save your preference here
// ...
listener.themechanged(2);
disable.setVisibility(View.GONE);
enable.setVisibility(View.VISIBLE);
}
}
}
Let me share this more complex example which can cover this and future needs: https://gist.github.com/walterpalladino/4f5509cbc8fc3ecf1497f05e37675111
The PersistenceManager class is generic, all your app data should be included in the Settings class.
I hope it helps.
I'm developping a camera app on android studio using openCv 3.0.0.It's my first time doing this and I'm facing some problems. But I have 2 issues:
1) I want to add a button to switch between the front camera and the back camera. But I can't seem to find a way to switch.
Here is my onCreate method:
private Camera mCamera;
private CameraPreview mPreview;
private static int number = Camera.CameraInfo.CAMERA_FACING_FRONT;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.face_detect_surface_view);
// Create an instance of Camera
mCamera = getCameraInstance(number);// This funtion opens the camera
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, number , mCamera);
final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
// Add a listener to the Capture button
ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Image captured!", Toast.LENGTH_SHORT).show();
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);
// Add a listener to the Change button
ImageButton changeButton = (ImageButton) findViewById(R.id.button_change);
changeButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (number == Camera.CameraInfo.CAMERA_FACING_FRONT)
number = Camera.CameraInfo.CAMERA_FACING_BACK;
else
number = Camera.CameraInfo.CAMERA_FACING_FRONT;
//HERE SHOULD BE THE STEPS TO SWITCH
Toast.makeText(getApplicationContext(), "Camera changed!", Toast.LENGTH_SHORT).show();
// get an image from the camera
}
}
);
}
2)I want to use openCv for face detection on the image that's captures but I don't know whether it's possible. I couldn't find anything on the web. I've already tried the example faceDetect from openCv 3.0.0 and it worked when I was using the camera. That's what I wanted to do at first but after I changed my layout to contain a frame layout instead of org.opencv.android.JavaCameraView it doesn't work anymore. So if anyone has an idea why I'd be very grateful.
All *CameraView classes have disableView and enableView methods. You need to disable view, set mCameraIndex field of View object and enable view again. mCameraView is protected method, so the only solution is to implement view subclass and setters/getters. See tutorial-3 example for more details.
<org.opencv.android.NativeCameraView
android:id="#+id/tutorial1_activity_native_surface_view"
android:layout_width="350px"
android:layout_height="350px"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
opencv:camera_id="front" />
How can I have an on and off state for an Image Button? My goal is to have sound play when the image button is clicked, and for sound to stop when the button is clicked again. Thank you!
you can use event's. like on click listener.
get your imageView and setOnClickListener for this.
ImageView mImageView = (ImageView) findViewById(R.id.sound_imageView);
Boolean flag = false;
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(flag) {
//play sound
flag = false;
} else {
//stop sound
flag = true;
}
}
});
There are a few Views that exist in Android that provide toggle functionality like you are looking for. You might want to research the android.widget.CompoundButton class for ideas, or reference this tutorial.
You can do it manually. First when you will click the button you will check whether the music is running or not. If it is running then stop it , if not running then play it.
Something like that -
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(musicPlayer!=null && musicPlayer.isPlaying()){
musicPlayer.stop();
}else{
musicPlayer=new MediaPlayer();
AssetFileDescriptor afd = getActivity().getAssets().openFd("AudioFile.mp3");
musicPlayer.setDataSource(afd.getFileDescriptor());
musicPlayer.prepare();
musicPlayer.start();
}
}
});
I can't figure out how I would go about implementing an up button in a PreferenceScreen. An up button displays a caret in your action bar next to your app icon that allows you to navigate the app's hierarchy, more info here.
I have a Preference Fragment that displays when my main activity is opened and I can get the up button to display by adding this line " getActionBar().setDisplayHomeAsUpEnabled(true);":
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
This causes the up button to display in the Preference Fragment, but I only want to show the up button when one of my PreferenceScreens is opened, allowing navigation to the main PreferenceFragment.
My app is analogous to the main settings app. Only the child screens, like Location Access, that opens from the main Settings app has the up arrow.
From this question, I simply added these two code blocks to my Preference Fragment:
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
// If the user has clicked on a preference screen, set up the action bar
if (preference instanceof PreferenceScreen) {
initializeActionBar((PreferenceScreen) preference);
}
return false;
}
And this one:
/** Sets up the action bar for an {#link PreferenceScreen} */
public static void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
// Inialize the action bar
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
// Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
// events instead of passing to the activity
// Related Issue: https://code.google.com/p/android/issues/detail?id=4611
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
OnClickListener dismissDialogClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
};
// Prepare yourselves for some hacky programming
ViewParent homeBtnContainer = homeBtn.getParent();
// The home button is an ImageView inside a FrameLayout
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
// This view also contains the title text, set the whole view as clickable
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
// Just set it on the home button
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
// The 'If all else fails' default case
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
}
If your complete application is a preferences screen, then you can make your main activity a PreferenceActivity and the sub-levels can be fragments. This way the 'up' functionality is going to be by default what you are looking for.
Have a look at this.
For the XML:
<Preference android:title="Acts like a button"
android:key="button"
android:summary="This will act like a button"/>
Then for the Java in your onCreate()
Preference button = (Preference)findPreference("button");
button.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
//code for what you want it to do
return true;
}
});