How can a add the delete item functionality in my Android app? - android

I would like to implement the same technique as used in the stock android photos app to delete pictures. What I mean by that, is that I would like to be able to select the pictures / items when perform a long item click. Then the actionbar should also display how many pictures I have selected. If you know the app, you will also know what I mean.
Basically what I have so far, is the action bar itself (I am using the appcompat one) and the gridview. There I will have add this functionlity somewhere in here:
private void setGridViewClickListener() {
mGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
File imgFile = new File(mImagePaths.get(position));
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
zoomImageFromThumb(new View(mContext), myBitmap);
}
}
});

You should set a ChoiceMode to CHOICE_MODE_MULTIPLE_MODAL to your gridview and a MultiChoiceMode listener:
gridview.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gridview.setMultiChoiceModeListener(new MultiChoiceModeListener());
Then create an ActionMode (Reference) in this listener with these methods (I added a few example):
onPrepareActionMode: clear the menu, inflate the menu
onCreateActionMode: clear menu, inflate.. or whatever
onItemCheckedStateChanged: update the title in the ActionBar (like +1 item string), re/set a background to the clicked view..
onActionItemClicked: retreive the id clicked and perform actions (as notifyDataSetChanged method)
onDestroyActionMode: update the activity (like reset the background for views)
This tutorial: Multiple Selection GridView in Android might help you to do this. Also see the Guide Topic in Enabling batch contextual actions in a ListView or GridView section.
Hope this helps.

Related

Android Viewpager2 and RecyclerView Item onClick listener

I have implemented Viewpager2 Image slider using the code idea mentioned in the following link.
I want handle clicks on the slider images. How to Open another activity if the specific slider item clicked.
Here is the link of code
https://androidapps-development-blogs.medium.com/android-modern-image-slider-using-viewpager-2-and-kenburnsview-android-studio-23a7b74317e8
On your adapter go to TravelLocationViewHolder.
After this line
textLocation = itemView.findViewById(R.id.textLocation);
try to set a click listener on itemview
itemView.setOnClickListener( v-> { System.out.println("THIS IS IT"); } );

Android partially active Activity

I have an activity A. I am creating a kind of tutorial for user for this activity, to teach him how he can use the app on that screen.
For that, my requirement is :
I want to blur all the views of the activity except one view. I want to prompt user to click on that view through a hand image pointing at that view.
Nothing should happen if the user clicks on the blurred/greyed out area, but if he taps on that particular active view, it should react to that touch.
I was thinking of using a full screen fragment for this. The Fragment will take the following input from the activity :
for what coordinates, is should not blur the screen and pass the touch event to the activity
the coordinates on which it should show that pointing hand image.
After from these coordinates, the fragment background would be blur.
I wanted to confirm if that's possible, to make the fragment partially active, i.e. delegate it's touch events to the activity for a particular view of the activity.
Also, please let me know if there is any other better approach of achieving the same thing.
Edit1 :
Thinking of using a fragment here, because I'd want this type of behaviour on different screen in future. In that case, I'd make that fragment generic which takes some inputs (as described above) and use it on different screens.
There's a very good library called SCV which does what you're trying to achieve, you're able to customize the styles for it too. I've used this for first time the app is opened to show the user a tutorial.
According to their Github
The ShowcaseView (SCV) library is designed to highlight and showcase specific parts of apps to the user with a distinctive and attractive overlay. This library is great for pointing out points of interest for users, gestures, or obscure but useful items.
Further Reading:
Android Arsenal - Showcase Views Tutorial
ShowCaseView on Android - Indipendev
I found it much easier to include an 'extra' layout around the UI of my activity, and then to add a highest-z grey mostly-transparent filter to it and put the instructions on that.
Each "step" of the instructions was a different layout that was dynamically loaded into that layout container as they clicked. (Just another approach)
The 'container' layout is a: FrameLayout
then in my Activity I have: (ignore bad naming)
private void addOverlayLayout() {
frameLayout = (FrameLayout) findViewById(R.id.framelayoutInner);
frameLayout3 = (FrameLayout) findViewById(R.id.framelayout3);
frameLayout3.setBackgroundColor(Color.DKGRAY);
frameLayout3.setAlpha(0.3f);
// Dynamically create a relativelayout which will be appended to framelayout
relativeLayout = new RelativeLayout(getApplicationContext());
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
instructionOverlays.add(createSimpleClickInstruction(R.layout.instruction_reader_1));
instructionOverlays.add(createSimpleClickInstruction(R.layout.instruction_reader_2));
if (FullscreenReaderActivity.isFirstRun) {
displayNextGuide();
}
}
public void displayNextGuide() {
// clean relative layout if it has views
relativeLayout.removeAllViews();
// clean frame layout if it has child (safe if empty)
frameLayout.removeView(relativeLayout);
if (!isFirstRun) {
return;
}
if (instructionOverlays.size() > 0) {
runOnUiThread(instructionOverlays.get(0));
instructionOverlays.remove(0);
} else {
frameLayout3.setBackgroundColor(Color.TRANSPARENT);
frameLayout3.setAlpha(1.0f);
}
}
public Runnable createSimpleClickInstruction(final int resource) {
return new Runnable() {
#Override
public void run() {
getLayoutInflater().inflate(
resource,
relativeLayout,
true
);
relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayNextGuide();
}
});
frameLayout.addView(relativeLayout);
}
};
}

Custom button form (House/Tree etc.)

I'm a bit new to Android, therefore question may sound ridiculous, but:
Is there any way to make a button in form of smthg? There ate tons of apps, where you press on a tree or some house and some action starts. How is that made?
Not action, but form. Or there is no way to make button NOT rectangular?
You are talking about imagebutton.Image button documentation
For example creating a button with tree background.Example:
<ImageButton android:src="#drawable/tree.png"></ImageButton>
Yes you can do that. Considering your trees and house as an image.
ImageView im;
im = (ImageView) findViewById(R.id.image_of_tree);
im.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//perform some action
}
});
Off-course When you get inflated your layout and your view(can be any view) is available by id. your button can be an ImageView, ImageButton etc. just find the id for your view and set whatever event you want to listen by this view.

ListView Images showed only when visible

I have a ListView that loads images to the ImageView asynchronously. To achieve this i am using Android-Universal-Image-Loader
But i would like to start loading this images only when they are visible in the listview. For example if the visible items of the listview are from 5 to 9, only those should be loaded. Also if the user scrolls very fast the ListView only when stopped those items should be loaded.
What is the best way to this?
If you use "view reusing" in your listview adapter then you shouldn't do anything. UIL do it for you. UIL won't load ALL scrolled images, only those which are got in task pool (you can set set pool size in configuration). If you use "view reusing" then images which were scrolled fast won't be loaded.
Look into example project on GitHub.
UPD: Since 1.7.0 version UIL have PauseOnScrollListener.
boolean pauseOnScroll = true;
boolean pauseOnFling = true;
listView.setOnScrollListener(new PauseOnScrollListener(pauseOnScroll, pauseOnFling));
Use an OnScrollListener, the onScrollStateChanged() method is called when the ListView switches between SCROLL_STATE_IDLE, SCROLL_STATE_TOUCH_SCROLL (slower scrolling), and SCROLL_STATE_FLING (faster scrolling). With this you can choose to load new images only when the states is "Idle" or "Touch".
Addition
In the first run the visible items of the ListView aren't shown. For example when the app starts if the ListView has 4 items visible those 4 images should be loaded.
I haven't used the Universal Image Loader myself, but from what you described below you need to know how many rows will be displayed before you start downloading. Try this:
Write a Runnable to start the asynchronous downloads.
Use your ListView's built-in Handler to call the Runnable after the rows have been drawn
For example:
private Runnable loadImages = new Runnable() {
#Override
public void run() {
// Start the asynchronous downloads
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mListView.post(loadImages);
}
The loadImages will be called after the ListView is drawn so you will know exactly how many rows are visible.

Android gallery onItemClick show larger image

I've just finished copying the android walkthrough for the gallery (http://developer.android.com/resources/tutorials/views/hello-gallery.html) and was wondering how I would go about having the thumbnail photo scale into a larger one within the onClickItem method.
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(ProjectPreview.this, "" + position, Toast.LENGTH_SHORT).show();
}
Currently it just shows the position as depicted in the Toast. Could someone point me in the right direction or show me a simple way?
Thanks in advance.
EDIT: Added this method and called it in the onItemClick sending it the position. And it works.
public void showLarger(int position){
ImageView image = (ImageView) findViewById(R.id.iv1);
image.setLayoutParams(new Gallery.LayoutParams(200, 100));
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setImageResource(mImageIds[position]);
}
Use a Dialog with a layout which contains an imageview, and set its source case the position you have and with the size you want.
I would use a Dialog. You can create a new activity and have an ImageView in its layout, and disguise that Activity as a Dialog. See this question for how to do all that: Android Image Dialog/Popup
As mentioned above, you can use a Dialog to display the larger image or you can just fire an intent that would start a regular Activity(You will have to add the dialog Theme to your Activity if you do it as described as above).
You'd have to do something like call v.getItemID(position) and store that in a String, then pass that into your next Activity.
Either way, you'll have to pass in some data to your Activty that will host the ImageView, and then use that data(path, uri, etc) to inflate your larger ImageView. Just don't forget to add the theme to your manifest if you want use the Dialog route.

Categories

Resources