i am trying to use an ImageView over an ImageView in a GridView. I already found this quesiton Android, ImageView over ImageView and it is really familiar with my problem, but i want to use this Layout descripted in the question inside a GridView.
So i created a .xml-file containing the <GridView></GridView> (i think this is not really useful to show, so i skip this file) and a .xml-file with the layout i want to use in every cell in the gridview. This is the following file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rl_act_question_list_alternative" >
<ImageView
android:id="#+id/iv_question_list_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/germany" />
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/icon_question_solved" />
As the next step i am creating a subclass of a BaseAdapter overriding the method getView(int position, View convertView, ViewGroup parent) with the following code:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ActivityLevelsAlternative.this).inflate(R.layout.act_question_list_alternative_item, null);
}
ImageView mCountry = (ImageView) convertView.findViewById(R.id.iv_question_list_country);
ImageView mIconSolved = (ImageView) convertView.findViewById(R.id.iv_question_list_solved);
final VOQuestion question = mLevel.getAllQuestions().get(position);
mCountry.setImageResource(question.getPicture());
mIconSolved.setImageResource(mPrefManager.isQuestionSolved(question.getId()) ? R.drawable.icon_question_solved : R.drawable.icon_question_unsolved);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), ActivityQuestion.class);
i.putExtra(ActivityQuestion.QUESTION_ID, question.getId());
ActivityLevelsAlternative.this.startActivity(i);
}
});
return convertView;
}
But the the result is not the expected... see: http://q63i.img-up.net/Screenshotc656.png
My question: how can i get the check mark over the country image?
Try to change your check mark image like this:
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignTop="#id/iv_question_list_country"
android:layout_alignLeft="#id/iv_question_list_country"
android:layout_alignRight="#id/iv_question_list_country"
android:layout_alignBottom="#id/iv_question_list_country"/>
Related
I would like to populate my listview with subitems.
My target is, when someone clicks at a listview row, this listview shows a subitem, just like this:
Image
I shouldn't call this a subitem, but I found that this was the best way to describe what I'm trying to do.
I'm not using libraries to do that, I just included in my listview rows an layout, and I'm trying to hide and show it when someone clicks on it.
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="400sp"
android:orientation="horizontal"
android:padding="2sp"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200sp"
android:id="#+id/relativeLayout3">
</RelativeLayout>
<include layout="#layout/activity_subitem"
android:layout_width="wrap_content"
android:id="#+id/subitem"
android:layout_height="200sp"
android:layout_below="#+id/relativeLayout3"
android:layout_alignParentStart="true" />
</RelativeLayout>
I built a simple adapter, and I'm trying to implement there my ideas, but is not working, because nothing happens
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View v = super.getView(position, convertView, parent);
layout = (RelativeLayout) v.findViewById(R.id.layout);
subitem = v.findViewById(R.id.subitem);
if (flag){
subitem.setVisibility(View.GONE);
layout.getLayoutParams().height = 200;
}
v.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subitem.setVisibility(View.VISIBLE);
layout.getLayoutParams().height = 400;
flag = false;
}
});
return v;
}
Can you guys help me?
Thank you.
I have a GridView adapter(For gallery used).
What i want is to put Image(Check Image) when the photo is clicked.
But I am unable to do that with this code.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
cbClicked = false;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.imagelayout, parent, false);
final CheckBox cb = (CheckBox) view.findViewById(R.id.name);
final ImageView img = (ImageView) view.findViewById(R.id.img);
final LinearLayout imgTop = (LinearLayout) view.findViewById(R.id.imgTop);
ImageView img2 = new ImageView(ActivityReviewUpload.this);
img2.setImageResource(R.drawable.ic_my_loc);
imgTop.addView(text);
return view;
}
And for my XML is this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/imgTop"
android:layout_width="match_parent"
android:layout_height="120dp">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="120dip"
android:background="#color/colorBlack"
android:padding="2dp" />
</LinearLayout>
<CheckBox
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
This is an example of what I'm doing.
In this example it's number,
what I'm doing is i will put just CHECK in the selected images.
The image is not showing.
what am i doing wrong in this part?
thanks
There's some problem with your BaseAdapter:
You should use ViewHolder or ReyclerView with GridLayoutManager to avoid laggy when scrolling your ListView.
Look at this block of code:
ImageView img2 = new ImageView(ActivityReviewUpload.this);
img2.setImageResource(R.drawable.ic_my_loc);
imgTop.addView(text);
Above code will be called everytime you sroll your ListView. Therefore, you always add new ImageView to each row when scrolling. You should remove it.
How to achieve what you want? Do it as below:
// declare selectedPosition = -1 in your `Adapter`
// in your get view added below code:
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = position;
notifyDataSetChanged();
}
});
cb.setChecked(selectedPosition == position? true: false);
Those code will change checkbox state every time you click to your image.
I have a ListView (with an Adapter of my own) where I'd like to add a delete button by swaping an item on the ListView (like on iPhones).
I really don't know how to do it and where to start ...
Could you please give me some hints ?
Thanks
What you want to implement is a custom ListView. You need a layout for your row, here's an example
res/layout/row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
<TextView android:id="#+id/Browse_DateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
<ImageButton android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
Then you need to overide the getView() method of your adapter, a little like this:
setListAdapter(new ArrayAdapter<Object>(this, R.layout.row, R.id.Browse_DateTime, ourRows) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)BrowseActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView time = (TextView) v.findViewById (R.id.Browse_DateTime);
time.setText(ourRows[position].dateTime);
ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
delete.setFocusable(false);
delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));
delete.setOnClickListener(BrowseActivity.this);
delete.setId(position);
return v;
}
};)
Best regards.
PS: this is cut&paste from my code, BrowseActivity is just the name of the activity this code resides in, R.layout.row is my row.xml file, you name it any which way, just put it in /res/layout/, and if your delete button is an imagebutton, you DO need the delete.setFocusable(false); (try it without and see why).
I am not quite sure my question really has something to do with listview.There is an app named Gleeo Time Tracker ,and here has a screenview of it.When you press the symbol "+",a new item will be created,and you can delete one item by pressing the "-".More is that when I click the record button on the left of the item,the background will change.
My question is,what is it in the end,a listview? How can I achieve such thing?Thank you all!
What you want to do, is build a custom ListView.
This is done by providing a layout file, for example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
<ImageView android:id="#+id/Plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
<TextView android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
</LinearLayout>
This layout is then applied to each row of your ListView by overriding its Adapter's getView() method, for example something like this:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
ImageView plus = (ImageView) v.findViewById(R.id.plus);
icon.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.plus));
TextView title = (TextView) v.findViewById (R.id.Browse_FileName);
// add a listener to your button and you're done
return v;
}
read about list view in developer docs and here is an example
Thanks for reading!
I am building a custom Gallery app where the first thumbnail is an album cover displaying album details. Here's the flow:
getView() {
//inflate cover.xml which includes two textviews and an imageview.
if(position == 0)
//set some album-specific text
else
//set image-specific text
}
Here's the actual getView() code:
public View getView(int position, View convertView, ViewGroup parent) {
//TODO: Recycle view
convertView = mInflater.inflate(R.layout.cover, null);
TextView tvTxt1 = (TextView)convertView.findViewById(R.cover.tvCoverText1);
TextView tvTxt2 = (TextView)convertView.findViewById(R.cover.tvCoverText2);
//ImageView imgView = (ImageView)convertView.findViewById(R.cover.imgImage);
if(position == 0) {
tvTxt1.setText("AlbumText1");
tvTxt2.setText("AlbumText2");
return convertView;
}
else {
tvTxt1.setText("ImageText1");
tvTxt2.setText("ImageText2");
ImageView imgView = new ImageView(mContext);
imgView.setImageResource(mImageIds[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(mGalleryItemBackground);
return imgView;
//return convertView;
}
}
The cover.xml contains an ImageView and two TextViews.
when I return convertView in the else block, I get a ClassCastException. I am certainly doing something wrong.
I have spent almost two days on this now :(
Please help!
Here's what it looks like to me. When position == 0 you are returning convertView, which is a View. When you return "else", you are returning a ImageView. Your method is set to return a View. Try casting your ImageView to a View before returning it.
Try: return (View) imgView;
Never tried it myself though...
Add this imageview into your layout xml, and then retrieve it from convertview and at the end return the convert view.
This may solve the problem.
I have worked a lot on Gallery widget, if there is more problem do let me know.
After trying all the suggestions given by helpful people here,I still wasn't able to get across the ClassCastException.
So, as a workaround - I sort of "overlayed" the Gallery with other views that I wanted to enable/disable.
This is a workaround, so if someone comes up with a better answer - do post it here so I can accept it.
So here's what worked for me:
public View getView(int position, View convertView, ViewGroup parent) {
//TODO: Recycle view
//onvertView = mInflater.inflate(R.layout.cover, null);
//ImageView imgView = (ImageView)convertView.findViewById(R.cover.imgImage);
ImageView imgView = new ImageView(mContext);
imgView.setImageResource(mImageIds[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(mGalleryItemBackground);
if(position == 0) {
tvText1.setText("AlbumText1");
tvText2.setText("AlbumText2");
tvText3.setVisibility(View.VISIBLE);
bottomBar.setVisibility(View.VISIBLE);
}
else {
tvText1.setText("ImageText1");
tvText2.setText("ImageText2");
tvText3.setVisibility(View.GONE);
bottomBar.setVisibility(View.GONE);
}
return imgView;
}
Here's my layout main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Gallery android:id="#+main/gallery" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- <ImageView android:id="#+main/imgImage" -->
<!-- android:layout_width="fill_parent" android:layout_height="fill_parent" -->
<!-- android:adjustViewBounds="true"> -->
<!-- </ImageView> -->
<TextView android:id="#+main/tvText2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:singleLine="true"
android:maxLines="1" android:text="Text2"
android:layout_alignParentBottom="true" />
<TextView android:id="#+main/tvText1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:maxLines="2"
android:text="Text1" android:layout_above="#main/tvText2" />
<RelativeLayout android:id="#+main/bottomBar" android:layout_alignParentBottom="true"
android:layout_width="fill_parent" android:layout_height="40dip"
android:background="#A3A1A1">
<TextView android:id="#+main/tvBottomText" android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="BottomBarText"/>
</RelativeLayout>
</RelativeLayout>
The rest of the code in Main.java (whose getView method I modified) is almost verbatim from here
Thanks again for helping out!