0

I am trying to read the custom attribute without extending the View. I am following this post:- How to read custom attributes in Android

Is it possible to add this in only one activity? or this is needed to be added whereever the custom attributes are needed to be fetched?

public abstract class BaseDialog extends DialogFragment {

@Override
public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getLayoutInflater().setFactory(new CustomAttrFactory());
}

public static class CustomAttrFactory implements LayoutInflater.Factory {

    @Override
    public View onCreateView(String name, Context context,
                             AttributeSet attrs) {
        String attributeValue = attrs
                .getAttributeValue(
                        "http://schemas.android.com/apk/res-auto",
                        "stringId");
        System.out.println("Attribute obtained using this method/" + attributeValue);
        // if attributeValue is non null then you know the attribute is
        // present on this view(you can use the name to identify the view,
        // or its id attribute)
        return null;
    }
}

The above implementation is not working for me.

Sahil
  • 75
  • 2
  • 9
  • Yes, it does need to be in every `Activity` where you need to do that. However, you could create a base `Activity` class that all of your other Activities extend, and set the `Factory` there, so you aren't repeating code in each one. – Mike M. Mar 11 '20 at 21:11
  • @MikeM. My use case is where I will specify the string Id in custom attribute for which I need to call a function to get the translation for the same. Now for all the views that are specified in the layout, I have to call this function. Will this be possible using the above implementation? – Sahil Mar 12 '20 at 13:15
  • Sure will. In fact, now that I think about it, you don't even need to set a `Factory` yourself. `Activity` is already a `Factory` for its own `LayoutInflater`, so in your `BaseActivity`, for example, simply override `onCreateView()`, and do your translation lookup there. Then, for each `Activity` that needs that behavior, simply subclass that base; e.g., `public class MainActivity extends BaseActivity`. – Mike M. Mar 12 '20 at 13:30
  • I should mention that there will actually be two `onCreateView()` methods that you can override in `Activity`; one like that shown in Luksprog's answer, and another one that has an additional `View parent` parameter. Either one will work (unless you're running on a super-ancient Android version, which is unlikely), so pick whichever you feel is appropriate for your use. – Mike M. Mar 12 '20 at 13:38
  • @MikeM. but suppose I have two Buttons in a layout, how will the translation occur for both of them? – Sahil Mar 13 '20 at 06:17
  • I'm not sure what you're asking. I don't really know the details of what you're trying to do. I was simply telling you how to use that `Factory` method in all of your `Activity` classes, per the stated question. – Mike M. Mar 13 '20 at 06:21
  • @MikeM., SO here is amn example: - The factory Method will be used to fetch the custom attribute of all the TextViews in the activity for which I will call a method. Now my question is how to access all of those attributes and change them when I have only one overridden OnCreateView. – Sahil Mar 13 '20 at 16:14
  • I'm still not quite sure what you're asking, but `onCreateView()` will be called separately for each individual tag that the `LayoutInflater` encounters. For example, say your layout is a `LinearLayout` with two `Button`s. `onCreateView()` will be called three times: once for the `LinearLayout`, then once for the first `Button`, and once more for the second `Button`. It's not like `Fragment`'s `onCreateView()`, which is called only once for the whole thing, if that's what you're thinking. – Mike M. Mar 13 '20 at 17:00
  • @MikeM., I am trying to implement this in BaseDialog that extends DialogFragment and this BaseDialog is used by each of my Dialog boxes. But this is not running for this Dialog. I am overriding the onViewCreated method of DialogFragment. Details updated above. – Sahil Mar 14 '20 at 10:59

0 Answers0