how to get variable into string (string.xml using kotlin - kotlin

Hi i'm new to programming so ...
I made a simple app and in MainActivity.kt I just have one textview that point to a string.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/firsttext"
in string.xml I have
lost of text that is just her for test\n\n does not make sence\n XXX and more text\n
and in middle YYY ad some text
so far so good ,, it show text.
I also have made a settingsFragment.
In root_preferances.xml i have
<EditTextPreference
android:defaultValue="not empty"
android:key="edit_text_FrontPress"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Front pressure should be"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="not empty"
android:key="edit_text_RearPress"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Rear pressure should be"
app:useSimpleSummaryProvider="true" />
Now here is my question !
do I get the value from settings menu
edit_text_RearPress into the string.xml (on place where now XXX is show)
edit_text_FrontPress into the string.xml (on place where now YYY is show)
so that is is visible in my text

Related

How can I control navigation and backStack from main activity

I have two activities A and B and Three fragments C,D and E.
I used intent to move from activity A to B and didn't finish activity A as I want to get back to it later.
Activity B contains a custom layout and a fragment container.
Custom layout contains a text view and a back icon image view. This layout act as action bar as I do not use default action bar as theme of activity B is android:theme="#style/AppTheme.NoActionBar".
Fragment Container hosts a navigation graph.
Fragment C is Start destination and navigation graph allows navigation as C->D->E
and back stack in following order.
C->D //After first back Stack
C //After Second back Stack reached final element in back stack.
A //Activity B is finish and Activity A is resumed after pressing one more back icon image
On press back icon image view of custom layout in activity B xml.
As there is no other action bar in any Fragment(C, D, E) so I can only use back icon image view of activity B.
I want to know how I can achieve this functionality
My activity B xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--android:id="#+id/container"-->
<include layout="#layout/custom_tool_bar"
android:id="#+id/toolbar_settings_activity"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_dashboard_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/settings_navigation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolbar_settings_activity"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
xml code for custom tool bar is given bellow
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/app_gradient_color_background">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iv_back"
android:layout_width="20dp"
android:layout_height="26dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="1dp"
android:contentDescription="#string/content_description"
android:scaleType="fitXY"
android:src="#drawable/ic_white_color_back_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<LinearLayout
android:id="#+id/customlayout_width"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
app:layout_constraintStart_toEndOf="#+id/iv_back"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingEnd="10dp"
android:paddingTop="6dp"
android:paddingStart="10dp"
android:text="#string/custom_tool_bar_Name"
android:textColor="#color/colorOffWhite"
android:textSize="26sp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
custom layout used as action bar
Activity B when navgraph start destination is set to C fragment
Navgraph to navigate from fragment C->D->E in Activity B
1- First in your Activity B get the navController:
val navHostFragment = binding.navHostDashboardFragment.getFragment<NavHostFragment>()
val navController = navHostFragment.navController
2- Then you can add a click listener to your back button:
binding.toolbarSettingsActivity.ivBack.setOnClickListener {
val isStackPopped = navController.popBackStack() // Navigate back to the last fragment
if (!isStackPopped) finish() // If there is no fragments in the stack finish the activity and go back to activity A
}

Textview won't scroll when using append

I'm trying to get a TextView/ScrollView in Kotlin to scroll to bottom after appending text.
Weird thing is, I have it working perfectly for 1 TextView, however, another (near identical) TextView doesn't work?
So after a while of struggling to find a difference, I finally found ... something ...
I was populating one of them:
textView = text
and the other
textView2.append ( text )
The one using append, would not scroll to bottom, nor would it show the shaded curve for "overScrollmode" (which is set to true).
Obviously, I was using append, because I'm adding a small amount of text, repeatedly.
The other textView, is an "all or nothing" replacement. (in reality, I don't want the latter to scroll down, I actually set it to scroll to 0,0 (top), however, I used it as a test to find this issue :) )
So why does a text view, when using append, not honor/scroll as a result ?
<androidx.cardview.widget.CardView
android:id="#+id/cvMoveHistory"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="5dp"
app:cardBackgroundColor="#025D68"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toTopOf="#id/cvMoveHint"
app:layout_constraintEnd_toStartOf="#+id/linearLayoutBoardButtons"
app:layout_constraintStart_toEndOf="#+id/frLayGameBoard"
app:layout_constraintTop_toTopOf="parent">
<ScrollView
android:id="#+id/svMoveHistory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:fillViewport="true">
<TextView
android:id="#+id/moveHistory"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#939393"
android:fontFamily="monospace"
android:padding="4dp"
android:scrollbars="vertical"
android:text="123) 5e2-5e3 1a1-1a3"
android:textColor="#000000"
android:textSize="11sp" />
</ScrollView>
</androidx.cardview.widget.CardView>
and in code:
-- in onCreate
tvMoveHistory = findViewById ( R.id.moveHistory )
svMoveHistory = findViewById ( R.id.svMoveHistory )
tvMoveHistory.movementMethod = ScrollingMovementMethod()
-- to actually add text
moveHistory.append ( newText )
-- during a central Refresh routine (yes, confirmed it runs via debugger)
svMoveHistory.post {
svMoveHistory.fullScroll(View.FOCUS_DOWN)
}
What's even more strange, I created a new project, minimal with just a few objects in order to re-create the issue.
It behaves perfectly whether I use append, or just straight assign it.
So I'm again baffled as to what's causing the issue ? (ie using append in my current project means no scroll to bottom, using .text = full text means scroll to bottom works).
However, re-testing in another fresh project results in expected behaviour?
Text I'm adding is very simple, no spannable string, no bold/html markup etc.
Just simple line in format of a simple chess-like notation:
12) a5-g6 e3-h7
... etc. (+ CR ie \n at end of line for new line)
I've scoured my logic, I literally don't touch that textView for any reason other than to append a small piece of text. Occassionally (ie new game), I reset it: = ""
but aside from above code, there is nothing else updating/modifying this textview/scrollview ?
Any ideas what I'm missing?

How can i use DataBinding for TextView?

i have a problem, i dont know how to use DataBinding for TextView. i made a DataBinding for Glide and it success, but for TextView i dont know how to do it
this is the code
viewModel2.shouldShowImageProfile.observe(this) {
Glide.with(binding.root)
.load(it)
.circleCrop()
.into(binding.rivProfile)
}
viewModel2.shouldShowUsername.observe(this){
TextView.with(binding.root) <- the TextView
.load(it)
.into(binding.tvHello)
}
I wrote my answer making an assumption about shouldShowUsername, but as that's your String value, you can do something like this:
viewModel2.shouldShowUsername.observe(this) { shouldShowUsername ->
binding.tvHello.isVisible = !shouldShowUsername.isNullOrEmpty()
binding.tvHello.text = shouldShowUsername
}
That'll cause your TextView to only be displayed when shouldShowUsername has a proper value then take that value and assign it to the TextView. If you want the TextView to always show up, you can skip that first line within the block.
If you dont want to use observe method on variables of viewmodel you can use data binding and change value of textview from the xml directly, so that you dont have to manually write binding.tvUserName.text = it.value. Below code shows example for usuage of data binding with xml and viewmodel.
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="viewModel"
type="com.example.kotlinbasics.android_architecture.mvvm_architecture.ChatViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light"
tools:context=".android_architecture.mvvm_architecture.UserOneFragment">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/etMessageToSecond"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/_10sdp"
android:background="#drawable/text_field_background"
android:gravity="center"
android:hint="#string/enter_message"
android:padding="#dimen/_7sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.7" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvCurrentMessage"
android:text="#{`Current Message: `+viewModel.latestMessageFromFirst}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_50sdp"
android:padding="#dimen/_10sdp"
android:textColor="#color/white"
android:textSize="#dimen/_20ssp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/etMessageToSecond" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
In the above example I am printing the live text in textview which user writes in the edittext field. Whenever user writes in edittext, its value gets updated in the viewmodel and textview access that value on runtime.
For image thing, you can do like, you can create custom binding adapter which updates the image. Example:
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivEmailVerification"
loadImageFromUrl="#{viewModel.emailVerifyImageUrl}"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvIsEmailValid"
app:layout_constraintWidth_percent="0.7" />
and on BindingAdpater
#BindingAdapter("loadImageFromUrl")
fun ImageView.loadImageFromUrl(imageUrl: String) {
Picasso.get().load(imageUrl).into(this)
}

Start activity from button.setOnClickListener which lies in my RecyclerView

I want an activity/class to run on a Button click. The Button is inside my RecyclerView. I first had the code in my MainActivity with:
btnComplete.setOnClickListener {
startActivity(Intent(this#MainActivity, DelComplete::class.java))}
This code was in my onCreate of my MainActivity. How ever on running the code I found the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
My first thought was that it could not reach the Button since it was in the RecyclerView. So I tried to put the code in my RecyclerView. But I read that it is not a good idea to put it in you RecyclerView and I dont know what to do with the Context as it keeps throwing errors.
RecyclerView:
txtbutton1.setOnClickListener {
confirmdel()
modal.tvdone = "Delivered"
Log.e("Clicked", "Successful delivery")
//this is where I add code to export data through api
modal.state = DataState.Success
Status = 1
notifyDataSetChanged()}
private fun confirmdel() {
startActivity(Intent(this, DelComplete::class.java))}
It might be as simple as filling in the Context but I am unsure what the Context must be to test that theory.
Recyclerview- table_list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/txtWOrder"
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#drawable/table_content_cell_bg"
android:text="#string/worder"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
<TextView
android:id="#+id/txtDElNote"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#drawable/table_content_cell_bg"
android:text="#string/delnote"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
<TextView
android:id="#+id/txtCompany"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#drawable/table_content_cell_bg"
android:text="#string/company"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
<Button
android:id="#+id/btnComplete"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#drawable/table_header_cell_bg"
android:drawableTint="#70D5C8"
android:text="#string/button1"
android:textSize="18sp" />
<Button
android:id="#+id/btnException"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#drawable/table_content_cell_bg"
android:text="#string/button2"
android:textSize="18sp" />
<TextView
android:id="#+id/txttvdone"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#drawable/table_content_cell_bg"
android:foregroundGravity="center_horizontal"
android:text=""
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtWeight"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#drawable/table_content_cell_bg"
android:text="#string/weight"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
</LinearLayout>
The reason why I have a Button in my RecyclerView is on every entry a driver should be able to give feedback on a delivery. Every entry is a different order. Did I go the wrong way of doing this?
My Button does the following:
txtbutton1.setOnClickListener {
confirmdel()
modal.tvdone = "Delivered"
Log.e("Clicked", "Successful delivery")
//this is where I add code to export data through api
modal.state = DataState.Success
Status = 1
notifyDataSetChanged()
}
It does that with no problem. Now however I want to add an AlertDialog and update data in my DB with the setOnClickListener. Will this be possible from within my adapter?
You can use the context like this :
class Adapter(private val context: Context, private val myList: List<String>) :
RecyclerView.Adapter<Adapter.ViewHolder>()
And pass the context where required like this
private fun confirmdel() {
startActivity(Intent(context, DelComplete::class.java))}
You shouldn't put a button inside of your RecyclerView, unless you are referring to a button as the item itself.
RecyclerViews are used to render as much as items as you have in your data source, instead of hardcoding them, so I think you want to handle the click on a single item and navigate to another Activity with the data of that item.
You can do that by following these steps. This is a codelab made by Google and explains how to handle the item click.
In addition, you should do those too, in order to better understanding.RecyclerViews

Text padding is not displayed well on Android 4.4

On Android 4.4 KitKat, my two apps has the problem. But on the other Android platform under 4.4 like 4.0 and 2.3.3, they display well.
The device I use is LG Nexus 5.
Here are two examples:
CheckBox:
<LinearLayout
android:id="#+id/CheckBoxGroup"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="vertical" >
<CheckBox
android:id="#+id/CheckBoxRememberMe"
style="#style/FontSmallBald"
android:layout_width="150dp"
android:layout_height="50dp"
android:button="#drawable/checkbox_remember_image_state"
android:checked="true"
android:gravity="center_vertical"
android:text="#string/remember_me" >
</CheckBox>
<CheckBox
android:id="#+id/CheckBoxKeepMeLogin"
style="#style/FontSmallBald"
android:layout_width="150dp"
android:layout_height="50dp"
android:button="#drawable/checkbox_remember_image_state"
android:checked="false"
android:gravity="center_vertical"
android:text="#string/keep_me_login" />
</LinearLayout>
<style name="FontSmallBald">
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
</style>
The CheckBox is displayed like this : BOX * no space * keep me login .
But on 4.0 or 2.3.3 it is displayed like this: BOX * default DP space * keep me login .
Button:
button.setGravity(Gravity.CENTER_VERTICAL);
button.setPadding(85,0,0,0);
Althouth I set 85dp paddingLeft but it results in different appearence.
On Android 4.4 the text is much more closer(not algin at and it seems that 85dp is shrinked to 50dp) to the left edge of the button but does not on the other.
Anyone knows why on Android 4.4 the rendering is different compared with other version of Android and how to set the attribute to make them look the same?
setPadding() set padding in pixels, not dp.
You can use screen width/x for padding to get a same result.
SEE:
setPadding: http://developer.android.com/reference/android/view/View.html#setPadding(int, int, int, int)
how to get device screen size