I would like to set an id to my layout programmatically but it always shows an error even thoug it executes just fine,
How do i end the error displayed in the code:
RelativeLayout newlayout = new RelativeLayout(getContext());
newlayout.setBackgroundColor(Color.GREEN);
newlayout.setId(int 12);
The execution is okay but my code always shows an error
Create a res folder for ids:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="id1" />
<item type="id" name="id2" />
<item type="id" name="id3" />
</resources>
Then add that resource ID to the setID() method:
newLayout.setId(R.id.id1);
Related
Is there a way to get a drawable resource from a boolean resource?
For example:
bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="use_version_1_drawables">true</bool>
</resources>
my_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/my_drawable_version_1"/>
<item android:drawable="#drawable/my_drawable_version_2"/>
</selector>
Is there a specific state I should be using because from what I understand they are all related to specific events (checking, focusing, etc). Perhaps I shouldn't be using a selector. I simply want to have one resource I can call upon that's actually linking to two others but will select one based on my bool.
You are close. You can think of a selector as an if/else statement. What you're missing now is your actual state. Here is an example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimaryDark" android:state_checked="true" />
<item android:color="#color/gray" android:state_checked="false" />
</selector>
If you're using it with something like a Checkbox, then just set this selector as a drawable resource for the checkbox, and state_checked will be updated automatically.
EDIT: Problem was slightly more complex, and solved in the comments.
I added a custom attribute to a textview called font. Which automatically loads a custom font.
Example xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.egeniq.widget.TextView
android:layout_width="match_parent"
android:layout_height="44dp"
app:font="MyriadPro"
android:text="#string/login_welcome" />
</RelativeLayout>
The custom widget does the following in its constructors:
TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TextView);
String customFont = styledAttrs.getString(font);
// snip. The error occurs here
The R.styleable.TextView is declared as such:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="font" format="string" />
<!-- TextView -->
<declare-styleable name="TextView">
<attr name="font" />
</declare-styleable>
</resources>
When everything is defined as above it works. the variable String customFont is set to the proper name (MyriadPro).
When trying to auto-apply this value trough a style the customFont string is empty.
style is declared as such:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.BicycleBuddy" parent="android:Theme.Light.NoTitleBar">
<item name="android:textViewStyle">#style/Widget.TextView</item>
</style>
<!-- Override defaults -->
<style name="Widget.TextView" parent="#android:style/Widget.TextView">
<item name="font">MyriadPro</item>
</style>
</resources>
The theme is included in the manifest:
<application
android:name=".Application"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.BicycleBuddy" >
</application>
The <item name="font"></item> gives no errors. The custom widget is loaded and hits the breakpoint. However like said above. if used like this the 'customFont' property is null after reading it.
Screenshots of the stack: The first (where customFont is not null) is where the XML itself includes the app:font="" property. The second, is where it does not include this property, but where it should be loaded from the style
The reason the View element is different is because these are simply 2 different textviews. with the exact same propertys. only 1 includes app:font="" and the other does not. Breakpoints were hit 1 after the other in the order that I would expect.
As you see, virtually everything is equal. Even the ID behind styledAttrs is the same. Yet if it do not explicitly declare the font property in the XML it isn't loaded.
I hope someone directly sees what connection I am missing. but I do realise this is a rather vague issue.
Please note that com.egeniq.* is a custom library attached to my project. The styles.xml is declared in the 'main project', while attrs.xml is from the library project
In my app I have ImageView that is added programmatically in onLayout method of its parent. I want it to have different background when it is focused different when pressed and different when on normal state. I defined some colors in values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="cream">#FBF9F5</color>
<color name="green">#93D932</color>
<color name="blue">#0095FF</color>
</resources>
Then I defined selector in drawables folder named table_background_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/green" />:
<item android:state_pressed="true" android:drawable="#color/blue" />:
<item android:drawable="#color/cream" />:
</selector>
Then i onLayout method of its parent after I initialized it I try to set background with:
v.setBackground(getContext().getResources().getDrawable(R.drawable.table_background_selector));
And that's when IntelliJ shows me problem:
"Cannot resolve method 'setBackground(android.graphics.drawable.Drawable)'"
And I fail to understand why because View class has method
setBackground(Drawable background)
Here is .png I set with setImageResource:
http://imgur.com/Skwzd2z
Center of it is transparent and now it shows whatever background is set to app but I want it to display different colors when pressed or focused
I would be thankful for any help
Check that you not importing the android R file by mistake instead of your local one, it certainly looks so.
UPDATE:
and for setting ImageView background do this:
image.setImageResource(R.drawable.your_drawable_resource);
first clean and builder you project still you getting same error then
Please check in import area whether you included this statement
import android.R;
if yes then you need to use your package name with include R like my.package.R.
I have the following code in my activity:
......
DataCell[i] = new TextView(context,null,R.style.TitleRow);
DataCell[i].setText(data[i]);
......
Here is my style.xml file, which is in res >> values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TitleRow" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#A0A0A0</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:paddingLeft">2dp</item>
<item name="android:paddingRight">2dp</item>
</style>
</resources>
No value is getting displayed..
But if I use DataCell[i] = new TextView(context) , its working fine. I know there is some problem with the fact that I am using null for attribute set. But after searching for a long time I am unable to find a perfect example of how to do it. I hope someone could clarify this to me once and for all.
Try to inflate your TextView. Like this:
layout/cell_row.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/TitleRow">
YourActivity.java
......
LayoutInflater inflater = getLayoutInflater(); // called from activity
......
DataCell[i] = inflater.inflate(R.layout.cell_row, null);
// or inflater.inflate(R.layout.cell_row, root, false); where root is a parent view for created TexView
DataCell[i].setText(data[i]);
......
I usually end up creating a simple layout file only containing the element I want to instantiate ( TextView in your case ) that has the proper style applied and use the layout inflater to create the instance for me.
layout file: styled_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView ... style="#+style/MyStyle" ... />
code:
activity.getLayoutInflater().inflate(R.layout.styled_textview, <root_node>, true);
I have a listview of countries in non-alphabetic order and started using fastscroll. I would like to display the country-flag when scrolling with fastscroll but it seems like the APIs has the FastScroll class as private so I cannot override it.
Have anyone else implemented a custom fastscroll view?
References:
http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:fastScrollEnabled
In your ListView XML definition, add
android:fastScrollEnabled="true"
or in code
listView.setFastScrollEnabled(true);
Create file fastscroll_thumb.xml in the res/drawable folder as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/fastscroll_pressed" />
<item android:drawable="#drawable/fastscroll" />
</selector>
In AndroidManifest.xml, set a custom theme for your application:
<application
android:theme="#style/ApplicationTheme"
...>
Create a values folder in the res folder. Create themes.xml files in res/values as follows:
<resources>
<style name="ApplicationTheme">
<item name="android:fastScrollThumbDrawable">#drawable/fastscroll_thumb</item>
</style>
</resources>
Lastly make sure that fastscroll.png and fastscroll_pressed.png exist in your drawable folder
(optional)
You can also set fast scroll always visible while you are debugging if you like
listView.setFastScrollAlwaysVisible(true);
or in XML
android:fastScrollAlwaysVisible="true"