Java wear os Scroll WearableRecyclerView by digital crown - wear-os

My list is not scrolling when I use digital crown
This is what I do ;
<androidx.wear.widget.WearableRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrv_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wrv_demo);
WearableRecyclerView wrv = findViewById(R.id.wrv_container);
wrv.setLayoutManager(new WearableLinearLayoutManager(this));
wrv.setAdapter(new DemoAdapter());
wrv.setHasFixedSize(true);
wrv.setCircularScrollingGestureEnabled(true);
wrv.setEdgeItemsCenteringEnabled(true);
wrv.setOnGenericMotionListener(new View.OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
return false;
}
});
wrv.setBezelFraction(0.5f);
wrv.setScrollDegreesPerScreen(90);
}
private static class ViewHolder extends RecyclerView.ViewHolder {
TextView mView;
ViewHolder(TextView itemView) {
super(itemView);
mView = itemView;
}
}
private static class DemoAdapter extends WearableRecyclerView.Adapter<ViewHolder> {
private static final int ITEM_COUNT = 100;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView view = new TextView(parent.getContext());
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mView.setText("Holder at position " + position);
holder.mView.setTag(position);
}
#Override
public int getItemCount() {
return ITEM_COUNT;
}
}
}

short answer:
wrv.requestFocus();
long answer:
this page outlines how to capture rotary input.
https://developer.android.com/training/wearables/ui/rotary-input
I found that my WearOS app's recyclerview worked automatically in the emulator without any of what it said on that page; however, it did not work on my watch. I implemented every step on that page and then the watch worked. Then I removed every thing on that page. For me, the only thing needed was the recyclerView.requestFocus() line.

Related

simplest way to show native ads appodeal

Anybody can show here how to simplest way to show native ads appodeal?
NativeAdViewAppWall = before content webview and NativeAdViewContentStream = after content webview.
Thank you.
simple way to show native ad :
xml file :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70sp"
android:id="#+id/native_holder"
></RelativeLayout>
in onCreate :
private List<NativeAd> nativeAds = new ArrayList<>();
Appodeal.setTesting(true);
Appodeal.setAutoCache(Appodeal.NATIVE, false);
Appodeal.initialize(this, "apikey", Appodeal.NATIVE , true);
setNaitivAD();
and use this method :
private void setNaitivAD(){
Appodeal.cache(this, Appodeal.NATIVE);
Appodeal.setNativeCallbacks(new NativeCallbacks() {
#Override
public void onNativeLoaded() {
Toast.makeText(MainActivity.this, "onNativeLoaded!", Toast.LENGTH_SHORT).show();
nativeAds = Appodeal.getNativeAds(1);
RelativeLayout holder = (RelativeLayout) findViewById(R.id.native_holder);
NativeAdViewAppWall nativeAdView = new NativeAdViewAppWall(MainActivity.this, nativeAds.get(0));
holder.addView(nativeAdView);
}
#Override
public void onNativeFailedToLoad() {
Toast.makeText(MainActivity.this, "onNativeFailedToLoad", Toast.LENGTH_SHORT).show();
}
#Override
public void onNativeShown(NativeAd nativeAd) {
Toast.makeText(MainActivity.this, "onNativeShown", Toast.LENGTH_SHORT).show();
}
#Override
public void onNativeShowFailed(NativeAd nativeAd) {
}
#Override
public void onNativeClicked(NativeAd nativeAd) {
Toast.makeText(MainActivity.this, "onNativeClicked", Toast.LENGTH_SHORT).show();
}
#Override
public void onNativeExpired() {
}
});
}

how to set cub out effect in viewpager with different fragmaents / layouts

my viewpager is different (with multy xml and java file.means page1 java and xml file,page2 java and xml file,page3 java and xml files are seprate).
and want to sate cubeout effect in my viewpager.
**this is my coad **
public class MercuryActivity extends FragmentActivity implements OnPageListener {
List fragments = new Vector();
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.fragments.add(Fragment.instantiate(this, Page1Fragment.class.getName()));
this.fragments.add(Fragment.instantiate(this, Page2Fragment.class.getName()));
this.fragments.add(Fragment.instantiate(this, Page3Fragment.class.getName()));
this.fragments.add(Fragment.instantiate(this, Page4Fragment.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), this.fragments);
this.mPager = (ViewPager) super.findViewById(R.id.pager);
this.mPager.setAdapter(this.mPagerAdapter);
}
public void onPage1(String s) {
((Page2Fragment) this.fragments.get(1)).getView();
((Page3Fragment) this.fragments.get(2)).getView();
((Page4Fragment) this.fragments.get(3)).getView();
}
public void onBackPressed() {
new Builder(this).setIcon(17301543).setTitle("Exit").setMessage("Are you sure you want to exit?").setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MercuryActivity.this.moveTaskToBack(true);
MercuryActivity.this.finish();
}
}).setNegativeButton("No", null).show();
}
}
and this is my page adapter
public class PagerAdapter extends FragmentPagerAdapter {
private List fragments;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
public Fragment getItem(int position) {
return (Fragment) this.fragments.get(position);
}
public int getCount() {
return this.fragments.size();
}
}

Map restart when screen rotation (Android Studio)

I have a problem about screen rotation. I know that I must use method "onSaveInstanceState(Bundle outState)" but I have no ideal how it works. My application have to add the "marker" to Google Map.
class AddMarker extends Fragment{
private static final String KEY = "marker";
private ArrayList<LatLng> mData;
private GoogleMap mMap;
private MapView mMapView;
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(KEY, mData);
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.map, container, false);
mMapView = (MapView) v.findViewById(R.id.mapview);
mMapView.onCreate(savedInstanceState);
mMap = mMapView.getMap();
mMap.setMyLocationEnabled(true);
if((savedInstanceState != null)
&& (savedInstanceState.getParcelableArrayList(KEY) != null)){
mData = savedInstanceState.getParcelableArrayList(KEY);
// What should I do here ????
// call drawMarker(mData) ???
}
else
drawMarker(mData);
return v;
}
}
}

Android-Universal-Image-Loader ImagePager - How to avoid the activity back to first page when device rotated

The ImagePagerActivity show the page 1 (In scrollview) when i rotate the device. How to solve it? Thanks.
public class ImagePagerActivity extends BaseActivity {
private static final String STATE_POSITION = "STATE_POSITION";
ViewPager pager;
public void onCreate(Bundle savedInstanceState) {
// ...
if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}
// ...
pager.setCurrentItem(pagerPosition);
}
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
}
...
}

How to display an image on a UiApplication in BlackBerry?

How to display an image on a UiApplication?
I have a code that displays a button and when clicked it goes to a MainScreen, but I'd like to display an image below that button. I found a way to insert a zoomable image but not a static one.
Here's my code so far:
public class HelloWorldDemo extends UiApplication {
private MainScreen _screen;
private ButtonField _nextScreen;
public static void main(String[] args) {
HelloWorldDemo instance = new HelloWorldDemo();
instance.enterEventDispatcher();
}
public HelloWorldDemo() {
EncodedImage myImg = EncodedImage.getEncodedImageResource("k.jpg");
ZoomScreen zoomableImg = new ZoomScreen(myImg);
_screen = new MainScreen();
_nextScreen = new ButtonField("Go to Next Screen",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
_nextScreen.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field,int context) {
pushScreen(new NextScreen());
}
});
_screen.setTitle(new LabelField("Hello World Demo",LabelField.USE_ALL_WIDTH));
_screen.add(new RichTextField("Hello to the BlackBerry World!",Field.NON_FOCUSABLE));
_screen.add(_nextScreen);
pushScreen(_screen);
pushScreen(zoomableImg);
}
}
//I'm confused about your question. But still I'm posting ans here hope it will help you.
public final class ZoomScreenDemo extends UiApplication
{
public static void main(final String[] args)
{
UiApplication app = new ZoomScreenDemo();
app.enterEventDispatcher();
}
public ZoomScreenDemo()
{
pushScreen(new ZoomScreenDemoScreen());
}
public final static class ZoomScreenDemoScreen extends MainScreen
{
private EncodedImage _image;
private ButtonField _nextScreen;
public ZoomScreenDemoScreen()
{
setTitle("Zoom Screen Demo");
_nextScreen = new ButtonField("Go to next screen ",ButtonField.CONSUME_CLICK);
_nextScreen.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new DemoZoomScreen(_image));
}
});
add( _nextScreen );
_image = EncodedImage.getEncodedImageResource("img/building.jpg");
BitmapField bitmapField = new BitmapField(_image.getBitmap(), FIELD_HCENTER | FOCUSABLE);
add(bitmapField);
}
}
static class DemoZoomScreen extends ZoomScreen
{
DemoZoomScreen(EncodedImage image)
{
super(image);
}
public void zoomedOutNearToFit()
{
close();
}
}
}

Resources