Display Bookmarks on Custom List View android programming
In our previous post, we were told you to add a bookmark and change bookmark icon while checking in the database accordingly.
Now, In this post, We will show that bookmarks on a ListView.
For that, We create a new activity called BookList.java in which we do all functionality about adding data to List view.
and in Booklist.xml we create a Listview and a Linear Layout which is used when our list is empty.
Booklist.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:orientation="vertical"
tools:context=".BookList">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="BookMarks"
app:titleTextColor="@color/white">
</androidx.appcompat.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/booklistview"
>
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/emptyview"
android:layout_gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:text="WHOOPS"
android:textColor="#212121"
android:textSize="26sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:text="There are no bookmarks at the moment"
android:textColor="#212121"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
Here, we create a ListView and an empty Linear Layout to use when the list is empty.
But for showing Bookmark on a listview we need a custom list with three text views.
For that, we create a custom listview called book_custom_list.xml
book_custom_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custombookid"
android:visibility="gone"
android:textColor="#343434"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custombooktitle"
android:textColor="#343434"
android:maxLines="2"
android:ellipsize="end"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custombookurl"
android:ellipsize="end"
android:layout_marginTop="35dp"
android:textColor="#343434"
android:textSize="14sp"/>
</RelativeLayout>
Here, we add three text views and we set visibility to gone for our Id Text view because we don't want to display Id in our ListView.
Then we do the remaining code in our BookList.java file.
BookList.java
package studio.harpreet.mybrowser;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class BookList extends AppCompatActivity {
DatabaseHelper mydb;
ListView booklist;
ListAdapter lviewadapter;
ArrayList<HashMap<String,String>> userlist;
LinearLayout empty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_list);
mydb = new DatabaseHelper(this);
booklist = findViewById(R.id.booklistview);
empty = findViewById(R.id.emptyview);
empty.setVisibility(View.GONE);
getdata();
booklist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Object o = booklist.getAdapter().getItem(i);
if(o instanceof Map)
{
Map map = (Map)o;
Intent intent = new Intent(BookList.this,MainActivity.class);
intent.putExtra("urlkey",String.valueOf(map.get("Url")));
startActivity(intent);
}
}
});
}
public void getdata()
{
userlist = mydb.Showdata();
if(userlist.isEmpty())
{
empty.setVisibility(View.VISIBLE);
return;
}
lviewadapter = new SimpleAdapter(BookList.this,userlist,R.layout.book_custom_list,
new String[]{"Id","Title","Url"},
new int[]{R.id.custombookid,R.id.custombooktitle,R.id.custombookurl});
booklist.setAdapter(lviewadapter);
}
@Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
}
Here, we add Listview, Arraylist, Listadapter, Database Helper and Linear Layout global variable.
Then, we create a method of getdata in which we add all data in our ArrayList. Then use the Listview adapter to add all list view to our custom list and then add that adapter to ListView.
Also, We create an onitemclicklistener method with ListView, when we click on ListView item, then it open MainActivity with key pair we provided then you have to handle intent extra. You have write this code in your mainactivity.java oncreate method
MainActivity.java
String url = "https://www.google.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent().getExtras() != null) {
url = getIntent().getStringExtra("urlkey");
}
In the next post, we will do a code of select all bookmarks and delete all selected bookmarks.
Follow us for more posts like this,
Subscribe Harpreet studio on Youtube
Like Harpreet Studio on Facebook
Follow me on Instagram
zscszxc
ReplyDeleteThey redid the actual personalization style, cheap niche edit, as well as produced an incredible brand new color scheme.
ReplyDeleteThe client praises the agency for their professionalism and efficient communication.
ReplyDeletebest companies for UX designers
They’re an exceptionally talented team with a variety of personalities
ReplyDeletemobile app agencies
Best Search Engine At this point you'll find out what is important, it all gives a url to the appealing page:
ReplyDeleteWhile the admin of the web site is working, no question soon it will likely be famous, due to its feature blogs.
ReplyDeletedigital web agency
I got this blog site through my friends and when I searched this really there were informative articles at the place.
ReplyDeleteweb design firm
"In this day and age, the energy we use and the manners in which we use it are evolving. For California to take the jump from business as usual to accomplishing environment and energy objectives at the most reduced conceivable expense, we want substantially more energy development" (Energy Innovations Small Grant Program, 2014). https://onohosting.com/
ReplyDeleteit tells the insurance agency that the patient was seen in light of the fact that they were whining of an irritated throat.
ReplyDelete• CPT, or methodology, codes, let the insurance agency know strategies performed on the patient on the day that they were seen. https://onohosting.com/
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. blisters for concentrate
ReplyDelete