Retrieving StringArray resource depending on the clicked button - android

I want to design a ListView that will be populated from a StringArray resource depending on which button was clicked. The array name will be retrieved and put into the getStringArray method like 'dynamically', after the button was clicked.
I'm trying this method to get the identifier by name:
getResources().getIdentifier(String name, String defType, String defPackage);
but when I am constructing the array name as a String made of clicked button ID and some other Strings like this:
Bundle extras = getIntent().getExtras();
Integer buttonId = extras.getInt("ButtonId");
String resourceName = "";
int resourceId = 0;
resourceName = "R" + buttonId.toString() + "WEEK" + "OUT";
resourceId = getResources().getIdentifier(resourceName, "array", this.getPackageName());
I get an error that there is no such resource and the getIdentifier method returns 0.
(The name of the resource is set to the same value as the resourceName).

Related

How to textview2 getText from String resource by my textView1 is equal string name

"If the value in textView1 is equal string name I want string to show textView2"
// String Example
<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>
//Example
if textView1 = a
textView2 Will Show Apple
Here you have stringName as a text in your textView1.
You can fetch String value programatically from resourses by stringName.
Step1 : first fetch this both values, and store it in a variable;
String mTvTextStrName = textView1.getText().toString().trim();
String strValue = getStringResourceByName(mTvTextStrName);
use this method to fetch String value.
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
You can also check this values via Log.e.
Step2 : Now set your strValue in your textView2.
// Do your code.. Show Apple
textView2.setText(strValue);
Try using getIdentifier() method of getResources() like below.
TextView textView1= findViewById(R.id.textView1);
TextView textView2= findViewById(R.id.textView2);
String textViewText=getResources().getString(getResources().getIdentifier(textView1.getText().toString(), "string", getPackageName()));
textView2.setText(textViewText);
Referenced from https://stackoverflow.com/a/17086690/9502601

Array Name passing variable

Let say I have 2 arrays
Array1 = 1,2,3,4,5
Array2 = a,b,c,d,e
String[] array = getResources().getStringArray(R.array.Array1);
That's work fine. But I don't want to use the code above again with another line
String[] array = getResources().getStringArray(R.array.Array2);
How do I get the below lines to work if
I had declared xxx as variable for array name
String xxx = Array1;
String[] array = getResources().getStringArray(R.array.xxx);
You can:
int xxx = R.array.Array1; //change to integer
String[] array = getResources().getStringArray(xxx); //pass the whole id
Instead of passing just the name of the resource, pass the whole ID (as integer).
If you still want to pass the name as string, you can:
String xxx = "Array1";
int resourceId = getResources().getIdentifier(xxx, "array", getPackageName());
String[] array = getResources().getStringArray(resourceId);
reference of the second option: https://stackoverflow.com/a/3476447/9038584
A simple util method in your activity or service:
String[] getArray(int id) {
return getResources().getStringArray(id);
}
Usage:
String[] array1 = getArray(R.array.array1);

Get String Value Dynamically

I am passing a value from one activity to another through putExtra() and getExtra() and getting value like:
Passing from
Activity 1:
intent = new Intent(this, blankScrollActivity.class);
intent.putExtra("alphabets", "a");
Recieving at
Activity 2: blankScrollActivity.class
Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");
TextView textView = (TextView) findViewByid(R.id.blankTextView);
String Resource MyString.xml:
<string name="a">My Example 1</string>
<string name="b">My Example 2</string>
I want to get the value of string dynamically to assign textView.
Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");
int resId = getResources().getIdentifier(varName , "string", getPackageName());
String data = getResources().getString(resId);
This way, you will get the String resource that you want based on the String value sent from the first Activity.

Android: Enable certain button with id as index

I have 50 styled buttons with identificators like "level_i", I need to enable button with certain i in id.
I have code to work with indexed aarays in string xml, but I have no proper ideas how to change it for my usage
Class<R.id.array> res;
Field field;
try {
res = R.array.class;
field = res.getField("words_" + fname);
//set myString to the string resource myArray[fname,y]
myString = getResources().obtainTypedArray(field.getInt(null)).getString(y);
}
catch (Exception e) {
e.printStackTrace();
}
I believe you are saying that you have an ID resource named "words_" + fname for example R.id.words_100. If that is correct, then you can first get the ID using the name of the resource with getIdentifier. Then you can get the actual ID, then you can find the view with that ID:
String resName = "words_" + fname";
Resources res = getResources();
int resId = res.getIdentifier(resName, "id", getPackageName());
View button = findViewById(resId);
EDIT:
Modified original answer to fetch resources from R.id rather than R.string-array.

How is it possible to implement read the image name by clicking on it

Have a code that displays a random image:
Random rand = new Random();
int rndId = rand.nextInt(24) + 1;
imgName = "drw" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imageView.setImageResource(id);
How is it possible to implement read the image name by clicking on it in the program and create a new window with a description of the image that is unique to each.
Yes, you can get the Image Name if you have its resource id by using getResourceEntryName(resource_id),
String image_name = getResources().
getResourceEntryName(R.drawable.ic_launcher);
Log.d("name", image_name);
OUTPUT:
ic_launcher

Categories

Resources