Set android widget visibility dynamycally - android

Okay i have one EditText and one spinner.
My goal is if item 1 on spinner selected the EditText visibility is true, and if item 2 selected EditText visibility is false. what the code for reach that goal ?
i use spiner get selected id like this :
String tipe = spiner.getSelectedItem().toString();
if (tipe=="item2"){
//edittext.visible = false; <-- i don't know how to make/what code this visibility become false
}

Use the below
if (tipe.equals("item2")){ // .equals to compare strings
edittext.setVisibiluty(View.INVISIBLE); //set the visibility
}
Use the below according to your needs
visible 0 Visible on screen; the default value.
invisible 1 Not displayed, but taken into account during layout (space is left for it).
gone 2 Completely hidden, as if the view had not been added.
http://developer.android.com/reference/android/view/View.html#setVisibility(int)

use equals() method for comparing of Two String. It will check the content of the String
String tipe = spiner.getSelectedItem().toString();
if (tipe.equals("item2")){
// do what u want here
}

Related

How to validate and set errors in recyclerview having multiple edittext?

I have a recycler view with multiple edittext and i need to validate each and every edittext before clicking add button and on tapping add button it again creates new edittexts but i need to validate previous edittexts before creating new ones and set errors how to implement this?
You need to add EditText values in array list and validate it when you click on Add Button like
val list = arraylistof<EditTextModel>()
add.setonclickListener{
val getItem = list[list.size.minus(1)]// get Last index of list
// because when you click add button always check last index because you already //check previous. when you add views on index 1 or index 2.... so on
if(getItem.editTextValue1.isEmpty() || getItem.editTextValue2.isEmpty()) {
Log.e("TAG","Edit 1 or Edit 2 may be empty. Please add values")
}
}

How to refer to a dynamically generated button?

I am trying to interact with dynamically generated buttons. I want to update text and background color for those player clicked and for those who is near by horizontal or vertical axis at the moment of a click.
What I've tried. I've found an id property of a button in XML which led me to idea that I can make a text key to refer to a programmatically generated button. But when I've tried to assign an id - IDE was expecting a number (Int), not a string. Since buttons form a square array - I decided to encode each button via 4 digit number where first 2 digits stand for a row number and other two stand for a column number. Though when I tried to use findViewById IDE told me that it was expecting a special id data type, not a number.
That's how it looks for now:
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
button.id = i*100 + j
constraintLayout.addView(button)
}
}
What idea or method could I look at?
If you created it dynamically you can save it in a variable (or an array) for later use.
val myButtons = ArrayList<Button>()
for (i in 1..size) {
for (j in 1..size){
val button = Button(this)
myButtons.add(button)
constraintLayout.addView(button)
}
}
if you have a layout with dynamically created views and you know their order you can get them with getChildAt(index).
Alternatively you can assign ids saved in xml like this.

How to change the text of a radio button based on the selection in the spinner which are both within a fragment

I have a spinner which holds the years as ranges.
fromYear-toYear
For example:
2015-2016
2016-2017
I need to check if the toYear is a leap year on selection and based on if it is leap year or not i need to change the text of radio buttons in a group placed below the spinner.
There are 3 radio buttons within the group.
Thanks in advance for the answer.
First you must get selected item String from your Spinner , to do that you must check setOnItemSelectedListener method and when user choosed one item from your spinner you get the selected item String value try this code to get it :
String Text = mySpinner.getSelectedItem().toString();
Then you must check text value to get ToYear from it .
In Java you can user substring() method.
For example if selected item text equals 2015-2016 :
text.substring(5,4);
This will return you 2016 and you can do something you like by checking it ,
You can change start and end index if it is't returned true value . this is the standard type of substring usage :
str.substring(startIndex, endIndex);

Not correctly display first avatar

I use ImageLoader, and display image in ListView, when havent image, server give me String "null", i have check:
if(!image.equals("null")) {
imageLoader.displayImage(image, avatar);
}
If not verified, then there should be an empty area, so all ListView item. But if first item .equals("null") , then first image same pattern as last item image, if onStop and onResume Activity, then image first item empty.
Why is this happening? If image.equals("null") then area must be empty.
Use poistion on getView(), Based on the scrolling
Fetch the value
Check if value exists then set value
else null
use ViewHolder effectively

Select Checkbox when its Value Equals the String from the EditText field

Problem: In my app, there exists two different ways for a user to add an item to a list: via a TextField and via checking an element in an ExpandableListView. Every time the user enters an item into the TextField, I would like to check if that value appears within any group of the ExpandableListView. If it does, then I would like to check that item.
Implementation of Solution: I have a HashMap that stores as keys all the values within the ExpandableListView, and its values are the corresponding group HashMap in which they are in. If the item is found then I plan to derive its location by using a second HashMap that stores its position within the group.
Question: Using this information, how can I check/uncheck that particular box? Specifically how can I identify a particular child of the ExpandableListView, and then identify a child of that?
Thank you for your time.
My approach is slightly different but it does the work.
Each element of yours must have a HashMap. Considering adding another element to it.
A string element call it ChkVal default value in it is false.
Now when you match the value from textbox to the element in that group. Using the hashmap.
Change its checkbox value.
Boolean b = false;
String chkCondition = hmap.get(chk).toString().toLowerCase();
if (chkCondition.equals("true"))
b = true;
chkBox.setChecked(b);

Categories

Resources