How to create swipeable and clickable cards in android using cardView? - java

I tried using https://github.com/yuyakaido/CardStackView but could not modify it to create a click functionality.
basically I want to use animate() when clicked on the card but not able to do so.
Main activity--
public class MainActivity extends AppCompatActivity {
public ArrayList<String> al;
public ArrayAdapter<String> arrayAdapter;
public int i;
#BindView(R.id.frame) SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
al = new ArrayList<>();
al.add("php");
al.add("c");
al.add("python");
al.add("java");
al.add("html");
al.add("c++");
al.add("css");
al.add("javascript");
arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al );
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
makeToast(MainActivity.this, "Left!");
}
#Override
public void onRightCardExit(Object dataObject) {
makeToast(MainActivity.this, "Right!");
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
#Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
// **Optionally add an OnItemClickListener Need solution for this**
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
ImageView img=findViewbyid(R.id.img);
img.animate().alpha(1f).setduration(2000);
}
});
}
static void makeToast(Context ctx, String s){
Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show();
}
#OnClick(R.id.right)
public void right() {
/**
* Trigger the right event manually.
*/
flingContainer.getTopCardListener().selectRight();
}
#OnClick(R.id.left)
public void left() {
flingContainer.getTopCardListener().selectLeft();
}
}

Related

Why my spinner onitemselected method doesn't initialize

I'm pulling the weekly data of students from firebase and adding an arraylist inside a class and I return this arraylist to Teacheractivity and I send this arraylist to spinner inside spinner class when i run emulator i can see data in spinner but when i click nothing happens. It doesn't give any error. I wrote it in separate classes to write clean code. Could this be a problem? I don't understand why this happened.
My TeacherActivity
public class TeacherDersActivity extends AppCompatActivity {
ActivityTeacherDersBinding binding;
String lesson_name;
ArrayList<String> WeekContentArraylist;
_Spinner spinner;
GetWeeks getWeeks;
static void SpinnerList(_Spinner spinner)
{
spinner.init();
}
public void init() {
Intent intent = getIntent();
lesson_name= intent.getStringExtra("teacher_lesson");
getWeeks = new GetWeeks(this);
WeekContentArraylist = getWeeks.GetWeekContent(lesson_name);
spinner = new _Spinner(TeacherDersActivity.this, WeekContentArraylist);
SpinnerList(spinner);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityTeacherDersBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
init();
}
}
GetWeeks class from which I pull data from Firebase
My GetWeeks class
public class GetWeeks {
FirebaseFirestore mFirestore;
ArrayList<String> arrayListWeekContent;
Activity _activity;
public GetWeeks(Activity activity)
{
_activity = activity;
mFirestore = FirebaseFirestore.getInstance();
arrayListWeekContent = new ArrayList<>();
}
public void init()
{
}
protected ArrayList<String> GetWeekContent(String lesson_name)
{
mFirestore.collection("Lessons")
.document(lesson_name)
.collection("Documents")
.document("Docs1")
.get()
.addOnSuccessListener(_activity, new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(#NonNull DocumentSnapshot documentsnapshots) {
arrayListWeekContent.add(documentsnapshots.getString("Week1"));
arrayListWeekContent.add(documentsnapshots.getString("Week2"));
arrayListWeekContent.add(documentsnapshots.getString("Week3"));
arrayListWeekContent.add(documentsnapshots.getString("Week4"));
arrayListWeekContent.add(documentsnapshots.getString("Week5"));
arrayListWeekContent.add(documentsnapshots.getString("meetLink"));
}
}).addOnFailureListener(_activity, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
System.out.println("GetWeeks FAIL");
}
});
return arrayListWeekContent;
}
}
My Spinner class
public class _Spinner extends Activity {
Activity activityTeacherDers;
ArrayList<String> WeekContentArraylist;
public _Spinner(Activity activityTeacherDers, ArrayList<String> WeekContentArraylist)
{
this.activityTeacherDers = activityTeacherDers;
this.WeekContentArraylist= WeekContentArraylist;
}
public void init()
{
Spinner spinner = activityTeacherDers.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activityTeacherDers,
android.R.layout.simple_spinner_item,WeekContentArraylist);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("Heloooasdasdsa dataa "); //<---- nothing happen hear
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}

How can I wait for a task to finish before starting another?

I have converted the contents of Textview texx; to a string where I can display it as an array however it displays the contents of textView before the doit() task has excecuted, the current output that appears on the screen is 'TextView', I want it to display the output of TextView texx; after it has finished its task. Any help would be greatly apreciated.
Im very new to android studio and java.
public class AppHome extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
TextView texx;
private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_home);
new doit().execute();
texx= findViewById(R.id.text1);
String text = texx.getText().toString();
//String num = text;
String[] str = text.split(",");
final ArrayList al = new ArrayList<String>(Arrays.asList(str));
(Full code below)
```
public class AppHome extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
TextView texx;
private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
private int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_home);
new doit().execute();
texx= findViewById(R.id.text1);
String text = texx.getText().toString();
//String num = text;
String[] str = text.split(",");
final ArrayList al = new ArrayList<String>(Arrays.asList(str));
arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al );
SwipeFlingAdapterView flingContainer = findViewById(R.id.frame);
registerForContextMenu(flingContainer);
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
Toast.makeText(AppHome.this, "left", Toast.LENGTH_SHORT).show();
}
#Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(AppHome.this, "right", Toast.LENGTH_SHORT).show();
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
#Override
public void onScroll(float scrollProgressPercent) {
}
});
// Optionally add an OnItemClickListener
/*
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
}
});
*/
}
public class doit extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... voids) {
String words = "";
try {
Document doc = Jsoup.connect("https://screenscrape4top40.000webhostapp.com/").get();
words = doc.text();
} catch(Exception e) {
e.printStackTrace();
}
return words;
}
#Override
public void onPostExecute(String words) {
super.onPostExecute(words);
texx.setText(words);
}
}
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.dropdown_menu1);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "Item 1 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Item 2 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "Item 3 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "Item 4 clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}

How to implement admob rewarded video ads in a list view?

I want to know how to implement AdMob rewarded video ads in a list view?
I'm using the source code from here
and I want to use it in this class StickerPackDetailsActivity.java
and the layout is gonna like this
![layout][1]
I want to lock add to WhatsApp and unlock it by watching video reward.
but this stickerdetails showed from listview from
![here][2]
so, how to implement video reward ads only in 1 specified item of the listview not all of them?
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,"ca-app-pub111111111");
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
listitem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadRewardedVideoAd();
}
});
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-",
new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
#Override
public void onRewardedVideoCompleted() {
}
#Override
protected void onPause() {
mRewardedVideoAd.pause(this);
super.onPause();
}
#Override
protected void onResume() {
mRewardedVideoAd.resume(this);
super.onResume();
}
}

Android Studio refresh RecyclerView item

I am trying to add item from one adapter into another adapter. But when I added item, doesn't appear in the another adapter's recyclerview list.
ContactDataAdapter.java
#Override
public void onBindViewHolder(final ContactDataAdapter.ViewHolder holder, int position) {
holder.titleTv.setText(arrList.get(position).toString());
holder.conIv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CallFragment cf = new CallFragment();
//CallDataAdapter cd = new CallDataAdapter();
String t = holder.titleTv.getText().toString();
cf.addItem(t.toString());
//cd.addData(t);
Toast.makeText(view.getContext(), "Added " + t, Toast.LENGTH_SHORT).show();
}
});
}
CallFragment.java
public void addItem(String title) {
adapter.addData(title.toString());
//adapter.notifyItemRangeChanged(0,data.size());
}
CallDataAdapter.java
public void addData(String title) {
dataList.add(title);
notifyDataSetChanged();
notifyItemRangeChanged(0, dataList.size());
notifyItemInserted(pos + 1);
}
The Image that clicked added button
A list that diplay added item("asd" added from the beginning.)
First setup EventBus
Define events:
public static class MessageEvent {
public String title;
}
in CallFragment declare it
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
adapter.addData(title.toString());
};
Register in your fragment from you setup ContactDataAdapter
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
Call the event
#Override
public void onBindViewHolder(final ContactDataAdapter.ViewHolder holder, int position) {
holder.titleTv.setText(arrList.get(position).toString());
holder.conIv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MessageEvent event=new MessageEvent();
event.title= holder.titleTv.getText().toString();
EventBus.getDefault().post(event);
//CallFragment cf = new CallFragment();
//CallDataAdapter cd = new CallDataAdapter();
//String t = holder.titleTv.getText().toString();
//cf.addItem(t.toString());
//cd.addData(t);
Toast.makeText(view.getContext(), "Added " + t, Toast.LENGTH_SHORT).show();
}
});
}

How do I put only true objects in a new listview

I have here an arraylist that is shown in a listview, when I give long click a dialog is displayed it contains the option of favorite that when selected the object is marked as true, how do I only display the items marked in my ActivityFavoritos?
MainActivity.class
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
ArrayList<memes> item;
ArrayAdapter<memes> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
lv = findViewById(R.id.lv);
mp = new MediaPlayer();
item = new ArrayList<>();
//itens
item.add(new memes("Fique apertado sobre o meme para compartilhar", R.raw.sharebagui));
item.add(new memes("2 mil anos", R.raw.milanos));
item.add(new memes("Acelera jesus", R.raw.acelera_jesus));
item.add(new memes("Azideia", R.raw.asideia));
item.add(new memes("Acertou mizeravi", R.raw.mizeravi));
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(arrayAdapter);
//play audio
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
playSong(position);
}
});
//PROGRESS
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
//PROGRESS
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Olá, Marilene!");
builder.setItems(Nomes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // Delete
sendWhatsAppAudio(position);
break;
case 1: // Copy
item.get(position).setmIsFavourite(true);
break;
default:
break;
}
}
});
AlertDialog alertDialog = builder.create();
builder.show();
return true;
}
});
}
public void playSong(int songIndex) {
mp.reset();
mp = MediaPlayer.create(this, arrayAdapter.getItem(songIndex).getResId());
mp.start();
}
#Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
memes.class
public class memes{
private String nome;
private int resID;
private Boolean mIsFavourite;
memes(String nome, int resID){
this.nome = nome;
this.resID = resID;
}
public String getNome(){
return nome;
}
int getResId(){
return resID;
}
#Override
public String toString(){
return nome;
}
public Boolean getmIsFavourite() {
return mIsFavourite;
}
public void setmIsFavourite(Boolean mIsFavouriteResource) {
this.mIsFavourite = mIsFavouriteResource;
}
}
this is my progress so far I am confused of what I should do in the favorite activity.
ActivityFavoritos.class
public class ActivityFavoritos {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_favoritos);
lv = findViewById(R.id.lvf);
if (list.get(position).getmIsFavourite()) {
//do want you want when its true
} else {
//do want your code when its false.
}
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(arrayAdapter);
}
}
In your memes class declare variable
private Boolean mIsFavourite;
And declare getter and setter method for same.
public Boolean getmIsFavourite() {
return mIsFavourite;
}
public void setmIsFavourite(Boolean mIsFavouriteResource) {
this.mIsFavourite = mIsFavouriteResource;
}
Now on Double click set value of that item to True
item.get(position).setmIsFavourite(true);
In your second Activity just check value of this by using getmIsFavourite()
EDIT :
To use this getmIsFavourite()
if(list.get(position).getmIsFavourite())
{
//do want you want when its true
}else{
//do want your code when its false.
}

Categories

Resources