17

I am trying to make an Activity which will hold all the recents screen in my app not in the android device. The idea it is to create the recents view like in android. I do not have for the moment an idea how to do that. I have searched in the official site of android but didn't get what I want. I have some activities which there is declared WebView and I want to take title, url and the view of that Url and to save to show at VisualHistory. The design I am able to do I have achieved but I do not know how to show the recents screen or views. The color of the background should based from url. If someone don't understand something let me know. Below you can find a photo which will show what I am trying to do, I have copied this photo from another app. I have planned these steps to follow to achieve that. In the Pojo.class and in db it is not defined the image which will be shown in the cardview because I do not know how to achieve that. I want the background which for the moment in blue to be depended from the url color. Is it any way to get the color of the url ?

Create a POJO.class
Create a DB which will hold this data
Create an Adapter
Create a Fragment

I have a Log here which shows me the items.

[VisualHistoryItem{id=109, title='Wikipedia, the free encyclopedia', url='https://en.m.wikipedia.org/w/index.php?title=Main_Page'}, VisualHistoryItem{id=112, title='', url='https://mail.google.com/'}, VisualHistoryItem{id=113, title='Gmail – kostenloser Speicherplatz und E-Mails von Google', url='https://www.google.com/intl/de/gmail/about/#'}]

This saves a visual history item.

mVisualHistory.setUrl(url);
mVisualHistory.setTitle(view.getTitle());
Bitmap topass= getSnapshoot.takeScreenShot(BookmarkActivity.this);
   try {
      String filename = mVisualHistory.getId()+".png";
      FileOutputStream stream = BookmarkActivity.this.openFileOutput(filename, Context.MODE_PRIVATE);
     topass.compress(Bitmap.CompressFormat.PNG, 100, stream);
     stream.close();
     topass.recycle();
      } catch (Exception e) {
          e.printStackTrace();
      }
  mVisualRepository.insertNoteTask(mVisualHistory);

I have created an Activity code is below.

public class ActivityTabs extends AppCompatActivity implements View.OnClickListener {
    private PopupMenu mPopupMenu;
    private FrameLayout settings;
    private FrameLayout frameLayout;
    private LinearLayout linearLayout;
    private ImageView incognito;
    private TextView textOfHistory;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_tabs);
        findViewById(R.id.vgGoMain).setOnClickListener(this);
        findViewById(R.id.vgAdd).setOnClickListener(this);
        settings = findViewById(R.id.vgSettingsHis);
        linearLayout = findViewById(R.id.layoutEmptyVisHistory);
        settings.setOnClickListener(this);
        textOfHistory = findViewById(R.id.tvEmptyHistory);



 FragmentVisualHistoryVertical newFragment = new FragmentVisualHistoryVertical();
    getSupportFragmentManager().beginTransaction().add(R.id.frameLayoutVisHistory, newFragment).commit();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.frameLayoutVisHistory, newFragment)
            .commit();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.vgGoMain:
                finish();
                return;
            case R.id.vgAdd:
                Intent intent = new Intent(this, ActivitySearchEngine.class);
                startActivity(intent);
                finish();
                return;
            case R.id.vgSettingsHis:
                showMenuSettings();
                return;
                default:
                    break;
        }
    }

    public void showMenuSettings() {
        mPopupMenu = new PopupMenu(this, settings);
        final MenuInflater menuInflater = mPopupMenu.getMenuInflater();
        menuInflater.inflate(R.menu.history_settings, mPopupMenu.getMenu());
        mPopupMenu.show();

    }
}

And this is the XML for this Activity.

<?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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/historyEmptyBack"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <FrameLayout
            android:id="@+id/frameLayoutVisHistory"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingBottom="@dimen/bottomPanelHeight"></FrameLayout>

        <LinearLayout
            android:id="@+id/layoutEmptyVisHistory"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:visibility="gone">

            <TextView
                android:id="@+id/tvEmptyHistoryTitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/common_24dp"
                android:gravity="center_horizontal"
                android:lineSpacingExtra="3.0sp"
                android:text="@string/VHVEmptyTite"
                android:textColor="@color/historyEmptyTitle"
                android:textSize="22.0sp" />

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="48.0dip"
                android:layout_weight="1.0">

                <LinearLayout
                    android:id="@+id/horizontalEmpty"
                    style="@style/LayoutEmptySmile"
                    android:layout_width="@dimen/visual_history_element_width"
                    android:layout_height="@dimen/visual_history_element_height"
                    android:orientation="vertical">

                    <ImageView
                        style="@style/EmptyHistorySmile"
                        android:src="@drawable/vh_smile_gray" />

                    <TextView
                        style="@style/EmptyHistoryText"
                        android:layout_marginTop="@dimen/common_16dp"
                        android:gravity="center_horizontal"
                        android:paddingLeft="@dimen/common_16dp"
                        android:paddingRight="@dimen/common_16dp"
                        android:text="@string/VHVEmptyDesc" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/verticalEmpty"
                    style="@style/LayoutEmptySmile"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/common_24dp"
                    android:layout_marginRight="@dimen/common_24dp"
                    android:orientation="horizontal"
                    android:paddingLeft="@dimen/common_16dp"
                    android:paddingTop="@dimen/common_16dp"
                    android:paddingRight="@dimen/common_16dp"
                    android:paddingBottom="@dimen/common_16dp">

                    <ImageView
                        style="@style/EmptyHistorySmile"
                        android:src="@drawable/vh_smile_gray" />

                    <TextView
                        style="@style/EmptyHistoryText"
                        android:layout_width="wrap_content"
                        android:gravity="left"
                        android:paddingLeft="@dimen/common_16dp"
                        android:text="@string/VHVEmptyDesc" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/layoutIncognito"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:paddingBottom="@dimen/bottomPanelHeight"
                    android:visibility="visible">

                    <LinearLayout
                        android:id="@+id/layoutEmptyDesc"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="@dimen/common_16dp"
                        android:gravity="center"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/ivEmptyHistory"
                            android:layout_width="@dimen/history_private"
                            android:layout_height="@dimen/history_private"
                            android:src="@drawable/incognito_icon_history" />

                        <TextView
                            android:id="@+id/tvEmptyHistory"
                            style="@style/EmptyHistoryText"
                            android:layout_marginTop="@dimen/common_16dp"
                            android:gravity="center_horizontal"
                            android:paddingLeft="@dimen/common_16dp"
                            android:paddingRight="@dimen/common_16dp"
                            android:text="@string/SVSearchPrivateMode"
                            android:textColor="@color/historyTextColor"
                            android:textSize="@dimen/common_18sp" />
                    </LinearLayout>
                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/historyEmptyBack"
            android:gravity="bottom"
            android:orientation="vertical"
            android:paddingLeft="2.0dip"
            android:paddingRight="2.0dip">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <LinearLayout style="@style/VisHistoryMenuSideLayout">

                    <FrameLayout
                        android:id="@+id/vgGoMain"
                        style="@style/VisHistoryFrLayoutMenu"
                        android:paddingRight="14.0dip">

                        <TextView
                            style="@style/VisHistoryTvMenu"
                            android:text="@string/VHVHomeBarButtonItemTitle" />
                    </FrameLayout>

                    <View style="@style/VisHistoryEmptyView" />
                </LinearLayout>

                <FrameLayout
                    android:id="@+id/vgAdd"
                    style="@style/VisHistoryFrLayoutMenu">

                    <ImageView
                        style="@style/VisHistoryMenuIv"
                        android:scaleX="0.8"
                        android:scaleY="0.8"
                        android:src="@drawable/newtab_button" />
                </FrameLayout>

                <LinearLayout style="@style/VisHistoryMenuSideLayout">
                    <View style="@style/VisHistoryEmptyView" />
                    <FrameLayout
                        android:id="@+id/vgTrash"
                        style="@style/VisHistoryFrLayoutMenu">
                        <ImageView
                            style="@style/VisHistoryMenuIv"
                            android:scaleX="1.3"
                            android:scaleY="1.3"
                            android:src="@drawable/trash" />
                    </FrameLayout>

                    <View style="@style/VisHistoryEmptyView" />

                    <FrameLayout
                        android:id="@+id/vgSettingsHis"
                        style="@style/VisHistoryFrLayoutMenu"
                        android:paddingLeft="0.0dip"
                        android:paddingRight="0.0dip">

                        <android.support.v7.widget.AppCompatImageView
                            style="@style/VisHistoryMenuIv"
                            android:layout_gravity="right"
                            app:srcCompat="@drawable/ic_dots_vertical"
                            tools:ignore="VectorDrawableCompat" />
                    </FrameLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>
</LinearLayout>

This is another Fragment Here will show of the list

  public class FragmentVisualHistoryVertical extends FragmentVisualHistory implements VisualRecyclerAdapter.OnVisualHistoryItemListener {
    public View paramView;
    private VisualRecyclerAdapter mVisualHistoryRecyclerAdapter;
    private RecyclerView mRecyclerView;
    private VisualHistoryRepository getmNoteRepository;

    private ArrayList<VisualHistoryItem> mVisualHistoryItems = new ArrayList<>();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.fragment_vis_history_vertical, container, false);
        mRecyclerView = paramView.findViewById(R.id.rvWebHistory);
        initRecyclerView();
        getmNoteRepository = new VisualHistoryRepository(getActivity());
        retrieveVisualHistory();
        return paramView;
    }

    private void initRecyclerView(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setReverseLayout(true);
        new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);
        mVisualHistoryRecyclerAdapter = new VisualRecyclerAdapter(mVisualHistoryItems, this, mContext);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(),
                linearLayoutManager.getOrientation());
        mRecyclerView.addItemDecoration(dividerItemDecoration);
        mRecyclerView.setAdapter(mVisualHistoryRecyclerAdapter);
    }
    private void retrieveVisualHistory() {
        getmNoteRepository.retrieveVisualHistoryTask().observe(this, new Observer<List<VisualHistoryItem>>() {
            @Override
            public void onChanged(@Nullable List<VisualHistoryItem> item) {
                if(mVisualHistoryItems.size() > 0){
                    mVisualHistoryItems.clear();
                }
                if(item != null){
                    mVisualHistoryItems.addAll(item);
                }
                mVisualHistoryRecyclerAdapter.notifyDataSetChanged();
            }
        });
    }


    @Override
    public void onItemClicked(int position) {

    }
    private void deleteNote(VisualHistoryItem item) {
        mVisualHistoryItems.remove(item);
        mVisualHistoryRecyclerAdapter.notifyDataSetChanged();

        getmNoteRepository.deleteVisualHistoryTask(item);
    }
    ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            deleteNote(mVisualHistoryItems.get(viewHolder.getAdapterPosition()));
        }
    };
}

This is the XML.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:aapt="http://schemas.android.com/aapt" android:id="@+id/rvWebHistory" android:layout_width="match_parent" android:layout_height="match_parent"/>

This is the room db

@Database(entities = {VisualHistoryItem.class}, version = 1)
public abstract class VisualHistoryDB extends RoomDatabase {
private static final String DATABASE_NAME = "visualHistory_db";
private static VisualHistoryDB instance;

public static VisualHistoryDB getInstance(final Context context) {
    if (instance == null) {
        instance = Room.databaseBuilder(
                context.getApplicationContext(),
                VisualHistoryDB.class,
                DATABASE_NAME
        ).build();
    }
    return instance;
}

public abstract VisualHistoryDao getVisualHistoryDao();
}

The room db Dao

@Dao
public interface VisualHistoryDao {

@Insert
long[] insertVisualHistory(VisualHistoryItem... visualHistoryItems);

@Query("SELECT * FROM visualHistory")
LiveData<List<VisualHistoryItem>> getVisualHistory();

@Delete
int delete(VisualHistoryItem... visualHistoryItems);
}

This is the pojo.class

@Entity(tableName = "visualHistory")
public class VisualHistoryItem implements Parcelable {

@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo(name = "title")
private String title;

@ColumnInfo(name = "url")
private String url;

public VisualHistoryItem(int id, String title, String url) {
    this.id = id;
    this.title = title;
    this.url = url;
}

@Ignore
public VisualHistoryItem() {
}

protected VisualHistoryItem(Parcel in) {
    id = in.readInt();
    title = in.readString();
    url = in.readString();
}

public static final Creator<VisualHistoryItem> CREATOR = new Creator<VisualHistoryItem>() {
    @Override
    public VisualHistoryItem createFromParcel(Parcel in) {
        return new VisualHistoryItem(in);
    }

    @Override
    public VisualHistoryItem[] newArray(int size) {
        return new VisualHistoryItem[size];
    }
};

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

public String getUrl() {
    return url;
}
public void setUrl(String url) {
    this.url = url;
}

@Override
public String toString() {
    return "VisualHistoryItem{" +
            "id=" + id +
            ", title='" + title + '\'' +
            ", url='" + url + '\'' +
            '}';
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeInt(id);
    parcel.writeString(title);
    parcel.writeString(url);
}
}

And this is the Adapter.

    public class VisualRecyclerAdapter extends RecyclerView.Adapter<VisualRecyclerAdapter.ViewHolder> {

    private ArrayList<VisualHistoryItem> mVisualHistoryItem  = new ArrayList<>();
    private OnVisualHistoryItemListener mItemListener;
    private final Context context;

    public VisualRecyclerAdapter(ArrayList<VisualHistoryItem> mVisualHistoryItem, OnVisualHistoryItemListener mItemListener, Context context) {
        this.context = context;
        this.mVisualHistoryItem = mVisualHistoryItem;
        this.mItemListener = mItemListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_visual_history, viewGroup, false);

        return new ViewHolder(view, mItemListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Resources res = viewHolder.itemView.getContext().getResources();
        viewHolder.visFragmentMain.setBackgroundColor(res.getColor(R.color.blue_text));
        viewHolder.tvPageUrl.setText(mVisualHistoryItem.get(i).getUrl());
        viewHolder.tvPageName.setText(mVisualHistoryItem.get(i).getTitle());
        Bitmap bmp = null;
        String filename = mVisualHistoryItem.get(i).getId()+".png";
        try {
            FileInputStream is = context.openFileInput(filename);
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bmp!=null) {
            BitmapDrawable ob = new BitmapDrawable(context.getResources(), bmp);
            viewHolder.ivVisualHistory.setBackground(ob);
        }
    }

    @Override
    public int getItemCount() {
        return mVisualHistoryItem.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView ivVisualHistory;
        OnVisualHistoryItemListener itemListener;
        TextView tvPageName, tvPageUrl;
        RelativeLayout visFragmentMain;
        CardView cardView;
        public ViewHolder(@NonNull View itemView, OnVisualHistoryItemListener mItemListener) {
            super(itemView);
            itemListener = mItemListener;
            ivVisualHistory = itemView.findViewById(R.id.ivVisualHistory);
            visFragmentMain = itemView.findViewById(R.id.visFragmentMain);
            tvPageName = itemView.findViewById(R.id.tvPageName);
            tvPageUrl = itemView.findViewById(R.id.tvPageUrl);
            cardView = itemView.findViewById(R.id.cardView);
        }

        @Override
        public void onClick(View v) {
            itemListener.onItemClicked(getAdapterPosition());
        }
    }

    public interface OnVisualHistoryItemListener {
        void onItemClicked(int position);
    }
}

This is what I am trying to achieve. enter image description here

This is my actual view.

enter image description here

TheCoderGuy
  • 771
  • 6
  • 24
  • 51
  • You don't need to both add and replace the fragment, and it should be in a `if(savedInstanceState == null` check. – EpicPandaForce May 10 '19 at 13:10
  • Can you be more specific where do I need to add this? – TheCoderGuy May 10 '19 at 13:13
  • What does the `R.layout.fragment_visual_history` look like? Guess you got this working already? – Erik May 17 '19 at 09:30
  • I have attached the second photo the look, but I want to look as the first photo. – TheCoderGuy May 17 '19 at 09:32
  • You have to adjust the xml. Your second picture show you must change the xml high and width for the view right!?? – Erik May 17 '19 at 09:39
  • @Erik I want for the when the acitivity is loaded to make a capture and that capture to save to db and to show un cardview and the title and url to show as in the first photo. – TheCoderGuy May 17 '19 at 09:40
  • In your `onBindViewHolder` you are only loading an image, your not setting any text like title and url to show as in the first photo – Erik May 17 '19 at 09:46
  • Yes you are right but both of `TextView` I need to call from another fragment of layout which I will edit my question and add them or If you have any idea how to do that into that layout will be great. And what can you say to me about the image to show on the left of the screen with `cardview` format. – TheCoderGuy May 17 '19 at 09:53
  • Here´s a guide for [RecyclerView and CardView](https://www.binpress.com/android-recyclerview-cardview-guide/) , also it´s a GitHub project – Erik May 17 '19 at 09:58
  • @Erik Thank you but how about in the `cardview` to show the view of url, and the background of each item depends on color of the url like in the first photo ? – TheCoderGuy May 17 '19 at 10:04
  • Dont you use Android Studio? You have to do it! Just start with getting `R.layout.fragment_visual_history` working with `TextView` and then insert the `cardView`, sprint-by-sprint you can do it – Erik May 17 '19 at 10:12
  • I am using Android Studio but I am newbie in this direction. – TheCoderGuy May 17 '19 at 10:16
  • The reason why nobody is helping you here at SO is because your question askes to much. Many developers feel they worked hard for there knowledge and they expect you to do the same – Erik May 17 '19 at 10:20
  • @Erik Thank you for the time, I am trying to achieve that but I thought a help from SO will lead me to the right answer. – TheCoderGuy May 17 '19 at 10:25
  • Well, I can say it's possible even tho I don't know it, you can see it in browsers "tabs" – Ahmed I. Elsayed May 22 '19 at 02:56
  • better yet, see the source of chromium android and see how tabs system is achieved, it's the same – Ahmed I. Elsayed May 22 '19 at 02:57

1 Answers1

4

Well you can get the favicon of the website and use the Palette class of android explained here to get the prominent color of the url and if you want to get the snapshot you can save the snapshot and save instance data before launching another activity. To take snapshot you can use takesnapshot meathod after creating util class, code as follows:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;

class Util {
    static Bitmap takeScreenShot(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;

        //Find the screen dimensions to create bitmap in the same size.
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }
}

in your activity use it to save it to png file as follows:

Bitmap topass=Util.takeScreenShot(this);
                try {
                    //Write file
                    String filename = "bitmap.png";
                    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
                    topass.compress(Bitmap.CompressFormat.PNG, 100, stream);

                    //Cleanup
                    stream.close();
                    topass.recycle();
                } catch (Exception e) {
                    e.printStackTrace();
                }

Instead of using name "bitmap.png" use your "id.png"(String.valueof(id)+".png") as you have put int "id" name in your db. You can save screenshot of every activity with its id and whenever you build the cardview of it, show the screenshot by the name of "id.png". To show screenshot, you can use

Bitmap bmp = null;
        String filename = String.valueof(getId())+".png";
        try {
            FileInputStream is = this.openFileInput(filename);
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(bmp!=null)
        {
            ConstraintLayout cl =findViewById(R.id.shopbk); //it can be your any view
            BitmapDrawable ob = new BitmapDrawable(getResources(), bmp);
            cl.setBackground(ob); /* you should use glide here to show bitmap drawable as your preview will be very small as compared to screenshot actual size*/
        }  

and as far as getting the color (you said the link is not working) is concerned you can get firstly the bitmap using

try {
        URL url = new URL("http://..somepath url../favicon.ico");
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch(IOException e) {
        System.out.println(e);
    }  

and then use palette to extract the color using

Palette.generateAsync(image, new Palette.PaletteAsyncListener() {
    public void onGenerated(Palette palette) {
        // Do something with colors...
    }
});

for this you will need to implement one dependancy

dependencies {
    ...
    compile 'com.android.support:palette-v7:21.0.+'
}  
Vanshaj Daga
  • 2,145
  • 11
  • 18
  • Thank you for the code, I will try this but at your answer you added a link for getting color but there no from URL, can you provide a code about that ? I need the taken color to be added to the `RelativeLayout` see for declaration at my `Adapter`. – TheCoderGuy May 22 '19 at 08:15
  • How can I show this snapshot in the CardView or to save at room db and in the adapter to show this snapshot. – TheCoderGuy May 25 '19 at 16:45
  • I have two question about your code. **1** What is this at the activity `this.openFileOutput` ? **2** Do I need to save the image in db or only to call as you said ? – TheCoderGuy May 25 '19 at 19:31
  • 1
    this is a reference to the context of the activity class which is running the code. No in this code you are not saving this image in db, instead you are saving it as a file with the name as of the id of the activity. You can update/delete/remove this file once the activity is updated/killed in your recent app. – Vanshaj Daga May 25 '19 at 19:48
  • I am having error at the line of code. `try { FileInputStream is = this.openFileInput(filename); bmp = BitmapFactory.decodeStream(is); is.close(); } catch (Exception e) { e.printStackTrace(); }` – TheCoderGuy May 25 '19 at 19:52
  • 1
    What exactly is the error? You are using this code in which class? – Vanshaj Daga May 25 '19 at 19:54
  • in the Adapter because there I show the data. I will edit my question and there add the code based on your answer. – TheCoderGuy May 25 '19 at 19:55
  • 1
    Use "mContext.this", i hope it will work fine in your code – Vanshaj Daga May 25 '19 at 19:59
  • @VashnajDaga `mContext.this` it is unknown, I will edit the question now. – TheCoderGuy May 25 '19 at 20:20
  • I have edited my answer see for changes at the `Adapter`, `Activity` and `Fragment` – TheCoderGuy May 25 '19 at 20:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193931/discussion-between-vanshaj-daga-and-spritzig). – Vanshaj Daga May 25 '19 at 21:03