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>
Related
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.
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
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; }
As you can see in my screenshot, whole of layout doesn't display in dialogFragment although it has enough space. I have no idea what is my mistake.
I expect to see a 3 * 3 table.
public class ChangeFormationDialogFragment extends DialogFragment {
public ChangeFormationDialogFragment() {
// Empty constructor required for DialogFragment
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_change_formation, container);
GridView gridView = (GridView) view.findViewById(R.id.formation_change_grid);
gridView.setAdapter(new FormationAdapter(getActivity(), 0));
return view;
}
}
It's xml file (fragment_change_formation):
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/myTem_bg_boost_cards"
android:id="#+id/formation_change_grid"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"/>
My adapter class:
public class FormationAdapter extends BaseAdapter {
private static final String TAG = "FormationAdapter";
private Context mContext;
private ArrayList<String> formations;
private int selected = 0;
public FormationAdapter(Context context, int selected) {
this.mContext = context;
formations = Formation.getAllFormationsAsArrayOfStrings();
this.selected = selected;
}
#Override
public int getCount() {
if(formations == null)
return 0;
return formations.size();
}
#Override
public Integer getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.row_formation, null);
}
holder = new ViewHolder();
holder.llFormation = (LinearLayout) convertView.findViewById(R.id.llFormation);
holder.llFormationViews = (LinearLayout) convertView.findViewById(R.id.llFormationViews);
holder.tvFormation = (AnyTextView) convertView.findViewById(R.id.tvFormation);
holder.tvBuyButton = (AnyTextView) convertView.findViewById(R.id.tvBuyButton);
holder.tvFormation.setText(formations.get(position));
if(position > 2)
holder.tvBuyButton.setVisibility(View.VISIBLE);
if(position == selected)
holder.llFormation.setBackgroundColor(mContext.getResources().getColor(R.color.myTeam_greenDark));
else
holder.llFormation.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
return convertView;
}
static class ViewHolder {
LinearLayout llFormation;
LinearLayout llFormationViews;
AnyTextView tvFormation;
AnyTextView tvBuyButton;
}
}
And finally, row:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:id="#+id/llFormation">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/llFormationViews">
</LinearLayout>
<com.allstarxi.widget.AnyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvFormation"
android:layout_gravity="center"
style="#style/font_normal_20.white"
custom:typeface="baltomobilebold.ttf"/>
<com.allstarxi.widget.AnyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBuyButton"
android:text="#string/myTeam_dialog_buy"
android:layout_gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="#color/myTem_bg_points"
android:textStyle="bold"
android:visibility="gone"
style="#style/font_normal_14.black"
custom:typeface="baltomobilebook.ttf"/>
</LinearLayout>
This is screenshot of what I see on my device:
Just try this:
Add the following code inside 'onResume()'
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes(params);
My activity contains a textview and under that it has gridview elements. Stage 1: When the activity starts, it adjusts the elements in the order that I have set(like item1, item2,..) stage1 picture. Stage 2: But when I click on edittext, the gridview elements get adjusted to allocate space for the softkeypad stage2 picture. Stage 3: So when I press back button, the soft keypad disappears(as usual), but the order of gridview elements are getting changed stage3 picture. The order of child items of gridview changes. Can anyone please suggest me how to avoid this uncertainty in the order? Below is my entire code and xml file, though I think these are not that much necessary to solve this issue.
public class PresentActivity extends Activity {
GridView gridView;
static final String[] TEXT = new String[] {
"item1", "item2","item3", "item4", "item5","item6","item7","item8" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setGridView();
}
public void setGridView()
{
gridView = (GridView)findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, TEXT));
gridView.setNumColumns(3);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String currentText=(String) ((TextView)v.findViewById(R.id.grid_item_label)).getText();
Toast.makeText(getApplicationContext(), currentText, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is my custom adapter class.
public class ImageAdapter extends BaseAdapter{
private Context context;
private final String[] textValues;
public ImageAdapter(Context context, String[] textValues) {
this.context = context;
this.textValues = textValues;
}
#Override
public int getCount() {
return textValues.length;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
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 imagewithtext.xml
gridView = inflater.inflate(R.layout.imagewithtext, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(textValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String extractedText = textValues[position];
System.out.println("the position value is>>>>>>"+position);
if (extractedText.equals("item1")) {
imageView.setImageResource(R.drawable.icon);
}
else if (extractedText.equals("item2")) {
imageView.setImageResource(R.drawable.icon);
} else if (extractedText.equals("item3")) {
imageView.setImageResource(R.drawable.icon);
}else if(extractedText.equals("item4")){
imageView.setImageResource(R.drawable.icon);
} else if (extractedText.equals("item5")) {
imageView.setImageResource(R.drawable.icon);
} else if (extractedText.equals("item6")) {
imageView.setImageResource(R.drawable.pic1);
}
else if(extractedText.equals("item7")){
imageView.setImageResource(R.drawable.pic1);
}
else{
imageView.setImageResource(R.drawable.pic1);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
}
xml file which contains gridview:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="#string/button"
android:onClick="button1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="#string/present_location"
/>
</RelativeLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/editText1_hint"
/>
<GridView
android:id="#+id/gridView1"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
</LinearLayout>
Let's assume your items contain just 1 ImageView and 1 TextView (for the sake of simplicity).
Your getView method in the Adapter should do this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
final LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.imagewithtext,null);
//Custom ViewHolder, you have to create it in the same class.
vh = new ViewHolder();
vh.imageView = convertView.findViewById(R.id.imageView1);
vh.textView = convertView.findViewById(R.id.textView1);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.imageView.setImageResource(R.drawable.whatever);
vh.textView.setText("Whatever text you want to set here");
return gridView;
}
And in your adapter, just add this:
private class ViewHolder{
public ViewHolder(){}
public ImageView imageView;
public TextView textView;
//Add any views you want to use in getView here
}
GridViewActivity:-(This is main Activity)
public class GridVieweActivity extends Activity {
GridView gridView;
static final String[] TEXT = new String[] { "item1", "item2", "item3",
"item4", "item5", "item6", "item7", "item8" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setGridView();
}
public void setGridView() {
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, TEXT));
gridView.setNumColumns(3);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String currentText = (String) ((TextView) v
.findViewById(R.id.textView1)).getText();
Toast.makeText(getApplicationContext(), currentText,
Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter:-
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] textValues;
public ImageAdapter(Context context, String[] textValues) {
this.context = context;
this.textValues = textValues;
}
#Override
public int getCount() {
return textValues.length;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
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 imagewithtext.xml
gridView = inflater.inflate(R.layout.imagewithtext, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.textView1);
textView.setText(textValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView1);
String extractedText = textValues[position];
System.out.println("the position value is>>>>>>" + position);
if (extractedText.equals("item1")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item2")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item3")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item4")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item5")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item6")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item7")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else {
imageView.setImageResource(R.drawable.ic_launcher);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
}
main.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:orientation="vertical" >
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="button1"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Present Location" />
</RelativeLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint" />
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</LinearLayout>
imagewithtext:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ImageView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:ellipsize="marquee"
android:text="TextView"
android:textSize="18sp" >
</TextView>
</RelativeLayout>
The above code is working fine.