I'm trying out new things (well, im also new to xml and android). This thing came up to my mind during my brain storming. Is it possible to reference an XML element attribute for a value of an element?
(sorry the english isn't quite well)
idea :
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView3"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:layout_toRightOf="#+id/textView4"
android:max="9"
android:progress="0" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView5"
android:layout_toRightOf="#+id/seekBar1"
**android:text="#id/seekBar1/progress"**
android:textAppearance="?android:attr/textAppearanceSmall" />
the highlighted part. Thanks for reading and for possible answers
Simple answer: No, unfortunately it's not.
And even if it worked, there were two additional issues:
It seems you would expect the TextView to be updated when the SeekBar progresses, which would not work even if the reference would work.
The system had to automatically convert the progress (a float) to a String.
Related
I'm developing an application which involves messing around with some Greek and math characters. I have been trying for some time to render them into my XML, but I don't know what I'm doing wrong, since I have used the unicode for the Strings that are looking bad, Html.fromHtml, strings.xml and even tried another font.
These are the letters that are not showing correctly: β, μ, σ and ω.
This is my XML for beta:
<Button
android:id="#+id/btn_beta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/special_chars_background"
android:text="#string/beta"
android:textColor="#color/black"
android:textSize="21sp" />
Where #string/beta equals to \u03B2 and it looks like a normal capital B.
Can you guys please help me? Thanks in advance.
try this : add android:textAllCaps="false" in Button property
<Button
android:id="#+id/btn_beta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/special_chars_background"
android:text="#string/beta"
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="21sp" />
in string.xml put this string
<string name="beta">β</string>
not urgent as I have a work around, however I would really like to understand whether there is a reason for a behaviour I observe when i make the changes below.
I have a simple layout with an EditText and a Button in an XML file.
It displays fine, and as the code below it works fine (triggering onClick)
<RelativeLayout android:id="#+id/group"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:id="#+id/Topic" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#+id/AddTopic"
android:layout_alignParentLeft="true" android:ems="10" />
<Button android:id="#+id/AddTopic"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:onClick="onClick" android:textSize="10dp" android:text="Add Topic" />
</RelativeLayout>
However, if I flip the objects around, as below, the button does not trigger - onClick does not run and nothing happens.
<RelativeLayout android:id="#+id/group"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/AddTopic"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:onClick="onClick" android:textSize="10dp" android:text="Add Topic" />
<EditText android:id="#+id/Topic" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#+id/AddTopic"
android:layout_alignParentLeft="true" android:ems="10" />
</RelativeLayout>
I might be missing something really obvious here, and as I say it doesn't matter in the sense it works in the first structure, but it does matter in the sense I would really like to know why. Any ideas appreciated.
Combining the information posted in the comments, the likely reason is the fact that you mistakenly use #+id/ when referring to other objects, instead of #id/. Specificly EditText contains android:layout_toLeftOf="#+id/AddTopic". The + sign should only be used when introducing new ids.
In the first example it is not harmful for the listener, since the button is declared after the EditText and #+id/ works as should, but in your second example the id of the button might be overriden when you declare the EditText.
TL;DR: in EditTextreplace android:layout_toLeftOf="#+id/AddTopic" with android:layout_toLeftOf="#id/AddTopic". Do this even if you use your "work around".
I am trying to make a listview with an on/off switch. I found this simple code but the problem is it is not working for me.
<Switch
android:id="#+id/itemListSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"/>
Yes, it displays a switch but the text (Vibrate on/off) is not. Only a circle that can be switched is displayed. I want to post a screenshot but i am not allowed to because i lack reputation. Can anyone tell me why because i tried to find answers but a simple code like the one above is working for them. If it helps, my Android phone version is 5.0.2. Thanks in advance!
First of all you should use SwitchCompat in order to make it compatible with all android versions and have the nice look of material design of the switch.
Back to your problem, you are missing an attribute for showing the text -app:showText -, here is an example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
...>
<android.support.v7.widget.SwitchCompat
android:id="#+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="25dp"
android:checked="false"
android:textOff="OFF"
android:textOn="ON"
app:showText="true"/>
</RelativeLayout>
I am an entry level software developer, and have found tons of great answers from this site, but I can't seem to find how to hide the 'box' of a checkbox in Android. I just want the check mark to show, when a user selects an option. Here are some of the most recent things I have tried.
chkSortOrder.setBackgroundResource(android.R.color.transparent);
chkSortOrder.setBackgroundResource(android.R.drawable.checkbox_off_background);
Both of these still show the box.
put the following attribute in your checkbox tag in XML.
android:button="#android:color/transparent"
It's quite late but in any case, if it helps anyone. If you wanna set custom background to RadioButton programmatically, then you can set it like this,
checkbox.setButtonDrawable(null);
checkbox.setBackgroundResource(R.drawable.your_custom_drawable);
It's 2020! and many things have changed. But if you are struggling with this like me and tried everything here (and other solutions) with no luck, then try this one too. What I've got with other solutions came to this (Emulator, API 16):
Weird behavior! Two drawables, right is my custom box and default one on the left. And then accidentally i added this:
app:buttonCompat="#null"
and the second one is gone finally!
Here is the complete definition:
<CheckBox
android:id="#+id/cb_fs_ExactSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:gravity="center_vertical"
android:padding="5dp"
android:text="#string/fs_options_exact"
android:textColor="#color/textPrimary"
android:textSize="#dimen/textSmall"
app:buttonCompat="#null"
app:drawableRightCompat="#drawable/drw_checkbox" />
You need to set both button and buttonCompat to null.
I found this error when updating from androidx.appcompat:appcompat:1.0.0 to 1.1.0.
None of the other answers were useful to me as setting button attribute to #null or #android:color/transparent doesn't work on Android API Level 20 or lower.
What I did is to change my CheckBox to ToggleButton and set textOn and textOff attributes to an empty string
<ToggleButton
...
android:background="#drawable/custom_selector"
android:textOff=""
android:textOn="" />
This way I could update to androidx appCompat 1.1.0
You can also toggle between a CheckBox and a TextView. Just make one view visible and another one hidden. This also makes sense because the CheckBox contains paddings around the check mark.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="#null"
android:checked="true"
android:gravity="center_vertical"
android:paddingStart="3dp"
android:paddingLeft="3dp"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My text"
android:textColor="#color/black"
android:textSize="12sp"
/>
</FrameLayout>
So, I have an android app to finish making for a project, the same one as I asked questions about here before but this time I'm having trouble with a completely different aspect. Since the app is about Pokémon, in one activity I have some TextViews, EditTexts and a button set up to make an IV calculator. If you're curious and don't know what "IVs" are in Pokémon, google it of course. Anyway, getting back on track...this image here I put together highlights my problem
Above the boxed line you see there is a ClassCastException thrown saying cannot cast from type TextView to EditText...I know what that means in a general sense but I cannot fathom why it is occurring here because as you can see in the relevant line of code (which i pasted into the image) the part of that line that involves casting is casting from View (return type of findViewById) to EditText. The arg R.id.EditText06 IS referring to an EditText in my activity so I don't see where the bad casting attempt is supposedly occurring. Just for a little clarification of the context of this, this is part of stuff coded into the OnClickListener of the button.
Apologies if this question seems perhaps incomplete as regards content shown about the problem but it is quite late for me (so much so that I'm going to bed upon posting this) and please do ask for me to post other stuff if you feel you need it to try to help me.
EDIT: here is my xml file for the activity in question. http://pastebin.com/g5B8d393
EDIT2: OK now this is getting worse :( My current setup is that I have a sort of dummy main activity with just a button to launch an activity. While testing I changed which one it was to launch as desired...until some time last night (and I really don't see what could've started this problem) it worked fine with my IVCalculatorActvity once I had it working fully and properly, and with the other activity. But now, it just crashes when I hit the button in the main activity...I'm just going to upload the project somewhere. (http://www.filehosting[DOT]org/file/details/429262/PokeUtility.zip)
Grrr...curse this reputation restriction on link posting >.>
I understand the reasoning but meh...
EDIT3: apologies for ssuch sudden editing but I only just saw you latest post now HalR. Testing app atm.
findViewById doesn't return an object of class View, but of whatever class that object is indicated in your XML file. Whatever you think you are describing your EditText06 as in your XML, it thinks its a TextView instead of and EditText. Double check and make sure you aren't defining the id="EditText06" in more than one spot.
If you show your xml, It would likely be easier for someone to point out the specific problem in your xml file.
You need to use the format "#+id/..." once for each label, the first time it shows up (either as a reference or as an id". Otherwise you generate multiple labels. I edited your file the way I think it needs.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".IVCalculatorActivity" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/calculate" />
<EditText
android:id="#+id/EditText01"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textViewD"
android:layout_alignBottom="#id/textViewD"
android:layout_alignLeft="#+id/EditText05"
android:ems="10" />
<EditText
android:id="#+id/EditText03"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView5"
android:layout_alignBottom="#id/textView5"
android:layout_alignLeft="#+id/EditText02"
android:ems="10" />
<EditText
android:id="#+id/EditText04"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#id/textView6"
android:layout_alignLeft="#+id/EditText03"
android:ems="10" />
<TextView
android:id="#+id/textViewA"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewC"
android:layout_alignParentTop="true"
android:text="#string/IVcalcHeader" />
<TextView
android:id="#id/textViewC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textViewD"
android:layout_below="#id/textViewA"
android:layout_marginTop="26dp"
android:text="Stat" />
<EditText
android:id="#id/EditText05"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/textViewC"
android:layout_alignBottom="#id/textViewC"
android:layout_alignRight="#id/textViewA"
android:ems="10" />
<TextView
android:id="#id/textViewD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewB"
android:layout_below="#id/EditText05"
android:layout_marginTop="27dp"
android:text="Stat value" />
<EditText
android:id="#+id/EditText02"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/textViewB"
android:layout_alignBottom="#id/textViewB"
android:layout_alignLeft="#id/EditText01"
android:ems="10" />
<TextView
android:id="#id/textViewB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView5"
android:layout_below="#id/EditText01"
android:layout_marginTop="28dp"
android:text="Level" />
<TextView
android:id="#id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView6"
android:layout_centerVertical="true"
android:text="EV count" />
<TextView
android:id="#id/textView6"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignRight="#id/button3"
android:layout_below="#+id/EditText03"
android:layout_marginTop="29dp"
android:text="Base Stat" />
<TextView
android:id="#+id/textViewE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView6"
android:layout_below="#id/textView6"
android:layout_marginTop="40dp"
android:text="Nature" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/EditText04"
android:layout_alignTop="#id/button3"
android:text=" " />
<EditText
android:id="#+id/EditText06"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/textViewE"
android:layout_alignBottom="#id/textViewE"
android:layout_alignLeft="#id/textView7"
android:ems="10" />
</RelativeLayout>