how to made grid layout using cardView? - java

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);
}
});

Related

Grid Item button not working

I have a GridView and an adapter called MyAdapter. My button in each grid is not showing a dialog box as it should do (MyAdapter.java). Here is my code, I am not sure what I am missing here:
MyAdapter.java
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Players> playerList;
private static LayoutInflater inflater = null;
Button btnRoleReveal;
public MyAdapter(Context context, ArrayList<Players> playerList){
this.context = context;
this.playerList = playerList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return playerList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(layout_grid_item, null);
final Button button = (Button) convertView.findViewById(R.id.btnRoleReveal);
TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
final TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(context)
.setTitle("Test")
.setMessage("Test");
}
});
Players p = new Players();
p = playerList.get(position);
idTextView.setText("ID: " + String.valueOf(p.getId()));
nameTextView.setText("Name: " + String.valueOf(p.getName()));
roleTextView.setText("Role: " + String.valueOf(p.getRole()));
return convertView;
}
}
RolesFragment.java
public class RolesFragment extends Fragment implements View.OnClickListener {
GridView gridView;
ArrayList<Players> playersList;
MyAdapter adapter;
Button role;
public RolesFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewroles, container, false);
gridView = (GridView) view.findViewById(R.id.gv_players);
role = (Button) view.findViewById(R.id.btnRoleReveal);
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
playersList = new ArrayList<Players>();
playersList = databaseHelper.getPlayers();
adapter = new MyAdapter(getActivity(), playersList);
gridView.setAdapter(adapter);
return view;
}
#Override
public void onClick(View v) {
}
}
You're missing the show() call on the dialog:
new AlertDialog.Builder(context)
.setTitle("Test")
.setMessage("Test")
.show();

Java Android Fragment ListView

I Have a fragment in which I have a list view this is my layout :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:drawSelectorOnTop="false"
android:visibility="visible" />
</FrameLayout>
Next I create a fragment (activity) :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_fragment_all_object, container,false);
listView = (ListView) view.findViewById(R.id.list);
Integer iloscObiektow = Singleton.getInstance().getListaODE().size();
int i, num_squares = iloscObiektow;
RowBean RowBean_data[] = new RowBean[num_squares];
for (i = 0; i < RowBean_data.length; i++) {
RowBean_data[i] = new RowBean(Singleton.getInstance().getListaODE().get(i).visible, Singleton.getInstance().getListaODE().get(i).name);
}
AllObjectAdapter a = new AllObjectAdapter(getActivity(),RowBean_data);
listView.setAdapter(a);
return view;
}
And this is my AllObjectAdapter :
public class AllObjectAdapter extends BaseAdapter {
private LayoutInflater inflater;
private RowBean data[];
public AllObjectAdapter(Context context, RowBean[] data) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.data = data;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
RowBean rowBean = data[position];
if(view == null){
holder = new ViewHolder();
view = inflater.inflate(R.layout.all_object_holder, null);
holder.name = (TextView) view.findViewById(R.id.showText);
holder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
}else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(rowBean.getTitle());
return null;
}
public class ViewHolder {
CheckBox checkBox;
TextView name;
}
}
I have a fragment and an activity . I want to display name of tags(fragments) but I don't want to show name of application in actionBar : this is my activity : When I remove a ActionBarActivity I can not use a FragmentManager
public class ObjectListActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener {
private ViewPager viewPager;
private android.support.v7.app.ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_list2);
// View pager for showing many fragments over a single activity
viewPager = (ViewPager) findViewById(R.id.pager);
// Getting fragment manager to control fragments
FragmentManager fragmnetManager = getSupportFragmentManager();
// Setting adapter over view pager
viewPager.setAdapter(new MyAdapter(fragmnetManager));
// Implementing view pager pagechangelistener to navigate between tabs
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
// Setting navigation of tabs to actionbar
actionBar.setSelectedNavigationItem(pos);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
// Getting actionbar
actionBar = getSupportActionBar();
// Setting navigation mode to actionbar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Now adding a new tab to action bar and setting title, icon and
// implementing listener
android.support.v7.app.ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Wszytskie");
// tab1.setIcon(R.drawable.ic_launcher);
tab1.setTabListener(this);
android.support.v7.app.ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("Grupy");
tab2.setTabListener(this);
// Now finally adding all tabs to actionbar
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
#Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
FragmentTransaction arg1) {
}
#Override
public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
FragmentTransaction arg1) {
// Setting current position of tab to view pager
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
FragmentTransaction arg1) {
}
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
// Getting fragments according to selected position
Fragment fragment = null;
if (i == 0) {
fragment = new FragmentAllObjectActivity();
}
if (i == 1) {
fragment = new FragmentGroupObjectsActivity();
}
// and finally returning fragments
return fragment;
}
#Override
public int getCount() {
// Returning no. of counts of fragments
return 2;
}
}
your are returning null in your getView() method of the adapter
Change it to return the view object
#Override
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
RowBean rowBean = data[position];
if(view == null){
holder = new ViewHolder();
view = inflater.inflate(R.layout.all_object_holder, null);
holder.name = (TextView) view.findViewById(R.id.showText);
holder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
}else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(rowBean.getTitle());
return view; //change here
}
To remove actionbar use:
// Getting actionbar
actionBar = getSupportActionBar();
actionBar.hide(); //add this

Android GridView dynamic app themes

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
};
}

Pass object from GridView of Fragment to ListView of another Fragment

I have a GridView in FragmentA containing all objects. When the user tap on each object, these objects are added into ListView of FragmentB.
Note that FragmentA and FragmentB are both in the same Activity, side by side.
I have tried using Bundle to send object from FragmentA to FragmentB but I was stucked as I have no idea how FragmentB is going to know that a new object has been added.
Bundle object is sent from ProductGridAdapter (under FragmentA) and receiver will be FragmentB where FragmentB will update the ListView for the latest objects added by FragmentA
Here is my code:
ProductGridAdapter.java
public class ProductGridAdapter extends ArrayAdapter<Product> {
private List<Product> productList;
private Context context;
public ProductGridAdapter(Context context, int resource,
List<Product> objects) {
super(context, resource, objects);
this.context = context;
this.productList = objects;
}
#Override
public int getCount() {
return ((null != productList) ?
productList.size() : 0);
}
#Override
public Product getItem(int position) {
return ((null != productList) ?
productList.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(null == view) {
view = layoutInflater.inflate(R.layout.grid_product, null);
}
final Product product = productList.get(position);
if(product != null) {
final CardView productGridLayout = (CardView) view.findViewById(R.id.product_gridlayout);
final TextView productName = (TextView) view.findViewById(R.id.product_name);
final ImageView productIcon = (ImageView) view.findViewById(R.id.product_icon);
productName.setText(product.getName());
productIcon.setImageDrawable(product.getDrawable());
productGridLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
bundle.putSerializable("product", product);
fragment.setArguments(bundle);
}
});
}
return view;
}
}
FragmentA.java
public class FragmentA extends Fragment {
private GridView gridView;
private ProductGridAdapter productAdapter;
public FragmentA() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_product_display, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridview);
productAdapter = new ProductGridAdapter(getActivity(), R.layout.fragment_product_display, getProductList());
gridView.setAdapter(productAdapter);
gridView.destroyDrawingCache();
gridView.setVisibility(GridView.INVISIBLE);
gridView.setVisibility(GridView.VISIBLE);
return rootView;
}
}
FragmentB.java
public class FragmentB extends Fragment {
private ListView listView;
private PaymentListAdapter paymentAdapter;
private LinearLayout button;
private ArrayList<String> arr;
public FragmentB() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_payment, container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, getProductList());
// button = (LinearLayout) rootView.findViewById(R.id.payment);
listView.setAdapter(paymentAdapter);
Bundle bundle = this.getArguments();
Product product = (Product) bundle.getSerializable("product");
return rootView;
}
you can have two common method in interface class in main activity, To update view in both fragment,When some changes happens in FragmentA you have to notify that changes to main activity then main activity should communicate to FragementB about that changes.try to do as like this,Hope this will help you.Happy Coding!

Adapters for text in android

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.

Categories

Resources