How to change background of the app? - java

Is there a way to change background directly in the app? (!not homescreen wallpaper)

You need to specify the layout that its background will be changed. I guess you have a LinearLayout, so add an id to the layout (if it is not exist):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=#+id/parentLayout>
Then you can change the background in Java with:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.parentLayout);
linearLayout.setBackgroundResource(R.drawable.new_background);
Hope it helps you!

i have a link for you is easy to change background app.
Try something like that https://www.youtube.com/watch?v=mDKuWIlSNJE

Related

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);

Facebook LikeView change button size

I am using a LikeView in my android application and want to change the button size. Changing the view's size using layout parameters or the width/height at addView() does not help.
I'm using libGDX and have never worked with XML + libGDX, so a way to do it with code only would be nice!
Does anyone know how to do this? Currently I use the following code to create my LikeView:
likeButton = new LikeView(application);
likeButton.setLikeViewStyle(LikeView.Style.BUTTON);
likeButton.setX(x);
likeButton.setY(y);
likeButton.setObjectId(LIKE_URL);
likeButton.setVisibility(View.GONE);
application.layout.addView(likeButton,width,height);
likeButton.invalidate();
application.layout is a RelativeLayout
Try to create your LikeView from XML (or inflate this layout at runtime):
<com.facebook.widget.LikeView
android:id="#+id/button_like"
android:layout_height="31dp"
android:layout_width="wrap_content" />
I finally fixed it using setScale on the view. It's not the most elegant solution, but it does the job.
likeButton.setScaleX(1.8f);
likeButton.setScaleY(1.8f);

How can I set an setOnClickPendingIntent for my entire widget layout?

I'd like to set an setOnClickPendingIntent for my entire widget layout but I haven't found a a way to do this. I guess it's something very trivial but I've overlooked it. Currently I'm setting an intent for each of my views in the layout and this makes my code very messy. Here's what I'm doing currently:
remView = new RemoteViews(ctxContext.getPackageName(), R.layout.initial);
Intent ittRetry = new Intent(ctxContext, SettingsActivity.class);
ittRetry.setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
ittRetry.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, intInstance);
PendingIntent pitRetry = PendingIntent.getBroadcast(ctxContext, 0, ittRetry, PendingIntent.FLAG_UPDATE_CURRENT);
remView.setOnClickPendingIntent(R.id.widget_rows, pitRetry);
remView.setOnClickPendingIntent(R.id.widget_message, pitRetry);
wigManager.updateAppWidget(intInstance, remView);
Currently I'm setting an intent for each of my views in the layout and this makes my code very messy
However, that is your only option. You have to call setOnClickPendingIntent() for every widget for which you want to get control in a click.
You are welcome to use a loop, putting your widget IDs in an array and iterating over that array, calling setOnClickPendingIntent() in each pass.
You can create a transparent button on top of all your widget views at the end of your layout xml that covers the whole widget (match_parent is important!):
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
And reference it in your widget code:
[...]
views.setOnClickPendingIntent(R.id.button, pendingIntent);
[...]
My solution for this issue (years after the original question) is to set an id for the master layout. So my layout has a RelativeLayout and this has an id, which I can use for the pending intent.

How to set background color of an Activity to white programmatically?

How can I set the background color of an Activity to white programatically?
Add this single line in your activity, after setContentView() call
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
Get a handle to the root layout used, then set the background color on that. The root layout is whatever you called setContentView with.
setContentView(R.layout.main);
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView();
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
I prefer coloring by theme
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:id="#+id/myScreen"
</LinearLayout>
In other words, "android:background" is the tag in the XML you want to change.
If you need to dynamically update the background value, see the following:
Exercise: Change background color, by SeekBar
In your onCreate() method:
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
Also you need to add to values folder a new XML file called color.xml and Assign there a new color property:
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>
Note that you can name the color.xml any name you want but you refer to it by code as R.color.yourId.
EDIT
Because getResources().getColor() is deprecated, use getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
instead.
You can use this to call predefined android colours:
element.setBackgroundColor(android.R.color.red);
If you want to use one of your own custom colours, you can add your custom colour to strings.xml and then use the below to call it.
element.setBackgroundColor(R.color.mycolour);
However if you want to set the colour in your layout.xml you can modify and add the below to any element that accepts it.
android:background="#FFFFFF"
Button btn;
View root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
root =findViewById(R.id.activity_main).getRootView();
root.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
});
}
To get the root view defined in your xml file, without action bar, you can use this:
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
So, to change color to white:
root.setBackgroundResource(Color.WHITE);
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
worked for me. thank you.
final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);
for activity
findViewById(android.R.id.content).setBackgroundColor(color)
The best method right now is of course
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
Please be aware though, if you have anything set as the background color in Designer, it will overwrite anything you try to set in your code.

Categories

Resources