Issue
I have an app which loads users profile URls by firebase recycler adapter. I am wondering how to create preview links just like when we share a link in whatsapp and a preview is generated. The preview will contain an image, a header and small description. Thanks in advance. Below is my code DashboardActivity
fAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
dRef = FirebaseDatabase.getInstance().getReference("Users");
// This will display the click counts for current online user
user = FirebaseAuth.getInstance().getCurrentUser();
UID = user.getUid();
dRef.child(UID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
remainingClicks = String.valueOf(snapshot.child("clicksRemain").getValue(Integer.class));
showGigCount = String.valueOf(snapshot.child("showGigCount").getValue(Integer.class));
clicks.setText(remainingClicks);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
recyclerView = findViewById(R.id.dashboardRCV);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
LoadData();
}
private void LoadData() {
options = new FirebaseRecyclerOptions.Builder<ModelClass>()
.setQuery(dRef, ModelClass.class)
.build();
adapter = new FirebaseRecyclerAdapter<ModelClass, MyViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull ModelClass model) {
//This will hide the gig if its showGigCount becomes 0
Query query = dRef.orderByChild("showGigCount").equalTo(0);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
holder.geglink.setVisibility(View.GONE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
query.addListenerForSingleValueEvent(valueEventListener);
if (finalClicksCount == 0){
holder.geglink.setText(model.getGig());
}else if (dRef.child("showGigCount").equals(0)){
holder.geglink.setVisibility(View.GONE);
}else{
holder.geglink.setText(model.getGig());
}
holder.geglink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getGig()));
startActivity(browserIntent);
finalClicksCount = Integer.parseInt(remainingClicks);
remaingGigShow = Integer.parseInt(showGigCount);
if (finalClicksCount >= 1) {
finalClicksCount--;
remaingGigShow ++;
clicks.setText(String.valueOf(finalClicksCount));
UserClicksCounts();
UserShowGigCounts();
} else if (finalClicksCount == 0){
zeroClicks.setVisibility(View.VISIBLE);
}
}
});
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout, parent, false);
return new MyViewHolder(view);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
Single Row xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/rcvGigLink"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat"
android:gravity="center_horizontal"
android:text="https://www.fiverr.com/share/NwPBXR"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rcvAbout" />
<TextView
android:id="@+id/rcvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat"
android:gravity="center_horizontal"
android:text="Zeeshan"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/rcvAbout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/montserrat"
android:gravity="center_horizontal"
android:text="I am a user"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rcvName" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Solution
You can use this Link Preview Library
Just use the view directly
<io.github.ponnamkarthik.richlinkpreview.RichLinkView
android:id="@+id/richLinkView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</io.github.ponnamkarthik.richlinkpreview.RichLinkView>
Answered By - Gouse Mohiddin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.