Am trying to make a ui with flipview as in this tutorial. IThe tutorial deals with activities, but i want it in fragments case. That ie, the flip effect as well as its parent is a fragment. When i use activity it works, but on using fragment,only the empty textview shows up. Can anyone help me with this?
This is some part of the code
page.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff3333" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/banner"
android:id="#+id/head"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:id="#+id/header"
android:gravity="center"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/des"
android:textSize="30dp"
android:gravity="center"/>
</FrameLayout>
fragment_news_feed :
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</merge>
NewsFeedFragment.java
public class NewsFeedFragment extends Fragment implements NewsFlipAdapter.Callback, FlipView.OnFlipListener, FlipView.OnOverFlipListener{
private FlipView mFlipView;
private NewsFlipAdapter mAdapter;
int i=0,no_of_items=7;
public NewsFeedFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout wrapper = new LinearLayout(getActivity());
View view = inflater.inflate(R.layout.fragment_news_feed, wrapper, true);
setUpView(view);
return wrapper;
}
private void setUpView(View view) {
mFlipView = (FlipView) view.findViewById(R.id.flip_view);
mAdapter = new NewsFlipAdapter(getActivity());
mAdapter.setCallback(this);
mFlipView.setAdapter(mAdapter);
mFlipView.setOnFlipListener(this);
mFlipView.peakNext(false);
mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
mFlipView.setOnOverFlipListener(this);
}
#Override
public void onPageRequested(int page) {
mFlipView.smoothFlipTo(page);
}
#Override
public void onFlippedToPage(FlipView v, int position, long id) {
Log.i("pageflip", "Page: " + position);
if(position > mFlipView.getPageCount()-3 && mFlipView.getPageCount()<30){
mAdapter.addItems(5);
}
}
#Override
public void onOverFlip(FlipView v, OverFlipMode mode,
boolean overFlippingPrevious, float overFlipDistance,
float flipDistancePerPage) {
Log.i("overflip", "overFlipDistance = "+overFlipDistance);
}
NewsFlipAdapter.java
public class NewsFlipAdapter extends BaseAdapter {
public interface Callback {
public void onPageRequested(int page);
}
static class Item {
static long id = 0;
long mId;
public Item() {
mId = id++;
}
long getId() {
return mId;
}
}
private LayoutInflater inflater;
private Callback callback;
private List<Item> items = new ArrayList<Item>();
public NewsFlipAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public void setCallback(Callback callback) {
this.callback = callback;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return items.get(position).getId();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.page, parent, false);
holder.heading = (TextView) convertView.findViewById(R.id.header);
holder.description = (TextView) convertView.findViewById(R.id.des);
holder.header = (ImageView) convertView.findViewById(R.id.head);
// holder.firstPage = (Button) convertView.findViewById(R.id.first_page);
// holder.lastPage = (Button) convertView.findViewById(R.id.last_page);
// holder.firstPage.setOnClickListener(this);
// holder.lastPage.setOnClickListener(this);
// convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView heading;
ImageView header;
TextView description;
}
/* #Override
public void onClick(View v) {
switch(v.getId()){
case R.id.first_page:
if(callback != null){
callback.onPageRequested(0);
}
break;
case R.id.last_page:
if(callback != null){
callback.onPageRequested(getCount()-1);
}
break;
}
}*/
public void addItems(int amount) {
TextView text;
for (int i = 0; i < amount; i++) {
items.add(new Item());
}
notifyDataSetChanged();
}
public void addItemsBefore(int amount) {
for (int i = 0; i < amount; i++) {
items.add(0, new Item());
}
notifyDataSetChanged();
}
I am not a 100 % sure about the following explanation, so please correct me if I am wrong.
The <merge> tag merges the inflated layout with it's parent view.
It generally doesn't seem to sit well with fragments as explained in more detail here.
So try to replace merge with LinearLayout in your fragment_newsfeed_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:flipview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<se.emilsjolander.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
flipview:orientation="vertical"
tools:context="com.excel.excelapp.fragment.NewsFeedFragment" >
</se.emilsjolander.flipview.FlipView>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Empty!"
android:textSize="32sp"
android:visibility="gone" />
</LinearLayout>
Related
In my Activity I have an EditText, a Button and a GridView. The user selects the image from the GridView and enters the name in the EditText, and then clicks the done button.
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/l1"
>
<View
android:layout_width="#dimen/btm_sht_line"
android:layout_height="#dimen/btm_sht_height"
android:layout_marginTop="#dimen/btm_top"
android:layout_gravity="center_horizontal"
android:background="#color/colorPrimaryDark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/header_create_a_new_list"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/l2"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/marin_top"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listname"
android:hint="#string/list_title_name"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="#dimen/marin_top"
android:layout_marginStart="#dimen/marin_top"
android:inputType="text"
android:autofillHints="#string/list_title_name" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/done"
android:src="#drawable/ic_check_circle"
android:layout_marginRight="15dp"
android:layout_marginEnd="#dimen/marin_top"
android:layout_marginLeft="#dimen/margin_left"
android:layout_marginStart="#dimen/margin_left"
tools:ignore="ContentDescription"
/> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/l3"
android:orientation="vertical"
>
<View style="#style/Divider"
android:layout_marginTop="#dimen/marin_top"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r1"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll"
><GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridview"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</ScrollView>
</RelativeLayout>
</LinearLayout>
I want to move the selected image and the entered text to another Activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CheckslateHome">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/ima"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/getlistname"
android:text="hi welcome"
android:layout_below="#+id/ima"
/>
</RelativeLayout>
In my code when the user selects the image, before entering the name, it loads to a new empty window. Also I have 6 images in my drawable Folder and only 3 images are being displayed in the GridView.
Java class
public class NewListCreate extends BottomSheetDialogFragment {
Integer[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
GridView gridView;
ArrayList<imageModel> arrayList;
public NewListCreate() {}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.new_list_create, container, false);
ImageButton done = view.findViewById(R.id.done);
final EditText listname = (EditText) view.findViewById(R.id.listname);
final GridView gridView = (GridView) view.findViewById(R.id.gridview);
arrayList = new ArrayList<imageModel>();
for (int i = 0; i < images.length; i++) {
imageModel imagemodel = new imageModel();
imagemodel.setmThumbIds(images[i]);
//add in array list
arrayList.add(imagemodel);
}
final ImageAdapterGridView adapterGridView = new ImageAdapterGridView(getContext(), arrayList);
gridView.setAdapter(adapterGridView);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapterGridView.setSelectedPosition(i);
adapterGridView.notifyDataSetChanged();
int imageRes = images[i];
Intent intent = new Intent(getContext(),CheckslateHome.class);
intent.putExtra("IMAGE_RES", imageRes);
startActivity(intent);
}
});
return view;
}
}
AdapterClass
public class ImageAdapterGridView extends BaseAdapter {
private int selectedPosition = -1;
Context context;
ArrayList<imageModel> arrayList;
public ImageAdapterGridView(Context context, ArrayList<imageModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int i) {
return arrayList.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view==null)
{
view = LayoutInflater.from(context).inflate(R.layout.image_list, viewGroup, false);
}
ImageView imageView;
imageView = (ImageView) view.findViewById(R.id.image);
imageView.setImageResource(arrayList.get(i).getmThumbIds());
if (i == selectedPosition) {
view.setBackgroundColor(Color.WHITE);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
return view;
}
public void setSelectedPosition(int position) {
selectedPosition = position;
}
}
what i was Looking was Something Like this
[![enter image description here][1]][1]
Copy and paste this hope it solve ur Problem
public class NewListCreate extends BottomSheetDialogFragment {
int[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
int imageRes = images[0];
public NewListCreate() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_list_create, container, false);
ImageButton done = view.findViewById(R.id.done);
final EditText listname = (EditText) view.findViewById(R.id.listname);
final GridView gridView = (GridView) view.findViewById(R.id.gridview);
final CustomAdpter customAdpter = new CustomAdpter(images,getContext());
gridView.setAdapter(customAdpter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
customAdpter.selectedImage = i;
customAdpter.notifyDataSetChanged();
imageRes = images[i];
}
});
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String itemname = listname.getText().toString();
if (!TextUtils.isEmpty(listname.getText().toString())) {
startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image",imageRes));
dismiss();
} else {
Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
public class CustomAdpter extends BaseAdapter{
private int[] icons;
private Context context;
private LayoutInflater layoutInflater;
public int selectedImage = 0;
public CustomAdpter(int[] icons, Context context) {
this.icons = icons;
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return icons.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null)
{
view = layoutInflater .inflate(R.layout.image_list,viewGroup,false);
}
ImageView imageicons = view.findViewById(R.id.image);
if (i < icons.length) {
imageicons.setImageResource(icons[i]);
if (i != selectedImage) {
imageicons.setImageAlpha(50);
}
imageicons.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageicons.setLayoutParams(new GridView.LayoutParams(150, 150));
if (i == selectedImage) {
view.setBackgroundColor(Color.WHITE);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
};
return view;
}
}
I think what you're looking for is passing data through navigation.
It will allow you send your image and name data from one activity/fragment to the next one.
Check out data-binding as well, might be useful.
I have 2 recycler views and a view pager in a fragment.
Everything else works fine but both recycler view items are showing a random mark on the upper left corner of every items. And same thing is happening for the view pager items.
This fragment is in a fragment activity and view pager is using pager adapter. And I am using Picasso library to load images on the items and also using network policy to cache the images in disk.
recycler view items appearing like this
view pager items appearing like this
Recycler View adapter
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseListViewHolder> {
public static final int HOME_PAGE = 1;
public static final int DISPLAY_COURSE = 2;
private Picasso mPicasso;
private List<DisplayCourse> courseList;
private static final String TAG = "CourseListAdapter";
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position, View view);
}
private String mSender;
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public CourseListAdapter(List<DisplayCourse> courseList, String sender) {
this.courseList = courseList;
this.mSender = sender;
mPicasso = Picasso.get();
}
#NonNull
#Override
public CourseListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case 1:
View view1 = inflater.inflate(R.layout.recom_course_home_layout, parent, false);
return new CourseListViewHolder(view1, mListener);
case 2:
View view2 = inflater.inflate(R.layout.display_course_layout, parent, false);
return new CourseListViewHolder(view2, mListener);
default:
View view3 = inflater.inflate(R.layout.section_video_item_layout, parent, false);
return new CourseListViewHolder(view3, mListener);
}
}
#Override
public void onBindViewHolder(#NonNull CourseListViewHolder holder, int position) {
holder.title.setText(courseList.get(position).getCourseTitle());
mPicasso.load(courseList.get(position).getThumbnailURL())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
mPicasso.load(courseList.get(position).getThumbnailURL())
.error(R.drawable.ofklogo)
.into(holder.thumbnail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
}
#Override
public int getItemCount() {
return courseList.size();
}
public static class CourseListViewHolder extends RecyclerView.ViewHolder {
ImageView thumbnail;
TextView title;
public CourseListViewHolder(#NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
title = itemView.findViewById(R.id.courseTitle);
thumbnail = itemView.findViewById(R.id.courseThumbNailImageView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null) {
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position, view);
}
}
}
});
}
}
#Override
public int getItemViewType(int position) {
if (mSender.equals("home_page")) {
return HOME_PAGE;
} else if (mSender.equals("displayCourse")) {
return DISPLAY_COURSE;
}
return -1;
}
}
View pager adapter
public class VideoSliderAdapter extends PagerAdapter {
private boolean doNotifyDataSetChangedOnce = false;
private static final String TAG = "VideoSliderAdapter";
private YouTubePlayerView youTubePlayerView;
private View gradientView;
private ImageView thumbNail;
private LinearLayout layout;
private Picasso picasso;
private List<Video> videoList;
private Context mContext;
private Lifecycle mLifeCycle;
public VideoSliderAdapter(List<Video> videoList, Context context, Lifecycle lifecycle) {
this.videoList = videoList;
this.mContext = context;
this.mLifeCycle = lifecycle;
doNotifyDataSetChangedOnce = true;
picasso = Picasso.get();
}
#Override
public int getCount() {
if (doNotifyDataSetChangedOnce) {
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
}
return videoList.size();
}
#Override
public int getItemPosition(#NonNull Object object) {
return POSITION_NONE;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return (view == (CardView) object);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.video_paly_layout, container, false);
TextView title = view.findViewById(R.id.videoTitle);
title.setText(videoList.get(position).getVideoTitle());
gradientView = view.findViewById(R.id.gradientView);
thumbNail = view.findViewById(R.id.videoThumbNail);
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
picasso.load(videoList.get(position).getVideoThumbNail())
.error(R.drawable.ofklogo)
.into(thumbNail, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Exception e) {
}
});
}
});
layout = view.findViewById(R.id.videoPlayLayout);
youTubePlayerView = view.findViewById(R.id.youtube_player_view);
mLifeCycle.addObserver(youTubePlayerView);
new AddListener(youTubePlayerView, position).execute();
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((CardView) object);
}
Recycler view holder layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/courseThumbNailImageView"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/art_thumb" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/courseThumbNailImageView">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxLines="2"
android:text="Course title"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
view pager layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="200dp"
app:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="#+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:autoPlay="false" />
<ImageView
android:id="#+id/videoThumbNail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<View
android:id="#+id/gradientView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_drawable" />
<LinearLayout
android:id="#+id/videoPlayLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:padding="8dp"
android:src="#drawable/play_button" />
<TextView
android:id="#+id/videoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="Course title"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
When loading image using Picasso use: setIndicatorsEnabled(false)
picasso.load(videoList.get(position).getVideoThumbNail())
.networkPolicy(NetworkPolicy.OFFLINE)
.setIndicatorsEnabled(false)
.into(...);
The colors indicate this:
Green: Image is fetched from memory
Blue: Image is fetched from disk
Red: Image is fetched from network
I am coding a listView that gets the data from mySQL Server.
I have created the followings classes.
Class ListView. It has two contractors all in Strings. And I sat getters and setters.
I believe I have a problem with the Adapter its self. I can click on more than one option. While I have created the custom adapter in the layout as the followings:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/RG_Adapter"
android:layoutDirection="rtl"
android:orientation="horizontal">
<RadioButton
android:id="#+id/TRIP_NAME"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:textAlignment="center"
android:textSize="25sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/SUM_TRIPS"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#000000"
android:textAlignment="center"
android:textSize="25sp" />
</RadioGroup>
I believe my problem is with the Adapter class.
I have created it like the followings:
public class TRIPS_LISTVIEW_ADAPTER extends ArrayAdapter<TRIPS_LISTVIEW> {
private Context mContext;
private ArrayList<TRIPS_LISTVIEW> mData;
private MyFunctionsClass myFunctionsClass = new MyFunctionsClass();
public TRIPS_LISTVIEW_ADAPTER (Context mContext, ArrayList<TRIPS_LISTVIEW> mData) {
super(mContext, R.layout.summary_shape_layout,mData);
this.mContext = mContext;
this.mData = mData;
}
public int getCount() {
return mData.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.summary_shape_layout, null);
}
RadioGroup RG = (RadioGroup) convertView.findViewById(R.id.RG_Adapter);
TextView TRIP_NAME = (TextView) convertView.findViewById(R.id.TRIP_NAME);
TRIP_NAME.setTypeface(myFunctionsClass.FONT( TRIP_NAME.getContext().getAssets(),1));
TRIP_NAME.setText(myFunctionsClass.get_The_trip(mData.get(position).getTRIP_TITLE()));
TextView SUM_TRIPS = (TextView) convertView.findViewById(R.id.SUM_TRIPS);
SUM_TRIPS.setTypeface(myFunctionsClass.FONT( SUM_TRIPS.getContext().getAssets(),1));
SUM_TRIPS.setText(mData.get(position).getTRIP_COUNT());
return convertView;
}
}
The Data in my MainActivity Class are retrieved correctly. But as I mentioned I have multiple mode selection.
Try the following:
1) Demo2.class:-------------
public class Demo2 extends AppCompatActivity {
private ListView lv;
private CheckBox cb;
private Adapter adapter;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo2);
checkBoxState = new ArrayList<>();
checkBoxText = new ArrayList<>();
for(int i = 0 ; i<10 ; i++){
if(i == 0) {
checkBoxState.add(i, true);
}else{
checkBoxState.add(i , false);
}
checkBoxText.add( i , "C" + (i+1));
}
lv = (ListView) findViewById(R.id.lv);
adapter = new Adapter(getApplicationContext() , checkBoxState , checkBoxText);
lv.setAdapter(adapter);
}
}
2) Adapter.class:------
public class Adapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;
public Adapter(Context context, List<Boolean> checkBoxState , List<String> checkBoxText) {
this.context = context;
layoutInflater = LayoutInflater.from(context);
this.checkBoxState = checkBoxState;
this.checkBoxText = checkBoxText;
}
public int getCount() {
return checkBoxState.size();
}
public Object getItem(int position) {
return checkBoxState.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final CheckBox cb_list_item;
if (convertView == null) {
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.list_item, null);
}
}
cb_list_item = (CheckBox) view.findViewById(R.id.cb_list_item);
cb_list_item.setText(checkBoxText.get(position));
cb_list_item.setOnCheckedChangeListener(null); // mask onCheckedChangeListener()
cb_list_item.setChecked(checkBoxState.get(position));
cb_list_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b) { // already selected
compoundButton.setChecked(true);
} else { // is selected now
checkSelected(position);
}
}
});
return view;
}
private void checkSelected(int position){
try {
for (int i = 0; i < checkBoxState.size(); i++) {
checkBoxState.set(i, false);
}
checkBoxState.set(position, true);
this.notifyDataSetChanged();
}catch (Exception e){
e.printStackTrace();
}
}
}
3) demo2.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:weightSum="100"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
</ListView>
</LinearLayout>
4) list_item.xml:----------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="#+id/cb_list_item"/>
</android.support.constraint.ConstraintLayout>
5) Output:--------
There are 3 textviews and 1 button in my listview.One of textview contains time.I want button must get clicked after 20mins of intime.I want to compare Intime with current time and if difference >20 mins then button must get clicked.Following is my code-
MainActivity.java
public class MainActivity extends Activity {
TextView txtno,txtname,txtintime;
ListView listView;
ArrayList<Model> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtno=(TextView)findViewById(R.id.no);
txtname=(TextView)findViewById(R.id.name);
txtintime=(TextView)findViewById(R.id.intime);
listView = (ListView) findViewById(R.id.list);
data=new ArrayList<>();
data.add(new Model(1,"ABC","11:45:50 AM"));
data.add(new Model(2,"PQR","11:50:50 AM"));
data.add(new Model(1,"XyZ","12:45:50 PM"));
data.add(new Model(1,"SHjhsj","04:45:50 PM"));
Custom c=new Custom(this,data);
listView.setAdapter(c);
}
}
Model.java
public class Model {
int no;
String name,intime;
public Model(int no, String name, String intime) {
this.no = no;
this.name = name;
this.intime = intime;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIntime() {
return intime;
}
public void setIntime(String intime) {
this.intime = intime;
}
}
Custom.java
public class Custom extends BaseAdapter {
Activity a;
ArrayList<Model> data;
public Custom(Activity a, ArrayList<Model> data) {
this.a = a;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return data.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
public class Viewholder {
TextView srno, name, intime;
Button end;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
Viewholder viewholder = null;
if (convertView == null) {
viewholder = new Viewholder();
LayoutInflater li = a.getLayoutInflater();
convertView = li.inflate(R.layout.list_item, viewGroup, false);
viewholder.srno = (TextView) convertView.findViewById(R.id.cno);
viewholder.name = (TextView) convertView.findViewById(R.id.cname);
viewholder.intime = (TextView) convertView.findViewById(R.id.ctime);
viewholder.end = (Button) convertView.findViewById(R.id.cend);
convertView.setTag(viewholder);
}else {
viewholder=(Viewholder)convertView.getTag();
}
final Model model = data.get(i);
viewholder.srno.setText(valueOf(data.get(i).getNo()));
viewholder.name.setText(valueOf(data.get(i).getName()));
viewholder.intime.setText(valueOf(data.get(i).getIntime()));
viewholder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(a, data.get(i).getName()+" Exited", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.abc.timer.MainActivity">
<TextView
android:layout_width="70dp"
android:layout_height="50dp"
android:id="#+id/no"
android:text="CustNo"
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_width="80dp"
android:layout_height="50dp"
android:id="#+id/name"
android:layout_toRightOf="#+id/no"
android:text="Name"
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_width="80dp"
android:layout_height="50dp"
android:id="#+id/intime"
android:layout_toRightOf="#+id/name"
android:text="InTime"
android:textSize="20dp"
android:textColor="#000"/>
<TextView
android:layout_width="70dp"
android:layout_height="50dp"
android:id="#+id/exit"
android:text="Exit"
android:layout_toRightOf="#+id/intime"
android:textSize="20dp"
android:textColor="#000"/>
<ListView
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="#+id/no"
android:id="#+id/list"
></ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="#+id/cno"
android:gravity="center"/>
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="#+id/cname"
android:gravity="center"/>
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:id="#+id/ctime"
android:gravity="center"/>
<Button
android:layout_width="90dp"
android:layout_height="40dp"
android:text="End"
android:id="#+id/cend"
android:background="#c14c4a"
android:gravity="center"/>
</LinearLayout>
Use an AlarmManager with your specified time and the moment alarm will trigger you can enable the button, hence enabling the click event for the button.
Edit:
Either you can go ahead with handler object and use handler.postDelayed() method or there itself you can create a Timer Object and schedule the timer task within that object and do the stuff.
Assuming that u know how to convert the time strings into timestamps,modify ur adapter like this:
public class Custom extends BaseAdapter {
Activity a;
ArrayList<Model> data;
public Custom(Activity a, ArrayList<Model> data) {
this.a = a;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return data.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
public class Viewholder {
TextView srno, name, intime;
Button end;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
Viewholder viewholder = null;
if (convertView == null) {
viewholder = new Viewholder();
LayoutInflater li = a.getLayoutInflater();
convertView = li.inflate(R.layout.list_item, viewGroup, false);
viewholder.srno = (TextView) convertView.findViewById(R.id.cno);
viewholder.name = (TextView) convertView.findViewById(R.id.cname);
viewholder.intime = (TextView) convertView.findViewById(R.id.ctime);
viewholder.end = (Button) convertView.findViewById(R.id.cend);
convertView.setTag(viewholder);
}else {
viewholder=(Viewholder)convertView.getTag();
}
final Model model = data.get(i);
viewholder.srno.setText(valueOf(data.get(i).getNo()));
viewholder.name.setText(valueOf(data.get(i).getName()));
viewholder.intime.setText(valueOf(data.get(i).getIntime()));
final Runnable r = new Runnable() {
public void run() {
viewholder.end.setEnabled(true);
}
};
if(yourObjectTimestamp - currentTimeStamp > 20){ //this u need to do on ur own
viewholder.end.setEnabled(true); // I assumed u know how to convert times to timestamps
}else{
viewholder.end.setEnabled(false);
long timeDiffInMillis = yourObjectTimestamp - currentTimeStamp;
handler.postDelayed(r,timeDiffInMillis );
}
viewholder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(a, data.get(i).getName()+" Exited", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
As you can see in my screenshot, whole of layout doesn't display in dialogFragment although it has enough space. I have no idea what is my mistake.
I expect to see a 3 * 3 table.
public class ChangeFormationDialogFragment extends DialogFragment {
public ChangeFormationDialogFragment() {
// Empty constructor required for DialogFragment
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_change_formation, container);
GridView gridView = (GridView) view.findViewById(R.id.formation_change_grid);
gridView.setAdapter(new FormationAdapter(getActivity(), 0));
return view;
}
}
It's xml file (fragment_change_formation):
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/myTem_bg_boost_cards"
android:id="#+id/formation_change_grid"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"/>
My adapter class:
public class FormationAdapter extends BaseAdapter {
private static final String TAG = "FormationAdapter";
private Context mContext;
private ArrayList<String> formations;
private int selected = 0;
public FormationAdapter(Context context, int selected) {
this.mContext = context;
formations = Formation.getAllFormationsAsArrayOfStrings();
this.selected = selected;
}
#Override
public int getCount() {
if(formations == null)
return 0;
return formations.size();
}
#Override
public Integer getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.row_formation, null);
}
holder = new ViewHolder();
holder.llFormation = (LinearLayout) convertView.findViewById(R.id.llFormation);
holder.llFormationViews = (LinearLayout) convertView.findViewById(R.id.llFormationViews);
holder.tvFormation = (AnyTextView) convertView.findViewById(R.id.tvFormation);
holder.tvBuyButton = (AnyTextView) convertView.findViewById(R.id.tvBuyButton);
holder.tvFormation.setText(formations.get(position));
if(position > 2)
holder.tvBuyButton.setVisibility(View.VISIBLE);
if(position == selected)
holder.llFormation.setBackgroundColor(mContext.getResources().getColor(R.color.myTeam_greenDark));
else
holder.llFormation.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
return convertView;
}
static class ViewHolder {
LinearLayout llFormation;
LinearLayout llFormationViews;
AnyTextView tvFormation;
AnyTextView tvBuyButton;
}
}
And finally, row:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:id="#+id/llFormation">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/llFormationViews">
</LinearLayout>
<com.allstarxi.widget.AnyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvFormation"
android:layout_gravity="center"
style="#style/font_normal_20.white"
custom:typeface="baltomobilebold.ttf"/>
<com.allstarxi.widget.AnyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBuyButton"
android:text="#string/myTeam_dialog_buy"
android:layout_gravity="center"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="#color/myTem_bg_points"
android:textStyle="bold"
android:visibility="gone"
style="#style/font_normal_14.black"
custom:typeface="baltomobilebook.ttf"/>
</LinearLayout>
This is screenshot of what I see on my device:
Just try this:
Add the following code inside 'onResume()'
WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.MATCH_PARENT;
getDialog().getWindow().setAttributes(params);