I would like to make the message arguments text bold given the following string file.how can i make it?
code is given blow:
<string name="meter_reading_dialog_message">Current Reading: %s m³ \nPrevious Reading: %s m³ \nConsumption Reading: %s m³</string>
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val toCurrentReading =
requireArguments().getString(CURRENT_READING)
val toPreviousReading =
requireArguments().getString(PREVIOUS_READING)
val toConsumption =
requireArguments().getString(CONSUMPTION)
return AlertDialog.Builder(requireContext())
.setTitle(getString(R.string.confirmation_reading))
.setMessage(
getString(
R.string.meter_reading_dialog_message,
toCurrentReading,
toPreviousReading,
toConsumption
)
)
.setPositiveButton(R.string.deactivate_save) { _, _ ->
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_OK, null)
}
.setNegativeButton(android.R.string.cancel) { _, _ ->
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_CANCELED, null)
}
.create()
}
I think you can use this for your situation.
you first define a string with it.
val mystring = getString(
R.string.meter_reading_dialog_message,
toCurrentReading,
toPreviousReading,
toConsumption
)
After that, you can set bold value like in this example.
How to set part of text to bold when using AlertDialog.setMessage() in Android?
You can use this method.
fun boldText(text: String) : String {
return Html.fromHtml(text)
}
println(boldText("<b>This text bold.</b> This text normal."))
output=> This text bold. This text normal.
Related
I noticed that if I have multiple multi-line BasicTextField in a Column, if one the text fields has focus and I press the up and down arrow keys on my physical keyboard (using Android Studio emulator), the focus is moved to a different text field instead of going up or down to a different line of text within the text field.
I kind of like the idea of letting the up and down arrow keys change focus, but only if the cursor is at the first or last line.
I came up with a solution, but I am wondering if there is an easier way to do this. Also, if I want to allow a change of focus using arrow keys AND through other methods, it seems I would have to modify this somehow to check the key that was pressed with one of those key modifiers, but I am not sure the best way to do that. Do you know how to determine if a key press is also triggering focus movement/change?
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Column {
repeat(5) {
BasicTextFieldWithText()
}
}
}
}
}
#OptIn(ExperimentalComposeUiApi::class)
#Composable
fun BasicTextFieldWithText() {
var text by remember {
mutableStateOf(
TextFieldValue("BasicTextField\n" + "with multiple lines\n" + "of text")
)
}
var textLayout by remember {
mutableStateOf<TextLayoutResult?>(null)
}
val atFirstLine by remember {
derivedStateOf {
textLayout?.let {
val range = text.selection
range.collapsed && it.getLineForOffset(range.end) == 0
} ?: false
}
}
val atLastLine by remember {
derivedStateOf {
textLayout?.let {
val range = text.selection
range.collapsed && it.getLineForOffset(range.end) == (it.lineCount - 1)
} ?: false
}
}
BasicTextField(
value = text,
onValueChange = { text = it },
onTextLayout = { textLayout = it },
modifier = Modifier
.border(1.dp, Color.Blue)
.padding(8.dp)
.focusProperties {
up = if (atFirstLine) FocusRequester.Default else FocusRequester.Cancel
down = if (atLastLine) FocusRequester.Default else FocusRequester.Cancel
}
)
}
I'm trying to dynamically swap a text inside a LottieAnimation in jetpack compose.
The lottie file is exported without glyphs
It's working when using the old android view inside a
AndroidView(factory = { context ->
val view = LottieAnimationView(context).apply {
setAnimation(R.raw.testing_no_glyphs)
playAnimation()
repeatCount = LottieConstants.IterateForever
}
val textDel = object : TextDelegate(view) {
override fun getText(layerName: String?, input: String?): String {
return when (layerName) {
"Test234" -> "OtherLettersHere"
else -> super.getText(layerName, input)
}
}
}
val fontDel = object : FontAssetDelegate() {
override fun getFontPath(fontFamily: String?, fontStyle: String?, fontName: String?): String {
return "fonts/[MyFontInside /assets].ttf"
}
}
view.setTextDelegate(textDel)
view.setFontAssetDelegate(fontDel)
return#AndroidView view
})
But I can't find the correct handles in the JetpackCompose version of Lottie to get the same result.
If we export the lottie with glyphs, it's works for the letters in the chars array inside the lottie json. But we want to be able to replace with any letters/symbols, so this isn't a viable solution.
I've noticed in the 5.3.0-SNAPSHOT that a fontMap parameter has been added, but I can't figure out which key to hit with it.
Here is my code:
val dynamicProperties = rememberLottieDynamicProperties(
rememberLottieDynamicProperty(LottieProperty.TEXT, value = "AaBbCcEeFf", keyPath = arrayOf("Test234"))
)
val composition by rememberLottieComposition(
spec = LottieCompositionSpec.RawRes(R.raw.testing)
)
val progress by animateLottieCompositionAsState(composition, iterations = LottieConstants.IterateForever)
LottieAnimation(
composition,
{ progress },
dynamicProperties = dynamicProperties,
fontMap = mapOf("fName" to Typeface.createFromAsset(LocalContext.current.assets, "fonts/[MyFontInside /assets].ttf"))
)
It just shows a blank for all the texts inside the Lottie Animation - so this is kinda where i'm stuck.
After some trial an error I found a way to add the typeface for a specific layer:
val typeface = Typeface.createFromAsset(LocalContext.current.assets, "fonts/[MyFontInside /assets].ttf")
val dynamicProperties = rememberLottieDynamicProperties(
rememberLottieDynamicProperty(LottieProperty.TEXT, value = "AaBbCcEeFf", keyPath = arrayOf("Test234")),
--> rememberLottieDynamicProperty(LottieProperty.TYPEFACE, value = typeface, keyPath = arrayOf("Test234")),
)
Hence there is no need for the fontMap in my case
Thanks for taking time to help
I have a radio group with 3 radio buttons and a FloatingActionButton in my activity_main.xml
Then, in my MainActivity.kt, I've set a setOnCheckedChangeListener to that radio group
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val actionButtonhome: ExtendedFloatingActionButton = findViewById(R.id.actionButton_home)
val bottomNav : BottomNavigationView = findViewById(R.id.bottom_nav_view)
val radio_group: RadioGroup = findViewById(R.id.radio_group)
val radio_0: RadioButton = findViewById(R.id.radio_0)
val radio_1: RadioButton = findViewById(R.id.radio_1)
val radio_2: RadioButton = findViewById(R.id.radio_2)
radio_group.setOnCheckedChangeListener {radio_group, optionId -> run {
when (optionId) {
R.id.radio_0 -> {
<<-- Problem is this line -->>
actionButtonhome.text = radio_0.text
}
R.id.radio_1 -> {
}
R.id.radio_2 -> {
}
else -> {
}
}
}}
What I would like, is to have the button's Text as the radio button text + "this string"
But when I try actionButtonhome.text = radio_0.text + "_" + "this string"
I get a huge error and the + symbols are highlighted red
Replace radio_0.text with radio_0.text.toString(). The problem is that .text returns a CharSequence instead of a String and you can’t concatenate CharSequences with +.
I'm trying to create a to-do list. first click menu item (add) and alertdialog write edittext and save but I'm trying to get editable text with alarm box but i get always same number.
number photo
i write bread but same number generate photo
here is a menu code
R.id.add -> {
val mDialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_todo, null)
AlertDialog.Builder(this).setView(mDialogView).setTitle("ADD TODO").setPositiveButton("Save"){
dialogInterface, i ->
val todoTitle = R.id.et_dialog_add.toString()
if(todoTitle.isNotEmpty()) {
val todo = Todo(todoTitle)
todoAdapter.addTodo(todo)
alert dialog:
<EditText
android:id="#+id/et_dialog_add"
android:hint="Buy a Bread"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
here is adapters:
fun addTodo(todo: Todo) {
todos.add(todo)
notifyItemInserted(todos.size -1)
}
This is your problem:
val todoTitle = R.id.et_dialog_add.toString()
This is not how you get the text value from an EditText field. You need to first use findViewById() to get a reference to the EditText, and then you can use its text property to get what the user entered:
val todoTitleView = mDialogView.findViewById<EditText>(R.id.et_dialog_add)
val todoTitle = todoTitleView.text
I want to capizalize every word typed in EditText inside onTextChanged. I've tried some solutions but none of them worked. Problem what I'm facing is if you change capitalize letter on keyboard and you will type James JoNEs it should repair that String to correct form after you type E character to Jone. This is not working with default android:inputType="textCapWords". I've used some function what I've found but it is not working at all.
fun onFieldChanged(s: String, tv: TextWatcher, et: EditText) {
et.removeTextChangedListener(tv)
val changedString = capitalizeFirstLetterWord(s)
with(et) {
text.clear()
append(changedString)
setSelection(changedString.length)
}
et.addTextChangedListener(tv)
}
fun capitalizeFirstLetterWord(s: String): String{
var finalStr = ""
if(s != "") {
val strArray = s.split("[\\s']")
if (strArray.isNotEmpty()) {
for(i in strArray.indices){
finalStr+= capitalize(strArray[i])
}
}
}
return finalStr
}
You can try to achieve that with something like this
"yourString".split(" ").map { it.toLowerCase().capitalize() }.joinToString(" ")