i am new to android and while gallery is being showing as depreciated try to use the viewpager concept to dynamically loading custom layout which consist of imageview and textview everything is going great but while loading the viewpager the image appear to left and right side of the viewpager and showing large space so the question is how can we remove the space in between the page or view while using viewpager.
here is the screenshot what i try to achieve:
and here is what i am getting :
so as u see the red circled two image are those image which is adding to the viewpager and there is lot of space in between two image.
mentioning the pageadapter class where the image and text are there
public class ImagePagerAdapter extends PagerAdapter {
private ArrayList<Integer> filter_image_arr;
private ArrayList<String> filter_desc;
private Context mcontext;
private LayoutInflater inflator;
private View layout;
private TextView filter_title;
private ImageView filter_image;
public ImagePagerAdapter(Context context) {
// TODO Auto-generated constructor stub
mcontext=context;
/*
* add image to the arraylist
*/
filter_image_arr=new ArrayList<Integer>();
filter_image_arr.add(R.drawable.cakenormal);
filter_image_arr.add(R.drawable.blur);
filter_image_arr.add(R.drawable.emboss);
filter_image_arr.add(R.drawable.gray);
filter_image_arr.add(R.drawable.hue);
filter_image_arr.add(R.drawable.monochrome);
filter_image_arr.add(R.drawable.nored);
filter_image_arr.add(R.drawable.oil);
filter_image_arr.add(R.drawable.pixellate);
filter_image_arr.add(R.drawable.posterize);
filter_image_arr.add(R.drawable.sepia);
filter_image_arr.add(R.drawable.vibrance);
filter_image_arr.add(R.drawable.vintage);
/*
* add the desc for the filter effect....
*
*/
filter_desc=new ArrayList<String>();
filter_desc.add("NORMAL");
filter_desc.add("BLUR");
filter_desc.add("EMBOSS");
filter_desc.add("GRAY");
filter_desc.add("HUE");
filter_desc.add("MONOCHROME");
filter_desc.add("NO RED");
filter_desc.add("OIL");
filter_desc.add("PIXELATE");
filter_desc.add("POSTERIZE");
filter_desc.add("SEPIA");
filter_desc.add("VIBRANCE");
filter_desc.add("VINTAGE");
inflator=(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
layout=inflator.inflate(R.layout.custom_gallery_item_layout, null);
filter_image=(ImageView) layout.findViewById(R.id.filter_image);
filter_image.setImageResource(filter_image_arr.get(position));
filter_image.setScaleType(ImageView.ScaleType.FIT_START);
int padding=mcontext.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
filter_image.setTag(position);
//filter_image.setPadding(padding, padding, padding, padding);
filter_title=(TextView) layout.findViewById(R.id.filter_desc);
filter_title.setText(filter_desc.get(position));
filter_title.setTag(position);
((ViewPager)container).addView(layout, 0);
return layout;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==((View)arg1);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return filter_image_arr.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager)container).removeView((View)object);
}
}
and here is the activity class where adding adapter to the viewpager
public class MainActivity extends Activity {
//private GPUImageView gpuimageview;
private Button save_image;
private Context mcontext;
private ViewPager gallery_pager;
private ImagePagerAdapter pageadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext=MainActivity.this;
//gpuimageview=(GPUImageView) findViewById(R.id.gpuimageview);
gallery_pager=(ViewPager) findViewById(R.id.view_pager);
gallery_pager.setPageMargin(-50);
gallery_pager.setHorizontalFadingEdgeEnabled(true);
gallery_pager.setFadingEdgeLength(30);
pageadapter=new ImagePagerAdapter(mcontext);
gallery_pager.setAdapter(pageadapter);
gallery_pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
so can anyone tell me how the thing can be achieve of the first screenshot or i go with the old gallery concept.
here the xml part which contain the viewpager
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/filtergallery_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="14dp"
android:background="#AA000000" >
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</RelativeLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/gpuimageview"
android:layout_alignParentLeft="true"
android:text="save" />
</RelativeLayout>
and here is the custom layout which contain the imageview and textview which i m inflating
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/filter_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher"
/>
<TextView
android:id="#+id/filter_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/filter_image"
android:layout_alignParentLeft="true"
android:layout_marginLeft="3dp"
android:background="#07000000"
android:text="filter" />
</RelativeLayout>
so any help will be appreciated.Thanks.
WiewPager doesn't support wrap_content, you can either set match_parent, or a fixed size . If you set wrap_content then it will be considered by the system as match_parent
Related
I want to add a circle indicator to images when images change from one to another. The circle indicator has to change at the same time.
You can use ViewPager and Fragment to do this. The activity layout should contains a ViewPager inside. and the Fragment layout needs nothing but an ImageView.
In Java code, the fragment needs an adapter, which should be something like this:
private class MyPagerAdapter extends FragmentPagerAdapter {
private ArrayList<String> imageList;
private int imagePosition;
public MyPagerAdapter(FragmentManager fragmentManager, ArrayList<String> imageList, int imagePosition) {
super(fragmentManager);
this.imageList = imageList;
this.imagePosition = imagePosition;
}
#Override
public Fragment getItem(int index) {
return new GalleryFragment(imageList.get(index));
}
#Override
public int getCount() {
return imageList.size();
}
}
imageList is used to hold the URLs of pictures you want to display. You can just replace it by ArrayList<Integer> imageList if the pictures you want to show is in the drawable folder.
For the indicator part, TextView with text "●" would be fine.It may looks a little strange, but it's quite neat and easy. You can change the size and the color of the indicators as you wish.
Then what's left is just to put the Fragment into ViewPager
gallery_pager.setAdapter(new MyPagerAdapter(GalleryActivity.this.getSupportFragmentManager(),
curImageList, imagePosition));
gallery_pager.setCurrentItem(imagePosition);
gallery_pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int index) {
// TODO Auto-generated method stub
for (int i = 0; i < curImageList.size(); i++) {
PagerIndicator[i].setVisibility(View.VISIBLE);
PagerIndicator[i].setTextColor(0xff666666);
if (i == index) {
PagerIndicator[i].setTextColor(0xffffffff);
}
}
}
});
In the method onPageSelected you can controll how the dots would be like when a picture is slided to.
Edited
Most parts of the codes had been added above, what you may need is the code for the Fragment:
public class GalleryFragment extends Fragment{
private Context context;
private String imageUrl;
public GalleryFragment(String imageUrl)
{
this.imageUrl = imageUrl;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = GalleryFragment.this.getActivity();
ImageView image = new ImageView(context);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
image.setLayoutParams(params);
image.setScaleType(ScaleType.FIT_CENTER);
//TODO use the imageUrl to load and display the image;
LinearLayout layout = new LinearLayout(context);
layout.setGravity(Gravity.CENTER);
layout.addView(image);
return layout;
}
}
and the XML of the activity should be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#222222" >
<android.support.v4.view.ViewPager
android:id="#+id/gallery_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/dot1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="●"
android:textColor="#15EDE2"
android:textSize="13sp" />
<!--add as many dots here as you need. If the size of the imageList changes, just keep the same amount of dots VISIBLE and others GONE-->
</LinearLayout>
</RelativeLayout>
I want to implement ImageView and Button in ViewPager. But when I tried to implement it just crash my app and I am not able to implement it, Please help me as I am new for Android.
public class FullScreenImageActivity extends Activity {
private static int NUM_VIEWS = 5;
private MyPagerAdapter adapter;
private ViewPager pager;
int gotbiscuit;
public String TAG = "hello";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen_view);
Intent i = getIntent();
int gotbiscuit = i.getExtras().getInt("position");
adapter = new MyPagerAdapter(this);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}
public class MyPagerAdapter extends PagerAdapter {
public Integer[] images1 = { R.drawable.i_3, R.drawable.i_4,
R.drawable.i_6 };
public Integer[] images2 = { R.drawable.i_8, R.drawable.i_9,
R.drawable.i_10 };
public Context mContext;
public MyPagerAdapter(Context context) {
super();
this.mContext = context;
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return images1.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((ImageView) object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((ImageView) view);
}
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(container
.getContext());
View view1 = inflater.inflate(R.layout.fullscreen_view, null);
ImageView view = (ImageView) findViewById(R.id.imgDisplay);
Button btn = (Button) findViewById(R.id.wallb);
// view1.setScaleType(ScaleType.CENTER_CROP);
switch (gotbiscuit) {
case 0:
view.setImageResource(images1[position]);
break;
case 1:
view.setImageResource(images2[position]);
break;
}
view.setAdjustViewBounds(true);
((ViewPager) container).addView(view1, 0);
return view1;
}
}
}
this is my Pager xml file for pagerview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
This
int gotbiscuit = i.getExtras().getInt("position"); // local to onCreate
should ve
gotbiscuit = i.getExtras().getInt("position"); // already declared
You also have
setContentView(R.layout.fullscreen_view);
and inflate the same layout
View view1 = inflater.inflate(R.layout.fullscreen_view, null);
should probabbly be
setContentView(R.layout.pager_view); // pager xml
Modify the below according to your requirement
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager)findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
view = inflater.inflate(R.layout.pagerview, null);
((ViewPager) collection).addView(view, 0);
Button btn = (Button)view.findViewById(R.id.button1);
ImageView iv = (ImageView)view.findViewById(R.id.imageView1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
}
});
return view;
}
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
pagerview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="176dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Snap
Yes it can be done by something like this ...
i made an xml layout file and named it settings_merge.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/set_settings"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/share" />
<Button
android:id="#+id/facebook_share"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/facebook" />
Now in my viewpager activity layout i used include tag to inflate the above layout
<FrameLayout
android:id="#+id/transparentlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<include layout="#layout/settings_merge" />
</FrameLayout>
and then finally in your PagerActivity
ImageView set_settings;
Button facebook_share;
set_settings=(ImageView)findViewById(R.id.set_settings);
//the onclick listener
set_settings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Do whatever you want to do
}
});
and you are done ..:)
I was trying to implement simple customlistview.What i tried to do is to inflate a text and a picture inside each list row in the listview.I got no output i.e. layout was not inflating..I got a blank screen.I used 2 Java class and 2 xml files.Below is my code..Please let me know where is my mistake and what should i correct?
CListView.java
package com.example.customlistview;
public class Clistview extends Activity {
int p1[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] Shapes1={"circle","circle","circle","circle","circle"};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clistview);
lv=(ListView)findViewById(R.id.listView1);
CustomListViewAdapter clvadapter=new CustomListViewAdapter(Clistview.this,Shapes1,p1);
lv.setAdapter(clvadapter);
}
}
CustomListViewAdapter.java
package com.example.customlistview;
public class CustomListViewAdapter extends BaseAdapter {
Context context;
String shape1[];
int pic[];
public CustomListViewAdapter(Context c,String[] sh,int[] p)
{
this.context=c;
this.shape1=sh;
this.pic=p;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public String getItem(int pos) {
// TODO Auto-generated method stub
return shape1[pos];
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View v, ViewGroup vg) {
// TODO Auto-generated method stub
View itemview=v;
TextView tv1;
ImageView im1;
if(itemview==null)
{
LayoutInflater inflater=(LayoutInflater)((Activity)context).getLayoutInflater();
itemview=inflater.inflate(R.layout.clistview1,vg,false);
}
tv1=(TextView)itemview.findViewById(R.id.textView1);
im1=(ImageView)itemview.findViewById(R.id.imageView1);
tv1.setText(shape1[pos]);
im1.setImageResource(pic[pos]);
return itemview;
}
}
activity_clistview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Clistview" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp" >
</ListView>
</RelativeLayout>
clistview1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
/>
</LinearLayout>
Inside CustomListViewAdapter, change this
public int getCount() {
// TODO Auto-generated method stub
return shape1.length;
}
It is an important function that decides the number of items in a ListView.
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
You are returning 0 .
Return pic.length or shape1.length as per your requirement.
public int getCount() {
return pic.length;
}
Method getCount() returns the number of items in the datatset for the adapter.
getCount()
I'm really new to android and I know you shouldnt paste whole codes but I honestly have no idea where the problem is.
Im trying to create a somewhat custom list view, with a text and an image, but keep geting this exception and have no idea how to solve it
02-13 13:23:06.477: E/AndroidRuntime(1834): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
so was hoping someone here could help me out. this is what my launcher activity looks like
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list=(ListView) this.findViewById(R.id.list_view);
int[] stringIds=new int[2];
stringIds[0]=R.string.text1;
stringIds[1]=R.string.text2;
int[] imgIds=new int[2];
imgIds[0]=R.drawable.ic_launcher;
imgIds[1]=R.drawable.ic_launcher;
CustomAdapter custom=new CustomAdapter(this, stringIds, imgIds);
list.setAdapter(custom);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
My CustomAdapter:
public class CustomAdapter extends BaseAdapter {
Context context;
int[] stringIds;
int[] imgIds;
public CustomAdapter(Context context, int[]stringIds, int[] imgIds) {
this.stringIds=stringIds;
this.imgIds=imgIds;
this.context=context;
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View finalView=convertView;
if(finalView==null){
LayoutInflater inflater=((Activity)context).getLayoutInflater();
finalView= inflater.inflate(R.layout.list_view_row, parent);
}
TextView txt=(TextView)finalView.findViewById(R.id.list_view_row_text_view);
ImageView img=(ImageView)finalView.findViewById(R.id.imageView1);
img.setImageResource(imgIds[position]);
return finalView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return this.stringIds.length;
}
#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;
}
}
activity_main.xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_view_row.xml layout
<?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/list_view_row_text_view"
android:layout_width="match_parent"
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" />
</LinearLayout>
Here:
finalView= inflater.inflate(R.layout.list_view_row, parent);
remove parent
finalView= inflater.inflate(R.layout.list_view_row, null);
or
finalView= inflater.inflate(R.layout.list_view_row, parent, false);
also use
setContentView(R.layout.activity_main);
before getting
ListView
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);