I have a strange behavior with the use of singleLine=true and ellipsize=start with a Button.
First of all, the declaration of my button :
<Button
android:id="#+id/enterDeparture"
android:layout_width="175dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="#drawable/field_button"
android:text="#string/research_enterDeparture"
android:textColor="#drawable/field_button_textcolor"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:ellipsize="start"
android:singleLine="true" />
With this declaration, no text is displayed inside the button. But if I write Log.d(TAG, "the text is : " + findViewById(R.id.enterDeparture));, the LogCat gives me the correct value...
I tried to set the text programatically, either in the onCreateView() and in the onResume() methods : same behavior. But if I set the text really later of if I put an AlertDialog over my screen, the content come back immediatly...
To finish, if I remove the two lines android:ellipsize="start" and android:singleLine="true", everything is normal : my text is displayed in the first time.
EDIT
I tried to remove the singleLine=true line : the initial content is actually displayed but the ellipsize behavior doesn't work anymore...
So I tried with maxLines=1 : the initial content is displayed, but the "..." are not shown anymore (the content is just truncated).
Try changing the height value:
android:layout_height="wrap_content"
You could be elipsising your text to ... and because the button padding it could be being hidden.
Also you want to try this instead:
Log.d(TAG, "the text is : " + ((Button)findViewById(R.id.enterDeparture)).getText())
Related
When adding a field for entering a number(Number widget), the error "No speakable text present at Android Studio" takes off
enter image description here
content_main.xml: enter image description here
activity_main.xml: enter image description here
The problem is you are missing content labeling for the view, you should add content description so the user could simply understand what data he should enter into the view
for example, if you want the user to enter the number of cookies he wants you should add a content description as seen below:
android:contentDescription="Enter How Much Cookies You Want"
You should also add an android:hint so the user would have it in front of them an example of the data you want inputted for example:
android:hint="e.g 5"
So your views XML code should look as follows
<EditText
android:id="#+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:minHeight="48dp"
android:contentDescription="Enter How Much Cookies You Want"
android:hint="e.g 8" />
The solution is simple you just need to add the text into the hint part.
search hint in search-bar ant type something in hint block.
and hit Enter.enter image description here
The problem is missing constraints. Any view you add in Constraint layout, you must set the margins otherwise you will get those errors and even if your app managed to run, your edit text will not be place properly.
Add this to your editText;
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Let me know if it worked.
Remember you can twick this to your desired position.
I have a button with centralized icon+text, and I need to change the app:icon and android:text properties when some event occurred. I know that there is setText() method to change the text, but is there a way to change the icon?
XML:
<Button
android:id="#+id/bottom_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="#dimen/default_margin"
android:layout_marginVertical="#dimen/bottom_bar_content_vertical_margin"
android:backgroundTint="#color/light_green"
android:text="#string/next"
android:textAllCaps="false"
app:icon="#drawable/ic_baseline_next_plan_24"
app:iconGravity="textStart" />
This is a function that is called after appropriate event, and I need to change icon in that function. icon and text are ids of desirable drawable and string:
private void setBottomButton(int icon, int text) {
button.setText(getString(text));
}
You can use the methods:
setIcon
setIconResource
Example:
button.setIcon(drawable)
button.setIconResource(iconId)
Here the doc.
If you check the docs, you'll see the code equivalent for each XML attribute.
Check this link : enter link description here
Searching for drawableLeft shows:
android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)
I am attempting to make a textview clickable by adding an email autolink attribute to it with android:autoLink="email"but as soon as I add it, the text just disappears. What's interesting is that other combinations of text appear just fine. When typing in an email, as soon as I type the "o" in sample#gmail.com, the text just disappears. Am I doing something wrong?
My TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:text="sample#gmail.com"
android:clickable="true"
android:autoLink="email"
android:textColor="#54ACE6"
android:textSize="13dp" />
Try to set android:textColorLink="#color/blue" or any other color different than your background.
I'm working on a fragment that contains three toggle buttons at the moment. In the fragments layout I have:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/corkRdToggle"
android:layout_below="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_marginRight="15dp"
android:textSize="12sp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/collegeStToggle"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textSize="12sp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/allRoomsToggle"
android:layout_below="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:layout_marginLeft="15dp"
android:textSize="12sp" />
In my fragments onViewCreated() method I'm finding the toggle buttons by id, setting both the 'on' and 'off' text, and setting one button to be active:
corkRdToggle = (ToggleButton) view.findViewById(R.id.corkRdToggle);
collegeStToggle = (ToggleButton) view.findViewById(R.id.collegeStToggle);
allRoomsToggle = (ToggleButton) view.findViewById(R.id.allRoomsToggle);
corkRdToggle.setTextOn("Cork Rd.");
collegeStToggle.setTextOn("College St.");
allRoomsToggle.setTextOn("All Rooms");
collegeStToggle.setTextOff("College St.");
allRoomsToggle.setTextOff("All Rooms");
corkRdToggle.setTextOff("Cork Rd.");
allRoomsToggle.setChecked(true);
And attaching listeners to them:
corkRdToggle.setOnClickListener(this);
collegeStToggle.setOnClickListener(this);
allRoomsToggle.setOnClickListener(this);
And in my onClick():
case R.id.corkRdToggle:
if(allRoomsToggle.isChecked() || collegeStToggle.isChecked()){
allRoomsToggle.setChecked(false);
collegeStToggle.setChecked(false);
}
corkRdToggle.setChecked(true);
break;
case R.id.collegeStToggle:
if(allRoomsToggle.isChecked() || corkRdToggle.isChecked()){
allRoomsToggle.setChecked(false);
corkRdToggle.setChecked(false);
}
collegeStToggle.setChecked(true);
break;
case R.id.allRoomsToggle:
if(corkRdToggle.isChecked() || collegeStToggle.isChecked()){
corkRdToggle.setChecked(false);
collegeStToggle.setChecked(false);
}
allRoomsToggle.setChecked(true);
break;
I have the three toggle buttons, they're linked to the buttons in the layout, the text has been set for both on and off states, and the 'All Rooms' button has been set to be active by default when the screen is created. Whichever button is pressed will become active, while deactivating both others. Everything is working fine.....except for the initial state. When the screen is created the 'All Rooms' button is indeed activated, and shows the text, but the other two toggle buttons only show 'OFF':
When screen is created
Pressing either of them will deactivate the 'All Rooms' button and the proper text will display in all buttons:
Proper text showing
This remains the case for as long as the screen is being used (due to my rep I can only post 2 links, take my word that it's working).
However the problem is back when I recreate the screen. Done a good bit of looking into this but there doesn't seem to be any info relating to this specifically, and my novice skills are exhausted. If anything jumps out at you I'd really appreciate the pointer, thanks!
Rather than setOnClickListener(), try setOnCheckChangeListener()
corkRdToggle.setOnCheckChangeListener(this);
collegeStToggle.setOnCheckChangeListener(this);
allRoomsToggle.setOnCheckChangeListener(this);
I tend to only use onClickListener for toggles when i want to specifically control if the toggle should indeed be toggled on click (eg. i want to show a warning first or something)
You need to set setChecked(false) to the other buttons to set the initial state OFF text.
I have set up some text in a textView to explain the commands that the app responds to but the code gets formated (flattened) no matter what i type for gravity. The text is supposed to look like this :
Commands and functions:
"left"- Sets tv gravity to left and changes text.
"right"- Sets tv gravity to right and changes text.
"center"- Sets tv gravity to center and changes text.
"blue"- sets tColor to BLUE.
"reset"- resets all tv properties to default
"WTF"- randomises tSize and tColour
But instead turns out like this :
And here is my code(TextView is in a linear layout) :
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/tvTextPlayExplain"
android:typeface="serif"
/>
Remove the line android:gravity="center", and for the text string that you input, you should use the character "\n" to make a newline instead of Enter. For example, the string "This is line 1 \n This is line two" will give you output
This is line 1
This is line two
What you can do is declare the text in the string.xml file like this and you will be able to use html tags to make a list and keep the format :
<string name="tvTextPlayExplain">
<![CDATA[
<p>Commands and functions:</p><br/>
<ul>
<li>"left"- Sets tv gravity to left and changes text.</li>
.... and so on
<li>"WTF"- randomises tSize and tColour</li>
</ul>
]]>
</string>
and then use :
myTextView.setText(Html.fromHtml(getString(R.string.tvTextPlayExplain)));