set textsize for multiple textview with same ID - android

Is it possible to set textsize for multiple textview with same ID ? I have them with android:id :"#+id/lbl" ? Actually I set dp based on folder values/dimen.xml
TextView lbl=(TextView) findViewById(R.id.lbl);
lbl.setTextSize(dp);
The first textview only get this effect, but the rest of textview dont get this effect. Let me know how to solve this problem.

You need to do it separately as every activty/fragment/adapter has its own layouts (xml). In R.java they would have its own unique number.
android:textSize="24sp"
Everytime you add the views in XML, just add the text size

In android platform each view must have a unique id identified by "android:id" attribute of view. Although you are able to define multiple views with same id but android very first declaration and leaves the rest view as having no id.
Lets have an example now:
<TextView
android:id="#+id/response_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="27dp"
android:layout_toLeftOf="#+id/scan_format"
android:text="TextView" />
<TextView
android:id="#+id/response_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/response_text"
android:layout_marginBottom="74dp"
android:layout_toLeftOf="#+id/response_text"
android:text="Trepeattet" />
here are two textviews with same id, and now see R.java
public static final int radio=0x7f05002f;
public static final int response_text=0x7f050040;
public static final int scan_button=0x7f05003c;
public static final int scan_content=0x7f05003e;
public static final int scan_format=0x7f05003d;
in R.java only one textview is declared with its id(this hex id is created by platform compiler itself when you first run your code).
So although application does not produce any error in compile time or run time but will not do anything to any other view with same id either except very first one because of only one reference available that belongs to first textview.
As per this post here is what you can do:
View IDs are plain integers, so you can simply have an array of ids you want to change. For example:
int[] ids = { R.id.lbl1, R.id.lbl2, ... };
for (int id: ids) {
TextView lbl=(TextView) findViewById(id);
lbl.setTextSize(dp);
}
This will work.

You can get all TextViews of your parent view. Then check their ids in a loop

Related

Android BindAdapter with custom attribute with custom values (similar to visible attribute)

I've successfully made my first bind adapter and I wish to know a bit more about it.
I want to know how to make an attribute that can get only specific strings for a different state for my view.
For example every view has the visibility attribute that it can be"gone", "visible", "invisible"
<TextView
android:id="#+id/loading_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#id/inventory_items_recycler"
app:layout_constraintEnd_toEndOf="#+id/inventory_items_recycler"
app:layout_constraintStart_toStartOf="#id/inventory_items_recycler"
app:layout_constraintBottom_toBottomOf="#id/inventory_items_recycler"
android:textSize="18sp"
android:visibility="gone"
app:item_id="#{ItemID.BLACK_GLOVES.ordinal()}"
/>
I've made a custom attribute called item_id that get a number that represent enum value. And in my binding utils I have this code:
#BindingAdapter("item_id")
public static void setItemName(TextView tv, int itemId) {
tv.setText(ItemData.get(ItemID.values()[itemId]).getName());
}
I prefer to have something similar to the visibility attribute that it value can be either "visible", "invisible" or "gone"
Bonus::
I wish android studio can auto-complete me for the possibilities that I can use.
You could pass directly the enum to your binding adapter, instead of converting it first to an int and than back to enum.
#BindingAdapter("item_id")
public static void setItemName(TextView tv, ItemID itemId) {
..
}
Then you could pass directly the enum in your xml:
app:item_id="#{ItemID.BLACK_GLOVES}"
This way you'll have a limited number of possibilities to enter and will be less likely to accidentally enter a meaningless integer.
However, binding adapters and custom attibutes are different. With a binding adapter, you still need to use the syntax of binding expression, ie: "#{ }".
android:visibility , on the other hand, is an attribute. You can also define custom attributes for your custom views and get something similar (have a limited number of input options and IDE shows you your options etc). But you shouldn't confuse that with binding adapters. These are two different concepts.
Try this:
#BindingAdapter("isGone")
#JvmStatic
fun View.setVisibility(isGone: Boolean) {
if (isGone) this.visibility = View.GONE else View.VISIBLE
}
Inside your xml:
<com.google.android.material.checkbox.MaterialCheckBox
android:id="#+id/cb_class"
style="#style/TextStyleNormal.White"
android:layout_marginStart="#dimen/margin_large"
isGone="#{isSharedDailyActivity}"// it take boolean value
app:buttonTint="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:useMaterialThemeColors="true" />

How we define the id in kotlin without any use of third party library?

In java, we used to define the id by using findviewbyid. I am Wondering how we can define the id in kotlin without any use of third party library.
You don't have to define the view id's in Kotlin. All you have to do is use a non declared variable which has same name as the view in layout xml file. This reduces the chance of you running into a bug.
Assume you have this TextView in the layout xml
<TextView
android:id="#+id/mytextview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="18sp"/>
then this is how you can access this by a variable name mytextview, without declaring it in the file. This is how you would set the text
mytextview.text = "My text view"
You can read more at https://kotlinlang.org/docs/tutorials/android-plugin.html
Another way could be following but I wouldn't suggest it
private var textview: TextView? = null
textview = findViewById(R.id.mytextview) as TextView // old way
textview = findViewById<TextView>(R.id.mytextview) // new way

No resource found error in layout xml file android

I get this error in xml file very often.
here is the code in xml file
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_above="#id/tRowMain" // in this line i get error resource not found that matches given name
android:textColor="#color/selectLevel"
android:id="#+id/tvOnOption"
android:text="Select Mode"
/>
<TableRow android:layout_width="fill_parent" android:id="#+id/tRowMain"
android:layout_height="wrap_content" android:gravity="center" android:layout_centerVertical="true" android:layout_centerHorizontal="true" >
//then i checked in R.java file and the id for this name is there
public static final class id {
public static final int ibtn_retry=0x7f060006;
public static final int rLayoutMain=0x7f060000;
public static final int tRowMain=0x7f060002;
}
please help me figure out whats wrong with this...
thanks
You should use the #+id/tRowMain syntax in the first place the ID is used, not necessarily the first place where you define it as the ID of the element.
Change:
android:layout_above="#id/tRowMain" to android:layout_above="#+id/tRowMain"
and
android:id="#+id/tRowMain" to android:id="#id/tRowMain
In other words, when deciding whether or not to use #+id or #id, it doesn't matter which attribute you're assigning the id to. Always use #+id the first time you mention your ID in the XML.
android:layout_above="#+id/tRowMain"
If it does'nt work delete your R.java file. It will be re-generated

Android button id

If I have created a button in xml and i want to use this button multiple times but give them unique id's, how do i do that? I don't know how many buttons i will have, so i can't create a number of buttons in my xml-file. I want to be able to change the buttons id while running the program. I've tried to do button.setId() but then everything breaks and wont work.
You can make an independent button.xml file (or even do it as a style), then inflate that in your code as needed.
For instance, let's say you have an array of strings representing a list of countries, and you want a button for each country. This is nice because if you add or remove any, you only have to modify the array, not your xml or the for loop.
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
String[] countries = {"US", "Canada", "UK", "Australia"};
String country;
// Don't forget to add the following layout to your xml.
LinearLayout buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
buttonLayout.removeAllViews();
for (int i = 0; i < countries.length(); i++) {
country = countries.getString(i);
Button temp = (Button)inflater.inflate(R.layout.button);
// Don't forget that id is an int, not a string.
button.setId(id);
button.setText(country);
buttonLayout.addView(temp);
}
Bonus: you can also include this button in another xml file, and update the id in the include statement as follows:
<include layout="#layout/button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
/>
The question is not very clear, but I think you can use dynamic button creation.
Button button = new Button();
// init it here
layout.add(button, new LayoutParams(...));
give id in xml like
<Button android:id="#+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/title_close" />
When you are not sure of the instances you need in your application of a certain widget, then go for dynamic creation.
XML is mostly for static creation.

Android: Get an attribute's value from an XML item

Is there an easy way to grab a attribute value from an xml item in your Java class definition? I'm looking for something like this:
// In xml layout:
<TextView android:id="#+id/MyXMLitem" android:textColor="#000000" />
// in Java Class definition
String some_text_color;
some_text_color = R.id.MyXMLitem.attr.textColor; // I'd like this to return "#000000"
I know you can grab similar xml attributes from the converted objects using getters/setters like View.getText()... I'm just wondering if there's a way to grab an xml attribute right from the item itself.
Views take the XML values and store them into class level variables in their constructors so it's not possible to get values from the object itself once the layout has been created.
You can see this in the source of the View object at https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/View.java
if you search for AttributeSet (which is the object used to pass the XML layout values to the constructor).
You can use XmlResourceParser to read data straight from XML resources.
<TextView android:id="#+id/MyXMLitem" android:textColor="#000000" />
You can use getCurrentTextColor().
TextView tv = (TextView)findViewbyId(R.id.MyXMLitem);
String color = Integer.toHexString(tv.getCurrentTextColor());
It returns ff000000 instead of #000000 though.

Categories

Resources