extra unwanted TextView appear in GridView - android

i have a GridView with only a picture in the Row layout
but i keep getting this unwanted TextView with "title" text within it
i searched the row xml file over and all the code but i can't find where is this TextView come from
fragment_category_layout
<?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:padding="2dp">
<TextView style="#style/WizardPageTitle" />
<GridView
android:id="#+id/gridCategoryList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:columnWidth="125dp"
android:drawSelectorOnTop="true"
android:listSelector="#drawable/grid_selector"
android:gravity="center"
android:numColumns="3"
android:padding="2dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:focusable="true"
android:clickable="false"/>
</RelativeLayout>
category_row
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ededed">
<ImageView
android:id="#+id/categoryImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_cat_cars" />
</LinearLayout>
fragment_category
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_category_layout, container, false);
txtTitle = ((TextView) rootView.findViewById(android.R.id.title));
txtTitle.setText(mPage.getTitle());
final int[] icons = new int[mChoices.size()];//mChoices.toArray(]);
for (int i = 0; i < icons.length; i++)
icons[i] = Integer.valueOf(mChoices.get(i));
GridView gridView = (GridView) rootView.findViewById(R.id.gridCategoryList);
gridView.setAdapter(new CategoryAdapter(getActivity(), icons));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (previousSelectedView != null) previousSelectedView.setSelected(false);
v.setSelected(true);
previousSelectedView = v;
mPage.getData().putString(Page.SIMPLE_DATA_KEY,
String.valueOf(icons[position]));
wizSession.setValue(pageKey, String.valueOf(position + 1));
//wizSession.setValue(Page.SIMPLE_DATA_KEY, String.valueOf(position + 1));
//mPage.getData().putString(Page.SIMPLE_DATA_KEY, String.valueOf(position + 1));
mPage.notifyDataChanged();
}
});
return rootView;
}
Category_adapter
public class CategoryAdapter extends BaseAdapter {
int[] mCategoryImg;
LayoutInflater inflater;
public CategoryAdapter(Context context, int[] imageItems) {
this.mCategoryImg = imageItems;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mCategoryImg.length;
}
#Override
public Object getItem(int i) {
return mCategoryImg[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View rootView = inflater.inflate(R.layout.category_row, null);
ImageView imageView = (ImageView) rootView.findViewById(R.id.categoryImage);
imageView.setImageResource(mCategoryImg[i]);
return rootView;
}
}
a picture to sample what i mean
highlighted with red

The problem resolved, what was causing the problem that I have included a library which has the same layout name (category_item.xml), I just did refractory to rename the layout and it's solved :D

Related

How to hold the selected image in GridView and pass to next activity after I click Button

In my Activity I have an EditText, a Button and a GridView. The user selects the image from the GridView and enters the name in the EditText, and then clicks the done button.
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/l1"
>
<View
android:layout_width="#dimen/btm_sht_line"
android:layout_height="#dimen/btm_sht_height"
android:layout_marginTop="#dimen/btm_top"
android:layout_gravity="center_horizontal"
android:background="#color/colorPrimaryDark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/header_create_a_new_list"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/l2"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/marin_top"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listname"
android:hint="#string/list_title_name"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="#dimen/marin_top"
android:layout_marginStart="#dimen/marin_top"
android:inputType="text"
android:autofillHints="#string/list_title_name" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/done"
android:src="#drawable/ic_check_circle"
android:layout_marginRight="15dp"
android:layout_marginEnd="#dimen/marin_top"
android:layout_marginLeft="#dimen/margin_left"
android:layout_marginStart="#dimen/margin_left"
tools:ignore="ContentDescription"
/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/l3"
android:orientation="vertical"
>
<View style="#style/Divider"
android:layout_marginTop="#dimen/marin_top"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r1"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll"
><GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridview"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</ScrollView>
</RelativeLayout>
</LinearLayout>
I want to move the selected image and the entered text to another Activity
<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=".CheckslateHome">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/ima"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/getlistname"
android:text="hi welcome"
android:layout_below="#+id/ima"
/>
</RelativeLayout>
In my code when the user selects the image, before entering the name, it loads to a new empty window. Also I have 6 images in my drawable Folder and only 3 images are being displayed in the GridView.
Java class
public class NewListCreate extends BottomSheetDialogFragment {
Integer[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
GridView gridView;
ArrayList<imageModel> arrayList;
public NewListCreate() {}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.new_list_create, container, false);
ImageButton done = view.findViewById(R.id.done);
final EditText listname = (EditText) view.findViewById(R.id.listname);
final GridView gridView = (GridView) view.findViewById(R.id.gridview);
arrayList = new ArrayList<imageModel>();
for (int i = 0; i < images.length; i++) {
imageModel imagemodel = new imageModel();
imagemodel.setmThumbIds(images[i]);
//add in array list
arrayList.add(imagemodel);
}
final ImageAdapterGridView adapterGridView = new ImageAdapterGridView(getContext(), arrayList);
gridView.setAdapter(adapterGridView);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapterGridView.setSelectedPosition(i);
adapterGridView.notifyDataSetChanged();
int imageRes = images[i];
Intent intent = new Intent(getContext(),CheckslateHome.class);
intent.putExtra("IMAGE_RES", imageRes);
startActivity(intent);
}
});
return view;
}
}
AdapterClass
public class ImageAdapterGridView extends BaseAdapter {
private int selectedPosition = -1;
Context context;
ArrayList<imageModel> arrayList;
public ImageAdapterGridView(Context context, ArrayList<imageModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int i) {
return arrayList.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view==null)
{
view = LayoutInflater.from(context).inflate(R.layout.image_list, viewGroup, false);
}
ImageView imageView;
imageView = (ImageView) view.findViewById(R.id.image);
imageView.setImageResource(arrayList.get(i).getmThumbIds());
if (i == selectedPosition) {
view.setBackgroundColor(Color.WHITE);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
public void setSelectedPosition(int position) {
selectedPosition = position;
}
}
what i was Looking was Something Like this
[![enter image description here][1]][1]
Copy and paste this hope it solve ur Problem
public class NewListCreate extends BottomSheetDialogFragment {
int[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
int imageRes = images[0];
public NewListCreate() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_list_create, container, false);
ImageButton done = view.findViewById(R.id.done);
final EditText listname = (EditText) view.findViewById(R.id.listname);
final GridView gridView = (GridView) view.findViewById(R.id.gridview);
final CustomAdpter customAdpter = new CustomAdpter(images,getContext());
gridView.setAdapter(customAdpter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
customAdpter.selectedImage = i;
customAdpter.notifyDataSetChanged();
imageRes = images[i];
}
});
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String itemname = listname.getText().toString();
if (!TextUtils.isEmpty(listname.getText().toString())) {
startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image",imageRes));
dismiss();
} else {
Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
public class CustomAdpter extends BaseAdapter{
private int[] icons;
private Context context;
private LayoutInflater layoutInflater;
public int selectedImage = 0;
public CustomAdpter(int[] icons, Context context) {
this.icons = icons;
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return icons.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null)
{
view = layoutInflater .inflate(R.layout.image_list,viewGroup,false);
}
ImageView imageicons = view.findViewById(R.id.image);
if (i < icons.length) {
imageicons.setImageResource(icons[i]);
if (i != selectedImage) {
imageicons.setImageAlpha(50);
}
imageicons.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageicons.setLayoutParams(new GridView.LayoutParams(150, 150));
if (i == selectedImage) {
view.setBackgroundColor(Color.WHITE);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
};
return view;
}
}
I think what you're looking for is passing data through navigation.
It will allow you send your image and name data from one activity/fragment to the next one.
Check out data-binding as well, might be useful.

Android ListView item clicked not working

I don't understand what's going wrong, when clicking an item on the list nothing appends.
my environment is minsdk:21, maxsdk:27, java 8 (openjdk), android-studio 3.1.4, terminal android 6.0
note : using FragmentManager or SupportFragmentManager provides the same result.
the fragment list 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"
android:descendantFocusability="blocksDescendants"
>
<ListView
android:id="#+id/list"
android:dividerHeight="1sp"
android:divider="#color/blue_serenity_transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
List row 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="wrap_content"
android:orientation="horizontal"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:useDefaultMargins="true"
android:background="#color/blue_serenity_transparent"
android:columnCount="3"
android:rowCount="2"
>
<ImageView
android:id="#+id/notif_type"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="center_vertical"
/>
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_column="1"
android:layout_row="0"
android:textSize="18dp"
android:textColor="#color/black"
/>
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_row="1"
android:layout_column="1"
/>
</GridLayout>
</LinearLayout>
list setup
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<NotificationBean> data = new NotificationBean().list(getActivity().getBaseContext());
View v = inflater.inflate(R.layout.fragment_notifications, container, false);
list = (ListView) v.findViewById(R.id.list);
listAdapter = new Adapter(getActivity().getBaseContext(), data);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "row clicked");
}
});
return v;
}
List adapter
class NotificationsAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater = null;
private List<NotificationBean> notifications;
public NotificationsAdapter(Context context, List<NotificationBean> notifications){
this.context = context;
this.notifications = notifications;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return notifications.size();
}
#Override
public Object getItem(int position) {
return notifications.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
v = inflater.inflate(R.layout.fragment_notifications_row, null);
NotificationBean n = notifications.get(position);
GridLayout layout = (GridLayout) v.findViewById(R.id.row_layout);
if (dark) layout.setBackgroundColor(getResources().getColor(R.color.blue_serenity_transparent));
else layout.setBackgroundColor(getResources().getColor(R.color.white));
dark = !dark;
ImageView notifType = (ImageView) v.findViewById(R.id.notif_type);
notifType.setImageResource(n.getIcon());
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(n.getTitle());
TextView date = (TextView) v.findViewById(R.id.date);
date.setText(Constants.DTF.format(n.getDate()));
return v;
}
}

Custom GridView adapter changing image visibility status

I have a custom gridView and inside it i am having two images in a relative layout . Now when i click on the gridView i want the visibility of my images to change.
MainActivity:
public class Main extends Fragment implements OnMapReadyCallback{
GridView gridview,video_gridview;
GridViewAdapter adapter;
View v;
public Main() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_main,
container, false);
gridview = (GridView)v.findViewById(R.id.sub_notify_gridView);
gridview.setNumColumns(3);
adapter = new GridViewAdapter(getActivity(),FilePathStrings,
FileNameStrings,Image_Status,time);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int
position, long id) {
//here i need to know which image has been clicked
}
});
return v;
}
MainActivity Layout:
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".35"
android:id="#+id/sub_notify_gridView">
<!--<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridViewImageid"/>-->
</GridView>
Cusotm GridView Adapter:
public class GridViewAdapter extends BaseAdapter {
// Declare variables
private Activity context;
private String[] filepath;
private String[] imageStatus;
private String Imgtime;
String[] separated;
private static LayoutInflater inflater = null;
public GridViewAdapter(FragmentActivity activity, String[] fpath, String[] fname, String[] image_status, String time) {
context = activity;
filepath = fpath;
imageStatus = image_status;
Imgtime = time;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.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 vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
//TextView text = (TextView) vi.findViewById(R.id.text);
final ImageView image = (ImageView) vi.findViewById(R.id.image);
ImageView upload_image = (ImageView) vi.findViewById(R.id.upload_image);
TextView time = (TextView) vi.findViewById(R.id.textView);
time.setText(Imgtime);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setPadding(5, 3, 5, 2); //itrb
//text.setText(filename[position]);
Bitmap ThumbImage =ThumbnailUtils.extractThumbnail
(BitmapFactory.decodeFile(filepath[position]),
128, 128);
if(imageStatus[position].equals("0")){
upload_image.setVisibility(View.VISIBLE);
}
if (filepath[position] == null){
image.setImageResource(R.drawable.imageicon);
}
else{
if (position > 4){
image.setImageBitmap(ThumbImage);
}
else{
image.setImageBitmap(ThumbImage);
}
}
return vi;
}
GridView_item Layout File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:custom="http://schemas.android.com/tools"
android:padding="5dip">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignBottom="#+id/image"
android:layout_alignRight="#+id/image"
android:layout_alignEnd="#+id/image"
android:paddingRight="5dp"
android:paddingBottom="1.5dp"
android:id="#+id/textView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingBottom="1.5dp"
android:id="#+id/upload_image"
android:visibility="gone"
android:src="#drawable/accept_icon"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

"Gridview Images" and "Full screen" fragments in multi pane(android)

I'm new android..my english little bit:(
I want develop multi pane app.
Two fragments "Gridview" and "Full screen"
I know multi pane, but i dont know Gridview and Full screen in multi pane.
Because all samples for ListView.
help me please
this app good for me but too complex=
http://www.codeproject.com/Articles/779293/Building-Dynamic-UI-for-Android-Devices
Its a simple implementation if i understand your question correctly
------------- Creating GridView ------------
For GridView you need an adapter for your GridView Object and images for columns, an example using country list and flag images,
Create drawable folder inside res and add country flag images inside drawable folder
Create gridlayout.xml in res/layout
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/border"
android:padding="5dp">
<ImageView
android:id="#+id/gridimage"
android:layout_height="65dp"
android:layout_width="65dp"
android:src="#drawable/icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:id="#+id/gridtext"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:ellipsize="marquee"></TextView>
</RelativeLayout>
Create gridrow.xml in res/Layout folder
<GridView
android:id="#+id/gridView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
Create GridAdapter.java class
public class GridAdapter extends BaseAdapter {
private ArrayList<String> CountryList;
private ArrayList<Integer> CountryFlag;
private Activity activity;
public GridAdapter(Activity activity,ArrayList<String> CountryList, ArrayList<Integer> CountryFlag){
super();this.CountryList = CountryList;
this.CountryFlag = CountryFlag;
this.activity = activity;}
#Override
public int getCount() {
return CountryList.size();
}
#Override
public String getItem(int position) {
return CountryList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.gridtext);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.gridimage);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(CountryList.get(position));
view.imgViewFlag.setImageResource(CountryFlag.get(position));
return convertView;
}
}
Create Fragment GridViewActivty
public class GridViewActivty extends Fragment {
private ArrayList CountryList = new ArrayList();
private ArrayList CountryFlag = new ArrayList();
private GridView mygrid;
private GridAdapter adapter;
private Context context;
public GridViewActivty(){}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{ View rootView = inflater.inflate(R.layout.gridlayout.xml, container, false);
CountryList = new ArrayList<String>();
CountryList.add("South Africa");
CountryList.add("Spain");
CountryList.add("Canada");
CountryList.add("China");
CountryList.add("France");
CountryList.add("Germany");
CountryList.add("Iran");
CountryList.add("Italy");
CountryList.add("Japan");
CountryList.add("Korea");
CountryList.add("Mexico");
CountryList.add("Netherlands");
CountryFlag = new ArrayList<Integer>();
CountryFlag.add(R.drawable.southafrica);
CountryFlag.add(R.drawable.spain);
CountryFlag.add(R.drawable.canada);
CountryFlag.add(R.drawable.china);
CountryFlag.add(R.drawable.france);
CountryFlag.add(R.drawable.germany);
CountryFlag.add(R.drawable.iran);
CountryFlag.add(R.drawable.italy);
CountryFlag.add(R.drawable.japan);
CountryFlag.add(R.drawable.korea);
CountryFlag.add(R.drawable.mexico);
CountryFlag.add(R.drawable.netherlands);
adapter = new GridAdapter(this.getActivity(),CountryList, CountryFlag);
mygrid = (GridView)rootView.findViewById(R.id.gridview1);
mygrid.setAdapter(adapter);
mygrid.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
}
);
return rootView;
}
}
----------- Creating Full Screen --------
Create fullscreen.xml inside Layout Folder
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
>
Create FullScreen.java Fragment
public class Fullscreen extends Fragment{ #Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{ View rootView = inflater.inflate(R.layout.fullscreen.xml, container, false);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
return convertView; }

Gridview numbering

I have this gridview. is it possible to have numbers on the side of gridview like in excel 1,2,3 or a, b, c ,d
my code
<?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" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:stretchMode="columnWidth" />
</RelativeLayout>
something i want to achieve
You'll have to add numbering to each of the cells and only display it when it is the leftmost cell.
Cell layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/row_number"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- The rest of your layout -->
</LinearLayout>
</LinearLayout>
In your Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.grid_cell);
TextView rowNumber = (TextView) v.findViewById(R.id.row_number);
if (position % colNum == 0) { // colNum: number of columns
int row = position / colNum + 1; // get the row number
rowNumber.setText(String.valueOf(row));
rowNumber.setVisibility(View.VISIBLE);
} else {
rowNumber.setVisibility(View.GONE);
}
return v;
}
Note: For the sake of simplicity I am not using the ViewHolder pattern. But you should!
// try this way hope this will help you...
grid_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/txtGridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<ImageView
android:id="#+id/imgGridItem"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:stretchMode="columnWidth" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private GridView gridview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
ArrayList<String> images = new ArrayList<String>();
images.add("image1path");
images.add("image2path");
images.add("image3path");
images.add("image4path");
images.add("image5path");
images.add("image6path");
images.add("image7path");
images.add("image8path");
images.add("image9path");
images.add("image10path");
images.add("image11path");
images.add("image12path");
gridview.setAdapter(new GridAdapter(images,this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,String.valueOf(position+1),Toast.LENGTH_SHORT).show();
}
});
}
class GridAdapter extends BaseAdapter{
private Context context;
private ArrayList<String> images;
public GridAdapter(ArrayList<String> images,Context context){
this.context = context;
this.images = images;
}
#Override
public int getCount() {
return images.size();
}
#Override
public Object getItem(int position) {
return images.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.grid_item,null,false);
holder.imgGridItem = (ImageView) convertView.findViewById(R.id.imgGridItem);
holder.txtGridItem = (TextView) convertView.findViewById(R.id.txtGridItem);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
if(position%4 == 0){
holder.txtGridItem.setVisibility(View.VISIBLE);
holder.txtGridItem.setText(String.valueOf((position / 4)+1));
}else{
holder.txtGridItem.setVisibility(View.GONE);
}
holder.imgGridItem.setImageResource(R.drawable.ic_launcher);
return convertView;
}
class ViewHolder{
ImageView imgGridItem;
TextView txtGridItem;
}
}
}

Categories

Resources