How to loop through xml resource to set ID programatically - android

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

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
}

Delete a image in a ImaveView programmatically created using kotlin

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.

How to set an onTouchListener for multiple ImageButtons

I have this:
// Set up touch listeners for all the buttons
View cardButton = findViewById(R.id.C_0);
cardButton.setTag(0);
cardButton.setOnTouchListener(this);
View cardButton1 = findViewById(R.id.C_1);
cardButton1.setTag(1);
cardButton1.setOnTouchListener(this);
View cardButton2 = findViewById(R.id.C_2);
cardButton2.setTag(2);
cardButton2.setOnTouchListener(this);
View cardButton3 = findViewById(R.id.C_3);
cardButton3.setTag(3);
cardButton3.setOnTouchListener(this);
View cardButton4 = findViewById(R.id.C_4);
cardButton4.setTag(4);
cardButton4.setOnTouchListener(this);
View cardButton5 = findViewById(R.id.C_5);
cardButton5.setTag(5);
cardButton5.setOnTouchListener(this);
View cardButton6 = findViewById(R.id.C_6);
cardButton6.setTag(6);
cardButton6.setOnTouchListener(this);
View cardButton7 = findViewById(R.id.C_7);
cardButton7.setTag(7);
cardButton7.setOnTouchListener(this);
View cardButton8 = findViewById(R.id.C_8);
cardButton8.setTag(8);
cardButton8.setOnTouchListener(this);
But I need to generate it programmatically so I can't have it written out like this. Unfortunately, I have tried three methods but none work. Firstly:
for(int i = 0; i < 9; i++)
{
String bufferName = "C_" + i;
int tempid = getResources().getIdentifier(bufferName, "drawable", getPackageName());
View cardButton = findViewById(tempid);
cardButton.setTag(i);
cardButton.setOnTouchListener(this);
}
This gives me a NullPointerException. I'm not sure how to pinpoint it. Also, the android thing says you shouldn't use getIdentifier anyway because it is expensive.
Next attempt:
ViewGroup rootLayout=(ViewGroup) sv.getRootView();
View v;
int id = 0;
for(int i = 0; i < rootLayout.getChildCount(); i++) {
v = rootLayout.getChildAt(i);
if(v instanceof View)
{
v.setTag(id);
id += 1;
v.setOnTouchListener(this);
};
}
No errors here, but none of the buttons work anymore. Either the listeners are not getting set or the touches aren't getting recorded properly or whatever.
Third attempt:
int[] ids = {R.id.C_0, R.id.C_1, R.id.C_2, R.id.C_3, R.id.C_4, R.id.C_5, R.id.C_6, R.id.C_7, R.id.C_8};
for (int id:ids)
{
ImageView b = (ImageView)findViewById(id);
b.setTag(ids);
b.setOnTouchListener(this);
}
This is the worst method and I don't want to use it, but even this would not work. I was getting ClassCastException. I tried using ImageButton b = (ImageButton)findViewById(id); but that still got me the same error. Sorry if this is totally newb, but I've spent hours trying to figure out a way to do this. :(
Firstly:
Your first attempt is very close...
You are using the wrong type here:
int tempid = getResources().getIdentifier(bufferName, "drawable", getPackageName());
You want an id, use:
int tempid = getResources().getIdentifier(bufferName, "id", getPackageName());
Next attempt:
This one looks okay, maybe it is the OnTouchListener that has an error.
Third attempt:
The ClassCastException simply means that whatever View type you declared in the XML must be the class the View type you use in your Java code...

FlexMobileProject, setStyle("textFormat",number) in textArea doesn't work

I create dynamic TextArea in Flex, I want to change its fontSize dynmically, but setStyle doesn't work.
This is my code:
var textArea:TextArea = new TextArea();
textArea.id = "txtCreaTaskAnalysis" +contatoreNumeroTextAreaCreaTaskAnalysis;
textArea.left = 140;
textArea.right = 45;
textArea.horizontalCenter = 47;
textArea.height = 110;
textArea.y = posizioneYTextArea;
var tfor:TextFormat = new TextFormat();
tfor.size= 25 ;
textArea.setStyle("textFormat",tfor);
textArea.text = tfor.size.toString();
addElement(textArea);
I have this code in a buttonClickHandler, but the fontSize doesn't change.
Help, please..
Try using :
textArea.setStyle("fontSize",25);
I don't think TextArea has a style called "textFormat".

Comparing string array with string and posting my result to main.xlm

I am having trouble with understanding how to compare strings in Java for Android. I have written code to do this in JavaScript and Palm but am new to Java and am a little confused. Case in point, I am trying to modify the example on the Android Developers site for SpinnerActivity (http://developer.android.com/resources/samples/Spinner/src/com/android/example/spinner/SpinnerActivity.html). In my application I am looking at pipe sizes in the spinner not planets. When the user picks a pipe size I want to reference an array of pipe sizes and be able to pick other parameters associated with that pipe size like the outside diameter (OD) of the pipe. I have modified the above sample code and added and array for the pipe sizes and the OD sizes. I then try to compare what the user picked in the pipe sizes spinner with my pipe sizes array and use the number of the array that matches to pick the associated OD. There is something wrong with the way I am trying to make this comparision. I set both of these values as stings but they never seem to find one another.
HelloSpinner1.java section I have changed is:
public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {
HelloSpinner1.this.mPos = pos;
HelloSpinner1.this.mSelection = parent.getItemAtPosition(pos).toString();
/*
* Set the value of the text field in the UI
*/
TextView resultText = (TextView)findViewById(R.id.SpinnerResult);
resultText.setText(HelloSpinner1.this.mSelection);
String[] OD; // array of pipe ODs
OD = new String[30]; // allocates memory for 30 floating point numbers
OD[0] = "0.405";
OD[1] = "0.540";
OD[2] = "0.675";
OD[3] = "0.840";
OD[4] = "1.050";
OD[5] = "1.315";
OD[6] = "1.660";
OD[7] = "1.9";
OD[8] = "2.375";
OD[9] = "2.875";
OD[10] = "3.5";
OD[11] = "4";
OD[12] = "4.5";
OD[13] = "5.563";
OD[14] = "6.625";
OD[15] = "8.625";
OD[16] = "10.750";
OD[17] = "12.75";
OD[18] = "14";
OD[19] = "16";
OD[20] = "18";
OD[21] = "20";
OD[22] = "22";
OD[23] = "24";
OD[24] = "26";
OD[25] = "28";
OD[26] = "30";
OD[27] = "32";
OD[28] = "34";
OD[29] = "36";
String [] Size;
Size = new String [30];
Size[0] = "1/8";
Size[1] = "1/4";
Size[2] = "3/8";
Size[3] = "1/2";
Size[4] = "3/4";
Size[5] = "1";
Size[6] = "1-1/4";
Size[7] = "1-1/2";
Size[8] = "2";
Size[9] = "2-1/2";
Size[10] = "3";
Size[11] = "3-1/2";
Size[12] = "4";
Size[13] = "5";
Size[14] = "6";
Size[15] = "8";
Size[16] = "10";
Size[17] = "12";
Size[18] = "14";
Size[19] = "16";
Size[20] = "18";
Size[21] = "20";
Size[22] = "22";
Size[23] = "24";
Size[24] = "26";
Size[25] = "28";
Size[26] = "30";
Size[27] = "32";
Size[28] = "34";
Size[29] = "36";
String ODSize;
for (int i = 0; i <= 29; i++){
if (Size.equals("HelloSpinner1.this.mSelection")) {
ODSize = OD[i];
break;
}
}
}
The associated strings.xml rorm the android site with slight modifications is:
Pipe and Tube
1/8
1/4
3/8
3/4
1
1-1/4
1-1/2
2
2-1/2
3
3-1/2
4
5
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
Select a Pipe Size
Just a quick note, but I see two things here:
1) You have "HelloSpinner1.this.mSelection" in quotes. Inside .equals, that is comparing your variable Size directly to that string ... not the value stored in that object. For example, you're asking: "1/8" ?= "HelloSpinner1.this.mSelection" ... not, "1/8" ?= "1/4" in this case. That might be most of your problem here.
2) You could just use the position of the spinner inside your Listener method. That gives you the position of the selection on the spinner. If you aren't modifying those values, you would already know the index into your array. If you were modifying them, /then/ you could do a string comparison (or concurrently modify your array to keep them up to date).
You might also want to check what you're comparing against once you eliminate the quotes problem. A very simple way to do that would be to declare a String for each value, then run it in the debugger or log the output.
Lastly, as personal preference, I don't like to store long arrays which aren't going to change in the xml file. Just code that up as an array which doesn't have to be interpreted later. That'll give you the array access directly, and speed up execution some.

Categories

Resources