Breaking News

How to create/Add Bookmark menu Button in Web Browser app Android



Key features of Post:-

1. Add Bookmark Button in Menu Bar.
2. Check if Bookmark is available in Database or not, when clicking on Button.
3. Change the menu button between Bookmark and Bookmarked at runtime.
4. Show bookmarks on Listview in the next post.

In this, we add a bookmark button in our Menu bar, to bookmark a webpage for further use.

    *For creating a webview or related queries, view our previous post.*


For that, firstly we add a button in our menu.xml


menu.xml

  <item  
     android:id="@+id/bookmark"  
     app:showAsAction="always"  
     android:title="Bookmark"  
     android:orderInCategory="100"  
     android:icon="@drawable/ic_bookmark_outline_white_24dp"  
     />  

Here, we add a Bookmark button in the menu.xml file, also we import two icons. You can download icons from here.


Then, we create a show data method in DatabaseHelper.java to show inserted data.


   * For SQlite related commands and queries, view our previous posts*


DatabaseHelper.java

 public static final String Database_name = "Bookmarks.db";  
   public static final String Table_name = "BookMarks";  
   public static final String col_id = "Id";  
   public static final String col_title = "Title";  
   public static final String col_Url = "Url";  
  public ArrayList<HashMap<String,String>> Showdata()      
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     ArrayList<HashMap<String,String>> userlist = new ArrayList<>();  
     Cursor cursor = db.rawQuery("select * from "+Table_name,null);  
     while(cursor.moveToNext())  
     {  
       HashMap<String,String> user = new HashMap<>();  
       user.put("Id",cursor.getString(cursor.getColumnIndex(col_id)));  
       user.put("Title",cursor.getString(cursor.getColumnIndex(col_title)));  
       user.put("Url",cursor.getString(cursor.getColumnIndex(col_Url)));  
       userlist.add(user);  
     }  
     return userlist;  
   }  

Here, we create a Hashmap array list show method in which we put all data in an array list which is of Hashmap type. Also, we create an Insert data method


Then, we create an add bookmark function and perform clicking on our bookmark button in our MainActivity.java file


MainActivity.java

 String mapbookurl,mapbookid,mapoptionsmenu;  
   ArrayList<String> mapurllist = new ArrayList<>();  
   ArrayList<String> mapidlist = new ArrayList<>();  
   ArrayList<String> mapoptionslist = new ArrayList<>();  
   DatabaseHelper mydb;  
   ArrayList<HashMap<String,String>> userlist;  

Here are some needed Global Variables.

Also, we create an alter method in DatabaseHelper.java
 public void alter()  
   {  
     SQLiteDatabase db = this.getWritableDatabase();  
     db.execSQL(("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE NAME = ' "+Table_name+" ' "));  
   }  


Then, in oncreate method of our MainActivity.java

 protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     mydb = new DatabaseHelper(this);  
 }  
 public void addbookmark()  
   {  
     String title = mywebview.getTitle();  
     String url = mywebview.getUrl();  
     boolean isInserted = mydb.insertData(title,url);  
     if(isInserted)  
     {  
       Toast.makeText(this, "Bookmarked", Toast.LENGTH_SHORT).show();  
     }  
     else  
     {  
       Toast.makeText(this, "Error adding Bookmark", Toast.LENGTH_SHORT).show();  
     }  
   }  
 @Override  
   public boolean onOptionsItemSelected(MenuItem item) {  
     int id = item.getItemId();  
     invalidateOptionsMenu();  
     if(id==R.id.bookmark)  
     {  
       userlist = mydb.Showdata();  
       for(HashMap link : userlist)  
       {  
         HashMap hashmap = (HashMap)link;  
         mapbookid = (String)hashmap.get("Id");  
         mapbookurl = (String)hashmap.get("Url");  
         mapidlist.add(mapbookid);  
         mapurllist.add(mapbookurl);  
       }  
       if(mapbookurl!=null && mapbookid!=null)  
       {  
         if(mapurllist.contains(tooltext.getText().toString())) {  
           int val = mapurllist.indexOf(tooltext.getText().toString());  
           String val1 = mapidlist.get(val);  
           int parseval1 = Integer.parseInt(val1);  
           Integer delete = mydb.delete(String.valueOf(parseval1));  
           if (delete > 0) {  
             Toast.makeText(this, "Bookmark Removed", Toast.LENGTH_SHORT).show();  
             mapurllist.clear();  
             mapidlist.clear();  
             mapoptionslist.clear();  
             userlist = mydb.Showdata();  
             mydb.alter();  
           } else {  
             Toast.makeText(this, "Error Removing Bookmark", Toast.LENGTH_SHORT).show();  
             mapurllist.clear();  
             mapidlist.clear();  
             mapoptionslist.clear();  
           }  
         }  
           else  
           {  
             mapurllist.clear();  
             mapidlist.clear();  
             mapoptionslist.clear();  
             addbookmark();  
             Toast.makeText(this, "Bookmarked", Toast.LENGTH_SHORT).show();  
           }  
         }  
       else  
       {  
         mapurllist.clear();  
         mapidlist.clear();  
         mapoptionslist.clear();  
         addbookmark();  
         Toast.makeText(this, "Bookmarked", Toast.LENGTH_SHORT).show();  
       }  
       invalidateOptionsMenu();  
     }  
     return super.onOptionsItemSelected(item);  
   }  


Here, we create add bookmark method to add values to the database.
and when it's clicking on the bookmark button, then firstly we check if URL is available in the database or not, if it's available, the bookmark removed else bookmark added.

The whole process is done by mapping of ArrayList.

We map the Id value and URL value of data and store in different ArrayList and delete on the behalf of id and get index from both the ArrayList.

and don't forget to clear the ArrayList all times.


Now it is only when we click on the bookmark button, but when a new URL opens, we should check that if the URL is present in the database or not.  


It's done when Menu is creating.


Now in our oncreateoptionsmenu we create a condition of checking that URL is present in the database or not.


oncreateoptionsmenu

 @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.menu_main,menu);  
     userlist = mydb.Showdata();  
     for(HashMap msg : userlist)  
     {  
       HashMap hashmap = (HashMap)msg;  
       mapoptionsmenu = (String)(hashmap.get("Url"));  
       mapoptionslist.add(mapoptionsmenu);  
     }  
     if(mapoptionsmenu!=null)  
     {  
       if(mapoptionslist.contains(tooltext.getText().toString()))  
       {  
         menu.findItem(R.id.bookmark).setIcon(R.drawable.ic_bookmark_white_24dp);  
         menu.findItem(R.id.bookmark).setTitle("Bookmarked");  
       }  
       else  
       {  
         menu.findItem(R.id.bookmark).setIcon(R.drawable.ic_bookmark_outline_white_24dp);  
       }  
     }  
     else  
     {  
       menu.findItem(R.id.bookmark).setIcon(R.drawable.ic_bookmark_outline_white_24dp);  
     }  
     return true;  
   }  

Here, we change our Bookmark icon on runtime (when creating options menu on new URL opening).

Next post, we will show Bookmarks in List view.   



Follow us for more posts like this, 

Subscribe Harpreet studio  on Youtube 
Like Harpreet Studio on Facebook 
 Follow me on Instagram 

No comments