In my project, there are some products..
I created a text beside the button, and when this button is clicked the amount of text should be increased.
My code increases the amount of text only once.
My code:
holder.add.setOnClickListener {
val num = 1
val add = num+1
holder.amount.text = add.toString()
}
}
change the code in this way.
var num = 1
holder.add.setOnClickListener {
val add = num+1
holder.amount.text = add.toString()
}
The reason behind that, at every button click num is initialized with 1.
Lets create a global variable num and everytime button is clicked then num will get updated
var num = 1
holder.rl_additvie.visibility = View.GONE
holder.addicon.setOnClickListener {
holder.rl_additvie.visibility = View.VISIBLE
holder.remmove.visibility = View.GONE
num = num+1
holder.amount.text = num.toString()
}
Related
so I'm new to Kotlin. Been working in it for a few weeks. I am currently trying to figure out how to remove the blue banner header thingy. When I try to remove the banner using headersState = BrowseSupportFragment.HEADERS_DISABLED it makes the whole page go blank.
This is what it looks like with the HEADERS_ENABLED
private fun setupUIElements() {
//title = getString(R.string.browse_title)
// over title
headersState = BrowseSupportFragment.HEADERS_ENABLED
isHeadersTransitionOnBackEnabled = true
// set fastLane (or headers) background color
brandColor = ContextCompat.getColor(activity!!, R.color.fastlane_background)
// set search icon color
searchAffordanceColor = ContextCompat.getColor(activity!!, R.color.search_opaque)
}
Screen Shot with Header Enabled
This is what it looks like when I set headersState = BrowseSupportFragment.HEADERS_DISABLED
Screen shot with Header Disabled
Basically I'm trying to get it to show all the video's with out the blue thing on the left.
I think this function might have something to do with the issue, but I'm not 100% sure.
private fun loadRows() {
val list = MovieList.list
val rowsAdapter = ArrayObjectAdapter(ListRowPresenter())
val cardPresenter = CardPresenter()
for (i in 0 until NUM_ROWS) {
if (i != 0) {
Collections.shuffle(list)
}
val listRowAdapter = ArrayObjectAdapter(cardPresenter)
for (j in 0 until NUM_COLS) {
listRowAdapter.add(list[j % 5])
}
val header = HeaderItem(i.toLong(), MovieList.MOVIE_CATEGORY[i])
val row_list = ListRow(header, listRowAdapter)
rowsAdapter.add(row_list)
}
adapter = rowsAdapter
}
const order = ['margherita', 'pepperoni'];
var total = 0.0;
for (var item in order) {
final price = pizzaPrices[item];
if (price != null) {
total += price;
} else {
print('$item pizza is not on the menu');
}
}
print('Total: \$$total');
Here final variable is accessed twice, one is final price = 'margherita' and another one is price = 'pepperoni'....why it is not showing an error as the final price variable is accessed twice.....but the below code is showing the error...
var item = 'margherita';
final price = pizzaPrices[item];
item = 'pepperoni';
price = pizzaPrices[item];
print(price);
here it is showing error cause final variable price is accessed twice by dartpad
in the first example, you are defining a final variable once in the block. in every iteration, it removes and reinitializes the variables. so you are changing it only once. but in the second example, you are changing a variable twice in a block. so you will get an error.
I need to display a different image according to a condition. The problem is that the previous image remains visible under the next image?
I have nowhere found using the correct command to delete the previous image before displaying the next one.
private fun showDecision(warning: String) {
var imgResId = 0
var resId = 0
// accessing our custom image which we have in drawable folder
when (warning) {
"wrong" -> {
imgResId = R.drawable.wrong
resId = imgResId
}
"notGood" -> {
imgResId = R.drawable.notGood
resId = imgResId
}
"bad" -> {
imgResId = R.drawable.bad
resId = imgResId
}
}
// image de l'arbitre
val decisionView = ImageView(this)
// setting height and width of imageview
decisionView.layoutParams = RelativeLayout.LayoutParams(392, 700)
decisionView.x = hut.x - 80 // setting margin from left
decisionView.y = aDecision.y // setting margin from top
// accessing our relative layout from activity_main.xml
decisionLayout = findViewById<RelativeLayout>(R.id.decision_layout)
decisionView.setImageResource(resId)
// Add ImageView to linearLayout
decisionLayout?.addView(decisionView) // adding image to the layout
decisionLayout?.visibility = View.VISIBLE
decisionShown = true
}
setBackgroundResource(0) does nothing.
Before that, in my onClick(view: View?) I use GONE as below
if (decisionShown) {
decision_layout.visibility = View.GONE
decisionShown = false
}
What did I not do correctly? Thank you
You can clear imageview content by calling setImageDrawable(null);
imgView.setImageDrawable(null);
such usage is documented in the official docs: ImageView#setImageDrawable .
The right instruction is view.removeAllViews() for me and it works. Why "AllViews"? Just because it could be some views, not only the one preceded.
I'm copying code from an Android alarm app.
After setting up the alarm, I want to know how to add a container to the main screen when I press the Save button.
To sum up, my question is:
When you click a button on Activity2,
This is how to add a new container to the Activity1.
savebtn.setOnClickListener {
var item_view = R.layout.alram_list
var alram_title = alram_title1
var alram_time = alram_time
var al_on = al_on
var intent1 = Intent(this#AdeDetail, MainActivity::class.java)
intent1.putExtra("title", title)
intent1.putExtra("day", t)
intent1.putExtra("hour", hour)
intent1.putExtra("min", min)
startActivity(intent1)
}
This is part of activity code 1.
If the statement keeps false and does not seem to receive the value. I'll be waiting for your help. Thank you.
if (intent1.getStringExtra("title") != null){
Log.d("title","111")
var title = intent.getStringExtra("title")
var min = intent.getStringExtra("min")
var hour = intent.getStringExtra("hour")
var day = intent.getStringExtra("day")
var inflater = LayoutInflater.from(this#MainActivity)
var item_view = inflater.inflate(R.layout.alram_list, null)
var alram_time = item_view.findViewById<TextView>(R.id.alram_time)
var alram_title = item_view.findViewById<TextView>(R.id.alram_title1)
var alram_on = item_view.findViewById<RadioButton>(R.id.al_on)
alram_time.text = day+ "hour:" +"min"
alram_title.text = title
alram_on.isChecked =true
container.addView(item_view)
}
First of you need to write receiving intent in 2nd activity like
var intent = getIntent();
then you can retrive values like
intent.getStringExtra("title")
same for others
What i am trying to do is take input from the user and on button click event i want to display that EditText input on TextView. On button click listener, textView should display the input string in All caps and then clicking the same button it should convert that string into lowercase an show it on TextView. How can I achieve this?
This is what i have tried.
var userInput = findViewById<EditText>(R.id.editText)
var caseButton = findViewById<Button>(R.id.upperLowerButton)
var caseView = findViewById<TextView>(R.id.textUpperLower)
caseButton.setOnClickListener {
caseView.text = userInput.text
}
You can use something like:
val uppercase = userInput.text.toString().toUpperCase()
val lowerCase = uppercase.toLowerCase()
Using methods - toUpperCase() and toLowerCase() can easily solve this problem.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String h = textView.getText().toString();
if (isCaps) {
textView.setText(h.toUpperCase());
isCaps = false;
}
else {
textView.setText(h.toLowerCase());
isCaps = true;}
}
});
You can use .toUpperCase() and .toLowerCase() for your string values e.g.
userInput.text.toString().toUpperCase()
and
userInput.text.toString().toLowerCase()
You can try like following.
var isUpperCase = false;
var txt = userInput.text.toString()
caseButton.setOnClickListener {
if(isUpperCase){
txt = txt.toLowerCase()
isUpperCase = false
}else{
txt = txt.toUpperCase()
isUpperCase = true
}
caseView.text = txt
}
In Kotlin as toUperCase() and toLowerCase() are deprecated we can use uppercase() instead of toUpperCase() and lowercase() instead of toLowerCase().
For example,
val lower="abc"
Log.e("TAG", "Uppercase: ${lower.uppercase()}" ) //ABC
val upper="ABC"
Log.e("TAG", "Lowercase: ${upper.lowercase()}" ) //abc