Android - Set Application Background - java

I am trying to set my applications background to a user chosen image, but I am having some trouble. Is it possible for someone to give me an example of how to do this? I can set the image ok from my resources, but when I try to use an image on the phone I can't seem to make it work.

Assuming you have created a method to let the user choose the path to the image, use this:
// Variable with the path to the background
String bg_path = "/sdcard/bg/background.png" // <-- This path can be whatever you like
//Change background of Activity
getWindow().setBackgroundDrawable(Drawable.createFromPath(bg_path));
Oh Don't forget to set you Layout background color to transparent in the XML file or you won't see the image. (this is valid to anything that fills parent window like a listview, for instance)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout="#+id/m_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android.background="Color.TRANSPARENT"
>

Related

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.
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.
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

Dim Complete Background When a Fragment is opened in Android

How to dim complete background when a fragment is opened likewise when a DialogFragment is created.
Rightnow I am able to dim the background of only the app window using a view with match_parent as height and width. But using it does not dim the background of the status bar which happens with the DialogFragment.
I want to dim complete background including apps window and the status bar when the fragment is created or opened.
Right now this happens with the DialogFragment. But I have to use only Fragment.
That's what I did
I took a relative layout with height and width match_parent, apart from my fragment's main layout
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background_transition"/>
This is my background_transition file
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="00FFFFFF" />
<item android:drawable="#B3FFFFFF" />
</transition>
Then in order to dim the background, create TransitionDrawable object like this in your fragment
private TransitionDrawable transition;
and then call
transition.startTransition(300);
to dim the background, and
transition.reverseTransition(300);
to go back to normal background.
Hope that helps.
I tried Nuzha's version and my app crashed as well.
I did find two possible solutions.
1) If you use the following code it will make the back-fragment whiter, not dimmed:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (Build.VERSION.SDK_INT >= 23) {
view.alpha = 0.2F
}
Note:
Depending on the value you set, the closer the Float is to 0, the more transparent-white the background will be (only works with 23+)
2) If you would like the back-fragment to be dimmed (dark)
First, you can create a color in your resources folder and make it a bit transparent. I for one used the following one for a darker effect: #7E000000
Next, in the onViewCreated() method, create a variable of the color and add it as a ColorDrawable to the view's foreground.
if (Build.VERSION.SDK_INT >= 23) {
val color: Int = R.color.black_transparent
view.foreground = ColorDrawable(resources.getColor(color, resources.newTheme()))
Note
Make sure to disable any buttons that might be visible in the back-fragment, otherwise, if the user presses it, the app will crash
buttonRegisterEvent.isEnabled = false
As a final step, if you want the user to navigate back to the former fragment, make sure to remove the foreground color and enable any buttons that you might have disabled.
Try this:
fragmentParentLayout.getForeground().setAlpha(215);

How to change Exomedia's default video control bar's color?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:EMVideoView="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/MUVCastleRock">
<com.devbrackets.android.exomedia.ui.widget.EMVideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
EMVideoView:useDefaultControls="true" />
    
How do I change the color of the default controls (where you would see the play button)? I don't want to change the background behind the video, but instead the play bar. This is because my background is grey and so I want to change the color so that the bar is more visible.
EDIT:
#Shank suggested that I replace the images of the buttons in /exomedia/ui/widget/VideoControls.java
Although, I wasn't trying to change the images of these buttons, analyzing this class lead to my answer.
There are several functions within this class to change the settings of the default video controls.
You can set a title, description, subtitle, change out the button images (as Shank suggested), and solve my particular problem by changing the characteristics of the containers that contain the video controls (named controlsContainer).
The default container is initialized in retrieveViews() by the line:
controlsContainer = (ViewGroup) findViewById(R.id.exomedia_controls_interactive_container);
Using this, I simply called this reference from my EMVideoView and changed the color as appropriate:
emVideoView = (EMVideoView) myView.findViewById(R.id.video_view);
ViewGroup textContainer = (ViewGroup) emVideoView.findViewById(R.id.exomedia_controls_interactive_container);
textContainer.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.myColor));
Other useful methods that I discovered were as such:
emVideoView.getVideoControls().setTitle("title");
emVideoView.getVideoControls().setDescription("description");
emVideoView.getVideoControls().setSubTitle("sub");
emVideoView.getVideoControls().setPlayPauseImages(R.drawable.logo,R.mipmap.ic_launcher);

android change imageview programmatically

I am making an android app where the same imageview is displayed every time the user enters a value. but I want the imageview to be changed every time the user enters a value. I wrote a code to do this but the problem is after the first image is displayed I face black screen and it takes me to the first page in the app, it doesn't crash just shows black screen.
here is the imageview code in xml file:
<ImageView
android:id="#+id/ImageSuccess"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:scaleType="centerInside"
android:src="#drawable/image1" />
and here where I changed the imageview in java file:
ImageView myImage= (ImageView) findViewById(R.id.ImageSuccess);
if(userVlue.equals("SCHOOL")){
myImage.setImageResource(R.drawable.image1);
}
else if(userVlue.equals("CAR")){
myImage.setImageResource(R.drawable.image2);
}
else if(userVlue.equals("TOY")){
myImage.setImageResource(R.drawable.image3);
}
One of myImage or userVlue is not set properly and probably is null.
Debug and share logs. Your partcular activity is getting distroyed probably because of nullpointexception hence returning to main activity in backtrack.
I tried with a sample app and your logic is working perfectly.
You are handling user action on some UI element, when you do findViewById, cotext is that UI element, so this method searches ImageSuccess in children of it. Try giving page layout as rootlayout and do rootlayout.findViewById("ImageSuccess");
I found the solution finally, the black screen appeared because the images were only in the drawable file, so I needed to add the images in all drawbridge folders such as drawable-ldp, drawable-ldpi etc...., so that the app can work in different phone sizes.

How to change widget background using RemoteViews?

<RelativeLayout
android:id="#+id/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/my_background"
android:orientation="vertical" >
And now I want to change this background to "#drawable/your_bck" using RemoteViews. I tried something like this
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.yout_bck);
remoteViews.setImageViewBitmap(R.id.widget, bitmap);
But i then it shows "Widget failed to load"
It must be as a background because i need to set a text on the center of image :)
You're calling setImageViewBitmap on a RelativeLayout. A RelativeLayout is not an ImageView. You could put an ImageView in there instead that fills the RelativeLayout and call it on that.

Categories

Resources