Horizontal and Vertical ViewPager - android

Do anyone have successfully implemented ViewPager which scrolls Horizontally and Vertically?
I know Directional ViewPager,When i use it it gives
<i>31 12:11:29.954: E/AndroidRuntime(14670): java.lang.NoClassDefFoundError: com.directionalviewpager.DirectionalViewPager$DataSetObserver
10-31 12:11:29.954: E/AndroidRuntime(14670): at com.directionalviewpager.DirectionalViewPager.setAdapter(DirectionalViewPager.java:164)</i>
line no 164:mObserver = new DataSetObserver();
public void setAdapter(PagerAdapter adapter) {
if (mAdapter != null) {
VerticalViewPagerCompat.setDataSetObserver(mAdapter, null);
}
mAdapter = adapter;
if (mAdapter != null) {
if (mObserver == null) {
mObserver = new DataSetObserver();
}
VerticalViewPagerCompat.setDataSetObserver(mAdapter, mObserver);
mPopulatePending = false;
if (mRestoredCurItem >= 0) {
mAdapter.restoreState(mRestoredAdapterState, mRestoredClassLoader);
setCurrentItemInternal(mRestoredCurItem, false, true);
mRestoredCurItem = -1;
mRestoredAdapterState = null;
mRestoredClassLoader = null;
} else {
populate();
}
}
}
Thanks,

I found your answer You just add the DirectionalViewPager as a library into your project
This libray is located at Android-DirectionalViewPager-master\Android-DirectionalViewPager-master\library\bin
and ya dont forget to include it from order and export tab

Related

Android Camera X ViewPort

I use this code for Camera X binding. When I run app for the first time everything works fine. But when I close app and start it again I get "ViewPort is NULL". Why viewPort is NULL I do not understand. What is wrong with my code?
#OptIn(markerClass = androidx.camera.lifecycle.ExperimentalUseCaseGroupLifecycle.class)
private void bindAllCameraUseCases() {
if (cameraProvider != null) {
// As required by CameraX API, unbinds all use cases before trying to re-bind any of them.
cameraProvider.unbindAll();
createPreviewUseCase();
createImageCaptureUseCase();
createAnalysisUseCase();
if (previewUseCase != null && analysisUseCase != null && imageCaptureUseCase != null) {
ViewPort viewPort = ((PreviewView)findViewById(R.id.preview_view)).getViewPort();
if (viewPort != null) {
#OptIn(markerClass = androidx.camera.core.ExperimentalUseCaseGroup.class)
UseCaseGroup useCaseGroup = new UseCaseGroup.Builder()
.addUseCase(previewUseCase)
.addUseCase(analysisUseCase)
.addUseCase(imageCaptureUseCase)
.setViewPort(viewPort)
.build();
camera = cameraProvider.bindToLifecycle(/* lifecycleOwner= */ this, cameraSelector, useCaseGroup);
LiveData<Integer> torchStateObserver = camera.getCameraInfo().getTorchState();
torchStateObserver.observe(this, new Observer<Integer>() {
#Override
public void onChanged(Integer state) {
torchState = state;
}
});
} else {
Toast.makeText(this, "VIEWPORT is NULL", Toast.LENGTH_SHORT).show();
}
}
}
}

Difference between getWindow().setBackgroundDrawable and getWindow().getDecorView().setBackgroundDrawable

What is the difference between them? As far as I can see they both do the same thing.
If you go to sources, you will find PhoneWindow.class that have implementation of Window.setBackgroundDrawable method:
#Override
public final void setBackgroundDrawable(Drawable drawable) {
if (drawable != mBackgroundDrawable || mBackgroundResource != 0) {
mBackgroundResource = 0;
mBackgroundDrawable = drawable;
if (mDecor != null) {
mDecor.setWindowBackground(drawable);
}
if (mBackgroundFallbackResource != 0) {
mDecor.setBackgroundFallback(drawable != null ? 0 : mBackgroundFallbackResource);
}
}
}
As you can see, it calls different method of DecorView setWindowBackground:
public void setWindowBackground(Drawable drawable) {
if (getBackground() != drawable) {
setBackgroundDrawable(drawable);
if (drawable != null) {
drawable.getPadding(mBackgroundPadding);
} else {
mBackgroundPadding.setEmpty();
}
drawableChanged();
}
}
Which is actually use method DecorView.setBackgroundDrawable but also provides additional logic for Window. So I would suggest you to use getWindow().setBackgroundDrawable().

Customizing InfowindowAdapter to add images from Content provider

I have an application of places shown on a map with markers, I have customized infowindow to show the contents of the places created, title, description and picture, these data are accessed via a content provider, my problem everything is going great until I delete a record from the database, the app goes crazy, or broken or just odd recognizes records id etc etc
The problem is clearly in the infowindow code customization, because to remove it all works correctly, create, delete, create markers etc. .. but even though I've tried have not given with the error.
The infernal code:
class MyInfoWindowAdapter implements InfoWindowAdapter {
View v;
MyInfoWindowAdapter() {
v = getLayoutInflater().inflate(R.layout.custom_info, null);
}
#SuppressWarnings("deprecation")
#Override
public View getInfoWindow(final Marker marker) {
MapaLugaresActivity.this.marker = marker;
c.requery();
TextView tvTitulo = (TextView) v.findViewById(R.id.tituloi);
tvTitulo.setText(marker.getTitle());
// TextView tvDescr = (TextView) v.findViewById(R.id.descripcioni);
// tvDescr.setText(marker.getSnippet());
ImageView imgi = (ImageView) v.findViewById(R.id.imgi);
if (marker.getSnippet() != null && marker != null
&& c.getCount() > 0) {
try {
if (c.moveToFirst()) {
c.moveToPosition(Integer.valueOf(marker.getSnippet()) - 1);
String img = c.getString(c.getColumnIndex("foto"));
if (img != null) {
resizedBitmap = DecodeImagen
.decodeSampledBitmapFromFile(img, 100, 100);
imgi.setAdjustViewBounds(true);
imgi.setImageBitmap(resizedBitmap);
} else {
imgi.setAdjustViewBounds(true);
imgi.setImageResource(R.drawable.paisaje);
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
// imgi.setImageResource(R.drawable.rdlv);
Typeface fuente = Typeface.createFromAsset(getAssets(),
"TravelingTypewriter.ttf");
tvTitulo.setTypeface(fuente);
// tvDescr.setTypeface(fuente);
return v;
}
#Override
public View getInfoContents(Marker marker) {
if (MapaLugaresActivity.this.marker != null
&& MapaLugaresActivity.this.marker.isInfoWindowShown()) {
MapaLugaresActivity.this.marker.hideInfoWindow();
MapaLugaresActivity.this.marker.showInfoWindow();
}
return null;
}
}
Add markers:
#SuppressWarnings("deprecation")
public void addMarker() {
map.clear();
c.requery();
if (c.getCount() > 0) {
c.moveToFirst();
do {
latlong = new LatLng(c.getFloat(c.getColumnIndex("latitud")),
c.getFloat(c.getColumnIndex("longitud")));
final MarkerOptions marker = new MarkerOptions().title(c
.getString(c.getColumnIndex("titulo")));
marker.snippet(c.getString(c.getColumnIndex("_id")));
marker.position(latlong);
map.addMarker(marker).showInfoWindow();
} while (c.moveToNext());
} else {
Toast.makeText(MapaLugaresActivity.this, "Añada un nuevo lugar",
Toast.LENGTH_SHORT).show();
}
}
I appreciate your help in advance, took several days trying to find the solution and I think it was time to ask for help, thanks. I have limited programming experience , hope your understanding
PS: I have uploaded the project on GitHub in case anyone wants to analyze it: D
The code in the activity MapaLugaresActivity
https://github.com/Shochlves/rdlv
Image infowindow:
https://lh5.ggpht.com/eBgrw0riQxfGph4FhxXvMRDxzYjlvdWw6L6qRqg_L1oV9yPGHPW9wV2Cal7zVGj1-HY=h900-rw
Solved! The solution has been implemented Hastable (http://developer.android.com/reference/java/util/Hashtable.html), I hit the code as it is helpful.
InfoWindowAdapter with image and title from SQLite (content provider)
InfoWindowAdapter:
public class MyInfoWindowAdapter implements InfoWindowAdapter {
private View v;
MyInfoWindowAdapter() {
v = getLayoutInflater().inflate(R.layout.custom_info, null);
}
#Override
public View getInfoContents(Marker marker) {
if (MapaLugaresActivity.this.marker != null
&& MapaLugaresActivity.this.marker.isInfoWindowShown()) {
MapaLugaresActivity.this.marker.hideInfoWindow();
MapaLugaresActivity.this.marker.showInfoWindow();
}
return null;
}
#Override
public View getInfoWindow(Marker marker) {
MapaLugaresActivity.this.marker = marker;
String img = null;
if (marker.getId() != null && markers != null && markers.size() > 0) {
if ( markers.get(marker.getId()) != null &&
markers.get(marker.getId()) != null) {
img = markers.get(marker.getId());
}
}
final ImageView imgi = (ImageView) v.findViewById(R.id.imgi);
if (img != null && !img.equalsIgnoreCase("null")
&& !img.equalsIgnoreCase("")) {
Bitmap resizedBitmap = DecodeImagen
.decodeSampledBitmapFromFile(img, 100, 100);
imgi.setImageBitmap(resizedBitmap);
} else {
imgi.setAdjustViewBounds(true);
imgi.setImageResource(R.drawable.paisaje);
}
TextView tvTitulo = (TextView) v.findViewById(R.id.tituloi);
tvTitulo.setText(marker.getTitle());
//TextView tvDescr = (TextView) v.findViewById(R.id.descripcioni);
//tvDescr.setText(marker.getSnippet());
Typeface fuente = Typeface.createFromAsset(getAssets(),
"TravelingTypewriter.ttf");
tvTitulo.setTypeface(fuente);
// tvDescr.setTypeface(fuente);
return v;
}
}
addMarker:
public void addMarker() {
markers = new Hashtable<String, String>();
c.requery();
if (c.getCount() > 0) {
c.moveToFirst();
do {
latlong = new LatLng(c.getFloat(c.getColumnIndex("latitud")),
c.getFloat(c.getColumnIndex("longitud")));
final Marker marker = map.addMarker(new MarkerOptions().title(c
.getString(c.getColumnIndex("titulo"))).snippet(c.getString(c.getColumnIndex("_id"))).position(latlong));
markers.put(marker.getId(), c.getString(c.getColumnIndex("foto")));
} while (c.moveToNext());
} else {
Toast.makeText(MapaLugaresActivity.this, "Añada un nuevo lugar",
Toast.LENGTH_SHORT).show();
}
}
onCreate:
if (map != null) {
map.setInfoWindowAdapter(new MyInfoWindowAdapter());
addMarker();
}
a greeting

Ideas to refactor code with many Lists

I need a little help to get an idea of how to refactor my code, but I can't see options besides what's done, I would like to add the objects but not using so many lists (and if's conditions).
Here is my code, if anyone could help, I appreciate. Thanks
#ViewById
BannerHomeViewPager place1, place2, place3, place4, place5, place6, place7,
place8, place9;
The lists:
private List<HomeItem> allHomeItems = new ArrayList<HomeItem>(),
placeItems1 = new ArrayList<HomeItem>(),
placeItems2 = new ArrayList<HomeItem>(),
placeItems3 = new ArrayList<HomeItem>(),
placeItems4 = new ArrayList<HomeItem>(),
placeItems5 = new ArrayList<HomeItem>(),
placeItems6 = new ArrayList<HomeItem>(),
placeItems7 = new ArrayList<HomeItem>(),
placeItems8 = new ArrayList<HomeItem>(),
placeItems9 = new ArrayList<HomeItem>();
1) Items mocked, ok.
2)
#UiThread
void updateUI() {
if (allHomeItems != null && allHomeItems.size() > 0) {
for (HomeItem item : allHomeItems) {
if (item.getPlacement().contains("1")) {
placeItems1.add(item);
} else if (item.getPlacement().contains("2")) {
placeItems2.add(item);
} else if (item.getPlacement().contains("3")) {
placeItems3.add(item);
} else if (item.getPlacement().contains("4")) {
placeItems4.add(item);
} else if (item.getPlacement().contains("5")) {
placeItems5.add(item);
} else if (item.getPlacement().contains("6")) {
placeItems6.add(item);
} else if (item.getPlacement().contains("7")) {
placeItems7.add(item);
} else if (item.getPlacement().contains("8")) {
placeItems8.add(item);
} else {
placeItems9.add(item);
}
}
}
setupAdapters();
}
3) setupAdapters()
private void setupAdapters() {
if (place1 != null)
place1.update(placeItems1);
if (place2 != null)
place2.update(placeItems2);
if (place3 != null)
place3.update(placeItems3);
if (place4 != null)
place4.update(placeItems4);
if (place5 != null)
place5.update(placeItems5);
if (place6 != null)
place6.update(placeItems6);
if (place7 != null)
place7.update(placeItems7);
if (place8 != null)
place8.update(placeItems8);
if (place9 != null)
place9.update(placeItems9);
}
As #DanielBo answer:
private Map<String, ArrayList<HomeItem>> placeItems = new HashMap<String,ArrayList<HomeItem>>();
void updateUI() {
if (allHomeItems != null && allHomeItems.size() > 0) {
for (HomeItem item : allHomeItems) {
if(!placeItems.containsKey(item.getPlacement())){
placeItems.put(item.getPlacement(), new ArrayList<HomeItem>());
}
placeItems.get(item.getPlacement()).add(item);
}
}
setupAdapters();
}
But why are you using so many views? I really can't say if this is correct bcs i don't know the purpose of this, but I can't figure a good use to so many list views in the same layout...

Chromecast SDK (Android) - is there a way to check whether the media playing on the cast device has finished playing?

Is there an onFinished listener of some sort? Or do we have to compare the current stream position against the duration of the track?
It's not pretty but you can make this call:
if (mRemoteMediaPlayer.getMediaStatus().getPlayerState() == MediaStatus.PLAYER_STATE_IDLE
&& mRemoteMediaPlayer.getMediaStatus().getIdleReason() == MediaStatus.IDLE_REASON_FINISHED) {
...
}
Prem,
There is currently no callback to register for such event. One alternative (and-not-so-pretty) approach is the following: on the receiver, listen for "ended" event of the media element and send an event back to the sender through a private channel. Another approach is what you suggested: check position against duration. When SDK graduates to general availability, better and cleaner approaches will be available to accomplish what you want.
Here is solution:
You just need to take one more variable mIdleReason.
1) Initialize mIdleReason as
public int mIdleReason=MediaStatus.IDLE_REASON_NONE;
2) Update value at method loadMedia
public void loadMedia(String url, MediaMetadata movieMetadata, CastSession castSession, boolean autoPlay, long position) {
if (castSession == null || !castSession.isConnected()) {
return;
}
MediaInfo mediaInfo = new MediaInfo.Builder(url)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType("videos/m3u8")
.setMetadata(movieMetadata)
.build();
mRemoteMediaClient = castSession.getRemoteMediaClient();
mRemoteMediaClient.addListener(mRemoteMediaClientListener);
mRemoteMediaClient.load(mediaInfo, autoPlay, position);
mIdleReason = MediaStatus.IDLE_REASON_NONE;
}
3) Update value at onStatusUpdate:
private RemoteMediaClient.Listener mRemoteMediaClientListener = new RemoteMediaClient.Listener() {
#Override
public void onStatusUpdated() {
if (mRemoteMediaClient == null || mediaPlayerListener == null) {
return;
}
MediaStatus mediaStatus = mRemoteMediaClient.getMediaStatus();
if (mediaStatus != null) {
int playerStatus = mediaStatus.getPlayerState();
Log.d("PlayerState", "onStatusUpdated() called, progress= "+mSeekBar.getProgress() +", stream duration= "+ mRemoteMediaClient.getStreamDuration()+" mSeekBar.getProgress() == mRemoteMediaClient.getStreamDuration()="+(mSeekBar.getProgress() == mRemoteMediaClient.getStreamDuration()));
Log.d("PlayerState", "onStatusUpdated() called playerStatus="+playerStatus+", idleReason="+mediaStatus.getIdleReason());
if (playerStatus == MediaStatus.PLAYER_STATE_PLAYING) {
mediaPlayerListener.playing();
mIdleReason = MediaStatus.IDLE_REASON_FINISHED;
} else if (playerStatus == MediaStatus.PLAYER_STATE_BUFFERING) {
mediaPlayerListener.buffering();
mIdleReason = MediaStatus.IDLE_REASON_FINISHED;
} else if (playerStatus == MediaStatus.PLAYER_STATE_PAUSED) {
mediaPlayerListener.paused();
} else if (playerStatus == MediaStatus.IDLE_REASON_INTERRUPTED) {
mediaPlayerListener.error();
} else if (playerStatus == MediaStatus.IDLE_REASON_ERROR) {
mediaPlayerListener.error();
}else if(playerStatus == MediaStatus.PLAYER_STATE_IDLE && mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_FINISHED&& mIdleReason == MediaStatus.IDLE_REASON_FINISHED){
mediaPlayerListener.played();
}
}
}
#Override
public void onMetadataUpdated() {
Log.d("", "onMetadataUpdated: ");
}
#Override
public void onQueueStatusUpdated() {
Log.d("", "onQueueStatusUpdated: ");
}
#Override
public void onPreloadStatusUpdated() {
Log.d("", "onPreloadStatusUpdated: ");
}
#Override
public void onSendingRemoteMediaRequest() {
Log.d("", "onSendingRemoteMediaRequest: ");
}
#Override
public void onAdBreakStatusUpdated() {
Log.d("", "onAdBreakStatusUpdated: ");
}
};

Categories

Resources