I have a image button and a image view. I am trying to get a hover theme,like when the user clicks an item there will be a hover image over the item which means he checked the item. My xml:
layout1:
<?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"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/fetchedImageObj"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="#drawable/test"
android:background="#drawable/image_selector"
android:clickable="true"
android:padding="0dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/hoverImage"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerInside"
android:src="#drawable/correct"
android:visibility="gone"/>
</RelativeLayout>
And this is my adapter where i am trying to do the task. But the problem whenerver i select a item and scrolls down it disappears or switch to other object
Adapter:
public class FetchItemAdapter extends BaseAdapter {
private Activity myContext;
private LayoutInflater inflater;
public FetchModel fetchModel;
private boolean check_click=false;
public FetchItemAdapter(Activity context,FetchModel fetchModel)
{
this.myContext=context;
inflater = context.getLayoutInflater();
this.fetchModel = fetchModel;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return this.fetchModel.images.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private static class ViewHolder {
ImageButton fetchedImageObj;
ImageView selection;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
System.out.println("get View");
final ViewHolder viewHolder = new ViewHolder();
convertView = inflater.inflate(com.yolove.R.layout.fetched_images_row,null);
//viewHolder = new ViewHolder();
viewHolder.fetchedImageObj = (ImageButton) convertView.findViewById(com.yolove.R.id.fetchedImageObj);
viewHolder.selection=(ImageView)convertView.findViewById(com.yolove.R.id.hoverImage);
convertView.setTag(viewHolder);
ImageLoader.getInstance().displayImage(fetchModel.images.get(position).imageurl,viewHolder.fetchedImageObj);
viewHolder.fetchedImageObj.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(check_click==false)
{
viewHolder.selection.setVisibility(View.VISIBLE);
check_click=true;
}
else
{
viewHolder.selection.setVisibility(View.GONE);
check_click=false;
}
}
});
return convertView;
}
}
I have made my holder final to get the viewholder.selection object.
On my opinion you need to make a selector xml for your image background and set the image view visible in xml file.
Related
I have a ListView with custom list items using a BaseAdapter and ViewHolder. Each list item has two Buttons, and a TextView. I have set click listeners for both buttons. Now the onItemClickEvent for the ListView does not work. I think the buttons consume the click event or something similar.
I can find out which button was clicked, but I can't get the row position,
as the ViewHolder is recycled. I cant do this without ViewHolder` as there is high possibility of the number of list items can become very large.
How can i get the row position when a button is clicked .
I tried this after doing some googling , But still there is no change in the out come.
Could someone give any suggestions.
Below is my ListItem Layout and BaseAdapter
ListItem
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/llv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Large Text"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
BaseAdapter
class MyBaseAdapter extends BaseAdapter
{
LayoutInflater li;
Context context;
public MyBaseAdapter(Context context)
{
this.context=context;
li=LayoutInflater.from(context);
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return 10;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO u-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
holder=new ViewHolder();
convertView=li.inflate(R.layout.listitem, parent,false);
holder.iv1=(ImageView) convertView.findViewById(R.id.imageView1);
ImageView iv=(ImageView) convertView.findViewById(R.id.imageView2);
iv.setTag(position);
holder.iv2=iv;
holder.tv1=(TextView) convertView.findViewById(R.id.textView1);
holder.pos=position;
holder.iv1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(context, "IV1 "+holder.pos,
Toast.LENGTH_SHORT).show();
}
});
holder.iv2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int x=(int) v.getTag();
Toast.makeText(context, "IV2 "+x,
Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(holder);
}
else
{
holder=(ViewHolder) convertView.getTag();
}
holder.tv1.setText("LIST ITEM "+position);
return convertView;
}
class ViewHolder
{
TextView tv1;
ImageView iv1,iv2;
int pos;
}
}
You have to set position every time.
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView=li.inflate(R.layout.listitem, parent,false);
holder.iv1=(ImageView) convertView.findViewById(R.id.imageView1);
holder.iv2 =(ImageView) convertView.findViewById(R.id.imageView2);
holder.iv1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// get position from tag.
int selection = (int) v.getTag();
Toast.makeText(context, "IV1 "+selection,
Toast.LENGTH_SHORT).show();
}
});
...
} else {
holder = (ViewHolder) convertView.getTag();
}
// set position.
holder.v1.setTag(position);
...
I am trying to create a master/detail flow application on Android 4.2.
I have created a master detail flow project, but I want to implement an ExpandableListView instead of the ListView that is provided.
The master/detail uses fragments, and here’s where I am stuck… I have successfully created an expandable list view in a separate project. How can I implement it inside a master/detail flow?
I assume you used the IDE wizard to create the Master/Detail example project. If this is so, then you probably saw that the wizard created an ItemListFragment class which by default extends ListFragment.
If you need to replace the simple list with an expandable one, then you'll have to:
Extend from Fragment, instead of ListFragment
Create an xml layout file where you declare an ExpandableListView
Override onCreateView() and inflate the layout that contains the ExpandableListView
Get a reference to the ExpandableListView, and then use it as you did before.
Something like this:
// extend from Fragment
public class ItemListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the layout that contains the ExpandableListView
View view = inflater.inflate(R.layout.fragment_items_list, container, false);
// get a reference to ExpandableListView
ExpandableListView list = (ExpandableListView)view.findViewById(R.id.my_list);
// set the adapter
// set listeners
return view;
}
}
I have no idea how to create ExpandableListView, but you can achieve with custom listview like I implemented in my Project
1. Create Custom list row
<?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"
android:background="#00000000" >
<RelativeLayout
android:id="#+id/rel_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/imageView1"
android:paddingBottom="15dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#685f56" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/text1"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:src="#drawable/highlight_icon" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rel_main"
android:visibility="gone" >
</RelativeLayout>
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1"
android:background="#685f56" />
</RelativeLayout>
**2. Create Custom Adapter in "ItemListFragment.java" **
public class CustomAdepeterNewJob extends BaseAdapter{
//String[] tablecontent;
int Click=0;
Map<String, DummyItem> tablecontent = new HashMap<String, DummyItem>();
Context context;
public CustomAdepeterNewJob(Context context, Map<String, DummyItem> titles)
{
this.context = context;
this.tablecontent = titles;
}
public class ViewHolder {
public TextView txt;
public ImageView img;
public RelativeLayout relativeLayout1,rel_main;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return tablecontent.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.c_simple_list_item, parent, false);
holder = new ViewHolder();
holder.txt = (TextView) view.findViewById(R.id.text1);
holder.img = (ImageView) view.findViewById(R.id.imageView1);
holder.rel_main=(RelativeLayout) view.findViewById(R.id.rel_main);
holder.relativeLayout1=(RelativeLayout) view.findViewById(R.id.relativeLayout1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
String s=DummyContent.ITEMS.get(position).content;
System.out.println("==dummy "+s);
holder.txt.setText(s);
if (s.equalsIgnoreCase("All"))
{
holder.img.setImageResource(R.drawable.all_icon);
}else if (s.equalsIgnoreCase("Note")) {
holder.img.setImageResource(R.drawable.notes_icon);
}else if (s.equalsIgnoreCase("Highlight")) {
holder.img.setImageResource(R.drawable.highlight_icon);
}else if (s.equalsIgnoreCase("Snapshots")) {
holder.img.setImageResource(R.drawable.snapshot_icon);
}else if (s.equalsIgnoreCase("Draw")) {
holder.img.setImageResource(R.drawable.draw_icon);
}else if (s.equalsIgnoreCase("Record Sounde")) {
holder.img.setImageResource(R.drawable.sound_recorder_icon);
}
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
if (holder.relativeLayout1.getVisibility()==View.GONE)
{
holder.rel_main.setBackgroundResource(R.drawable.list_item_bg_pressed);
holder.relativeLayout1.setVisibility(View.VISIBLE);
}else {
holder.relativeLayout1.setVisibility(View.GONE);
holder.rel_main.setBackgroundColor(Color.TRANSPARENT);
}
}
});
return view;
}
}
3. And ad Custom Adapter in "onCreate" in ItemListFregment
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
/*setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
R.layout.c_simple_list_item, R.id.text1, DummyContent.ITEMS));*/
setListAdapter(new CustomAdepeterNewJob(getActivity(), DummyContent.ITEM_MAP));
}
In my application I am trying to create a ListView that contains an ImageView a TextView and a Button. I have created a separate XML file and drag all the above mentioned elements in that XML and in my main java file I have created an Object of BaseAdapter and in the getView() method I have declared these elements but when I run the application I cant see the list. I haven't used the BaseAdapter, so I am missing with some code in order to view the List. I also want to apply some operations on the button so please let me know that code also.
Code for my main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Code for my list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="0dp"
android:layout_marginTop="15dp"
android:text="Medium Text"
android:textSize="15dp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="25dp"
android:text="Button" />
</LinearLayout>
Code for my main.java file:
public class CustomListActivity extends Activity {
/** Called when the activity is first created. */
ListView lv;
LayoutInflater inflator;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listView1);
BaseAdapter bs = new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vw = inflator.inflate(R.layout.list_items, null);
ImageView img = (ImageView)findViewById(R.id.imageView1);
TextView tv = (TextView)findViewById(R.id.textView1);
Button btn = (Button)findViewById(R.id.button1);
return vw;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
};
}
}
Thanks in advance....
ok,
public class App_Adapter extends BaseAdapter implements OnClickListener{
private Activity mActivity;
private List<App_List> mList;
private static LayoutInflater inflater=null;
private PackageManager pm;
private String appclass;
private ApplicationTask mApplicationTask;
private String link=null;
public App_Adapter (FavouriteApp favouriteApp,List<App_List> mAppList, String appclass) {
// TODO Auto-generated constructor stub
this.mActivity= favouriteApp;
this.mList= mAppList;
this.appclass = appclass;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm=mActivity.getPackageManager();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView=convertView;
if (convertView == null)
mView = inflater.inflate(R.layout.app_adapter, parent,false);
App_List mAppList= mList.get(position);
**here i am setting two textview and one button**
((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());
boolean status = isAppInstalled(mAppList.getApp_Pkg());
Button btn = (Button) mView.findViewById(R.id.button_appStatus);
**// register the button for clicklistener**
btn.setOnClickListener(this);
return mView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
and call this adapter class from your activity.
You are definitely missing lv.setAdapter(bs);
I have to create UI of an application as shelf which holds magazines. I am using customize grid view, with grid element consisting of thumbnail of magazine having shelf as background image.
shelf.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4" >
</GridView>
</LinearLayout>
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/grid">
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="41dp"
android:src="#drawable/book1" />
</RelativeLayout>
LibraryActivity.java
public class LibraryActivity extends Activity {
GridView grid = null;
ArrayList<Integer> image = new ArrayList<Integer>();
ImageView thumbnail;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.shelf);
image.add(R.drawable.book1);
image.add(R.drawable.book2);
image.add(R.drawable.book3);
image.add(R.drawable.book4);
System.out.println("before adapter");
grid = (GridView) findViewById(R.id.gridView1);
CustomAdapter adapter = new CustomAdapter(this, R.layout.item);
grid.setAdapter(adapter);
}
public class CustomAdapter extends BaseAdapter {
Context mContext = null;
int mitem = 0;
LayoutInflater mInflater = null;
public CustomAdapter(Context context, int item) {
this.mContext = context;
this.mitem = item;
this.mInflater = (LayoutInflater) context
.getSystemService(LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return image.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
System.out.println("****CHECKING 2**********");
convertView = mInflater.inflate(mitem, null);
System.out.println("****CHECKING 3**********");
thumbnail = (ImageView) convertView
.findViewById(R.id.imageView1);
System.out.println("****CHECKING 3**********");
}
thumbnail.setImageResource(image.get(position));
return convertView;
}
}
}
But using this code the shelf with magazine is only being displayed. I want to display empty shelf to occupy whole screen of device. How can i do that??
Is there any alternate way to implement the shelf??
There's a great project called Shelves made by Romain Guy from Google. You should check it out.
The app I'm struggling to make has a simple list and a button present in the bottom no matter what. My problem is that my custom BaseAdapter doesn't show elements. I know that since my elements are only a string I could use an ArrayAdapter, but the assignment requires it. The code:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
return element;
}
}
public class WeatherAppActivity extends ListActivity {
Button buton;
ListaOrase lista;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lista=new ListaOrase(this);
buton=(Button)findViewById(R.id.buton);
setListAdapter(lista);
lista.add("Bucuresti");
lista.add("Sibiu");
}
}
My XML files go like this:
main.xml -- for the Activity
<?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"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_alignParentTop="true"/>
<RelativeLayout
android:id="#+id/relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/buton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="add"
android:text="Add"
android:layout_gravity=""
/>
</RelativeLayout>
</RelativeLayout>
lista.xml -- for the list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/elementLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
The button "downstairs" opens a dialog that adds items to my list. It obviously doesn't work, but I think it's the BaseAdapter (ListaOrase) itself, as the intents throw no exceptions. I would be thankful if the items I hardcoded in would show up (Bucuresti and Sibiu).
What am I doing wrong?
Thank you very much! :)
Your code almost perfect issue is return orase.size() in getcount() method,you are returning 0, add unimplemented methods in ListaOrase class.
your ListaOrase should be like this:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
Log.e("",orase.get(position));
return element;
}
public int getCount() {
// TODO Auto-generated method stub
return orase.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
hope this helps
getCount method was really returning 0 so consquently getView() wasn't being called. When I changed to:
getcount() {
return arrayObject.Size();
}