Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

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 

12 comments:

  1. Sir send application sorce code me

    ReplyDelete
  2. Puzzling read, Positive page, where did a couple of the articles on your site page now, and I genuinely like your style. You rock and if it's astoundingly not all that whole difficulty, keep up the sensible work. dark0de market

    ReplyDelete
  3. I should say perseveringly that its battering! The blog is edifying and continually produce astounding things. dark0de market url

    ReplyDelete
  4. This is a decent post. This post gives really quality data. I'm unquestionably going to investigate it. Truly exceptionally valuable tips are given here. Much obliged to you to such an extent. Keep up the acts of kindness. dark0de

    ReplyDelete
  5. But did you know that there are different kinds of sports bras for different levels of activity?먹튀검증

    ReplyDelete
  6. i am harpreet singh fro patiala please give me full source code of browser brother .... i want to use .

    ReplyDelete
  7. please check message in your youtube browser playlist and reply me on preet2cops@gmail.com

    ReplyDelete
  8. Attracting target audience through SEO hamping can help increase your conversion rate and ultimately your sales. By optimizing your website content and using the right keywords, you can attract an audience that is interested in your products or services. This will make website visitors to become real customers and increase the conversion rate.

    ReplyDelete
  9. خدمات حمل و نقل تریدست شامل حمل و نقل هوایی، زمینی، ریلی و دریایی می‌شود. این تنوع در خدمات، امکان انتخاب بهترین و مناسب‌ترین روش حمل و نقل را برای مشتریان فراهم می‌کند. تریدست با داشتن شبکه گسترده‌ای از شرکای تجاری در سراسر جهان، توانسته است خدمات ترانزیت بین‌المللی را با سرعت و دقت بالا انجام دهد.

    ReplyDelete
  10. بهبود سئوی وب سایت های عربی می تواند تاثیر بسزایی در بهبود جایگاه کلمات کلیدی هدف داشته باشد. با استفاده از تکنیک های بهینه سازی برای موتورهای جستجو می توان محتوای وب سایت عربی را به گونه ای تنظیم کرد که موتورهای جستجو بتوانند به راحتی آن را شناسایی و رتبه بندی کنند. این شامل بهینه سازی عناوین، توضیحات متا، استفاده از کلمات کلیدی مرتبط و بهبود ساختار لینک های داخلی و خارجی است.

    ReplyDelete