I want to open a horizontal ScrollView with images by clicking on a central image. I already have my ScrollView loading dynamically my images:
int[] images = new int[] { R.drawable.image1, R.drawable.image2, R.drawable.image3 };
LinearLayout sv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advanced);
sv = (LinearLayout) findViewById(R.id.ScrollView);
for (int i = 0; i<images.length; i++) {
ImageView iv = new ImageView(this);
iv.setBackgroundResource(images[i]);
sv.addView(iv);
}
}
I have following problem, right now the ScrollView is already expand, but I want the ScrollView to expand when clicking on the last selected image from the ScrollView (and a default one if its the first time). I dont know if ScrollView is the right way to do that or if there is a better method for that. I already searched on Stackoverflow and found similiar questions but no of them did really fit my problem.
Hope someone can help me
If this question is already answered somewhere and I didnt found it, send me the link please.
Thanks in advance
Related
I have a layout for recyclerview adapter that it's like a instagram. That means it has a slider for images and like button and etc.
Now I want to load multiple images in the slider and clients should change images with dragging like instagram exactly.
First I used PosterSlider library, but its problem is that doesn't have scale type so I cannot manage images size.
So now I wanna try ViewFLipper, but my problem is I don't know how can I use ViewFlipper like a slider.
That means how can I change images by dragging in ViewFLipper?
If you have another idea for changing picture, I will be glad to hear it.
This is my code for ViewFlipper that I know it is wrong, because it shows just one Image and it doesn't have any code for changing picture by dragging:
try {
JSONArray jsonArray = new JSONArray(newsLetterList.get(position).Jsonsrc);
for(int i = 0;i < jsonArray.length(); i++){
setImageInFlipper(jsonArray.getString(i),holder);
}
} catch (JSONException e) {
e.printStackTrace();
}
setImageInFLipper method:
private void setImageInFlipper(String imgUrl,NewsLetterViewHolder holder){
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(context).load(imgUrl).into(imageView);
holder.slider.addView(imageView);
}
You can take some reference from here. They have implemented the same viewflipper slide both with next(flip) button and without using any button. Hope that helps!
I am trying to add buttons to a GridView for each string from textArray.
void addButtons() {
GridView gridView = (GridView) findViewById(R.id.gridView);
List<Button> buttons = new ArrayList<Button>();
for (int i = 0; i < textArray.length; i++) {
Button newButton = new Button(this);
newButton.setText(textArray[i]);
newButton.setId(i);
newButton.setOnClickListener(onClickListener);
buttons.add(newButton);
}
ArrayAdapter<Button> arrayAdapter = new ArrayAdapter<Button>
(this, android.R.layout.simple_list_item_1, buttons);
gridView.setAdapter(arrayAdapter);
}
But in result, I get this: Virtual Device screen
What can be wrong? Or maybe there is a better way to do the same thing? I've tried LinearLayout and everything was ok, but I was not able to scroll down.
how to add button in gridview dynamically might have a solution, but, to be honest, it is too hard for me at the moment.
Try this out, it shows how to create buttons during runtime https://forums.xamarin.com/discussion/49225/dynamic-buttons-in-gridview
So the initial problem was to get a view with programmatically generated buttons. The first solution was a LinearLayout, but I found out that I was not able to scroll it down, so the number of buttons was limited. On the internet, I found that GridView also can do such a task and it would be scrollable. Well, it is true, but I have stuck upon the problem mentioned in this thread.
After a few days of googling I found that LinearLayout inside ScrollView is what I need. So here is both my xml and java code for someone who stumbles up on the same problem:
<ScrollView
<LinearLayout
android:id="#+id/cityLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
void addButtons() {
LinearLayout linearLayout = findViewById(R.id.cityLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < cityArray.length; i++) {
Button newButton = new Button(this);
newButton.setText(cityArray[i]);
newButton.setId(i);
newButton.setOnClickListener(onClickListener);
linearLayout.addView(newButton, layoutParams);
}
}
Anyway, thanks to everyone for help!
I have the following scenario:
In a layout, I have imageviews aligned horizontally and vertically as shown in image.
First, which layout should be used for this purpose? RelativeLayout or FrameLayout? ListView inside the layout?
Also, instead of writing setOnClickListener for every imageview, how can I write just one click listener to get the clicked imageview?
A GridView is perfect for this. For more information on GridView, look here.
As for your onClickListener question: there is no easy way to do this, but what you can do is something like this:
Have an array containing every ImageView id like so:
public static final int[] IMAGE_VIEWS = {R.id.imageView1, R.id.imageView2, R.id.imageView3 /*etc*/}; //Make sure to list from the first imageview to the last image view in correct order
Define an onClickListener
private View.OnClickListener imageViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < IMAGE_VIEWS.length; i++) {
if (v.getId() == IMAGE_VIEWS[i]) {
doSomethingWithImageViewClick(i); //if ImageView 4 was clicked, variable 'i' will be 3 (because arrays start at index = 0).
}
}
}
}
Set the onClickListeners for all your imageViews:
final ImageView view1 = (ImageView) view.findViewById(R.id.imageView1);
view1.setOnClickListener(imageViewListener);
//etc
Use 'GridView' in the layout and use 'setOnItemClickListener' to handle click events,
You can find more details in below link
GridView.
GridView can achieve the effect. I think you can look at this Grid View.
Is it possible to make one element overlap others in GridView as it is shown on screenshot here: https://bigbob.lv/elem.png? I mean blue element.
I would like to distinguish current level this way.
I have tried bringToFront(); method in getView(); method of my custom ArrayAdapter but it doesn't work. I tried it this way:
if(i == parentActivity.currentLevel) {
RelativeLayout parentLL = (RelativeLayout) gridView.findViewById(R.id.levelLL);
ViewGroup.LayoutParams rp = (ViewGroup.LayoutParams) parentLL.getLayoutParams();
rp.width = parentActivity.getRelativeWidth(20f);
parentLL.bringToFront();
parentLL.requestLayout();
}
Thanks!
I am using custom adapter for listview where I put 4 sections.
Now i have photo section in which I put HorizontalScrollView. I got bunch of images from server so put them in string array.
if (postDisplay.getImageUrl().equals("null")) {
holder.photo_layout.setVisibility(View.GONE);
} else {
holder.photo_layout.setVisibility(View.VISIBLE);
String SharePhotos=postDisplay.getImageUrl();
String temp=SharePhotos.replace("small", "big");
String[] SharePicArray=temp.split("~");
for (int i=0; i<SharePicArray.length; i++) {
ImageView imageview = new ImageView(activity);
imageview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
imageLoader.DisplayImage(SharePicArray[i], imageview);
String URL=SharePicArray[i];
holder.image_layout.addView(imageview);
}
}
When I run this code first time it gives me right solution but when I scroll down and then scroll up to see again the images it again create another imageview.
If there is any other solution please let me know.
If there is any other view I can take to show images from server.