I'm implementing horizontal scrolling textview list something like an ebook with thumbing pages. I take the Gallery widget dispaying TextViews. The first problem I faced is that the left and right edges of each page look rounded.
Here is the sample code:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:spacing="0px"/>
</LinearLayout>
page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallery_item"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"/>
</LinearLayout>
GalleryActivity.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;
public class GalleryActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new GalleryAdapter(this));
}
private class GalleryAdapter extends BaseAdapter {
private Context context; // needed to create the view
public GalleryAdapter(Context c) {
context = c;
}
public int getCount() {
return 5;
}
public Object getItem(int position) {
return position; //TODO: get the object on the position
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView == null)
v = LayoutInflater.from(context).inflate(R.layout.page, parent, false);
else
v = convertView;
TextView tv = (TextView) v.findViewById(R.id.textView);
tv.setText("Page" + position);
return v;
}
}
}
Any Idea how to get page edges like on the e.g. titlebar? Maybe another way to achive the goal?
in page.xml instead of givin match_parent give a fxd width for layout and give sm padding for txtview.
And FYI Gallery is deprecated, use view pager instead.
The View pager comes with the compatibility package too:http://developer.android.com/tools/extras/support-library.html
and if u can't give a fxd width then u can only try with padding ,this might sove ur prblm
Related
I am trying to create a listView with just images. Please this is not a stupid question so any help is appreciated. I think my code is correct, but when I run it, it just shows the contentView but empty. It does not show the images I tell it to show.I have created the java file and then two xml files, one with the listView which is the contentView for the java file, and the other for one single row of the listView.
My java file:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class Craft extends Activity {
int[]images={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.craft);
listView=(ListView)findViewById(R.id.listView);
MyAdapter adapter=new MyAdapter(Craft.this,images);
listView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String> {
Context c;
int[] images;
int count = 0;
public MyAdapter(Context c,int imgs[]) {
super(Craft.this, R.layout.single_row);
this.c = Craft.this;
this.images = imgs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
Log.d("Plates", "Count: " + count++);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView23);
myImage.setImageResource(images[position]);
return row;
}
}
}
my craft.xml
<?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:background="#drawable/woodcrop">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
My single_row.xml file
<?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:background="#drawable/woodcrop">
<ImageView
android:layout_margin="20dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/one"
android:id="#+id/imageView23" />
Any help is appreciated!
You should change your ListView height to:
<ListView
.
.
.
android:layout_height="match_parent"/>
I see, you have not overriden getCount() in your adapter:
So your listview dosen't know that there are items.
You should add this to your adapter:
#Override
public int getCount() {
return images.length;
}
Ty to use this super statement
Super(c , R.layout.single_row,imgs);
Hello Guys How can arrange appname under the icon ? If you need i give the source.
You can see the code used to create this view here:
package com.example.draw;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class AppInfoAdapter extends BaseAdapter {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}
#Override
public int getCount() {
return mListAppInfo.size();
}
#Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = mListAppInfo.get(position);
// reference to convertView
View v = convertView;
// inflate new layout if null
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.activity_appinfo, null);
}
// load controls from layout resources
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
// return view
return v;
}
}
Text come from
TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
tvAppName.setText(entry.loadLabel(mPackManager));
main_appinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:padding="5dip" >
<ImageView
android:id="#+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="#string/desc"
android:scaleType="center"
android:gravity="center_horizontal|center_vertical"/>
</LinearLayout>
The LinearLayout in layout file horizontal, make it vertical.
android:orientation="horizontal" to android:orientation="vertical"
App Icon and title format is part of the Home Screen App in android, you can't modify its look.
If you want you can create your own Home Screen App and do whatever you want with it.
Here is an example for home screen apps : Home App Android sdk Sample
I have a problem with android galleryview, in my app I have a lot of pictures. And I wanna display them by gallerview and make them selectable. I figured out displaying them horizontal scrollable and make them selectable with Galleryview, but I need one image on the screen at a time, after horizontal scrolling the scrollbar by the user, another picture must be shown. My test code is below and this shows 3 pictures on the screen, from http://www.androidpeople.com/android-gallery-example:
package com.projects.cards;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GaleryTestActivity extends Activity {
private Gallery gallery;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galery);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Displaying the position when the gallery item in clicked
Toast.makeText(GaleryTestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
// Adding images.
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
// Fixing width & height for image to display
imgView.setLayoutParams(new Gallery.LayoutParams(200, 160));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
How can I solve this problem?
Thanks in advance..
As I understand it, you want the currently focused image to take up all the screen space. You can achieve this by setting the width of the ImageView where you bind it to the Gallery (in getView). Try setting the width of the image to match the width of the device screen.
Note that you can also set the spacing between the items in the Gallery with the gallery.setSpacing(...) method.
I can not tell you how to show a scrollbar, sorry.
I have implemented gallery view like, one that is mentioned in the below in the link. Please try that also: Android gallery with caption
You can try returning view with fill parent as its width which contains your imageview from your adapters getView() ...
Or alse you can do something like this
getview function
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater iteminflater = LayoutInflater.from(cont);
View gallaryitem = iteminflater .inflate(R.layout.gallaryitem, null);
ImageView imgView= (ImageView ) gallaryitem .findViewById(R.id.image);
imgView.setImageResource(Imgid[position]);
gallaryitem .setBackgroundResource(GalItemBg);
return gallaryitem ;
}
gallaryitem.xml
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView android:layout_width="wrap_content"
android:id="#+id/image"
android:layout_height="wrap_content"
android:src="#drawable/icon"
xmlns:android="http://schemas.android.com/apk/res/android" >
</ImageView>
</LinearLayout>
</LinearLayout>
I've been looking around how to solve several problems and got several answers to some of my question, but one thing is still under construction and won't be finished if none of you can help me. :/
I've been trying to zoom in and out of a GridView, but got over to an other solution, since I do only need two states: an overview and a detailed view. Therefor I've made two Gridviews. The first one is the one where the images inside both gridviews are shrunk and displayed without scrolling. The other one is the one where the images are displayed in their original size. You can scroll horizontally and vertically inside that one.
My problem is the switching between those two gridviews. I've tried to "set the visibility" of both to either "gone" or "visible" if i clicked on one of them.
Here's my code:
Starter:
package test.scroll;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.view.View;
import android.view.View.OnClickListener;
public class TestScrollActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GridView grd_overview = (GridView)this.findViewById(R.id.grd_overview);
grd_overview.setAdapter(new OverviewImageAdapter(this));
final GridView grd_detailed = (GridView)this.findViewById(R.id.grd_detailed);
grd_detailed.setVisibility(2);
grd_detailed.setAdapter(new DetailedImageAdapter(this));
grd_overview.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
grd_overview.setVisibility(2);
grd_detailed.setVisibility(0);
}
});
grd_detailed.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
grd_detailed.setVisibility(2);
grd_overview.setVisibility(0);
}
});
}
}
OverviewAdapter:
package test.scroll;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class OverviewImageAdapter extends BaseAdapter {
private Context mContext;
public OverviewImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(82, 82));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.memory_1, R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,
R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1,R.drawable.memory_1
};
}
DetailedAdapter:
package test.scroll;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class DetailedImageAdapter extends BaseAdapter {
private Context mContext;
public DetailedImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(82, 82));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.memory_2, R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,
R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2,R.drawable.memory_2
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/app_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- PLAYGROUND -->
<test.scroll.TwoDScrollView
android:id="#+id/scene_scroller"
android:drawingCacheQuality="low"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/grds"
android:drawingCacheQuality="low"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<GridView
android:id="#+id/grd_overview"
android:drawingCacheQuality="low"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:id="#+id/grd_detailed"
android:drawingCacheQuality="low"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</test.scroll.TwoDScrollView>
<!-- ATTRIBUTES -->
<Button
android:id="#+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scene_scroller"
android:text="cancel"
/>
</RelativeLayout>
Do you have any suggestions for me on how to switch between those two gridview? Let me know :)
Basti
Imho you could really nicely implement a ViewFlipper to switch between your 2 Grids!
From android reference guide:
ViewFlipper is a simple ViewAnimator that will animate
between two or more views that have
been added to it. Only one child is
shown at a time. If requested, can
automatically flip between each child
at a regular interval.
Here is an example on how to implement this.
Here is another example that demonstrates this ( with animation )
Why not just place both GridViews within a ViewSwitcher/ViewFlipper, and just flip between them that way?
You should be able to remove the grid you don't wanna see, from app_layout and add the one you want to see. Example:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.app_layout);
GridView gv = (GridView) findViewById(R.id.grid01); // Replace the ID
GridView gv2 = (GridView) findViewById(R.id.grid02); // Replace too
//...
rl.removeView(gv);
rl.addView(gv2);
// ...
rl.removeView(gv2);
rl.addView(gv);
And so on.
I want to have a list of elements (using a ListView) and each element in list is styled with a relative layout. Currently, the items are being displayed correctly, however, for some reason the listview items dont glow green when they're clicked. Why is this?
I have removed all code to minimals and it still does this.
Activity class
package com.test;
import android.app.Activity;
public class My_ListView extends Activity
{
private ListView_Adapter listViewAdapter;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview);
// initialise the list-view object
listViewAdapter = new ListView_Adapter(this);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(listViewAdapter);
for (int i=0;i<20;i++)
{
listViewAdapter.add("item "+i);
}
}
public void clicked(View v)
{
v.setBackgroundColor(0xFF0000FF);
}
}
The listview item adapter class
package com.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListView_Adapter extends ArrayAdapter<String>
{
public ListView_Adapter(Context c)
{
super(c, R.layout.my_listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
StationFinder_ListViewItemHolder holder = null;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.my_listview_item, parent, false);
holder = new StationFinder_ListViewItemHolder(row);
row.setTag(holder);
}
else
{
holder = (StationFinder_ListViewItemHolder) row.getTag();
}
holder.populateFrom(getItem(position));
return row;
}
static class StationFinder_ListViewItemHolder
{
private TextView destination = null;
StationFinder_ListViewItemHolder(View row)
{
destination = (TextView) row.findViewById(R.id.text1);
}
void populateFrom(String locationDistance)
{
destination.setText(locationDistance);
}
}
}
my_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<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/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
my_listview_item.xml
<?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="wrap_content"
android:onClick="clicked"
>
<TextView android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I think you may have to call invalidate() after setting the background color.
You may want to do this instead by setting the listSelector and possibly drawSelectorOnTop for your ListView. That way the selection/deselection and clicks will be handled in the normal manner.
Edit - also, since you're using a ListView, you probably want to listen for clicks by setting an OnItemClickListener on your ListView.