I want my GridView to display only 16 random letters, a 4x4 tile and if it's possible I don't want any space between the letters.
I also want it to display its corresponding text to a TextView to form a word. I put it in a toast for now to show how it should be. But it only shows the corresponding letter of the image. How can I achieve that? I dont have any idea on how should i do it.
Any help will be appreciated.
New to Android Programming
grid_row.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"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="1dp"
android:layout_marginEnd="1dp"
android:contentDescription="#string/hello_world"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="15sp"
android:visibility="gone" >
</TextView>
</LinearLayout>
activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.grid.gridgame.MainActivity" >
<TextView
android:id="#+id/wordHere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1234567890"/>
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</RelativeLayout>
MainActivity.java
package com.grid.gridgame;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView gridView;
String word;
int rando;
static final String[] setOne = new String[] {
"A", "M","C", "Qu",
"Z", "B", "D", "A",
"E", "I", "E", "L",
"M", "N", "T", "P" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridViewCustom);
gridView.setAdapter(new GridViewCustomAdapter(this, setOne));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.textView))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GridViewCustomAdapter
package com.grid.gridgame;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewCustomAdapter extends BaseAdapter {
private Context context;
private final String[] setOne;
public GridViewCustomAdapter(Context context, String[] setOne) {
this.context = context;
this.setOne = setOne;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
convertView = new ImageView(context);
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid_row, null);
// set value into TextView
TextView textView = (TextView) gridView
.findViewById(R.id.textView);
textView.setText(setOne[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
String allletters = setOne[position];
if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("C")) {
imageView.setImageResource(R.drawable.c);
} else if (allletters.equals("Qu")) {
imageView.setImageResource(R.drawable.qu);
} else if (allletters.equals("Z")) {
imageView.setImageResource(R.drawable.z);
} else if (allletters.equals("B")) {
imageView.setImageResource(R.drawable.b);
} else if (allletters.equals("D")) {
imageView.setImageResource(R.drawable.d);
} else if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("I")) {
imageView.setImageResource(R.drawable.i);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("N")) {
imageView.setImageResource(R.drawable.n);
} else if (allletters.equals("T")) {
imageView.setImageResource(R.drawable.t);
} else if (allletters.equals("P")) {
imageView.setImageResource(R.drawable.p);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return setOne.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
If you want to store the letter you can simply create a string and store the letter push by user.
Example code:
String word; // word that you want to be display
String letter = MOBILE_OS[id]; // get the corresponding letter from array from the user selected
word.add(letter);
Related
I created a custom adapter to fill the item i have depending on how many elements there is but it only fills the first item and i can't figure out why
main.xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:id="#+id/customList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView" />
</RelativeLayout>
item.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/item_text_2"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 2"
android:layout_gravity="right"
android:layout_below="#+id/item_text_1"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/item_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/item_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
Custom Class
public class CustomClass {
private String txt1, txt2, txt3;
public CustomClass(String txt1, String txt2, String txt3) {
this.txt1 = txt1;
this.txt2 = txt2;
this.txt3 = txt3;
}
public String getTxt1() {
return txt1;
}
public String getTxt2() {
return txt2;
}
public String getTxt3() {
return txt3;
}
}
MainActivity
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<CustomClass> lst_stuff = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCustomClass();
populateListView();
}
private void populateCustomClass()
{
lst_stuff.add(new CustomClass("First", "I Got", "FIRST!!!!!"));
lst_stuff.add(new CustomClass("Second", "How did you...", "you know what GG"));
lst_stuff.add(new CustomClass("Third", "I Got", "Last?!! ... I am just too good to be first"));
}
private void populateListView()
{
ArrayAdapter<CustomClass> adapter = new CustomAdapter();
ListView lst = (ListView) findViewById(R.id.customList);
lst.setAdapter(adapter);
}
private class CustomAdapter extends ArrayAdapter<CustomClass>
{
public CustomAdapter()
{
super(MainActivity.this, R.layout.item, lst_stuff);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.item, parent, false);
try
{
CustomClass customClass = lst_stuff.get(position);
TextView textView_1 = (TextView) findViewById(R.id.item_text_1);
textView_1.setText(customClass.getTxt1());
TextView textView_2 = (TextView) findViewById(R.id.item_text_2);
textView_2.setText(customClass.getTxt2());
TextView textView_3 = (TextView) findViewById(R.id.item_text_3);
textView_3.setText(customClass.getTxt3());
}
catch (NullPointerException exception)
{
Toast.makeText(MainActivity.this, "Exception", Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}
return itemView;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
is there something missing or something doing wrong?
All seems to be ok, but I would change what is on the method getView() by this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
CustomClass customClass = lst_stuff.get(position);
((TextView) rowView.findViewById(R.id.item_text_1)).
setText(customClass.getTxt1());
((TextView) rowView.findViewById(R.id.item_text_2)).
setText(customClass.getTxt2());
((TextView) rowView.findViewById(R.id.item_text_3)).
setText(customClass.getTxt3());
return rowView;
}
I think that you must obtain first a instance of inflater, and then inflate your view (rowView) with your layout (R.layout.item).
Good luck!
I have a ListView but I can´t put images on it.
The error is in Activity_listView at holder.image.setImageDrawable(datos[position].getImage());
The error message is
The method setImageDrawable(Drawable) in the type ImageView is not applicable
for the arguments (ImageView)
Any idea for solve this?
Activity (Activity_listView):
package com.simarro.asteroids;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Activity_listView extends ActionBarActivity {
private ListView lstOpciones;
ImageView asteroid = (ImageView)findViewById(R.id.img_asteroid);
private puntuacion[] datos = new puntuacion[] {
new puntuacion(asteroid,"Pepito Dominguez", "15489"),
new puntuacion(asteroid,"Pedro Martínez", "16598"),
new puntuacion(asteroid,"Paco Perez", "16332"),
new puntuacion(asteroid,"Rosana Fernandez", "18792"),
new puntuacion(asteroid,"Paco Jones", "960") };
class AdaptadorTitulares extends ArrayAdapter<puntuacion> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.puntuacion, datos);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ViewHolder holder;
if (item == null) {
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(R.layout.puntuacion, null);
holder = new ViewHolder();
holder.image=(ImageView)item.findViewById(R.id.img_asteroid);
holder.jugador = (TextView) item.findViewById(R.id.LblJugador);
holder.puntuacion = (TextView) item.findViewById(R.id.LblPuntuacion);
item.setTag(holder);
} else {
holder = (ViewHolder) item.getTag();
}
holder.image.setImageDrawable(datos[position].getImage());
holder.jugador.setText(datos[position].getJugador());
holder.puntuacion.setText(datos[position].getPuntuacion());
return (item);
}
}
static class ViewHolder {
ImageView image;
TextView jugador;
TextView puntuacion;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
AdaptadorTitulares adaptador = new AdaptadorTitulares(this);
lstOpciones = (ListView) findViewById(R.id.LstOpciones);
//¡FUUUUUUUUU-SIÓN!
lstOpciones.setAdapter(adaptador);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_list_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The xml of content for layout with listView (puntuacion.xml):
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal" >
<ImageView
android:id="#+id/img_asteroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/asteroid" />
<TextView
android:id="#+id/LblJugador"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:textSize="30px"
android:textStyle="bold" />
<TextView
android:id="#+id/LblPuntuacion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:textStyle="normal" />
</GridLayout>
The layout where is the listView (listView.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/LstOpciones"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The class for get values (Puntuacion)
package com.simarro.asteroids;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
public class puntuacion
{
private ImageView image;
private String jugador;
private String puntuacion;
public puntuacion(ImageView img, String tit, String sub){
image=img;
puntuacion = sub;
jugador = tit;
}
public ImageView getImage(){
return image;
}
public String getJugador(){
return jugador;
}
public String getPuntuacion(){
return puntuacion;
}
}
Instead of returning ImageView in getImage(), return a Drawable and change it to
public Drawable getImage() {
return image.getDrawable();
}
getDrawable() Docs
As the error says, the method setImageDrawable() takes a Drawable type but you are passing it an ImageView
I am fairly new to android development and i'm creating a few simple applications for myself.
I have ran into a problem in relation to animations, in particular a flip animation on each individual cell of a gridview when that cell is clicked.
What my application does so far is retrieves the contacts from the phone and displays the contact photo and the name in a gridview using an array adapter.
What i want to happen is when the user clicks on the grid cell of a contact it will perform a flip animation and display the contacts phone number on the back.
When the user clicks on that cell again it will flip back to the previous view of the name and photo.
I have searched the internet and tried a few tutorials but none that have been of any great help, must of them infact confuse me, so if someone could help that would be great.
I'll post my current code below!
Thanks in advance.
This code is used to retrieve the information of the contacts!
package content;
import android.provider.MediaStore.Images;
public class ContactBean {
private String name;
private String phoneNo;
private String proPic;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getProPic() {
return proPic;
}
public void setProPic(String proPic) {
this.proPic = proPic;
}
}
ViewContatcsActivity
package viewContacts;
import com.example.contactflipper.R;
import content.ContactBean;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ViewContactsActivity extends Activity implements
OnItemClickListener {
private GridView listView;
private List<ContactBean> list = new ArrayList<ContactBean>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_viewcon_grid);
listView = (GridView) findViewById(R.id.gridview);
listView.setOnItemClickListener(this);
String image_uri = "";
Bitmap bitmap = null;
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
image_uri = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
ContactBean objContact = new ContactBean();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
list.add(objContact);
}
phones.close();
GridAdapter objAdapter = new GridAdapter(
ViewContactsActivity.this, R.layout.card_front, list);
listView.setAdapter(objAdapter);
if (null != list && list.size() != 0) {
Collections.sort(list, new Comparator<ContactBean>() {
#Override
public int compare(ContactBean lhs, ContactBean rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});
AlertDialog alert = new AlertDialog.Builder(
ViewContactsActivity.this).create();
alert.setTitle("");
alert.setMessage(list.size() + " Contact Found!!!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
} else {
showToast("No Contact Found!!!");
}
}
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
#Override
public void onItemClick(AdapterView<?> listview, View v, int position,
long id) {
ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
//implement something on the click of each listed item - bean
}
}
GridAdapter
package viewContacts;
import java.util.List;
import com.example.contactflipper.R;
import com.example.contactflipper.R.id;
import content.ContactBean;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridAdapter extends ArrayAdapter<ContactBean> {
private Activity activity;
private List<ContactBean> items;
private int row;
private ContactBean objBean;
public GridAdapter(Activity act, int row, List<ContactBean> items) {
super(act, row, items);
this.activity = act;
this.row = row;
this.items = items;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.tvname = (TextView) view.findViewById(R.id.profileName);
//holder.tvPhoneNo = (TextView) view.findViewById(R.id.tvphone);
if (holder.tvname != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tvname.setText(Html.fromHtml(objBean.getName()));
}
if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
&& objBean.getPhoneNo().trim().length() > 0) {
holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
}
if (holder.ivPic != null && null != objBean.getProPic()
&& objBean.getProPic().trim().length() > 0) {
holder.ivPic.setBackground((Drawable) Html.fromHtml(objBean.getProPic()));
}
return view;
}
public class ViewHolder {
public TextView tvname, tvPhoneNo;
public ImageView ivPic;
}
}
ViewContactsFragment
package viewContacts;
import com.example.contactflipper.R;
import com.example.contactflipper.R.id;
import com.example.contactflipper.R.layout;
import content.AppDetails;
import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
public class VCGridFragment extends Fragment {
public static GridView mGridview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.menu_viewcon_grid, container, false);
Log.d("Called", "on Create View");
return view;
}
//super.onCreateView(inflater, container, savedInstanceState);
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
Log.d("Called", "created Grid");
mGridview = (GridView) view.findViewById(R.id.gridview);
Configuration config = getResources().getConfiguration();
if(AppDetails.isTablet){
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
mGridview.setNumColumns(4);
}else{
mGridview.setNumColumns(3);
}
}else{
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
mGridview.setNumColumns(3);
}else{
mGridview.setNumColumns(2);
}
}
/*
mGridview.setOnItemClickListener(new onItemClickLitener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
onGridItemClick((GridView) parent, view, position, id);
}
});
}
public void onGridItemClick(GridView g, View v, int position, long id){
Activity activity = getActivity();
}
*/
}
}
GridView xml -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/contact_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#ffffff"
android:scrollbars="none"
tools:context=".MainActivity" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/gradient"
android:padding="10dp" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:numColumns="2"
android:horizontalSpacing="8dp"
android:scrollbars="none"
android:verticalSpacing="8dp" >
</GridView>
<Button
android:id="#+id/backButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="#8043BFC7"
android:text="#string/edit_contacts"
android:textColor="#ffffff"
android:textStyle="bold" />
</FrameLayout>
</RelativeLayout>
FRONT OF CARD XML -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp"
android:layout_height="180dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/profileThumb"
android:layout_width="match_parent"
android:layout_height="140dp"
></ImageView>
<TextView
android:id="#+id/profileName"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:background="#40000000"
android:textColor="#ffffff"
android:textSize="22sp">
</TextView>
</LinearLayout>
BACK OF CARD XML -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp"
android:layout_height="180dp"
android:orientation="vertical"
android:background="#a6c"
android:padding="10dp"
android:gravity="bottom"
>
<TextView
android:id="#+id/infoName"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HCKDAHOIW"
></TextView>
<TextView
android:id="#+id/infoLocality"
style="?android:textAppearanceSmall"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#80ffffff"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="convoy"
></TextView>
<TextView
android:id="#+id/infoEmail"
style="?android:textAppearanceSmall"
android:textColor="#80ffffff"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="fowfow#email.com"
></TextView>
<TextView
android:id="#+id/infoNumber"
style="?android:textAppearanceSmall"
android:textColor="#80ffffff"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="(086) 123 4567"
></TextView>
</LinearLayout>
I would go about it something like this.
Let's say this is the layout you're going to inflate in your gridview and it's named view_flipper_layout. Of course you'd have to add all your textview components etc. to either the front or back linearlayout.
<?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" >
<ViewFlipper
android:id="#+id/my_view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:id="#+id/back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ViewFlipper>
</LinearLayout>
Now let's say this is the adapter
public GridAdapter(Activity act, int row, List<ContactBean> items) {
super(act, row, items);
this.activity = act;
this.row = row;
this.items = items;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//here is where you inflate your layout containing your viewflipper
view = inflater.inflate(R.layout.view_flipper_layout, null);
} else {
holder = (ViewHolder) view.getTag();
}
//reference the viewFlipper
ViewFlipper flipper = (viewFlipper) holder.findViewById(R.id.my_view_flipper);
//your front layout should be set to displayed be default
//now you can get get references to your textview or ImageViews contained within the layout
TextView name = (TextView) holder.findViewById(R.id.name);
name.setText("your text");
ImageView picture = (ImageView) holder.findViewById(R.id.picture);
//now you set your onclick and pass it the current viewflipper to control the displayed child
flipper.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View click) {
flipViewFlipper(flipper);
}
});
return view;
}
private void flipViewFlipper(ViewFlipper flipper){
if(flipper.getDisplayedChild() == 0){
flipper.setDisplayedChild(1);
}
else{
flipper.setDisplayeChild(0);
}
}
}
I'm typing this from my head, so just use it as a guide if you attempt to go this way.
I have followed this link to learn about implementing custom spinner. My requirement is just to get drop down menu on clicking a image, it should be like a drop down menu where in i load contents to the list from backend.
But i thought that sample example posted in the above link would work. but its not giving any list on clicking a image icon.
Here is the full source code.
package com.example.testdroid;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsSpinner;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
public class MainActivity extends SherlockActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createCustomActionBar();
}
// #Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
// return true;
// }
private void createCustomActionBar()
{
List<String> links = new ArrayList<String>();
links.add("Abc");
links.add("Def");
links.add("Ghi");
LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.externallink, links);
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
spinner.setAdapter(linkAdapter);
ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
private static class LinksAdapter extends ArrayAdapter<String> {
private List<String> strings;
private Context context;
private LinksAdapter(Context mainActivity, int textViewResourceId, List<String> objects) {
super(mainActivity, textViewResourceId, objects);
this.strings = objects;
this.context = mainActivity;
}
#Override
public int getCount() {
if (strings == null) return 0;
return strings.size();
}
#Override
public String getItem(int position) {
return super.getItem(position);
}
// return views of drop down items
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final String siteLink = strings.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// at 0 position show only icon
TextView site = (TextView) inflater.inflate(R.layout.externallink, null);
site.setText(siteLink);
site.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
//context.startActivity(i);
}
});
return site;
}
// return header view of drop down
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.custom_show_action_bar, null);
}
}
}
Here is my XML file, i.e layout xml file.
custom_show_action_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
>
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="20dp"
android:layout_gravity="center"
/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:paddingLeft="10dp"
android:layout_gravity="center"
android:background="#ffffff"
android:id="#+id/refresh"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:background="#ffffff"
android:layout_gravity="center"
android:id="#+id/settings"/>
</LinearLayout>
External_links.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Guide me where i'm wrong.
On home scree of application, how to display the menu which is similar to android menu but no items needs to be displayed in specific cells. Considering grid of 3 x 3, five items only needs to be displayed at (Row, Col): [0,1], [1,0], [1,1], [1,2], [2,1].
We have tried GridView and set visibility to GONE (convertView.setVisibility(View.GONE);) for items which need not be displayed. Following this, item is not displayed in grid but when user browses through blank item using up and down keys or click directly on blank item, that icon is hihglighted and selected as if it is blank item in grid. We want as it is blank it should not repond to user events neither highlighted not selected.
Code for Grid View:
package org.XXX;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class XXXActivity extends Activity {
GridView MyGrid;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.maingrid);
MyGrid = (GridView)findViewById(R.id.MyGrid);
MyGrid.setAdapter(new ImageAdapter(this));
MyGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(arg0.getContext(), position + " selected", Toast.LENGTH_LONG).show();
switch(position) {
case 0:break;
case 1:
//Browse
Intent newIntent = new Intent(XXXActivity.this, YYYListItemIcons.class);
startActivity(newIntent);
break;
case 2:break;
case 3:
//Saved Searches
newIntent = new Intent(XXXActivity.this, ZZZListItemIcons.class);
startActivity(newIntent);
break;
case 4:
//Sign in
break;
case 5:
//Reminders
break;
case 6:break;
case 7:
//Sign up
break;
case 8:break;
}
}
});
//onSearchRequested(); //to open search by default
}
public class ImageAdapter extends BaseAdapter
{
Context MyContext;
public ImageAdapter(Context _MyContext)
{
MyContext = _MyContext;
}
#Override
public int getCount()
{
return 9;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater mInflater = LayoutInflater.from(MyContext);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.grid_item_text);
holder.icon = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(getTextId(position));
holder.icon.setImageBitmap(BitmapFactory.decodeResource(MyContext.getResources(), getIconId(position)));
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
}
return convertView;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
private int getIconId(int position) {
int iconImages[] = {
R.drawable.nothing,
R.drawable.browse,
R.drawable.nothing,
R.drawable.saved_searches,
R.drawable.sign_in,
R.drawable.reminders,
R.drawable.nothing,
R.drawable.sign_up,
R.drawable.nothing
};
return iconImages[position];
}
private int getTextId(int position) {
int iconNames[] = {
R.string.nothing,
R.string.browse,
R.string.nothing,
R.string.saved_searches,
R.string.sign_in,
R.string.reminders,
R.string.nothing,
R.string.sign_up,
R.string.nothing
};
return iconNames[position];
}
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
GridLayout:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="20dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
PerItemIconLayout in Grid:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#FFFFFF">
</TextView>
</LinearLayout>
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
}
replace the above lines by below and try....
if(getIconId(position) == R.drawable.nothing) {
convertView.setVisibility(View.GONE);
convertView.setClickable(false);
convertView.setEnabled(false);
}
try this code in getview().
Now that you have posted your code, I am not sure if you can technically remove but you can disable the "highlighted" click that you are talking about, this way, when a user clicks on the one of the icons, it will no longer highlight.
This can be done via XML or in your code:
https://stackoverflow.com/questions/2865683/android-disable-highlighting-in-gridview
Code: GridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
XML: android:listSelector="#00000000"
However, this will affect all the icons in your gridview.
Also take a look at this:
https://stackoverflow.com/questions/5514629/how-to-disable-item-click-for-particular-positions-in-grid-view-in-android