Delete a image in a ImaveView programmatically created using kotlin - android

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.

Related

Kotlin Headerstate messing up display on Android TV

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
}

Text is updated only once on button click

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()
}

How to loop through xml resource to set ID programatically

Does somebody here have experienced how to loop through xml resource file to get ID from there and set it to view Programmaticaly.
i have already tried to research but nothing seems close to my problem
private fun createImageView(){
newView = ImageView(this)
coordinatorLayout.addView(newView)
newView.id = NewViewAssignedID //This is how i set up ID rightnow
newView.layoutParams.height = 400
newView.layoutParams.width = 400
newView.x = 520F
newView.y = 100F
newView.setImageResource(getResources().
getIdentifier(ImageToUse,"drawable","com.example.tema"))
newView.setOnTouchListener(listener)
btn_ScaleOk.isVisible = true
mScaleGestureDetector = ScaleGestureDetector(this, ScaleListener())
CanScaleNow = true
NewViewAssignedID++
Toast.makeText(this,newView.id.toString(),Toast.LENGTH_SHORT).show()
}
Need help

Change id name of ImageView with a String

Suppose we have :
ImageView imageView_A = findViewById(R.id.imageview_dhze);
ImageView imageView_B = findViewById(R.id.imageview_dhhkjhkze);
ImageView imageView_C = findViewById(R.id.imageview_dhkhkjze);
ImageView imageView_D = findViewById(R.id.imageview_dhhuihuybze);
I want to make a function like this :
changeImages(String NAME) {
imageView_1_NAME.setImageResource(R.drawable.img);
imageView_2_NAME.setImageResource(R.drawable.img);
imageView_3_NAME.setImageResource(R.drawable.img);
}
And for example, I could use it like this :
changeImage("A");
to obtain :
imageView_1_A.setImageResource(R.drawable.img);
imageView_2_A.setImageResource(R.drawable.img);
imageView_3_A.setImageResource(R.drawable.img);
It is possible ? How can I do that ?
You could put all the ImageViews in a HashMap like this:
HashMap<String, ImageView> imageViews = new HashMap<>();
Then you add the ImageViews to the HashMap:
imageViews.put("A", imageView_A);
imageViews.put("B", imageView_B);
...
And your function could look like this then:
private void changeImage(String name) {
imageViews.get(name).setImageResource(R.drawable.img);
}
In this case you should only call changeImage() when the HashMap contains a key with the same value of String name. If you are not sure if a key exists you need to check if imageViews.containsKey(name) first.
You can do that:
void changeImage(String NAME) {
int imageId = this.getResources().getIdentifier("imageView_" + NAME, "id", this.getPackageName());
if (imageId != 0) {
ImageView imageView = (ImageView) findViewById(imageId);
imageView.setImageResource(R.drawable.img);
}
}
I think what you're looking for is something like this
fun changeImage(which: String) {
when(which) {
"a" -> imageView_A.setImageResource(R.drawable.img)
"b" -> imageView_B.setImageResource(R.drawable.img)
"c" -> imageView_C.setImageResource(R.drawable.img)
"d" -> imageView_D.setImageResource(R.drawable.img)
else -> throw IllegalArgumentException("Unknown image view")
}
}
int[] images = {R.id.imageA,R.id.imageB,R.id.imageC,R.id.imageD,R.id.imageE};
int[] drawables = {R.drawable.imageA,R.drawable.imageB,R.drawable.imageC,R.drawable.imageD,R.drawable.imageE};
for(int i=0;i<5;i++)
{
findViewById(images[i]).setImageResource(drawables[i]);
}
It's easier.

Set imageview background on button click?

lets say I have a timer activity. Theres an image view that displays a number of 0 - 9(these are viewed as images, NOT text).
eg. the images names are clock_0, clock_1, clock_2...clock_9.
There is a plus and minus button that should change a counter(to pick the image). If current image is clock_0, pressing the plus button will change the view to clock_1 etc. How do I do this efficiently?
EDIT
Heres what I tried to do;
set an int[],
int[] clockResource = new int[10];
clockResource[0] = R.drawable.clock_0;
clockResource[1] = R.drawable.clock_1;
clockResource[2] = R.drawable.clock_2;
clockResource[3] = R.drawable.clock_3;
clockResource[4] = R.drawable.clock_4;
clockResource[5] = R.drawable.clock_5;
clockResource[6] = R.drawable.clock_6;
clockResource[7] = R.drawable.clock_7;
clockResource[8] = R.drawable.clock_8;
clockResource[9] = R.drawable.clock_9;
assign an onclicklistener for the addbutton
case R.id.addMin:
if (addMin.isEnabled()) {
if (mintwo < 9) {
mintwo++;
} else if (mintwo == 9) {
minone++;
mintwo = 0;
}
min2.setBackgroundResource(clockResource[mintwo]);
min1.setBackgroundResource(clockResource[minone]);
}
Since they're ImageViews, use setImageDrawable or setImageResource instead.
Related SO-question.
case R.id.addMin:
if (addMin.isEnabled()) {
if (mintwo < 9) {
mintwo++;
} else if (mintwo == 9) {
minone++;
mintwo = 0;
}
min2.setImageResource(clockResource[mintwo]); //updated
min1.setImageResource(clockResource[minone]); //updated
}

Categories

Resources