How can i put a XML view in an array variable? - java

i am creating a CAROUSEL view, that can be swipe left or right, but the problem that i am having is, i have 5 images and 1 XML view to be need to put in the carousel function, the 5 images is already working but for the last bullet in the carousel a view should be loaded on it, and it can still be swipe if they want to go back to the 5th images or to the first image.
here is the code:
public class AboutHelp extends AppCompatActivity {
CarouselView carouselView;
int[] sampleImages = {R.drawable.help_1, R.drawable.help_2, R.drawable.help_3, R.drawable.help_4, R.drawable.help_5,R.layout.activity_send_feed_back};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_help);
androidx.appcompat.widget.Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// add back arrow to toolbar
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
carouselView = findViewById(R.id.aboutCarouselHelp);
carouselView.setPageCount(sampleImages.length);
carouselView.setImageListener(imageListener);
}
ImageListener imageListener = new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
imageView.setImageResource(sampleImages[position]);
}
};
is this possible? can i load an XML to the last bullet? any help would be really appreciated.

You should use ViewListener instead of ImageListener. Try this
carouselView.setViewListener(viewListener);
ViewListener viewListener = new ViewListener() {
#Override
public View setViewForPosition(int position) {
if (position < 5) {
ImageView imageView = new ImageView(AboutHelp.this);
imageView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)); //setting image position
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(sampleImages[position]);
return imageView;
} else {
View customView = getLayoutInflater().inflate(sampleImages[5], null);
return customView;
}
}
};

Related

Why is the app-bar title change not working?

I have a collapsing toolbar. When it is collapsed, i set a app bar title, but once its expanded, i'm not able to remove the app bar title. The expand condition (verticalOffset == 0) is getting executed, but the title seem not getting changed.
public class MovieDetailsActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener{
private CollapsingToolbarLayout collapsingToolbarLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
appBarLayout.addOnOffsetChangedListener(this);
...
setTitle("");
...
}
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
//Closed
setTitle("Tmovies");
Log.i("test","Closed");
} else if (verticalOffset == 0) {
// Expanded
setTitle("");
Log.i("test","Expanded");
} else {
// Somewhere in between
}
}
}
use toolbar.setTitle(...) instead of setTitle(...)
You should prefer getSupportActionBar().setTitle("My Title"); instead of tool.setTitle("My Title"); as the latter might not work after setSupportActionBar .

android how to add ProgressBar to listView fetching data from parse?

I've looked everywhere for a tutorial, but couldn't find a tutorial that uses ProgressBar; they all use ProgressDialog which is simple but not in my need. So, can anyone give me an example?
Here's how I am setting the adapter:
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
list = (ListView) findViewById(R.id.list);
adapter = new mainAdapter(this);
list.setAdapter(adapter);
ParseQueryAdapter getItemView() class:
#Override
public View getItemView(Campaign campaign, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_single,null);
}
super.getItemView(campaign, v, parent);
ParseImageView mealImage = (ParseImageView) v.findViewById(android.R.id.icon);
ParseFile photoFile = campaign.getImage();
if (photoFile != null) {
mealImage.setParseFile(campaign.getImage());
mealImage.loadInBackground(new GetDataCallback() {
#Override
public void done(byte[] data, ParseException e) {
// nothing to do
}
});
}
else{
}
TextView titleTextView = (TextView) v.findViewById(R.id.title);
titleTextView.setText(campaign.getTitle());
TextView descriptionTextView = (TextView) v
.findViewById(R.id.description);
descriptionTextView.setText(campaign.getDescription());
return v;
}
Would be a pleasure if you didn't -1 without thinking. Thanks :)
ProgressBar's are pretty simple. In XML, I usually position them inside of a FrameLayout at the same level as the main content, such as a ScrollView. In done() after your data has been fetched, hide the ProgressBar and show the ScrollView (or whatever view holds your main content). Hope this helps.
EDIT: If fetching images, use INVISIBLE instead of GONE when hiding whatever layout/views contain your images while the ProgressBar is visible; otherwise the size of the images won't be calculated correctly.

How to use setVisibility on integer array in adapter list view by onclick on ImageView in android

I'm trying to invisible an image when it is clicked by storing images from drawable into integer array using adapter list view, but I'm unable to get it. This is the code I'm using:
When i click on an image it should get invisible.I am storing images in int array and applying setVisibilty invisible but its not working.i want an image to be displayed in centre of screen and the one which is clicked should get invisible.i am trying to store images in an integer array and setting it up in adapter list.i am calling this function
imageIDs[position].setVisible(false);
Integer[] imageIDs = {
R.drawable.c2,
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7,
R.drawable.c8
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Note that Gallery view is deprecated in Android 4.1---
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
//Adapter list
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
//onclick event
public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{//displaying image clicked i am trying to invisible this pic when click
Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",//dispplpaying msg
Toast.LENGTH_SHORT).show();
//imageIDs[position].setVisible(false);
// display the images selected
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
//setting image on screen from using xml
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
// sets a grey background; wraps around the images
TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
//imageIDs[position].setVisible(false);
//i am trying it here but its not working
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
I'm assuming you're trying to use this code:
//imageIDs[position].setVisible(false);
If so then what you're doing is calling setVisible on an Integer, which does not have that method. What you need to do is get a reference to the ImageView in which the image is being displayed and then call setVisibility(View.INVISIBLE) or setVisibility(View.GONE) on it.
Also it seems like you're trying to set the image to invisible but then you go and put the same resource back into the ImageView so I'm not sure what you're trying to do there.

changing imageview image inside a loop in android

i have some imageviews in my project with this id's: stagebtn_1,stagebtn_2,stagebtn_3 and...
by default all of them points to an image nad works perfectly...
now i want to change picture of some of them inside in a loop with this code:
for (int i=1;i<=3;i++) {
int imageViewId = getResources().getIdentifier("stagebtn_" + i, "id", "com.english.game");
ImageView imageView = (ImageView) findViewById(imageViewId);
imageView.setBackgroundResource(R.drawable.pic1);
}
but it doesnt work in the way that i want, this code doesnt change pictures, instead creates 3 new imageview with new picture...
how i can solve this?
you can use this way
public class MainActivity extends Activity {
ImageView imageView;
Integer[] image = { R.drawable.ic_launcher, R.drawable.tmp,R.drawable.android };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imageView = (ImageView) findViewById(R.id.img);
for(i=0;i<2;i++){
imageView.setImageResource(image[i]);
}
}
}

Create a clickable image in a GridView in Android

I have images displayed in a GridView as in this tutorial. I want to be able to click on a single image and do other events and I need to know what image was clicked.
Do I have to add imageView.onKeyDown(keyCode, event) in the ImageAdapter class? Here is the code as it currently exists:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
//does this need imageView.onKeyDown(keyCode, event)?
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
How will it indicate what image was clicked? How do I create the proper handler?
For a GridView you can use the setOnItemClickListener method to have an OnItemClickListener listener. That listener will give you a method that you must override with the signature
onItemClick(AdapterView<?> parent, View v, int position, long id)
where you get the position of the item in the grid that was clicked and the View that is inside the cell of the grid. Is that what you need?
I was able to get the position of the clicked image by making the position final and adding an onClick listener to the imageView. This logs the position of the image that was clicked.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("onClick","position ["+position+"]");
}
});
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
i tried the above mentioned method getView(final int position . . .)
realized that the position got "reset" after 28 items and the position when back to 0 after the 28th item in the gridview.
i suspected the final keyword is creating problem and after removing it i was able to get the positions as expected.
Below is a sample code with the click event being called in the activity that is showcasing the gridview.
public class MainActivity extends Activity {
ArrayList<Integer> item_ids = new ArrayList<Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
item_ids = //get your item ids method
GridView gridview = (GridView) findViewById(R.id.grid_featured);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(itemClickListener);
footer = (Footer)findViewById(R.id.layoutFooter);
footer.setActivity(this);
}
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG,"Position Clicked ["+position+"] with item id ["+item_ids.get(position)+"]");
}
};
}

Categories