Android: Tab Indicator line disappears when tab background is changed - android

My question is more or less similar to this question, but the response approved as the answer for the same doesn't solve my issue.
I'm implementing tab structures using the FragmentTabHost class. When the tab background color is not explicitly specified, the tab indicator line seems to appear as Android provides. But when I programatically change the tab's background color, the tab indicators disappear (hidden).
Currently, I assign the tab background color this way :
private void customizeTabbs() {
for (int i = 0; i < mTabHost.getTabWidget().getTabCount(); i++) {
View tabChild= mTabHost.getTabWidget().getChildAt(i);
..
..
tabChild.setBackgroundResource(someColorResource);
}
}
I also tried to change the background color in the XML way by adding to styles.xml , following this as:
<item name="tabBackground">#color/someColor</item>
<item name="tabIndicatorColor">?#color/someOtherColor</item>
The solution suggested for one similar question also did not help me.

Related

Ripple effect of TextView gets stuck on long click, when selection is used

I recently refactored my App to use the jetpack navigation. On my MainActivity, I had a ViewPager containing 3 fragments with 1 recyclerview each. I therefore moved the ViewPager from the Activity to a new Fragment. Everything works when it comes to the functionality itself, but a new issue arose which really bugs me: When I select an item on the recyclerview with a long press, the ripple effect gets stuck and stays there, as if I'm still pressing the item, which I don't. Here is a picture of a selected item. As it can be seen, the ripple effect stays active on the TextView.
When I unselect the Item, the ripple effect also stays active:
The selection itself is handled with the android SelectionTracker. The TextView has the background set to background="?android:attr/selectableItemBackground". The whole ViewItem has the background set to android:background="#drawable/bg_multi_selection, whereas the bg_multi_selection file looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/secondaryLightColor" android:state_activated="true" />
<item android:drawable="#android:color/transparent" />
</selector>
What could be the issue of that? This is especially weird, as the problem did not exist, when the ViewPager with its fragments was contained inside the Activity, and not a Fragment. Any suggestions on how to fix this?
I found a solution which works for me. So for anyone interested, I did the following:
In the recyclerviewadapter in the onBindViewHolder() method I check if the element is selected and additionally set the background color and background resource of the TextView programmatically.
if (elementIsSelected) {
viewHolder.viewBinding.itemTextView.setBackgroundColor(context.resources.getColor(R.color.transparent, context.theme))
} else {
val outValue = TypedValue()
context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)
viewHolder.viewBinding.itemTextView.setBackgroundResource(outValue.resourceId)
}
whereas I definied the color to be transparent, i.e. #00000000
It's odd that this problem occurs only on some devices, so I'm not sure if the "real" problem lies somewhere else, but at least that's how I fixed it. Hope that might be helpful for someone else.

How can i change the StatusBar color or set it to transparent on Android, using NativeScript with Angular?

I'm using NativeScript with Angular, and I can't seem to change the StatusBar color in my project or set it to transparent (any of these would be fine). Instead, it is not totally transparent, but is translucent black, so the background scrolls behind it but it is darkened by the StatusBar. I would like to set it to transparent or to change the color (to the same one as the page background)
What I've tried:
Changing the "ns_primaryDark" and "ns_primary" colors in App_Resources/Android/src/main/res/values/colors.xml (works on launch screen if i set the TranslucentStatus" to false;
Setting <item name="android:windowTranslucentStatus"></item> in <style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar"> in App_Resources/Android/src/main/res/values/styles.xml doesn't make any difference, despite working fine on <style name="LaunchScreenThemeBase" parent="Theme.AppCompat.Light.NoActionBar"> (the launch screen);
Setting <item name="android:windowLightStatusBar">true</item> changes the text color to black and works on both the launch screen and the main app;
Using the code below in any component's constructor doesn't change anything in my app, but it worked on the other project i tried (details below), setting the color to black:
let window = app.android.startActivity.getWindow();
window.setStatusBarColor(new Color("black").android);
One thing i also tried was doing these steps on another project i had downloaded (https://github.com/NativeScript/nativescript-ui-samples/tree/master/chart this one, to be exact) and it worked, so i thought the template i used in my application might be "overlaying" any settings for the StatusBar.
I then tried to make a new project with the same template as mine and i figured out that it didn't work either. Is the template my problem? If so, is there any way to get around it?
The template i used is this one: https://github.com/NativeScript/nativescript-app-templates/tree/master/packages/template-tab-navigation-ng (also works with "tns create my-app-name --template tns-template-tab-navigation-ng").
Big thanks in advance.
Add the following code to your main.ts, that should do the job.
import * as application from "tns-core-modules/application";
declare var android;
application.android.on(application.AndroidApplication.activityCreatedEvent, (event) => {
const activity = event.activity;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
activity.getWindow().setStatusBarColor(android.graphics.Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
const parent = activity.findViewById(android.R.id.content);
for (let i = 0; i < parent.getChildCount(); i++) {
const childView = parent.getChildAt(i);
if (childView instanceof android.view.ViewGroup) {
childView.setFitsSystemWindows(true);
childView.setClipToPadding(true);
}
}
});
It's pretty much as in the library. If you like to use the library, you will have to include it in your app grad and access the StatusBarUtil class.
You can use this library to transparent your status bar Library
compile 'com.jaeger.statusbarutil:library:1.4.0'
To make any specific activity to transparent you can simply use this
StatusBarUtil.setTransparent(Activity activity)
It will look like this

Set color tab host indicator

How do you set the color of the tab host indicator color I want to change the light blue default color to lets says RED.
And I need to this programatically as I am making the tabs programatically.
I did some research and looked at this example but it does not work for me. With the progamtic approach.
TabWidget current tab bottom line color
Thanks
You can do this programmatically, even change the color as you want, by following the solution in the linked question you mention, plus adding a ColorFilter to adjust the color.
So:
Create the appropriate drawable. The easiest way, as mentioned in one of the answers, is using https://jgilfelt.github.io/android-actionbarstylegenerator/
Place into your project the tab_indicator_ab_example.xml (in drawable) plus the 6 associated png files (tab_*.png) for each drawable density.
After creating the tabs, use the code that iterates over the TabWidget child views to set their background, however:
Instead of setting the drawable as-is, use a color filter to change its color to the desired one.
Instead of this code:
for(int i = 0; i < widget.getChildCount(); i++) {
... /* same as before */
v.setBackgroundResource(R.drawable.your_tab_selector_drawable);
}
write something like this:
for(int i = 0; i < widget.getChildCount(); i++) {
... /* same as before */
Drawable d = getResources().getDrawable(R.drawable.tab_indicator_ab_example);
d.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN);
v.setBackgroundDrawable(d);
}

MPAndroidChart white Background

I am using the MPAndroidChart library. I am setting the multi line chart with data using MPAndroidChart. It's working great but the background is coming white. This is what i am doing
nhChart = new LineChart(getActivity());
nhChart.setDescription("Number of Hits View");
nhChart = (LineChart) hitsView.findViewById(R.id.line_chart_number_of_hits);
//int color = Color.parseColor("#80101010");
nhChart.setBackgroundColor(Color.parseColor("#80101010"));
//nhChart.setBackgroundResource(R.drawable.background_portrate);
//nhChart.setBackground(getResources().getDrawable(R.drawable.background_portrate));
nhChart.setStartAtZero(true);
nhChart.setDrawBorder(true);
nhChart.setNoDataTextDescription("No Data available for Charts");
nhChart.setDrawYValues(false);
nhChart.setDrawBorder(true);
nhChart.setScaleEnabled(true);
nhChart.setHighlightEnabled(false);
nhChart.setTouchEnabled(true);
//nhChart.setGridColor(Color.WHITE & 0x70FFFFFF);
//nhChart.setDragScaleEnabled(true);
nhChart.setPinchZoom(true);
setData(valueDate.size(),10000);
nhChart.animateX(2500);
Legend l = nhChart.getLegend();
l.setForm(LegendForm.CIRCLE);
l.setFormSize(6f);
l.setTextColor(Color.WHITE);
YLabels y = nhChart.getYLabels();
y.setTextColor(Color.WHITE);
y.setLabelCount(6);
XLabels x1 = nhChart.getXLabels();
x1.setCenterXLabelText(true);
x1.setPosition(XLabelPosition.BOTTOM);
x1.setTextColor(Color.WHITE);
I am plotting line graphs in post execute method of AsyncTask in a fragment viewpager. Other fragment showing the graph shows the same white background. I tried setting a color for the background but nothing works. I also left it blank, but its still showing me the white background. I also updated the latest Jar but its not works. Please help.
Here is the image how it looks
You want this for transparent:
chart.setDrawGridBackground(false);
And this for transparent bar:
chart.setDrawBarShadow(false);
Well by default, the backgroud of the chart is transparent which means that it will have whatever color you set for the view/layout below the chart.
If you want to change the background (color, or maybe drawable), you can do that in the following ways:
in .xml (android:background="...")
--> set the background color of the chart in xml or set the backgroud color of the layout the chart is in.
by calling setBackgroundColor(...) or setBackgroundResource(...)
I guess you want your background to be some kind of black?
What happens if you call:
chart.setBackgroundColor(Color.BLACK)?
Does setting the background-color work then? Also check the example code in the github repo here. There are some cases in the example app that change the background color.
You can do this with:
chart.setBackgroundColor(getResources().getColor(R.color.transparent));
Please define first:
<color name="transparent">#00000000</color>

Overlapping views crossing boundaries (Fake actionbar)

I am trying to create a custom title bar strictly for Android 2.1 that somewhat emulates the ActionBar found in Android 3.0 and up.
So far I've had fairly decent luck, but I cannot find an easy way to make the drop-down lists that can appear below the icon / buttons in the ActionBar.
I would like these drop-down lists to be able to cross the boundary between the title layout and the content layout without being clipped / cut off. I would also like the drop-down lists to be positioned relatively below the icons / buttons in the ActionBar. I've seen advice to use FrameLayouts and RelativeLayouts already but these did not seem to solve my particular problem (unless I'm looking at it wrong). I keep thinking it must be possible since I believe the newer versions of Android have support for exactly this type of behavior.
Here is a screenshot of what I am trying to accomplish with some notes.
http://img217.imageshack.us/img217/3279/67343249.png
Please help. Thanks!!!
I think best way to do that is to create a linear layout and add your drop-down list item into that linear layout. Then add linear layout on button click and display it just below to the button by using relative layout layoutparams.
You need to hold your content with a FrameLayout. A FrameLayout can hold more than one child, and it will allow them to overlap on each other.
A possible layout xml could look like:
<FrameLayout>
<LinearLayout android:margin_top="30dip">...</LinearLayout>
<CustomActionBar />
</FrameLayout>
The 30dip is just the height of your ActionBar, you might want to define it in values. And the LienarLayout you can replace with anything which is just for holding the other content in your application.
PS. I am not sure if FrameLayout renders first child first or last. In case you found the linearlayout stuff overlaps the customactionbar, swap their position.
=== Edit ===
To make the code much cleaner, one can do in this way:
<CustomActionBar>
<LinearLayout/>
</CustomActionBar>
Which replaced the custom action bar to be the "root" element of any xml layout that utilized the action bar. When doing the custom action bar, you need to extend it from FrameLayout, and assign the child's margin accordingly. Which the idea is much the same as the one I presented above.
After a few days of research I found the best way to implement this is to use a custom dialog box with a custom theme that contains a listView. When the user clicks on the button the dialog should be displayed directly below the button. This provides a good user experience and also means you don't have to completely change your basic layout structure to accomodate this functionality. (This is basically the same approach that ActionBar Sherlocke uses for their backwards compatibility menus)
Here is a code snippet of what I'm describing (this is written in C# using Mono for Android but it should be easy to translate to Java):
Dialog dlgHome = new Dialog(this, Resource.Style.ActionBarMenu);
dlgHome.SetCanceledOnTouchOutside(true);
dlgHome.SetContentView(Resource.Layout.ActionBarMenu);
ListView lsvHome = (ListView)dlgHome.FindViewById(Resource.Id.lsvHome);
lsvHome.Adapter = new ArrayAdapter<string>(this, Resource.Layout.ActionBarMenuItem, new string[] { "Add Activity", "Edit Log", "Program Details" });
Rect r = new Rect();
btnHome.GetLocalVisibleRect(r);
int x = r.Left + (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 5, Resources.DisplayMetrics);
int y = r.Bottom;
WindowManagerLayoutParams param = dlgHome.Window.Attributes;
param.Gravity = (int)(GravityFlags.Top | GravityFlags.Left);
param.X = x;
param.Y = y;
dlgHome.Window.Attributes = param;
Also, here is the style xml (goes in style.xml)
<style name="ActionBarMenu" parent="android:Theme.Dialog">
<item name="android:windowBackground">#color/actionbar_menu_bg</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>

Categories

Resources