I am trying to implement the example shown on this page. I have tried on three different devices running android 4 and above, and in all cases I get a black screen with this warning:
01-27 20:01:22.683: W/TextureView(4728): A TextureView or a subclass can only be used with hardware acceleration enabled.
I have turned on hardware acceleration in the application manifest:
<application
android:hardwareAccelerated="true"
[etc...]
But the following check my custom view's onAttachedToWindow method always returns false
private class MyTextureView extends TextureView
{
public MyTextureView(Context context) {
super(context);
}
#Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
Log.d("", Boolean.toString(mTextureView.isHardwareAccelerated()));
}
}
Does anyone know what is wrong here?
Thanks
If you testing with Emulator then please check configuration for hardware acceleration.
Please check this thread for more info:
http://developer.android.com/tools/devices/emulator.html#acceleration
Related
I am trying to detect if an orientation change has happened to the phone using OrientationEventListener, but the code does not work as expected. Here are part of my code relevant to this question:
public class MainActivity extends Activity {
OrientationEventListener myOrientationEventListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
myOrientationEventListener
= new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
#Override
public void onOrientationChanged(int arg0) {
DO SOMETHING;
}
};
if (myOrientationEventListener.canDetectOrientation()) {
myOrientationEventListener.enable();
}
If I put a breakpoint in debug mode at line DO SOMETHING and without moving the phone look at the value of arg0, I see it changes to 8 and then 9 and then 8, etc. each time I enter the onOrientationChanged function.
My question is: why it enters the onOrientationChanged function, although the phone is placed on a desk and not moving?. And the second question is why the value of arg0 is changing between 8, and 9?
By the way I have forced my main layout to be portrait using android:screenOrientation="portrait" in the AndroidManifest.xml.
Is there anything in the code that I am missing or is not correct?
Thanks in advance for any help on this.
This is not an answer, but I do not have enough reputation to add this as a comment:
I have a smilar situation. I have an App that is fixed to portrait mode and I am using OrientationEventListener&&onOrientationChanged to monitor device rotation. In other words my setup is excatly same as TJ1.
What I have observed with my testing then above code is working fine on some devices, but then I have a device (Nokia6, api27) where the code is not working. The "onOrientationChanged"-fn is not called at all.
(As a bit offtopic, then I have asked a question that is related to this topic:
OrientationEventListener vs Rotation Vector Sensor to get “orientation”
)
I know that there are a lot of questions about capturing screenshots, and I have checked most of them. They have the same answer (with small code variations).
I have following method for screenshot capturing:
#NonNull
public static Bitmap takeScreenShot(Window window) throws IOException {
final View rootView = window.getDecorView().getRootView();
final boolean drawingCacheEnabled = rootView.isDrawingCacheEnabled();
rootView.setDrawingCacheEnabled(true);
try {
return Bitmap.createBitmap(rootView.getDrawingCache());
} finally {
rootView.setDrawingCacheEnabled(drawingCacheEnabled);
}
}
And you can use it like these: takeScreenShot(getActivity().getWindow())
However these approach has several limitations:
If you have some dialogs on the screen they will not be captured on
screenshot.
Will it work with hardware accelerated views? According
to documentation:
When hardware acceleration is turned on, enabling the
drawing cache has no effect on rendering because the system uses a
different mechanism for acceleration which ignores the flag
Screenshot contains black boxes
instead of GLviews. (e.g. when you app has maps.). It seems to be as a result of 2nd point.
So my question is, is there any solution without rooting that can solve at least some of my issues?
Check out the following GitHub repo (not mine!): https://github.com/AndroidDeveloperLB/ScreenshotSample
Also, the following will be useful reading:
How to properly take a screenshot, globally?
I'm developing a watch face on Android.
In onCreate method when I set watch face style, I see that a bunch of methods are deprecated, but on Android official website they are not. What should I do to get rid of these deprecated methods, or leave them as they are?
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
setWatchFaceStyle(new WatchFaceStyle.Builder(DigitalWatchFace.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.setAcceptsTapEvents(true)
.build());
// ...
}
Those methods are deprecated because of this changes in android wear 2.
The notifications will be shown for few seconds and then the watch face will be visible again. For this reason those methods don't have sense anymore.
I am creating a bunch of layout files and would like to preview roughly what they will look like on the Android. Is there a way to do this? and if so, what would be the best way?
Right now, I have to boot up the device and navigate to the Activity every time in order to see the layout. Also, it looks like the preview of Eclipse is not ideal as it doesn't display things correctly (is this the best it gets?)
You can create a Dummy Activity, and set the intent MAIN to it.
When you want to test how that activity looks like, just setLayout on onCreate to the xml that you want to test and you will see it.
I also don't trust in Eclipse preview, and AVDs are really slow, so I attach an android device and use a dummy activity to test layouts
You can use the utility at http://www.droiddraw.org/.
Simply copy and paste your layout xml at the Output window, and click "Load". This works both ways.
Besides running on a real device or in the emulator, Eclipse's preview is the best it gets. Make sure you set the right options for screen size and API version in the preview screen; that sometimes helps.
You can create an Activity like this:
//add the imports
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Ask for a full screen window
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
/* (non-Javadoc)
* #see android.app.Activity#onStart()
*/
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
// Get the info about the screen
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Change the activity_main layout and you can test any layout clicking in Graphical Layout of your xml.
If you don't trust in Eclipse-preview and you want to avoid the slow Android emulator, try to enable the Virtualization (Intel processors). You must install the packages Intel x86 Emulator Accelerator (HAXM) and Intel x86 System-Image. Next, you can create an AVD using that System-Image and the emulator will speed up.
Otherwise, if you haven't got a capable Virtualization processor, I'm sorry for you, but you must trust in Eclipse preview ;)
Bye!
Add as parameter of ListView in xml file following line:
tools:listitem="#layout/my_custom_list_item"
where my_custom_list_item is layout you want to use as list item.
Is there a way to detect if the device I'm currently running on has a hardware keyboard installed?
How do I query device capabilities anyway?
[android-developers] Re: Detect Physical Keyboard
Layout (ex: QWERTY vs QWERTZ)
The flags provided by getResources().getConfiguration().keyboard are a
good way of checking which keyboard (if any) is available.
Use the following method to ascertain presence of hard keyboard at any time:
(To my knowledge, soft keyboards all lack the features tested below )
public static boolean isHardKB(Context ctx) {
Configuration cf = ctx.getResources().getConfiguration();
return cf.navigation==Configuration.NAVIGATION_DPAD
|| cf.navigation==Configuration.NAVIGATION_TRACKBALL
|| cf.navigation==Configuration.NAVIGATION_WHEEL;
}
Optionally trap all run-time keyboard changes for each affected Activity via AndroidManifest:
android:configChanges="keyboard|keyboardHidden|navigation"
But be sure to support the above manifest change with (at least) a dummy onConfigurationChanged()
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Optionally employ 'isHardKB()'
}
To detect common qwerty keyboard connected use this:
private boolean isKeyboardConnected() {
return getResources().getConfiguration().keyboard == KEYBOARD_QWERTY;
}