Instead of using images like in this example I found from Android Developer Site, I want to use edit texts in the gridView. What kind of adapter would I use? Do I even need an adapter?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
Ok, so i tried doing it, but its not working. It keeps crashing.
#imports....
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scout);
GridView gridView = (GridView) findViewById(R.id.gridView);
//final String[] letters = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I"};
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, letters);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter.java
//used for adding Edit texts to the grid view in the main activity
#imports....
public class ImageAdapter extends BaseAdapter{
Context context;
public ImageAdapter(Context context)
{
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
{
gridView = new View(context);
gridView = inflater.inflate(R.layout.grid_items, null);
EditText editText = (EditText) gridView.findViewById(R.id.grid_item_edit_text);
}
gridView = (View)convertView;
return gridView;
}
#Override
public int getCount() {
return 9;//only 9 pegs on the center rack
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
You need a custom Gridview. In another word a Custom Adapter
This tutorial will help you.
Related
This is Adapter for grid view. I included image and letters in adapter class.
public class GridAdapter extends BaseAdapter {
private int icons[];
private String letters[];
private Context context;
private LayoutInflaterinflater;
public GridAdapter(Context context , int icons[] , String letters[]){
this.context=context;
this.icons=icons;
this.letters=letters;
}
#Override
public int getCount() {
return letters.length;
}
#Override
public Object getItem(int position) {
return letters[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){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = inflater.inflate(R.layout.custom_layout,null);
}
ImageView icon = (ImageView) gridView.findViewById(R.id.icons);
TextView letter = (TextView) gridView.findViewById(R.id.letters);
icon.setImageResource(icons[position]);
letter.setText(letters[position]);
return gridView;
}
}
This is main class. I made 8 card and want to add an activity for each card.how to add activity for each card.
public class NavigationMainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
GridView gridView;
String letterList[] = {"Types of industries", "Safety Sign's",
"Tools & equipments", "Slogens", "Low", "Definitions", "First aid kit", "Near by fire stations"};
int lettersIcon[] = {R.drawable.safetyicon, R.drawable.signs, R.drawable.tools,
R.drawable.slogen, R.drawable.low, R.drawable.definitions,
R.drawable.firstkit, R.drawable.firestations};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
gridView = (GridView) findViewById(R.id.gridView);
GridAdapter adapter = new GridAdapter(NavigationMainActivity.this, lettersIcon, letterList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), TypesOfIndusturies.class);
}
});
I have this:
public class First_fragment extends Fragment {
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_profesorado,container,false);
String[] menuItems = {
"Do some","kkk", ",,,,,", "Do some","kkk", ",,,,,",
"Do some", "kkk", ",,,,,", "Do some", "kkk", ",,,,,",
"Do some", "kkk", ",,,,,"
};
ListView listView = (ListView) v.findViewById(R.id.mymenu);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, menuItems
);
listView.setAdapter(listViewAdapter);
return v;
}
}
With the text is correct, I want to use IMAGES instead of text. 10 images in listview. How can do this? Do you have any examples? I am really tired of this because I have been programming all day about this and I failed.
Its possibly to do that using ListActivity and ItemsAdapter. See below code:
public class ItemsList extends ListActivity {
private ItemsAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.items_list);
this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems());
setListAdapter(this.adapter);
}
private class ItemsAdapter extends ArrayAdapter<Item> {
private Item[] items;
public ItemsAdapter(Context context, int textViewResourceId, Item[] items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.items_list_item, null);
}
Item it = items[position];
if (it != null) {
ImageView iv = (ImageView) v.findViewById(R.id.list_item_image);
if (iv != null) {
iv.setImageDrawable(it.getImage());
}
}
return v;
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
this.adapter.getItem(position).click(this.getApplicationContext());
}
}
EDIT: It seems like you are new to android development. You have to use ImageView:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/picture_you_want_to_use" />
I am working on a setup screen that allows the user to change the color theme of the app. I want to use a GridView of colored square images for the user to choose from. Right now I have a toast message that pops with the position in the color image array when clicked. The following opens my theme activity from a setup menu:
themeButton = (ImageButton) findViewById(R.id.imBtnAccentColor);
themeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SetupContactActivity.this, ThemeActivity.class);
startActivity(intent);
}
});
Here's an image of the GridView that the user will choose a theme color from.
gridViewTiles
Theme Activity:
public class ThemeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(ThemeActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is an ImageAdapter class that helps build the image view based on the number of images in my array of images:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// If it's not recycled, initialize some attributes.
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(320, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// References to the color images
private Integer[] mThumbIds = {
R.drawable.orange,
R.drawable.yellow,
R.drawable.yellow_orange,
R.drawable.brown,
R.drawable.medium_grey,
R.drawable.dark_grey,
R.drawable.pink,
R.drawable.light_blue,
R.drawable.medium_green,
R.drawable.purple,
R.drawable.blue,
R.drawable.brownish_green,
R.drawable.dark_purple,
R.drawable.dark_blue,
R.drawable.green
};
}
I made a listview in Android Studio. The listview has a image in every item but I don't know how to make it clickable. I did browse the internet for a solution but the hard part seems to be implementing it in my own code. I cannot figure that out.
Image in question = ex_img
-- Keep in mind when the image is clicked, it should also know its position in the list.
Thanks for reading and I hope you can help me out.
Adapter for list:
public class CustomList extends ArrayAdapter<String> {
private String[] ex_name;
private String[] ex_diff;
private String[] ex_muscle;
private String[] ex_dpr;
private Integer[] ex_img;
private Activity context;
public CustomList(Activity context, String[] ex_name, String[] ex_diff, String[] ex_muscle, String[] ex_dpr,
Integer[] ex_img) {
super(context, R.layout.row_layout, ex_name);
this.context = context;
this.ex_name = ex_name;
this.ex_muscle = ex_muscle;
this.ex_diff = ex_diff;
this.ex_dpr = ex_dpr;
this.ex_img = ex_img;
}
//LIST --> XML
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.row_layout, null, true);
TextView list_name = (TextView) listViewItem.findViewById(R.id.ex_name);
TextView list_diff = (TextView) listViewItem.findViewById(R.id.ex_diff);
TextView list_muscle = (TextView) listViewItem.findViewById(R.id.ex_muscle);
ImageView list_image = (ImageView) listViewItem.findViewById(R.id.ex_img);
TextView list_dpr = (TextView) listViewItem.findViewById(R.id.ex_dpr);
list_name.setText(ex_name[position]);
list_muscle.setText(ex_muscle[position]);
list_diff.setText(ex_diff[position]);
list_dpr.setText(ex_dpr[position]);
list_image.setImageResource(ex_img[position]);
return listViewItem;
}
}
Part from MainActivity that might be needed:
private Integer img[] = {
R.drawable.ic_favorite_white_24dp,
R.drawable.ic_location_on_white_24dp,
R.drawable.ic_update_white_24dp,
R.drawable.ic_local_dining_white_24dp,
R.drawable.ic_local_dining_white_24dp
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList customList = new CustomList(this, name, diff, muscle, dpr, img);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(customList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),"You Clicked "+name[i],Toast.LENGTH_SHORT).show();
}
});
}
Add onClickListener to your ImageView in getView() -
list_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here you have the position too.
});
Just make the position parameter final in getView()
I am new to developing for android, I'm trying to make a ListActivity with title and subtitle.
So far only managed to make the title:
this.data = new ArrayList<String>();
// add some objects into the array list
this.data.add("YOU WILL HEAR");
this.data.add("USEFUL PHRASES");
this.data.add("VOCABULARY");
this.data.add("DIALOGUES");
this.data.add("INFORMATION");
this.port = new ArrayList<String>();
this.port.add("BEM VINDO");
this.port.add("FRASES ÚTEIS");
this.port.add("VOCABULÁRIO");
this.port.add("DIÁLOGOS");
this.port.add("VOCABULÁRIO");
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.title,this.data));
ListView lv = getListView();
this code is in the onCreate method.
My question is, how i can populate the R.id.subtitle with the second array?
try the following code:
public ListView lv;
lv = (ListView) findViewById(R.id.ListView01);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setBackgroundResource(R.drawable.background);
lv.setCacheColorHint(00000000);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
class MySimpleArrayAdapter extends ArrayAdapter<String> {
private Context context;
public MySimpleArrayAdapter(Context context) {
super(context, R.layout.list);
this.context = context;
}
public int getCount() {
return names.size();
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = vi.inflate(R.layout.list, null);
}
TextView name = (TextView) rowView.findViewById(R.id.Name);
TextView number = (TextView) rowView.findViewById(R.id.no);
return rowView;
}
}