简体   繁体   中英

How to change the action bar color and open a file on click?

I'm working now on an app for Android and there are two things that I don't know how to do.

  1. I want to change the colour of the action bar at the request of the user, so I want to change it dynamically. I see some answers that say that I need to use:

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")));

But for some reason, it does not work for me.

  1. I've added to my app option to download a file, but I do it in an unusual way so it does not create a notification when the download is complete. I want to give my user an easy way to get to the file. Can I create a button that opens the file when clicked?

Thanks!

1) For that I would recommend you having Example here Toolbar View and set it as your Actionbar. Just add this view at the top:

<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

And then in Activity

 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
 setSupportActionBar(myToolbar);

After this you can change everything you want to the view myToolbar . Set background color etc. It will work. Using just Actionbar is old and depricated approach.

2) Yes you can, it is like opening new Activity with intent. Android has some inbuilt Intent Action Type which helps you to Open or View specific files, but for this you need to know which type of file you are going to handle.

Suppose If you have file type which categorized in document type you can use,

ACTION_OPEN_DOCUMENT with specific MIME_TYPE (Android 4.4 or Higher)

or If you going to handle some media file (Audio/Video)

you can use,

ACTION_VIEW

To identify MIME_TYPE of specific file you can use function

guessContentTypeFromName (String url) Link

Or getMimeTypeFromExtension(String extension) Link

Hope this helps :)

You can't change the color because you should call getSupportActionBar () and it's also for api >11;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.black)‌​));

or

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")​));

About your second question, we need more info and your code where you download file. You totally can access files onClick but you need to know the file type and the directory where you store the files and target api to check runtime permissions for example

I suggest you ask another question with all your code and this info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM