Custom ListView? - android

I have a code made by Fedor, it can be found "here".
The first image is what I have now,
and the second image is what I want to accomplish.
Can someone guide me with this. I have been struggling for days trying to solve this problem.
Please help me, Thanks in advance!

Here is an example of something similar. You have to create a custom adapter for your ListView.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
You can probably use most of that example. Just change the row.xml to create tha layout you want and the getView() in the adapter.

You just need to modify the list item layout (in item.xml) to have another ImageView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="#+id/image1" android:layout_width="50dip"
android:layout_height="50dip" android:src="#drawable/stub"
android:scaleType="centerCrop" />
<ImageView android:id="#+id/image2" android:layout_width="50dip"
android:layout_height="50dip" android:src="#drawable/stub"
android:scaleType="centerCrop" />
<TextView android:id="#+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_gravity="left|center_vertical" android:textSize="20dip"
android:layout_marginLeft="10dip" />
</LinearLayout>
and modify LazyAdapter's getView() method to add support for the second ImageView.

A tutorial for creating a Custom ListView can be found here:
http://justcallmebrian.com/?p=139
Just need to change the XML layout for each item to have 2 ImageViews like Robby said. Then in your getView of your Adapter (LazyAdapter if you followed along with the other people's answers) you should have something like this:
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setResource(R.drawable.icon1);
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setResource(R.drawable.icon2);
TextView text = (TextView)findViewById(R.id.text);
text.setText("I have 2 images");
The tutorial I pasted earlier depicts a way to make the generation of the list dynamic (i.e. not having the resource of R.drawable.icon1/2 and not having the text for your Text images). Something like this may work (assuming you have a Model class that will hold all 3 pieces of information):
int resid1 = context.getResources().getIdentifier("com.domain.sub:drawable/" + myList.get(position).getImage1Name, null, null);
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setResource(resid1);
int resid2 = context.getResources().getIdentifier("com.domain.sub:drawable/" + myList.get(position).getImage2Name, null, null);
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setResource(resid2);
TextView text = (TextView)findViewById(R.id.text);
text.setText(myList.get(position).getText());
Of course the snippet above assumes you have an ArrayList called myList that also getters to get the image names and the text to display.

Related

setImageAlpha of imageView causing error

I would like to set the alpha of this:
<ImageView
android:id="#+id/DashboardView_upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:src="#drawable/ab_logo"
android:paddingLeft="#dimen/layoutPaddingExtra"
android:paddingTop="#dimen/layoutPaddingExtra"
android:paddingRight="#dimen/layoutPaddingExtra"
android:paddingBottom="#dimen/layoutPaddingLess"
android:contentDescription="Up"
android:layout_gravity="center_horizontal" />
I have correctly got the resource in code and called it _upButton. This:
_upBtn.setImageAlpha(20);
Gives this:
android.widget.imageview does not contain a definition for setImageAlpha
Why?
The ImageView needs to be casted to an ImageView to allow the imageview to access the functions within the ImageView.
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
img.setImageAlpha(20);
If you are using an API below API-16, then please use:
img.setAlpha(20);
instead of:
img.setImageAlpha(20);
Good luck!

Android adding dynamic ImageViews to HorizontalScrollView does not work

I am desperately trying to achieve this for a quite long time but still could not make it work.
I have a ListView where in inside my Adapter I am trying to add image views dynamically based on my data. But, ScrollView does not show up with added images. When I tried myLinearLayout.getChildCount(), it gives proper results!!
What am I doing wrong? Any help is much appreciated :)
<!-- ScrollView (This lies inside listView's child view) -->
<HorizontalScrollView
android:id="#+id/card_txn_horizontal_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<LinearLayout
android:id="#+id/scroll_view_linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#color/header_bg">
</LinearLayout>
</HorizontalScrollView>
LinearLayout sharedWithView = (LinearLayout) convertView.findViewById(R.id.scroll_view_linear);
for (User sharer : data.getSharerModelList()) {
ImageView mImageView = (ImageView) mInflator.inflate(R.layout.photo, null);
Picasso.with(mImageView.getContext())
.load(expense.getPayeeModel().getProfilePicUrl())
.centerCrop()
.resize(QuickReturnUtils.dp2px(mImageView.getContext(), 38),
QuickReturnUtils.dp2px(mImageView.getContext(), 38))
.placeholder(R.drawable.ic_launcher)
.error(android.R.drawable.stat_notify_error)
.into(mImageView);
sharedWithView.addView(mImageView);
}
try android:layout_width="wrap_content" inside the LinearLayout

How to make a view as always on top?

I have a TextView and it is on an image. When I click the button, TextView's background color will change, but image won't disappear. For example:
My TextView at the beginning:
When I click a button:
If you want to do this using only one TextView then its may be not possible. So, I will suggest you to do this using FrameLayout. you can write your layout as below.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
When you want to change the color behind the TextView then change the ImageView background as below...
ImageView mageView view = (ImageView) findViewById(R.id.image);
view.setBackgroundColor(Color.BLUE);
If it must be a TextView and an ImageView you could also wrap it within a FrameLayout (which is supposed to contain only one child). A FrameLayout will place all containing children elements on the same "place", so the get overlapped.
Or when you need more elements, you could also use a RelativeLayout and give your elements the same layout rules (e.g. all centered).
You should use ImageView instead of TextView, the:
public void onClick(View v){
imageView.setBackgroundColor(Color.GREEN);
}
ImageButton is better option for you.Image source will be star as u said.then onclick you change the background. if want set Text in this
android:drawableTop="#drawable/any_drawable"
android:text="#string/any_text"

how to fix this imageview error?

Allow me to explain. I have :
a button with picture (located at #drawable/pic),
linear layout (id=linear1)
the button xml is below :
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:src="#drawable/pic"
android:maxWidth="80dp"
android:maxHeight="80dp"
tools:ignore="ContentDescription" />
the linear layout xml is as follow :
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center"
android:orientation="horizontal"
tools:ignore="UselessLeaf" >
what i want is, when i click the button i want to create/generate imageview programatically inside the linearlayout and i want to fill it with the same picture for the button (pic). The code is below :
//initiate imageview
ImageView img=new ImageView(this);
//get drawable from button
Drawable blabla=btn1.getDrawable();
//set drawable to imageview
img.setImageDrawable(blabla);
//set height and width of imageview to 50dp
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(50,50);
img.setLayoutParams(parms);
img.setScaleType(ScaleType.FIT_XY);
//place imageview to linearlayout
layoutTempat.addView(img);
The code works fine with displaying the imageview with the same image as the button have.
however the problem is : when i set the imageview to 50dp programatically, the image inside button changed too.. how can it happened ? i am so confused as iam a newbie..
Thanks before.
The two views are sharing the same drawable.
It's plausible your manipulations on one view are being sent to the underlying drawable, effecting how its displayed in the other view -- frankly I don't know. But assuming the case is as you described, this problem is easily fixed by cloning the drawable as follows:
Drawable dr = btn1.getDrawable().getConstantState().newDrawable();
img.setImageDrawable(dr);

default text in android layout

in instagram app, when image is not available it says
"tap to retry"
and when image is available it loads image.
my app needs something like that, if information is available it loads information onto the linear layout, if not available it returns nothing so my layout remains blank.
so how do i add the text like instagram like " no information available".
I tried using
android:hint
but it doesnt seem to be a good option.
Not sure how you have anything set up, but I would think something like this would work. Have your ImageView and TextView fill the same space, then toggle which one is visible and which one isn't based on the presence of the image. Hope this helps
Layout:
<RelativeLayout
... >
<ImageView
...
/>
<TextView
...
/>
</RelativeLayout>
Logic:
if(isImageAvail){
iv.setVisibility(View.VISIBLE);
tv.setVisibility(View.GONE);
}else{
iv.setVisibility(View.GONE);
tv.setVisibility(View.VISIBLE);
}
So you'd have your drawable...
Drawable imageDrawable = codeToGetYourImage;
TextView errorTxt = (TextView)findViewById(R.id.your_text_id);
ImageView imageView = (ImageView)findViewById(R.id.your_image_id);
if(imageDrawable == null){
imageView.setBackgroundDrawable(imageDrawable);
} else {
imageView.setVisibility(View.GONE);
errorTxt.setVisibility(View.VISIBLE);
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/your_text_id"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sorry, image cannot be displayed"
android:id="#+id/your_image_id"/>
</LinearLayout>

Categories

Resources