I have several images in my mipmap folder and I want to add them to the screen at a specific location and then move them. Here is the code I thought should work:
ImageView item = new ImageView(this);
item.setImageResource(R.mipmap.ball);
Where ball is a valid png file. Why doesn't this code show the image?
i assumed that your xml is look like this.
Xml code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout">
</RelativeLayout>
And try this one in your class file(i.e in activity)
RelativeLayout layout=(RelativeLayout)findViewById(R.id.layout);
ImageView imageView=new ImageView(this);
imageView.setImageResource(R.mipmap.add_symbol);
//which adds the imageview to your layout
layout.addView(imageView);
As stated in the comments, you are creating an image view unattached to any view. The instance is there, but it doesn't know where to render.
If you know where you want to insert it, lets say, some LinearLayout with id image_container, you could do something like (and I'm being very simplistic about it!):
ImageView item = new ImageView(this);
item.setImageResource(R.mipmap.ball);
LinearLayout layout = activity.findById(R.id.image_container);
layout.addView(item)
You might also want to use item.setScaleType( ... ) in order to get the desired scaling.
That being said, please keep in mind that the proper way of adding several items programatically would be using something like a RecyclerView.
Also, in order to get a nice performance for you images loading and rendering, something like Glide or Picasso would be very useful.
Related
I have designed the red highlighted layout like the picture:
What I have used is LinearLayout with orientation. Here an ImageView and Two Textviews are repeated in four times. But as far my knowledge I designed it using LinearLayout. So I have to write every time the same design code for four times.
Is there any better way to design it so that I have to write it one time instead of four times.
My code for the highlighted portion is [here](https://pastebin.com/C9ZHDaZV).
Create one layout called for example weather_layout.xml and place your ImageView and Two Textviews inside, then just use include four times in your final LinearLayout like this:
<LinearLayout
... >
<include
id="+#/top_left"
layout="#layout/weather_layout"
... />
<LinearLayout/>
And then you can access like this:
LinearLayout topLeft = (LinearLayout) mainView.findViewbyId(R.id.top_left)
ImageView v = (ImageView) topLeft.findViewbyId(R.id.imageView)
Use Grid Layout. That way you won't need to handle the spacing and partitioning yourself. One line of of code will suffice.
I'm looking to replicate airbnb's app image slider where you have one image at the top of a view that you can horizontally swipe and it automatically goes to the next image upon swipe. I created a horizontalscrollview but it doesn't quite have the right functionality. It doesn't flip from image to image, upon swipe it sort of scrolls along the image and eventually to the next image. I've googled around quite a bit and haven't seen a non deprecated solution.
Any ideas?
EDIT - adding some of my code per request:
Basically at a high level I'm pulling in imageURLs from my AWS database (in a different class not shown below) and then trying to load those URLs into a horizontalscrollview using Picasso.
//generic instructions to allow insertion of pictures into a horizontalimageslider
public View insertPhoto(String path){
LinearLayout layout = new LinearLayout(getActivity());
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getActivity() ).load(path).into(imageView); //tried this but got errors when running > resize(layout.getWidth(), layout.getHeight()), also tried .fit() after .load image wouldn't load
layout.addView(imageView);
return layout;
}
private class GetApartmentTask extends AsyncTask<ApiConnector,Long,Apartment >
//after a doInBackground
#Override
protected void onPostExecute(Apartment apartment) {
//to get image URL from database to then use with Picasso to download the image
mImageURLArraylist = mApartment.getApartmentImageArrayList();
for (int z = 0 ;z<mImageURLArraylist.size(); z++) {
mLinearLayout.addView(insertPhoto("http:" + mImageURLArraylist.get(z)) );
}
}}
in onCreateView method (alongside other things obviously)
mLinearLayout = (LinearLayout) v.findViewById(R.id.details_page_apartment_picture);
MyXML (relevant part)
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="500dp"
android:fillViewport="true">
<LinearLayout
android:id="#+id/details_page_apartment_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
After many hours of frustration and some help getting over the finish line, the answer to swiping some part of the screen in this way is to use a viewpager.
Some great references:
swipe some part of the screen
http://developer.android.com/training/animation/screen-slide.html
What is the role of " isViewFromObject (View view, Object object)" in FragmentStatePagerAdapter?
my working code is provided in another question I posted that got me over the finish line :) --
Android ViewPager Won't Show Second Page?
I don't know the specifics of the airbnb app image slider, but it sounds like you could use flexslider. It's basically a JQuery toolkit. I have used it before in a website and it seems to have the functionality you are looking for.
https://www.woothemes.com/flexslider/
I want to customize my listview to achieve this design,are there any library available for this?what can be best way to achieve this.Are there any disadvantage with such designs.
Thanks,
You can achieve this in two different ways
add an image instead of divider containing these tilt lines you can do this by using a simple line of code in your layout or in your class
android:divider="#drawable/divider_image"
or you can change it in you java class like this
list.setDivider(R.drawable.divider_image);
the second way of achieving this is you set a static image in you item layout below the item and change it according to your need as if the item is odd set a different image and if the item is even set a different image
<ImageView
android:id="#+id/imageViewDivider"
android:src="#drawable/divider_image"
android:scaleType="fitXY" />
and change the image in java class
ImageView imageView = (ImageView) findViewById(R.id.imageViewDivider);
imageView.setImageResource(R.drawable.divider_image);
I want on the initialization of my activity in android to set the position of some imageView's
by code.
Lets say I have five cards displayed on the screen, all placed in (0,0) by me in the XML.
I want to calculate the screen size (easy to do) and then place the first card at 0.2height , 0.2 width the second one 0.4height, 0.4 width, ETC.
I want to do it through code so i could change some constants in the future and the rest of the changes will apply automatically
Thanks.
Create a LinearLayout container view in your layout, and then use code to add your new View to it
<LinearLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
And then:
((ViewGroup)findViewById(R.id.container)).addView(myImageView);
try to use "findViewById()" to inflate some parental layouts,
"new ImageView()" to construct new Views and ".add(myImageView)" to add them.
In your code u can use all kinds of setters for the imageview...
(btw: more details would be great, the question is kinda ambiguous)
Please refer the image given in the url
http://docs.google.com/Doc?docid=0AQhgDtGvE2HgZGZ6cmtua185M2RneG5nYmNm&hl=en
My query is, How can I display the messages corresponding to the rounded buttons and the table row , when I click on the rounded button with question mark.
I know, I have to use listener for the ? button , but what should I do in listener exactly, such that when I click, it shows those alerts(images) and when I click again, it disappears.
For this UI, I have used Relative layout as was suggested here -- Aligning components at desired positions -- and it worked perfect for me.
So, do I need to change my base layout altogether for accomplishing this?
You can use a FrameLayout as the base for your ui layout and then add an ImageView overlay. For example:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Put your normal layout stuff here -->
</FrameLayout>
Then in your code you can create the ImageView and add it to the MainFrame and it will overlay your UI, like this:
FrameLayout mainFrame = (FrameLayout)findViewById(R.id.MainFrame);
ImageView overlay = new ImageView(this);
overlay.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.overlay));
mainFrame.addView(overlay);
Then later you can call:
mainFrame.removeView(overlay);
to have it go away.