Android how can i get the image button's id programmatically? - java

I created image button and I set that's image programmatically with this code:
myButton1[0][0].setImageResource(R.drawable.mayin2);
And I want to get that image's id or name from another method. How can i call image button's image's id or name?

You can't do that directly, it's just a reference to a drawable resource that gets retrieved, but the integer value isn't retained. One thing you could do is store the reference as a tag on the View:
myButton1[0][0].setImageResource(R.drawable.mayin2);
myButton1[0][0].setTag(R.drawable.mayin2);
Then you could retrieve it with:
Integer resourceID = (Integer) myButton1[0][0].getTag("resource_id");
EDIT: Alternately, if you need to store multiple tags, define an ID in your values folder (create some XML file, maybe ids.xml) like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="resource" type="id" />
</resources>
Then set a tag with that ID:
myButton1[0][0].setImageResource(R.drawable.mayin2);
myButton1[0][0].setTag(R.id.resource, R.drawable.mayin2);
Then retrieve by that same ID:
Integer resourceID = (Integer) myButton1[0][0].getTag(R.id.resource);

Related

Custom callback function attribute android

As android defines attributes inside the attrs files and I have seen that Button has attribute onClick listed. Is there a way to create custom attributes referencing functions as so and how does on go about implementing it.
<Button
...
android:onClick="onClickFunction"/>
What I want to have
<CustomView
...
android:onItemClick="onItemClicked"/>
attrs.xml
<attr name="onItemClick" format="string"/>
How does one create attach this function as a listener to the view?

How to set images for all list items

I didnt know how to set images for all list view item in same image in this tutorial link : https://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text if i change xml datasource in snows into my default image it will not no appear in output blank display so i need to where did make change in coding part to set all list for one image
In BinderDataClass remove following code:
String uri = "drawable/"+ weatherDataCollection.get(position).get(KEY_ICON);
int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier( uri, null, vi.getContext().getApplicationContext().getPackageName());
Drawable image = vi.getContext().getResources().getDrawable(imageResource); holder.tvWeatherImage.setImageDrawable(image);
If you want to change the image shown by default you can define it changing src property of ImageView in the list item xml file named list_row.xml
<ImageView
android:id="#+id/list_image"
android:contentDescription="#string/app_name"
android:layout_width="60dip"
android:layout_height="60dip"
android:src="#drawable/sunny" />

Background property on custom compound control

I have a compound control that essentially combines a Button with a ProgressBar. It includes a background property that I have declared in attrs.xml as:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<declare-styleable name="MyControl">
<attr name="background" format="reference" />
</declare-styleable>
</resources>
In the constructor of my control, I then pull the background out of the TypedArray and apply it.
All this worked fine until recently, when I had to add in a dependency on the v4 support library. Now I get this build error in my attrs.xml file:
Error APT0000: Attribute "background" has already been defined (APT0000)
Why is this? And what can I do as an alternative so that consumers of my compound control can set the background?
It's a conflict with an internal library that also defines the 'background' attribute.
For more details visit : https://groups.google.com/forum/#!topic/actionbarsherlock/_N0hn47zx6w
The attribute "background" has already been defined in the support library. So you dont have to define that again. you just add the attribute without defining it, like this,
If you want to use app:background in the custom view, then
<attr name="background"/> is enough in the attrs file.
If you want to use android:background in the custom view, then use this line in attrs.
<attr name="android:background"/>

colors.xml resource does not work

I created a colors.xml file in my Android app under /res/values/colors.xml. The contents are...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Green">#00ff00</color>
</resources>
I try to update the background of my a TableRow using...
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(R.color.Green);
This does not set it as green, it is gray instead. No matter what values I add to the colors.xml file, it is always the same gray color. However this does work...
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(android.graphics.Color.GREEN);
Is something wrong with my colors.xml?
You should use this instead:
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(getResources().getColor(R.color.Green));
Its unfortunate that resource ID and color have same type: int. You should get color value from resources via getColor() and use that valu as color. While you are using resource ID as color.
Try instead using the command setBackgroundResource, ie
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundResource(R.color.Green);

How to set the Default Value of a ListPreference

i need to set the defult value for a ListPreference when the Activity starts.
I've tried with ListPreference.setDefaultvalue("value"); but it makes the firts Entry of the List as default. I need it because i must check a condition and set as default the value which satisfies that condition, so I think it can't be done from the xml file (with android:defaultValue)
For example, suppose I have this array of values in the arrays.xml:
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
In the PreferenceScreen xml:
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="#array/opts"
android:entryValues="#array/opts_values" />
In the Activity I'd like to do something like this:
String mycolour;
if (something) {
mycolour="1";
} else {
mycolour="2";
}
ListPreference colour = (ListPreference) findPreference ("colour");
colour.setDefaultValue(mycolour);
But it doesn't work, because it makes the first choice as default. Could you explain me how to make another one as default? Thanks.
You don't need to programmatically handle the default value of ListPreferences. You can do this in xml setting file. Below is an example
<string-array name="opts">
<item>red</item>
<item>green</item>
<item>blue</item>
</string-array>
<string-array name="opts_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
...
<ListPreference
android:title="Colour select"
android:summary="Select your favourite"
android:key="colour"
android:entries="#array/opts"
android:entryValues="#array/opts_values"
android:defaultValue="2" />
here I selected 2 as a default value. Remember defaultvalue will be opts_values element.
Have you tried:
setValueIndex(int index);
Sorry my bad English.
List item
Retrieve the list Check if the value is null. If it is null set to the default value.
Code:
ListPreference dataPref = (ListPreference) findPreference("keyList");
if(dataPref.getValue() == null){
dataPref.setValueIndex(0); //set to index of your deafult value
}
You can set your default value by using the key like this
<string-array name="syncFrequency">
<item name="1">Block All Calls</item>
<item name="2">Block Black List</item>
<item name="3">Block Unknown Calls</item>
<item name="4">Allow White List</item>
<item name="5">Receive All Calls</item>
</string-array>
<string-array name="syncFrequencyValues">
<item name="1">Block_All_Calls</item>
<item name="2">Block_Black_List</item>
<item name="3">Block_Unknown_Calls</item>
<item name="4">Allow_White_List</item>
<item name="5">Receive_All_Calls</item>
</string-array>
<ListPreference
android:key="prefSyncFrequency"
android:entries="#array/syncFrequency"
android:summary="%s"
android:defaultValue="Block_Black_List"
android:entryValues="#array/syncFrequencyValues"
android:title="#string/call_block_options" />
or you can also try colour.setValue(mycolour);
Just for the record if someone else has this problem:
setValueIndex(int X) is setting the value # index X to the default value - so it is probably what you are looking for.
Set this value AFTER you added the Values! (stupid mistake but took me half an hour)
Actually it's because the SharedPreferences will persist after you re-build the app.
Uninstall it and try again.
((ListPreference) findPreference("pref_language")).setValue(Locale
.getDefault().getLanguage());
setValue() is ListPreference's method, and setDefaultvalue is Preference's method
This is an old post, but here's another way to set the default value for ListPreference with the following line of code:
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
Use the xml attribute android:defaultValue="<VALUE>" in your list tag to set the default value.
Note: the <VALUE> is the actual value and not the index of string
array.
If still its not working, try below steps.
Clear application data.
Uninstall and reinstall the app
Check the list preference, you will see the default value selected
Strange, I know but it worked in my case.
If you are using Android Jetpack Preference, known as androidx.preference:preference-ktx:1.1.1 in Kotlin, you can use:
app:defaultValue="<Value_in_string-array_with_values>".
Additionally: defaultValue is not an index number, is the actual value from the values array.
I also recommend using a string resource for the default value and clearing the data, uninstalling the app, or removing the file:
<package_name_in_app_manifest>_preferences.xml in data/data/shared_prefs/<package_name_in_app_manifest>. Replace <package_name_in_app_manifest> with real name like com.example.yourapp.
I was having the same issue and the defaultValue wasn't updating because it already had a wrong default value of "true". Solved it by using the Android Studio File Explorer and deleting the file.
And here is my solution example:
res/xml/root_preferences.xml
<ListPreference
app:key="mic"
app:title="#string/select_mic_title"
app:useSimpleSummaryProvider="true"
app:entries="#array/mic_entries"
app:entryValues="#array/mic_values"
app:defaultValue="#string/default_mic"
 />
res/values/arrays
<!-- Mic Preference -->
<string-array name="mic_entries">
<item>#string/default_mic</item>
<item>Headband</item>
</string-array>

<string-array name="mic_values">

 <item>#string/default_mic</item>
<item>Headband</item>

</string-array>
res/strings.xml
<string name="default_mic">Integrated</string>
Result:
Setting Fragment with default list value of "Integrated" given by string resource #string/default_mic

Categories