Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a recents screen like in android for the activities of my app

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

like image 616
Abedin.Zhuniqi Avatar asked May 10 '19 13:05

Abedin.Zhuniqi


People also ask

How do I view recent screens on Android?

You'll be able to see your recent apps with a single tap. From the Home screen, tap the Recents icon to the left of the Home button. All of your active or opened apps will be listed. If you've customized your Navigation bar, Recents may be located on the right, unless you're using full screen gestures.

What is recents screen?

The Recents screen (also referred to as the Overview screen, recent task list, or recent apps) is a system-level UI that lists recently accessed activities and tasks. The user can navigate through the list and select a task to resume, or the user can remove a task from the list by swiping it away.


1 Answers

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.+'
}  
like image 79
Vanshaj Daga Avatar answered Oct 17 '22 10:10

Vanshaj Daga