I want to use a way of bindings like this:
<ImageView
android:id="#+id/play"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#{play.isEnabled() ? #drawable/round_button : #drawable/round_disable_button}"
android:src="#drawable/play" />
I want that binding get updated when I disable or enable that ImageView in Java class, how can I do this?
You can't
Practically, you can create a custom view, extended from ImageView, and do this pragmatically.
Edit: you can use MVVM third party library for android like this: https://github.com/manas-chaudhari/android-mvvm
Related
In my XML layout I have some TextView with ids like slot0, slot1...slot15.
Is there any way to generate the corresponding Id dynamically in java like the following?
findViewById(R.id.*customStringForId*)
then access each of TextView using a for loop?
I am currently unable to use findViewById(R.id.*customStringForId*) because I can't find it in the XML.
Thats a bad practice for access component from your xml
You need set manual for id with findViewById for tell java class if in your xml there existing textview with id which already you set and give you access for do whatever like implement onclick event, settext, etc.
If you cant find your id, you need check if setContentView in your java point to your xml.
there are some ways to solve your problem but you should write your layout in the question to let us know how you design the layout.
But for example if you have a list of TextViews inside layout like the following:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/slot0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
<TextView
android:id="#+id/slot2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="example" />
</LinearLayout>
you can access the TextView Dynamically via the layout like the following:
public TextView getTextView(int index){
return ((LinearLayout) findViewById(R.id.layout)).getChildAt(index)
}
I am not able to use support library 26 app:autoSizeTextType attribute on textview it forces me to use AppCompatTextView class.Regular TextView doesn't recognize app prefix like official docs. When i use AppCompatTextView on widget it brings rendering issuses.Is there a way to achieve this ?
I believe this is an issue with Android's linter. Try using the tools:ignore="MissingPrefix" attribute.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="350dp"
android:layout_height="48dp"
android:maxLines="1"
app:autoSizeMaxTextSize="13sp"
app:autoSizeMinTextSize="8sp"
app:autoSizeTextType="uniform"
tools:ignore="MissingPrefix"/>
EDIT: Android has an official response into using AppCompat Views in a RemoteViews way (hint: don') https://issuetracker.google.com/issues/37071559
There is the DismissOverlayView, which you can implement to give the user an alternative way to exit the application on an android watch. This View implements something, that looks like a fullscreen FAB. Now I would like to know, how I could implement the same View with another button icon/color/behaviour. Since you can't change the DismissOverlayView which code looks btw. like this :
<android.support.wearable.view.DismissOverlayView
android:id="#+id/dismiss_overlay"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
I guess you have to implement some custom FAB, but I can't use the FAB in my watch XML either because there is an dependency missing or because it's simply not supported by the watch os. I tried following code for testing :
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"/>
Edit: For better understanding, I want the button look like this :
Edit: I just found this solution, I was not aware of that one :
https://developer.android.com/training/wearables/ui/confirm.html
But I would still be curious, if you could implement a more customisable version of this buttons.
Okay I found a solution for that problem, actually you can simply create a new layout.xml and overwrite the default code, which is the following one :
<merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/dismiss_overlay_explain"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="32dp"
android:layout_width="wrap_content"
android:padding="12dp"
style="#style/DismissOverlayText"
/>
<android.support.wearable.view.ActionPage
android:id="#+id/dismiss_overlay_button"
android:layout_height="match_parent"
android:background="#color/dismiss_overlay_bg"
android:layout_width="match_parent"
android:src="#drawable/ic_full_cancel"
android:color="#color/dismiss_close"
app:buttonRippleColor="#color/dismiss_close_pressed"
android:text="#string/dismiss_overlay_button_label"
/>
</merge>
Now you can use your newly created xml customise it and use this one instead of the original dimissOverlayView. But you should be careful with doing so, because this is not really intended from the design guidelines to do.
Im fairly new to android programming so im not sure on this.
Im trying to set a png as a background but the image is stretching when i use android:background="#drawable/bkgrnd" in my layout xml file
I found this custom class here http://www.anddev.org/viewtopic.php?p=27178#27178 but im not sure how to use it in my code.
Ive copied the code into its own class file and no errors are present.
How do I set the background to the drawable above using this class?
NB: I have abandoned this approach and gone with something much simpler. The answers below did give clues along with some other stackoverflow questions. Answer is below.
Please try this one it will help you.
android:src="#drawable/bkgrnd"
The result can be achieved using scale drawables instead
bkgrnd.xml (drawable xml file)
<?xml version="1.0" encoding="utf-8"?>`
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/bkgrnd"
android:scaleGravity="clip_horizontal"
android:scaleHeight="100%"
android:scaleWidth="100%"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
layout.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bkgrnd" />
In your XML, declare an ImageView like this
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/bkgrnd"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
/>
Is there a way I can store/retrieve arbitrary values from a view, similar to HTML5 data attributes?
This way, I can have a view call generic onClick() methods and the method can retrieve the related data.
e.g:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_germany" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_france" />
...
I would like to be able to retrieve a value from the one that got clicked.
public void setCountry(View v){
//retrieve data somehow
}
You can use the View's tag property. It is intented for that purpose.
For example:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="setCountry"
android:src="#drawable/ic_flag_germany"
android:tag="Germany" />
...
public void setCountry(View v) {
System.out.println(v.getTag());
}
Derive from ImageView, add a member variable "country" and accessor methods. Then you can reference your class in the layout file:
<com.foo.MyImageView ... />
Or use the tag property. It lets you attach an arbitrary object to a control, but there's no type safety, and no accessor methods with appropriate names. You can specify a string tag right there in the XML file.
Or use some kind of mapping from control ID to country.