I have written code for gridview in which i can show image and text but i want to show all image in single scrollable row like Pulse news apps.
I have implemented horizontalscroll-view for gridview in xml but it does not work at all.
I am using pageviwer for tabs and i am using fragments.
Here is my xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="horizontal" >
<GridView
android:layout_width="500dp"
android:layout_height="400dp"
android:id="#+id/gridview"
android:columnWidth="300dp"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:scrollbars="horizontal">
</GridView>
</HorizontalScrollView>
</RelativeLayout>
Here is my image adpator code
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private TextView t;
public ImageAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.showlist_item, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = mobileValues[position];
if (mobile.equals("Windows")) {
imageView.setImageResource(R.drawable.test_play_image);
} else if (mobile.equals("iOS")) {
imageView.setImageResource(R.drawable.test_play_image);
} else if (mobile.equals("Blackberry")) {
imageView.setImageResource(R.drawable.test_play_image);
} else {
imageView.setImageResource(R.drawable.test_play_image);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
private void clickedButton(TextView tv){
int num = Integer.parseInt(tv.getText().toString());
++num;
tv.setText(Integer.toString(num));
}
private void clickedButtonm(TextView tv){
int num = Integer.parseInt(tv.getText().toString());
if(num>0){
--num;
tv.setText(Integer.toString(num));
}
}
public int getCount() {
return mobileValues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
class MyOnClickListener implements OnClickListener{
public final TextView tv;
public MyOnClickListener(TextView tv){
this.tv=tv;
}
public void onClick(View v) {
// TODO Auto-generated method stub
clickedButton(tv);
}
}
class MyOnClickListenerm implements OnClickListener{
public final TextView tv;
public MyOnClickListenerm(TextView tv){
this.tv=tv;
}
public void onClick(View v) {
// TODO Auto-generated method stub
clickedButtonm(tv);
}
}
I want to display like this scrollable to right.
There is a nice solution in Android from now on (as Zainodis has said in its comment ) : HorizontalGridView.
1. Gradle dependency
dependencies {
compile 'com.android.support:leanback-v17:23.1.0'
}
2. Add it in your layout
your_activity.xml
<!-- your stuff before... -->
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:id="#+id/gridView"
/>
<!-- your stuff after... -->
3. Layout grid element
Create a layout for your grid element ( grid_element.xml ). I have created a simple one with only one button in it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
</LinearLayout>
4. Create an adapter
Highly inspired by this link : https://gist.github.com/gabrielemariotti/4c189fb1124df4556058
public class GridElementAdapter extends RecyclerView.Adapter<GridElementAdapter.SimpleViewHolder>{
private Context context;
private List<String> elements;
public GridElementAdapter(Context context){
this.context = context;
this.elements = new ArrayList<String>();
// Fill dummy list
for(int i = 0; i < 40 ; i++){
this.elements.add(i, "Position : " + i);
}
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final Button button;
public SimpleViewHolder(View view) {
super(view);
button = (Button) view.findViewById(R.id.button);
}
}
#Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(this.context).inflate(R.layout.grid_element, parent, false);
return new SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
holder.button.setText(elements.get(position));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Position =" + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
return this.elements.size();
}
}
5. Initialize it in your activity :
private HorizontalGridView horizontalGridView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
horizontalGridView = (HorizontalGridView) findViewById(R.id.gridView);
GridElementAdapter adapter = new GridElementAdapter(this);
horizontalGridView.setAdapter(adapter);
}
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/seatLegendLayout">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout_gridtableLayout"
android:layout_width="900dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="9"
android:horizontalSpacing="1dp"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="horizontal"
android:stretchMode="none"
android:verticalSpacing="1dp">
</GridView>
</LinearLayout>
</FrameLayout>
</HorizontalScrollView>
I found Two-way GridView helpful on github.
It has some methods:
scrollDirectionPortrait (vertical | horizontal)
scrollDirectionLandscape (vertical | horizontal)
numRows()
etc
There is a better cleaner working solution that i have tested without any kind of extra dependencies just change your layout xml something similar like below .
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontal_grid_view"
android:layout_width="wrap_content"
android:layout_height="112dp"
android:scrollbarAlwaysDrawVerticalTrack="true"></android.support.v7.widget.RecyclerView>
</HorizontalScrollView>
Also setSpan count based on your requirement while assigning Grid layout manager.
I don't think that gridviews can be made horizontally scrollable. Android has provided galleryView for this purpose. You can use that. Here is a thead that can help you making galleryView work as a horizontal gridview:-
Horizontal scrolling in android gridview
The library Android-DraggableGridViewPager by Justin(zzhouj)
Provides the following features
Grid view layout split into pages.
Horizontally swipe pages like ViewPager in support-v4 library.
Setting col & row count.
Setting listeners for: page change, item
click, item long click, rearrange.
Helped me create a horizontal scroll-able grid with page indicators, hope it helps someone trying to implement something similar.
You can use recyclerView as an GridView no need to use GridView if it's difficult for you. Have a look below
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvQuickAccess"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:spanCount="2"
android:gravity="center"
android:orientation="horizontal"
tools:listitem="#layout/layout_item"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
android:scrollbarAlwaysDrawHorizontalTrack="true"/>
You can set number of columns by app:spanCount = " ". Don't forget to set layoutManager as GridLayoutManager and orientation "Horizontal"
For a small set of data, a fine route would be to use a layout such as below for your container:
<HorizontalScrollView
android_id="#+id/scroll_view"
android_width="match_parent"
android_height="wrap_content">
<LinearLayout
android_id="#+id/my_list"
android_width="wrap_content"
android_height="wrap_content" >
</LinearLayout>
</HorizontalScrollView>
a different layout for your list items (list_item.xml):
<FrameLayout
android_id="#+id/item_container"
android_width="100dp"
android_height="100dp">
<ImageView
android_id="#+id/item_background"
android_width="match_parent"
android_height="match_parent" />
<TextView
android_id="#+id/item_title"
android_width="match_parent"
android_height="50dp"
android_background="#80000000"
android_gravity="center_vertical" />
</FrameLayout>
and then programatically add views as needed:
public void addItensToLayout(String... mobileValues, LayoutInflater inflater) {
LinearLayout list = (LinearLayout) findViewById(R.id.my_list);
for (String item : mobileValues) {
FrameLayout item = inflater.inflate(R.layout.list_item, list, false);
TextView textView = (TextView) gridView
.findViewById(R.id.item_title);
textView.setText(item);
ImageView imageView = (ImageView) item
.findViewById(R.id.item_background);
String mobile = mobileValues[position];
if (mobile.equals("Windows")) {
imageView.setImageResource(R.drawable.test_play_image);
} else if (mobile.equals("iOS")) {
imageView.setImageResource(R.drawable.test_play_image);
} else if (mobile.equals("Blackberry")) {
imageView.setImageResource(R.drawable.test_play_image);
} else {
imageView.setImageResource(R.drawable.test_play_image);
}
list.addView(item);
}
}
By doing so, you are not longer using an adapter. If you really need to use an adapter, you should use an ViewPager, there is plenty of info for doing so in android documentation, or you can start right here in StackOverflow:
Using viewpager in my application
android viewPager implementation
Please ask for more info if I missed any point back there.
Related
i am currently in the middle of making a basic gridview with custom layout. although i am only showing text in the gridview for now. i follow some tutorial online and follow everything step by step, i dont get any error from the android studio and i run it. But when i run it, it only show a blank white page. i did initialize the adapter this time, but it still show nothing. help what did i miss in this.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:id="#+id/firsttext"
android:layout_width="match_parent"
android:layout_height="#dimen/ButtonSize"
android:text="#string/hello"
android:gravity="center"/>
<GridView
android:layout_below="#+id/firsttext"
android:id="#+id/firstList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth">
</GridView>
</RelativeLayout>
gridlist.xml
<?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">
<TextView
android:id="#+id/gridtext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello"
android:textSize="#dimen/ButtonSize" />
</RelativeLayout>
gridadapter.java
public class gridAdapter extends BaseAdapter {
Context context;
String[] names;
LayoutInflater layoutInflater;
public gridAdapter(Context context,String[] names) {
this.context = context;
this.names = names;
}
#Override
public int getCount() {
return names.length;
}
#Override
public Object getItem(int position) {
return names[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridView = convertView;
if (convertView != null){
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = layoutInflater.inflate(R.layout.gridlist,null);
}
TextView textView = (TextView)gridView.findViewById(R.id.gridtext);
textView.setText(names[position]);
return gridView;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
String[] Names = {"great","good","average","great","good","average","great","good","average","great","good","average","great","good","average","great","good","average"};
GridView grid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridView)findViewById(R.id.firstList);
gridAdapter adapter = new gridAdapter(this,Names);
grid.setAdapter(adapter);
}
}
Don't use match_parent in your gridlist and TextView's layout_heiht repleace it by wrap_content and set special color for your TextView except for white color.
Change this line to: in gridadapter:
if (convertView == null) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = layoutInflater.inflate(R.layout.gridlist, parent, false);
}
In adapter change this :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= layoutInflater.inflate(R.layout.gridlist,null);
TextView textView = (TextView)convertView.findViewById(R.id.gridtext);
textView.setText(names[position]);
return convertView;
}
Use RecyclerView instead of GridView in future. Your problem is with setting color for TextView, check to set textColor to black
This is my first try to ask a question. I am a beginner in android.When I am using image OnClickListener() in a simple way it is working but when I use gridView which has one imageView and one TextView on Image Click I want to jump to a new activity.
What I do is just call gridView OnItemClickListener() and jump to another activity which is working as I show below
gridView = (GridView) findViewById(R.id.gridview1);
gridView.setAdapter(new Adapter1(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent sendID_intent = new Intent(Activtiy1.this, Activity2.class);
startActivity(sendID_intent);
}
});
wherein Adapter1 I am extends BaseAdapter where an image is load using Picasso library which is also working the only issue is with image click event which is not working properly.
public class Adapter1 extends BaseAdapter {
private Context mC1;
ArrayList<View> viewList = new ArrayList<View>();
public Adapter1(Context context) {
mC1 = context;
}
#Override
public int getCount() {
return normlSearchList.size();
}
#Override
public Object getItem(int position) {
return normlSearchList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view1=convertView;
if ((position + 1) > viewList.size()) {
LayoutInflater inflater = (LayoutInflater) mC1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view1 = inflater.inflate(R.layout.view_grid,null);
ImageView imageView = (ImageView) view1.findViewById(R.id.iv_galleryb);
TextView textView = (TextView) view1.findViewById(R.id.tv_name);
textView.setText(normlSearchList.get(position).getPost_title());
final String txtID;
String img_url = normlSearchList.get(position).getImage();
Picasso.with(mContext)
.load(img_url)
.placeholder(R.drawable.it_logo_td)
.error(R.drawable.it_logo_td)
.noFade().resize(150, 150)
.centerCrop()
.into(imageView);
viewList.add(view1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(mContext, Activity2.class);
mC1.startActivity(intent);
}
});
}
return view1;
}
}
My layout file simply contain relative layout which holds a linearLayout a TextView for display data not found message if no data is found and a gridView
<?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="vertical"
android:layout_margin="5dp"
>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
...................
</LinearLayout>
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:gravity="center_horizontal"
android:visibility="invisible"
/>
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/layout1"
android:numColumns="3"
android:columnWidth="200dp"
android:gravity="center"
android:stretchMode="columnWidth"
/>
My viewgrid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="80dp"
android:descendantFocusability="afterDescendants">
<ImageView
android:id="#+id/iv_galleryb"
android:layout_width="100dp"
android:layout_height="80dp"
android:clickable="true"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/iv_galleryb"
/>
I hope I make my problem clear.
I'd avoid use clickable views inside of GridView, instead of, I'd use RecyclerView. Because GridView implements onItemClick, it can conflict with ImageView clickable.
Anyway try add android:focusable="true" in <ImageView />
I am very new to android development and I feel like this is very simple, yet, I haven't managed to find anyone on google with the same problem.
I have a Gridview that is filled with a TextView (which has an image on top) and an ImageButton (to delete the current item). What I want to do is to remove the item of which I click the ImageButton.
Here is my Main :
public class ActivityMain extends Activity
{
GridView gridview;
public GridAdapter mainActivityAdapter;
public ArrayList<String> listService = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listService.add("Market");
listService.add("Recherche");
listService.add("Quiz");
gridview = (GridView) findViewById(R.id.mainActivity_grid);
mainActivityAdapter = new GridAdapter(this.getApplicationContext(), listService);
gridview.setAdapter(mainActivityAdapter);
}
}
And here is my Adapter :
public class GridAdapter extends BaseAdapter
{
Context context;
ArrayList<String> list = new ArrayList<String>();
GridAdapter adapter = this;
public GridAdapter(Context context, ArrayList<String> list)
{
this.context = context;
this.list = list;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public Object getItem(int arg0)
{
return null;
}
#Override
public long getItemId(int arg0)
{
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_gridmain, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.gridMain_text);
holder.close = (ImageButton) convertView.findViewById(R.id.mainActivity_delete);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
// Doing stuff on views
holder.close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg1)
{
// list.remove(position);
list.remove(position);
adapter.notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder
{
TextView textView;
ImageButton close;
}
}
The thing is, when I click on one ImageButton, it's always the last item that has been added that is being removed and I can't figure out why or how to fix this.
Thank you.
==================
EDIT :
Here is my activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DDDDDD"
android:orientation="vertical" >
<GridView
android:id="#+id/mainActivity_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:clickable="false"
android:gravity="center"
android:horizontalSpacing="15dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
And my item_gridmain.xml :
<?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="vertical" >
<TextView
android:id="#+id/gridMain_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:textColor="#android:color/holo_blue_dark"
/>
<ImageButton
android:id="#+id/mainActivity_delete"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/deleteFav"
android:src="#drawable/boutoncroixfermer" />
</RelativeLayout>
You have ImageButton's in every item right? You can set
holder.close.setTag(Integer.valueOf(position));
And then, you have all positions hidden in the right buttons.Change the OnClickListener like below:
close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
list.remove((Integer) v.getTag());
adapter.notifyDataSetChanged();
}
});
Ok actually it was my own mistake and I feel incredibly dumb now.
I was actually removing the right item but in each view I was replacing each item at position with what was there in the first place. So each item was the correct one but the picture/text were the old one because I was modifying them inside getView().
Sorry for that lose of time, my bad, I want to punch myself now.
Please use ViewHolder pattern, and also add your gridMain_text.xml
else you will keep receiving wrong index of click
http://www.binpress.com/tutorial/smooth-out-your-listviews-with-a-viewholder/9
This is example of ListView but equally applicable for GridView
Please use
holder.close.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg1)
{
// list.remove(position);
list.remove(position);
adapter.notifyDataSetChanged();
}
});
instead of close.setOnClickListener, i ran your this code an now it work fine
add Log.debug() in listener to ensure the remove position is right.
How do you know after click,the last item is removed?I mean,if there has text label in item view?Maybe you remove the right item,but after gridview refresh,it looks like the last item is removed.
You can add position label with setText in getView.That may helps you.
Ok,I know.Where did you do render item view?You must do it in getView.The AdapterView cached it's item view.As you removed the right position.May be the position is not the right view.You can run hierarchyviewer to detect what happen.
I am trying to achieve something similar to this :
When a user long press on a grid cell in a grid-view the menu should be drawn inside the grid cell area as shown above. Please help me if anyone has ever tried something similar to this or if there are any libraries available to do the same.
Here is a code snippet on how generally a context-menu is triggered on long-press.
My Activity class :
public class GridViewActivity extends Activity {
GridView gridView;
ImageAdapter mImageAdapter;
static final String[] MOBILE_OS = new String[] {
"Android", "iOS", "Windows", "Blackberry"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageAdapter = new ImageAdapter(this, MOBILE_OS);
gridView = (GridView)findViewById(R.id.gridView1);
registerForContextMenu(gridView);
gridView.setAdapter(mImageAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView)v.findViewById(R.id.grid_item_label)).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onCreateContextMenu(ContextMenu iMenu, View iView, ContextMenuInfo iMenuInfo) {
super.onCreateContextMenu(iMenu, iView, iMenuInfo);
iMenu.setHeaderTitle("OPTIONS");
iMenu.clear();
iMenu.add(Menu.NONE, 0, Menu.NONE, "VIEW");
iMenu.add(Menu.NONE, 1, Menu.NONE, "EDIT");
iMenu.add(Menu.NONE, 2, Menu.NONE, "SHARE");
iMenu.add(Menu.NONE, 3, Menu.NONE, "DELETE");
}
ImageAdapter class :
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
public LayoutInflater inflater;
public LinearLayout linearLayout;
public ImageAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set value into textview
linearLayout = (LinearLayout)gridView.findViewById(R.id.linear1);
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = mobileValues[position];
if (mobile.equals("Windows")) {
imageView.setImageResource(R.drawable.windows_logo);
} else if (mobile.equals("iOS")) {
imageView.setImageResource(R.drawable.ios_logo);
} else if (mobile.equals("Blackberry")) {
imageView.setImageResource(R.drawable.blackberry_logo);
} else {
imageView.setImageResource(R.drawable.android_logo);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return mobileValues.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
mobile.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px"
android:src="#drawable/android_logo" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:layout_marginTop="5px"
android:textSize="15px" >
</TextView>
</LinearLayout>
What am trying to achieve here is that on long-press on a grid cell it should replace this particular grid cell view with the above view and each of the boxes (i.e. view, edit, share and delete) shall respond to some event listener.
I would imagine doing something like this would be on a bigger screen right? But I guess you could maybe make each grid cell a RelativeLayout and house whatever is there. Then when you long press, add another view, probably a LinearLayout that houses 4 TextViews that say what you want it to say. Since whatever the lowest level View in a Relative layout produces sort of a floating effect, it could work. Plus you can add opaqueness to make it see through. Oh and I should probably add that Android comes with long press and letting go features so this shouldn't be too bad to implement.
I have a grid view in my application and i need to scroll it horizontally.I have tried changing the gridview to gallery.But then only one row is available,but i need different rows as in a grid view.So basically what i need is a gridview that can be scrolled horizontally.Is there any efficient way to do this?Thanks in advance.
Regards
Anu
Hi,Thanks for the reply.i have tried using a Gallery and implement an adapter that provides a multirow view in its getView method.
My java file is:
public class Gridview extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new GridAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gridview.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class GridAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
};
public GridAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null)
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
}
else
{
v = convertView;
}
return v;
}
}
}
main.xml is :
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
icon.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/icon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</ImageView>
</LinearLayout>
The output i got is : http://www.4shared.com/photo/MzUcmzel/device.html
But this is not i need actually.i want the icons in different rows.Any help is appreciated.
I think your best bet is to use a Gallery and implement an adapter that provides a multirow view in its getView method. See Hello Gallery and look at the ImageAdapter implementation within.
Instead of the ImageView that getView returns in that example, you can, for example, inflate your own custom layout, for example a LinearLayout with vertical orientation.
You might consider a TableLayout inside a HorizontalScrollView, but the disadvantage there is that everything will be in memory at once, making it difficult to scale to lots of items. The Gallery recycles Views and offers some resource advantages.
I have already posted this answer here and here, but these questions are
identical...
There is a nice solution in Android from now on : HorizontalGridView.
1. Gradle dependency
dependencies {
compile 'com.android.support:leanback-v17:23.1.0'
}
2. Add it in your layout
your_activity.xml
<!-- your stuff before... -->
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:id="#+id/gridView"
/>
<!-- your stuff after... -->
3. Layout grid element
Create a layout for your grid element ( grid_element.xml ). I have created a simple one with only one button in it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
</LinearLayout>
4. Create an adapter
Highly inspired by this link : https://gist.github.com/gabrielemariotti/4c189fb1124df4556058
public class GridElementAdapter extends RecyclerView.Adapter<GridElementAdapter.SimpleViewHolder>{
private Context context;
private List<String> elements;
public GridElementAdapter(Context context){
this.context = context;
this.elements = new ArrayList<String>();
// Fill dummy list
for(int i = 0; i < 40 ; i++){
this.elements.add(i, "Position : " + i);
}
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final Button button;
public SimpleViewHolder(View view) {
super(view);
button = (Button) view.findViewById(R.id.button);
}
}
#Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(this.context).inflate(R.layout.grid_element, parent, false);
return new SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
holder.button.setText(elements.get(position));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Position =" + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
return this.elements.size();
}
}
5. Initialize it in your activity :
private HorizontalGridView horizontalGridView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
horizontalGridView = (HorizontalGridView) findViewById(R.id.gridView);
GridElementAdapter adapter = new GridElementAdapter(this);
horizontalGridView.setAdapter(adapter);
}
True about using a Gallery or ListView re: re-using views. But if your list is not too long, you can try a TableLayout with a ViewFlipper. Here's a summary of possible options/solutions.
This post might get help you out what you wanted to achieve
Scroll like Shelf View
how about using a viewPager :
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
?
Try to wrap it in HorizontalScrollView