I am new to android and I want to add this viewpager library https://github.com/jfeinstein10/JazzyViewPager to my project. For that I first extracted the project. And than I right clicked on my project than Build Path -> configure build path-> than I selected libraries. After that I selected Add external jar and added the library. However, The project mentions that I should make changes in my Imagedadapter. Which I did.. However, I am getting yellow bulb with red cross and I it says JazzyViewPager cannot be resolved to a variable..I believe I am doing minor mistake..but can't figure out..Please help ..Thanks in advance
Following is my Imageadapter.java
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five
};
ImageAdapter(Context context){
this.context=context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_small);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType. FIT_XY );
Resources r = context.getResources();
Bitmap bmp = BitmapFactory.decodeResource(r, GalImages[position]);
int width=200;//set your width
int height=200;//set your height
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp, width, height, true);
Drawable d = new BitmapDrawable(r,resizedbitmap);
Drawable[] layers = new Drawable[2];
layers[0] = d;
layers[1] = r.getDrawable(R.drawable.a);
LayerDrawable layerDrawable = new LayerDrawable(layers);
imageView.setImageDrawable(layerDrawable);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
private JazzyViewPager mJazzy;
/* ... */
#Override
public Object instantiateItem(ViewGroup container, final int position) {
Object obj = super.instantiateItem(container, position);
mJazzy.setObjectForPosition(obj, position);
return obj;
}
}
}
Edited
Download the library project
Import it into your workspace
Right click on your project, click on Properties
In the "Library" section at the bottom, click on Add
Select the Jazzy ListView library , click OK
Try these:
click the import button in the file menu in eclispe.
select import existing projects into workspace.
out in the finder window select the JazzyViewPager library.
once the library is visible in your work space right click your project and select properties.
go to the android option.
6.at the bottom where the library window click add.
select your project.
this should work this is how you import libraries such as the facebook sdk and google play sdk if not let me know. hopes this helps.
Related
I'm trying to use a viewpager2 to show pictures taken from my app. Right now, I'm just using photos currently on my device to test, although the tutorial I followed (https://www.geeksforgeeks.org/viewpager2-in-android-with-example/) showed me how to make a viewpager2 with premade resource files. I'm trying to use an image bitmap instead, although whenever I run the code, the images come out blank. Is it an error with my files or is there a problem in my code? Here is my code:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
class ViewPager2Adapter extends RecyclerView.Adapter<ViewPager2Adapter.ViewHolder> {
// Array of images
// Adding images from drawable folder
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/DCIM/yes/yes_20220809_095311.jpg");
Bitmap bitmap2 = BitmapFactory.decodeFile("/sdcard/DCIM/yes/yes_20220809_095311___WITHTEXT.jpg");
private Bitmap[] images = {bitmap, bitmap2};
private Context ctx;
// Constructor of our ViewPager2Adapter class
ViewPager2Adapter(Context ctx) {
this.ctx = ctx;
}
// This method returns our layout
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ctx).inflate(R.layout.images_holder, parent, false);
return new ViewHolder(view);
}
// This method binds the screen with the view
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// This will set the images in imageview
holder.images.setImageBitmap(images[position]);
}
// This Method returns the size of the Array
#Override
public int getItemCount() {
return images.length;
}
// The ViewHolder class holds the view
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
images = itemView.findViewById(R.id.images);
}
}
}
PS: I am currently using hard coded files for testing, this will be changed later. The problem might be that the file path is wrong, but that is why I am asking this question.
I am trying to change icon image according to the item data . when pressing the icon, it should switch to other icon and change the item data. everything is going right but when i scroll up or down the new icon image change its place to some other items because i am using list view with adapter. how can i keep the new icons in the pressed items without mixing with other items.
package com.example.sairamkrishna.handymade;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class HandAdapter extends ArrayAdapter<HandClass> {
private int myColor;
public HandAdapter(Context context, ArrayList<HandClass> objects, int my_Color) {
super(context, 0, objects);
myColor = my_Color;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View myView = convertView;
final HandClass myData = getItem(position);
if (myView == null) {
myView = LayoutInflater.from(getContext()).inflate(
R.layout.label_item, parent, false);
}
ImageView aImage = (ImageView) myView.findViewById(R.id.itemImage);
aImage.setImageResource(myData.getClsImage());
TextView aName = (TextView) myView.findViewById(R.id.itemName);
aName.setText(myData.getClsName());
FrameLayout aColor = (FrameLayout) myView.findViewById(R.id.itemColor);
aColor.setBackgroundColor(myColor);
final ImageView aAddToBasket = (ImageView) myView.findViewById(R.id.itemAddToBasket);
aAddToBasket.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Boolean aClsToBasket = (Boolean) myData.getClsToBasket();
if (aClsToBasket) {
aAddToBasket.setImageResource(R.drawable.ic_add_circle);
myData.setClsToBasket(false);
Toast.makeText(getContext(), "Remove from basket"+ position, Toast.LENGTH_SHORT).show();
} else {
// if (!aClsToBasket) {
aAddToBasket.setImageResource(R.drawable.ic_remove_circle);
myData.setClsToBasket(true);
Toast.makeText(getContext(), "Add to basket"+ position, Toast.LENGTH_SHORT).show();
}
}
});
ImageView aAddToFavorite = (ImageView) myView.findViewById(R.id.itemAddToFavorite);
aAddToFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "Favorite - List item was clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return myView;
}
}
A fundamental concept of ListView and RecyclerView is that you need to "update" the value (in this case, the image) for each portion of the row every time getView() or onBindViewHolder() is called.
For example, every time getView() is called, you're always updating the (text) value of TextView aName. That is, there is always a call to aName.setText().
Right now, the only time you call aAddToBasket.setImageResource() is inside an OnClickListener. Of course, it makes sense to do it here, but you must also update the image outside of the listener.
Add this code right after your ImageView aAddToBasket line:
if ((Boolean) myData.getClsToBasket()) {
aAddToBasket.setImageResource(R.drawable.ic_remove_circle);
} else {
aAddToBasket.setImageResource(R.drawable.ic_add_circle);
}
Add this line after icon change.
adapter.notifyDataSetChanged();
I am facing a strange issue in my android app and I am not able to figure it out. Is there anyone who can help me regarding this issue.
Let me describe about my app and issue.
I am getting all the images from gallery and displaying them on a gridview using adapter. Once displayed, I am sending an image from this gridview of Activity A to next activity on click of gridview item and assigning this image on an image view in Activity B... When I come back from Activity B to Activity A on pressing Bsck button, it works fine. But when I repeat this process three to four times, app is crashing. I don't know what is the issue. I am sure that the issue is in Activity A..
Here is the code of Activity A....
package info.androidhive.tabsswipe;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class ImageLoader extends Fragment implements LoaderCallbacks<Cursor> {
GridView gridView;
SimpleCursorAdapter mAdapter;
Cursor cursor;
Uri uri;
String ID;
Intent intent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.videos, container, false);
gridView = (GridView) getActivity().findViewById(R.id.gridView1);
gridView.setAdapter(null);
mAdapter = null;
cursor = null;
uri = null;
ID = null;
intent = null;
mAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.gridview_item1,
null,
new String[] { "_data","_id"} ,
new int[] { R.id.video_thumbnail_imageview},
0
);
/** Setting adapter for the gridview */
gridView.setAdapter(mAdapter);
/** Loader to get images from the SD Card */
getActivity().getSupportLoaderManager().initLoader(0, null, this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
cursor = (Cursor)parent.getItemAtPosition(position);
ID = cursor.getString(cursor.getColumnIndex("image_id"));
intent = new Intent(getActivity(),Testing_Activity.class);
intent.putExtra("Image_Path",ID);
startActivity(intent);
}
});
return rootView;
}
#Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
/** Getting uri to the Thumbnail images stored in the external storage */
uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
/** Invoking the uri */
return new CursorLoader(getActivity(), uri, null, null, null, null);
}
/** A callback method, invoked after the requested content provider returned all the data */
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
mAdapter.swapCursor(arg1);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
}
Any help regarding this issue will be highly appreciated. Thanks in Advance.
I am currently working on a project in which I was updating a ListView's adapter from ArrayAdapter to a Custom CursorAdapter.
I deleted the R.java intentionally to regenerate the R.java file after inserting a new <string></string> to the res/string.xml file since it was not automatically regenerating. The file did not generate.
I continued to debug, and ran a Project->Clean... in attempt to regenerate the R.java file. This did not work either.
After checking StackOverflow, I also tried changing the version in AndroidManifest.xml in hope to regenerate the file. This also did not work. I've tried a few other hacks in hopes something might have Eclipse regenerate the file. No Luck.
Does anyone have any idea? I am on a tight deadline and this is really holding me back.
Here is the file I was working in when the issue arose:
package com.renaissanceartsmedia.gradingapp.controllers.fragments;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.util.ArrayMap;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.renaissanceartsmedia.gradingapp.R;
import com.renaissanceartsmedia.gradingapp.controllers.activities.CourseActivity;
import com.renaissanceartsmedia.gradingapp.model.Course;
import com.renaissanceartsmedia.gradingapp.model.CourseStore;
import com.renaissanceartsmedia.gradingapp.model.GradingAppDatabaseHelper.CourseCursor;
public class CourseListFragment extends ListFragment {
// DEBUG
private static final String TAG = "CourseListFragment";
// Create an ArrayList<String> to store flashcards
ArrayList<Course> mCourses;
ArrayMap<Long, Course> mCoursesById;
// Cursor Object
private CourseCursor mCursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the Title of the Fragment's Activity
getActivity().setTitle(R.string.course_list);
// Load Cursor to DB
mCursor = CourseStore.get(getActivity()).queryCourses();
// Set the list of FlashcardListFragments
mCourses = CourseStore.get(getActivity()).getCourses();
mCoursesById = CourseStore.get(getActivity()).getCoursesById();
// Create an ArrayAdapter to use for displaying FlashcardListFragments in FlashcardListActivity
/*
ArrayAdapter<Flashcard> adapter = new ArrayAdapter<Flashcard>(
getActivity(),
android.R.layout.simple_list_item_1,
mFlashcards);
*/
// OLD METHOD
//ListItemLayoutAdapter adapter = new ListItemLayoutAdapter(mCourses);
CourseCursorAdapter adapter = new CourseCursorAdapter(getActivity(), mCursor);
// Set the adapter for the list
setListAdapter(adapter);
}
/* Handles a user selection of a FlashcardListFragment
*
*/
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//Flashcard f = (Flashcard)(getListAdapter()).getItem(position);
// OLD WAY
//Course c = ((ListItemLayoutAdapter)getListAdapter()).getItem(position);
/*
Course c = ((CourseCursorAdapter))getListAdapter());
Log.d(TAG, c.getCourseTitle() + " was clicked");
// Start a new activity using an Intent
Intent openFlashcardDetail = new Intent(getActivity(), CourseActivity.class);
// Add EXTRAS to the intent
//openFlashcardDetail.putExtra(FlashcardFragment.EXTRA_FLASHCARD_ID, f.getId());
openFlashcardDetail.putExtra(Course.EXTRA_COURSE_ID, c.getId());
startActivity(openFlashcardDetail);
*/
}
// OLD METHOD
//class ListItemLayoutAdapter extends ArrayAdapter<Course> {
class CourseCursorAdapter extends CursorAdapter {
// Member Properties
// OLD METHOD
Course mCurrentListObject;
CourseCursor mCourseCursor;
// Constructor
// OLD METHOD
/*
public ListItemLayoutAdapter(ArrayList<Course> itemContent) {
super(getActivity(), 0, itemContent);
}
*/
public CourseCursorAdapter(Context context, CourseCursor cursor) {
super(context, cursor, 0);
mCourseCursor = cursor;
}
// OLD WAY
/*
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//If we weren't given a view, inflate a new view
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.list_item_layout, null);
}
// Configure the view for this object
mCurrentListObject = getItem(position);
// Make Connections from Layout to Java code
TextView mainTextView = (TextView) convertView.findViewById(R.id.item_main_text);
TextView subTextView = (TextView) convertView.findViewById(R.id.item_sub_text);
// Set the Contents of the Views
mainTextView.setText(setMainText());
subTextView.setText(setSubText());
return convertView;
}
*/
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Use a layout Inflater to get a row view
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Get the course for the current row
Course course = mCourseCursor.getCourse();
// Setup a new TextView
TextView courseTitleTextView = (TextView)view;
String mainText = context.getString(R.string.course_title_hint, course.getCourseTitle());
courseTitleTextView.setText(mainText);
}
public String setMainText() {
return mCurrentListObject.getCourseTitle();
}
public String setSubText() {
return mCurrentListObject.getSubject();
}
}
}
I have solution, I hope this will help you. !!
Step 1) Change Build Target
For Example, if you selected 2.2 as Build Target then select Maximum you have. (like 4.4)
And if you selected 4.4 then select 4.3 as target.
Step 2) Clean Project
It will create R.java again
Thank you.
If you are on Eclipse open your problems view (Window -> Show view -> Problems) and look for an error in your resources or manifest
Try restarting eclipse, sometimes it will do the trick. Check if you are using jdk 7, it only needs jdk 6.
This usually happens because there is an issue somewhere in your XML or resource file names.
Try restarting Eclipse, if it still does not indicate where the problem is, try retracing your steps the last changes you made to XML files before this problem occurred. You will find some kind of syntax error or spelling mistake or invalid file name somewhere. Fix it then clean again.
I already made a simple android application, which I also have integrated Google Maps in it..
It is also capable of connecting to MySQL (localhost) to display my desired places using longitude and latitude values..
My question is, is it possible to make another overlay item above Google Maps when a marker is clicked (just like what happens in foursquare)?
To be specific, i want to display a text that contains the name of a place.
Heres my class of displaying the overlay items.
I made an onTap method, but it display a dialog box, I want to display a simple text box that shows the name of the place.
package finddroid.map;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
private int markerHeight;
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
markerHeight = ((BitmapDrawable) defaultMarker).getBitmap().getHeight();
populate();
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context)
{
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i)
{
return mapOverlays.get(i);
}
#Override
public int size()
{
return mapOverlays.size();
}
#Override
//Event when a place is tapped
protected boolean onTap(int index)
{
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay)
{
mapOverlays.add(overlay);
this.populate();
}
}
Have a look at this project - balloon itemized overlay. It is using own class extending FrameLayout to show balloons.
So if you want to modify your code put this into your onTap method to display a TextView above taped item
TextView text = new TextView(context);
text.setText(item.getTitle());
MapView.LayoutParams params = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, item.getPoint(), MapView.LayoutParams.BOTTOM_CENTER);
params.mode = MapView.LayoutParams.MODE_MAP;
mMapView.addView(text, params);
I think this code is simple and easy to understand and you can improve it accordingly to your needs. To make it work you have to pass instance of MapView to constructor of your overlay and save it to private variable mMapView.
private MapVeiw mMapView;
public CustomItemizedOverlay(Drawable defaultMarker, Context context, MapView mapView) {
this(defaultMarker);
this.context = context;
this.mMapView = mapView;
}
And don't forget to add MapView as one of parameters when you call new CustomItemizedOverlay().