LayoutInflater with ContextThemeWrapper not always themed - java

I'm trying to inflate a layout with theme different from app default, and doing it like this:
final View view = context.getLayoutInflater().cloneInContext(new ContextThemeWrapper(context, getThemeForView())).inflate(R.layout.some_layout, root, false);
First time it's done view doesn't have theme I want, but main app theme. When view is recreated after few minutes, it has correct theme. Why is theme different when view is first created?
EDIT:
I can't set android:theme because I get theme dinamically.

I first created view in Activity.onCreate. With some testing I found out that if I create view in Activity.onWindowFocusChanged(boolean hasFocus) when hasFocus == true works fine. I'm still not sure why.

You can add
android:theme="#style/some_theme"
to the activity in the manifest file.

Related

TabLayout Fragment Issue

Current layout
The above is the app layout in Android app that I am working on. There is an activity, which has a BottomNavigationView. The navigation is managed using:
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Now, on this fragment, I have a TabLayout and a ViewPager as you can see in the image. The ViewPager listens to the TabLayout selector and it can be changed by either sliding the ViewPager or clicking on the tab. Works fine..
Once a tab is selected, I am loading another Fragment that has a RecyclerView on it. The contents in the RecyclerView is coming from a URL so its being updated on Post() as below:
CompletableFuture.runAsync(this::prepare);
recyclerView.post(() ->
{
adapter.list = newList;
adapter.notifyDataSetChanged();
progress.setVisibility(View.INVISIBLE);
});
Now the issue that I am facing:
Everything works fine for the first time when the Menu 1 in the layout image is clicked. The recycler view is able to call the service and load the content. Now if I switch the navigation to Menu 2 or any other and try to come back to Menu 1, nothing progresses from the ViewPager / TabLayout. Up to the TabLayout I can see Logs, and after that nothing. Basically setting the currentItem to the TabLayout is called but its not calling the create of the next Fragment and hence its not loading the RecyclerView again. The ViewPager is using FragmentStatePagerAdapter.
TabViewerAdapter extends FragmentStatePagerAdapter
The adapter is initialised using ChildFragmentAdapter as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 0, tabs.getTabCount());
I know behaviour "0" is deprecated so tried with "1" as well as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 1, tabs.getTabCount());
Tried invalidating views, notifying adapter and many other.. No use.
I also noticed that when I switch app (while its running by tapping the phone square button to go to another app) and come back (resume) then its loading the recycler view again.
This means, I am missing something and tried the onResume method. But didn't work.
Am I doing something stupid or against design patterns or conventions? No idea what to do..
Please help, thank you.
I resolved this issue by doing the things below:
Used a SharedViewModel to handle the list
Used ListAdapter for RecyclerView
All good now.

Set card view's background using Remote View

I use the following codes to set background of view such as TextView, LinearLayout, and it works perfectly.
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setInt(R.id.textview, "setBackgroundColor", Color.parseColor(widgetTextColor.toString()));
But I try to set the background of CardView, it didn't works. I have tried
views.setInt(R.id.cardview, "setCardBackgroundColor", Color.parseColor(widgetTextColor.toString()));
Any workarounds?
Based on the developer documentation in this link, u cannot use cardview for App widgets
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
and a discussion about this already been over here What views can i use in an appWidget?

How to choose a view group in my android app

I am developing an app and have successfully added a floating action button using the library shown here. The floating button displays well but when i navigate to another fragment through the navigation drawer the button still displays instead i want the button to only display in the activity i created it. I checked for those that had similar issues online and i saw comments...saying that i have to set the View by modifying this line of code found within the method show below.
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
Please can tell me how to achieve this, thanks in advance.
Library Method
public FloatingActionButton create() {
final FloatingActionButton button = new FloatingActionButton(activity);
button.setFloatingActionButtonColor(this.color);
button.setFloatingActionButtonDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
return button;
}
if you can access the FAB view using findViewById you can simply do:
button.setVisibility(VISIBILITY.GONE);
to hide it, that way you can set visibility back to visible when you go back to that activity.
or if you can access view called 'root' (or can access button via getActivity().findViewById for Fragments)
root.removeView(button);
Set the FloatingActionButton to be invisible by default in the XML file:
<ImageButton android:id="#+id/button" android:visibility="invisible"/>
and make it visible by using the following code in the required activity class:
button.setVisibility(View.VISIBLE);

Can't access to a spinner in Java

I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.

Android: Custom Gallery setSelection() issues

I have an extended BaseAdapter that has LinearLayout children (an ImageView and a TextView in each) hooked up to a custom Gallery.
When I first launch my Activity, I want to call setSelection(position) to cause the ImageView to change its selector to the "selected" image. This works once I fling the Gallery, on subsequent selected children, but not the very first time the app is launched.
My selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="#drawable/home_image_select" />
<item android:state_selected="false"
android:drawable="#drawable/home_image" />
</selector>
My first guess was to call notifyDataSetChanged() on the adapter after calling setSelection(), which I attempted to do like this:
((CustomAdapter) gallery.getAdapter()).notifyDataSetChanged();
That didn't do anything. I also tried overriding the setSelection() of the Gallery class to do this:
View v = this.getAdapter().getView(position, null, this);
((ImageView) v.findViewById(R.id.gallery_image)).setSelected(true);
That doesn't work either. Anything I'm missing or could try?
I found the solution to my own problem by overwriting the setSelection() of Gallery (it worked after all).
#Override
public void setSelection(int position) {
super.setSelection(position);
View v = this.getAdapter().getView(position, null, this);
v.setFocusable(true);
v.requestFocus();
}
I think you should not call notifyDataSetChanged(), selection state will be cleared when underlying dataset changed.
Just call setSelection(position), it works in my app.

Categories

Resources