Page curl animation to flip between views of a viewflipper - android

I've come across several examples of the page curl animation as well as viewflippers . Is it possible to navigate between children of a viewflipper via a page curl animation . The animations i have applied to viewflippers till now were very basic such as a slide-in/slide-out and I was wondering if the same could be done/has already be implemented using a page-curl animation.

There is an open source android project here: http://code.google.com/p/android-page-curl/.
I found another one here: https://github.com/harism/android_page_curl/.
But there is no native implementation if that is what you are asking.

You need to import one module library for this, from HERE
After that use following code:-
item_page.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:textColor="#android:color/black" />
</LinearLayout>
</ScrollView>
activity_flipper_view_controller.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flip="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:paddingLeft="10dp"
android:text="#string/header"
android:textAppearance="#android:style/TextAppearance.Large" />
<com.aphidmobile.flip.FlipViewController
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
flip:orientation="horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:paddingLeft="10dp"
android:text="#string/footer"
android:textAppearance="#android:style/TextAppearance.Large" />
</LinearLayout>
FlipperAdapter.java
package pratiksha.com.pagecurlviewdemo;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import pratiksha.com.pagecurlviewdemo.R;
/**
* Created by User(LPT-APR2015-02) on 11/7/2016.
*/
public class FlipperAdapter extends BaseAdapter {
private AppCompatActivity appCompatActivity;
private List<String> strings;
private int[] drawableIds = {R.mipmap.ic_launcher, R.mipmap.page1, R.mipmap.page2, R.mipmap.ic_launcher,
R.mipmap.page2, R.mipmap.page1, R.mipmap.page2, R.mipmap.ic_launcher,
R.mipmap.page1};
public FlipperAdapter(AppCompatActivity appCompatActivity, List<String> strings) {
super();
this.strings = strings;
this.appCompatActivity = appCompatActivity;
}
#Override
public int getCount() {
return strings.size();
}
#Override
public String getItem(int position) {
return strings.get(position);
}
#Override
public long getItemId(int position) {
return strings.indexOf(getItem(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) appCompatActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_page, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(getItem(position));
holder.imageView.setImageResource(drawableIds[position]);
return convertView;
}
private static class ViewHolder {
private TextView textView;
private ImageView imageView;
public ViewHolder(View v) {
imageView = (ImageView)v.findViewById(R.id.image);
textView = (TextView) v.findViewById(R.id.text);
}
}
}
Main Activity.java
package pratiksha.com.pagecurlviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.aphidmobile.flip.FlipViewController;
import java.util.ArrayList;
public class FlipperViewController extends AppCompatActivity {
private FlipViewController flipViewController;
private FlipperAdapter adapter;
private ArrayList<String> stringArrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flipper_view_controller);
flipViewController = (FlipViewController)findViewById(R.id.flip_view);
stringArrayList = new ArrayList<>();
readDataFromAssets();
//create and attach adapter to flipper view
adapter = new FlipperAdapter(this, stringArrayList);
flipViewController.setAdapter(adapter);
}
private void readDataFromAssets() {
for(int i=1;i<10;i++)
stringArrayList.add("Page Number "+i);
}
}

Related

How to create a 3/2 grid using grid view with base adapter android in java

6 grid should be shown without scrolling, that is it should be like setting layout_weight="1" & layout_height="1" for every grid cards.
6 cards should be shown respective of screen size.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridView
android:id="#+id/gvImages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
app:layout_constrainedHeight="true"
app:layout_constrainedWidth="true"
app:layout_constraintVertical_weight="1"
app:layout_constraintHorizontal_weight="1"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
grid_layout.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"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
tools:srcCompat="#tools:sample/avatars" />
</LinearLayout>
MyAdapter.java
package com.aijishnu.baseadaptersample;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.aijishnu.baseadaptersample.databinding.GridLayoutBinding;
public class MyAdapter extends BaseAdapter {
Context c;
int items[];
MyAdapter(Context c, int arr[]) {
this.c = c;
items = arr;
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
GridLayoutBinding itemBinding = GridLayoutBinding.inflate(LayoutInflater.from(parent.getContext()), parent,false);
holder = new ViewHolder(itemBinding);
holder.view = itemBinding.getRoot();
holder.view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.binding.imageView.setImageResource(items[position]);
return holder.view;
}
private static class ViewHolder {
private View view;
private GridLayoutBinding binding;
ViewHolder(GridLayoutBinding binding) {
this.view = binding.getRoot();
this.binding = binding;
}
}
}
MainActivity.java
package com.aijishnu.baseadaptersample;
import android.os.Bundle;
import com.aijishnu.baseadaptersample.databinding.ActivityMainBinding;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
int[] itemsarray = new int[] {
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground,
};
MyAdapter adapter = new MyAdapter(this, itemsarray);
binding.gvImages.setAdapter(adapter);
}
}
Above code results as like this, (This is not fit to screen height, also have scrolling option, I want 6 cards should fit to screen size)
I want to have like this(all cards equals according to screen size)
NB: These cards should be randomly generated using BaseAdapter or RecyclerAdapter
You can set the number of rows and columns of your GridLayout inside your xml file

Not being able to place an onClickListener on the Grid Items provided by a custom BaseAdapter

I currently have a grid view using a base adapter inside a fragment and I am trying to transfer to a different fragment when one of the items is clicked but none of the solutions I found on stack overflow has worked. I might miss something.
Adapter
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;
import com.licenta.joberfrontend.R;
import com.licenta.joberfrontend.rest.backend_entieties.Category;
import java.util.ArrayList;
import java.util.List;
public class CategoriesAdapter extends BaseAdapter {
public class ViewHolder {
TextView textName;
ImageView imageView;
}
private ArrayList<Category> categoryList;
public Context context;
public CategoriesAdapter(List<Category> apps, Context context) {
this.context = context;
this.categoryList = (ArrayList<Category>) apps;
}
#Override
public int getCount() {
return categoryList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View view, ViewGroup parent) // inflating the layout and initializing widgets
{
ViewHolder viewHolder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.category_list_content, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = view.findViewById(R.id.textName);
viewHolder.imageView = view.findViewById(R.id.iconView);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
// here we are setting up the names and images
viewHolder.textName.setText(categoryList.get(position).getName());
viewHolder.imageView.setImageResource(this.context.getResources().getIdentifier(categoryList.get(position).getCategoryIconId(), "mipmap", this.context.getPackageName()));
return view;
}
}
Fragment's OnCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LocalStorageSaver localStorageSaver = new LocalStorageSaver(Objects.requireNonNull(getContext()));
final ToastShower toastShower = new ToastShower();
//REST services creation
final RetrofitCreator retrofitCreator = new RetrofitCreator();
final Retrofit retrofit = retrofitCreator.getRetrofit();
final CategoryService categoryService = retrofit.create(CategoryService.class);
final Call<List<Category>> getCategoriesRequest = categoryService.getAllCategoriesAndTheirJobs(localStorageSaver.getValueFromStorage(Constants.TOKEN));
getCategoriesRequest.enqueue(
new Callback<List<Category>>() {
#Override
public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
toastShower.showToast("Categories succesfully retrieved from backend.", getContext());
final GridView gridView = Objects.requireNonNull(getView()).findViewById(R.id.gridViewNewContract);
gridView.setAdapter(new CategoriesAdapter(response.body(), getActivity()));
}
#Override
public void onFailure(Call<List<Category>> call, Throwable t) {
toastShower.showToast("There has been a problem with retrieving the categories data!", getContext());
}
}
);
}
Items inside the grid view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="15dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iconView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_list"
android:tint="#color/colorAccent" />
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="sans-serif"
android:maxLength="12"
android:text="#string/appName"
android:textColor="#color/colorAccent"
android:textSize="13sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
If you need any more information I will gladly provided.
I can mention that I've tried placing listeners both in the adapter and in the fragment directly on the grid.
please try this
viewHolder.imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});

How to make multiple layout for one screen?

I want to make Screen in android with multiple layouts. Fox example On the top of the screen there is the header with image slider (this part is done) below that there will be a list view and below that there will be grid view.
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.root.multipleview.MainActivity">
<!--Header image-->
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoStart="true"></android.support.v4.view.ViewPager>
<!--ListView Below header -->
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:height="10dp" />
</RelativeLayout>
CustomLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#f1f3f7"
android:orientation="vertical">
<!--Header image with slider-->
<ImageView
android:id="#+id/headerimg"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:src="#mipmap/ic_launcher" />
<!--ListView Below Header-->
<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_marginTop="20dp"
android:paddingLeft="2dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="0.05"
app:srcCompat="#mipmap/ic_launcher"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"/>
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="11dp"
android:layout_toEndOf="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:text="TextView" />
<TextView
android:id="#+id/textdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginBottom="20dp"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/textName"
android:layout_alignStart="#+id/textName"/>
</RelativeLayout>
</LinearLayout>
ListView.java
package com.example.root.multipleview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by root on 11/18/2017.
*/
public class ListView extends AppCompatActivity {
int[] Images = {R.drawable.salad,R.drawable.salami,R.drawable.flour,R.drawable.salt,
R.drawable.sandwich,R.drawable.sausage,R.drawable.seeds,R.drawable.cheese,R.drawable.shrimp,R.drawable.cupcake,R.drawable.cup,R.drawable.spices,R.drawable.cabbage
,R.drawable.spoon,R.drawable.apple,R.drawable.asparagus,R.drawable.cupcake,R.drawable.fish,R.drawable.corn,R.drawable.cupcake,R.drawable.spatula,R.drawable.olives
,R.drawable.garlic,R.drawable.taco,R.drawable.broccoli,R.drawable.cabbage};
String[] Names = {"a","B","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] Description = {"VVV","DDD","ddd","dddsa","ddsdsds","zsxx","wwwwq","ddwqK","EQWK","FgggFFF","FFFkklF","FghhFFF","FFhhFF","FFhhFF",
"FFmmmFF","FgggFFF","FFFFbbb","FFFFfeee","gfffFFFF","FFFFfff","FFFgffF","FFFFfgffg","FFFfgfgfF","FFFgffF","FFFgfggdF","FFFFdssa"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.widget.ListView listView= (android.widget.ListView) findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
}
public class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return Images.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) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
TextView textView_N = (TextView)view.findViewById(R.id.textName);
TextView textView_D = (TextView)view.findViewById(R.id.textdesc);
imageView.setImageResource(Images[i]);
textView_N.setText(Names[i]);
textView_D.setText(Description[i]);
return view;
}
}
}
MainActivity.java
package com.example.root.multipleview;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.viewPager);
ViewPageAdapter viewPageAdapter = new ViewPageAdapter( this);
viewPager.setAdapter(viewPageAdapter);
}
}
ViewPageAdapter
package com.example.root.multipleview;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* Created by root on 11/18/2017.
*/
public class ViewPageAdapter extends PagerAdapter{
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.b,R.drawable.c,R.drawable.e,R.drawable.j,R.drawable.r,R.drawable.x,R.drawable.y};
public ViewPageAdapter(Context context){this.context = context;}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate((R.layout.custom_layout), null);
ImageView imageView = (ImageView) view.findViewById(R.id.headerimg);
imageView.setImageResource((images[position]));
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
When I check this all layout separately it works but after adding in one layout it's not working. output screenshot is added
enter image description here
How to make multiple layout for one screen?
my approach for this goal is using Fragments
how?
i do make a single main_layout and set a FrameLayout in order to put fragments with different layouts.so i have a screen with different layouts.
this is my best answer for your question .

Android: How to identify which button is clicked from custom list view

I am creating a custom list view with:
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_display_list_view"
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.anandgupta.login.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/myListView" />
</RelativeLayout>
and list_row.xml containing only a TextView and a button.
<?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:background="#drawable/list_row_selector"
android:padding="8dp">
<!-- Movie Title -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/title"
android:textStyle="bold" />
<!-- Buy Ticket -->
<Button
android:id="#+id/buyTicket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Buy"
android:onClick="buyTicket" //Button calling buyTicket...Not working
/>
and MainActivity.java:
package com.anandgupta.login;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
private List<Movie> movieList = new ArrayList<Movie>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assume that MovieAdapter and Movie class is already created and its working fine.
MovieAdapter movieAdapter = new MovieAdapter(this,movieList);
listView = (ListView)findViewById(R.id.myListView);
listView.setAdapter(movieAdapter);
}
public void buyTicket() //Not working
{
Toast.makeText(MainActivity.this,"Ticket Booked",Toast.LENGTH_LONG).show();
}
}
and MovieAdapter.java:
package com.anandgupta.login;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MovieAdapter extends BaseAdapter {
private List<Movie> movieList;
private Activity activity;
private LayoutInflater layoutInflater;
public MovieAdapter(Activity activity, List<Movie> movieList) {
this.movieList = movieList;
this.activity = activity;
}
#Override
public Object getItem(int position) {
return movieList.get(position);
}
#Override
public int getCount() {
return movieList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(layoutInflater==null)
layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
convertView = layoutInflater.inflate(R.layout.list_row,null);
TextView title = (TextView)convertView.findViewById(R.id.title);
Movie m = movieList.get(position);
title.setText(m.getMovieTitle());
return convertView;
}
}
and Movie.java:
package com.anandgupta.login;
public class Movie {
private String title;
Movie()
{
}
public void setMovieTitle(String title)
{
this.title = title;
}
public void getMovieTitle(String title)
{
return title;
}
}
I am able to see the custom list but while trying to call the function buyTicket with the click of button my app get closed.
Any help will be appreciated.
Edit: The problem was that the method is not passed a view:
public void buyTicket(View view) { ....... }
This solve the above problem but now how to uniquely know which button from the list is clicked.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(layoutInflater==null)
layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
convertView = layoutInflater.inflate(R.layout.list_row,null);
TextView title = (TextView)convertView.findViewById(R.id.title);
Button buyTicketButton = (Button)convertView.findViewById(R.id.buyTicket);
Movie m = movieList.get(position);
title.setText(m.getMovieTitle());
buyTicketButton.setOnClickListener(new OnClickListener(){
//..... your code goes here
});
return convertView;
}
You can craete an Button Click event in the Adapter's getView() method.
Button btnBookTicket = (Button)convertView.findViewById(R.id.buyTicket);
btnBookTicket.setOnClickListener(new View.OnClickListener()
{
((MainACtivity)activity).buyMovieTicket();
});

cant get sections to work in contact app/listview

I've been through every stack discussion on this I could find, along with about a dozen tutorials. I'm just not getting it. I'm using 'getItemViewType' to determine which layout I should use. Here's where I run into a problem (and maybe the way I'm getting the position is the root issue, not sure):
What I'm doing is getting the first character of the contact's name at position x. If it's different than the first character in position x-1, I know that it's the next letter in the list and it needs a section header, which would be inserted ABOVE the current list item. How do I get my adapter to add a new layout in position x-1?
This is my adapter code. I've stripped the code which was causing the app to force close, which leaves me with just the 1 layout. I can't figure out how to insert the 'section' layout at position x-1. Below the adapter code I threw in the xml for my 2 layouts. Let me know if you need anything else. Thanks in advance.
import java.util.ArrayList;
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 ContactNameAdapter extends BaseAdapter {
public static final int CONTACT_NAME = 0;
public static final int ALPHA_HEADER = 1;
private static final int NUMBER_OF_LAYOUTS = 2;
Context context;
private ArrayList<ListItemDetails> sItemDetailsArrayList;
public ContactNameAdapter(ArrayList<ListItemDetails> data, Context context) {
sItemDetailsArrayList = data;
this.context = context;
}
#Override
public int getCount() {
return sItemDetailsArrayList.size();
}
#Override
public ListItemDetails getItem(int position) {
return sItemDetailsArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return NUMBER_OF_LAYOUTS;
}
#Override
public int getItemViewType(int position) {
if (position != 0) {
if (getItem(position).getName().toUpperCase().charAt(0) == getItem(
position - 1).getName().toUpperCase().charAt(0)) {
return CONTACT_NAME;
} else {
return ALPHA_HEADER;
}
} else {
return ALPHA_HEADER;
}
}
#Override
public View getView(int position, View view, ViewGroup parent) {
ImageView mImageView;
TextView mTextView;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.contactlistlayout, null);
mImageView = (ImageView) view.findViewById(R.id.ivContactPhoto);
mTextView = (TextView) view.findViewById(R.id.textView1);
view.setTag(new ViewHolder(mImageView, mTextView));
} else {
ViewHolder viewHolder = (ViewHolder) view.getTag();
mImageView = viewHolder.mImageView;
mTextView = viewHolder.mTextView;
}
ListItemDetails listItemDetails = getItem(position);
mTextView.setText(listItemDetails.getName());
mImageView.setImageBitmap(listItemDetails.getImage());
if (listItemDetails.getImage() == null) {
mImageView.setImageResource(R.raw.default_contact);
}
return view;
}
private static class ViewHolder {
public final TextView mTextView;
public final ImageView mImageView;
public ViewHolder(ImageView mImageView, TextView mTextView) {
this.mImageView = mImageView;
this.mTextView = mTextView;
}
}
}
list layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contactView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingLeft="2dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/ivContactPhoto"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
section layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<TextView
android:id="#+id/tvAlphaHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingBottom="10dp"
android:paddingLeft="2dp"
android:paddingTop="10dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="#+id/ivSectionLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/holo_blue_light"
android:focusable="true" />
</LinearLayout>
I've looked at a bunch of tutorials that ended up confusing me more than helping me, but I found this one to be great. If you've been around the world on this like I have, take a look at this tutorial :)
http://codelikes.blogspot.com/2012/04/android-alphabet-listview-like-contacts.html?zx=d40863f078ab91b4

Categories

Resources