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?
Related
I am trying to create a like and dislike button. I use Checkboxes to do so.
In the XML code I have two checkboxes one called like and the other dislike
I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.
public void onLike(View view) {
if (dislike.isChecked()) {
dislike.setChecked(false);
}
Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
}
The issue that I am having is that set setChecked(true) is not doing anything.
For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.
the way I initialized the checkbox in the main activity is as follows: -
View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
like = (CheckBox) cardViewLayout.findViewById(R.id.like);
dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);
any ideas what's going on?
ok, I've figured out the solution. Since I am using a recycler view with a custom adapter I need to bind the onClick listener via an interface.
Here is a link to another post that will show the necessary steps to implement click listeners in adapters: https://stackoverflow.com/a/49969478/11379938
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
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.
In my App i have two popupWindows. I open them both like..
aboutView = getLayoutInflater().inflate(R.layout.layout_about, null);
aboutWindow = new PopupWindow(aboutView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
aboutWindow.showAtLocation(findViewById(R.id.lay_main), Gravity.CENTER, 0, 0);
Both layouts are set to match_parent, where one of them opens just fine.
The second starts on the screens top, hidden below the Android status bar.
The reason must be within the layouts itself.
When i copy the good and just rename it..it shows up just fine.
When i change the content (add image views etc..), it's screwd up.
Is there a known reason?
EDIT:
I just figured out that i have an ScrollView in my layout..as soon as i remove that or change it to e.g. an RelativeView, thats when it slips under the status bar.
I am creating an app that uses a custom camera. The idea is that I just define a Framelayout in my xml file and them programmatically add the SurfaceView (this is the camera preview) and some other ImageViews (e.g. a shutter button, flash button...)
I managed to get the SurfaceView working but now I am a bit stuck. I want to add multiple imageview to the frame layout, but how can I get them setup correctly. I am referring to their location in the frame layout.
Can I create a relativelayout and add that to my frame layout programmatically?? If so, how do I do that?
Please, can anyone give me some tips??
Thanks!
FrameLayou can have only one direct child. You can try this
SurfaceView surface = .....;
FrameLayout frame = findViewById(R.id.frame);
RelativeLayout relativeLayout = new RelativeLayout(this);
frame.addView(relativeLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
relativeLayout.addView(surface, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// here you should add your images to relativeLayout