Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a Android custom view #3251

Closed
jusierra opened this issue Oct 6, 2015 · 11 comments
Closed

Create a Android custom view #3251

jusierra opened this issue Oct 6, 2015 · 11 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@jusierra
Copy link

jusierra commented Oct 6, 2015

I try to implement a custom view in a Android app. In the following code, my Toast is called but then I would like to apply a layout to my view.

public class YouTubeView extends View {

            public YouTubeView(Context context, Context activity) {
                  super(context);
                  Toast.makeText(context, "Well done.", Toast.LENGTH_LONG).show();
            }
}

Can I extend my class from something else to be able to add LinearLayout or whatever, set height and width and put some elements inside ? Can I use a xml layout that I could inflate ? The doc is not so accurate ...

I hope to be clear in my question.
Thank you very much for your answers.

@Pajn
Copy link
Contributor

Pajn commented Oct 6, 2015

You should be able to use or create any Android view. I did wrap the TabLayout which do have the tabs as subviews in a layout (can't remember which strait from my head).

@jusierra
Copy link
Author

jusierra commented Oct 9, 2015

I finally found how to do, sorry for the useless issue.

@jusierra jusierra closed this as completed Oct 9, 2015
@amilcar-andrade
Copy link
Contributor

@jujumoz how did you do it? I am facing a similar problem

@amilcar-andrade
Copy link
Contributor

I am trying to inflate a LL with an xml file

@amilcar-andrade
Copy link
Contributor

Nevermind I figured it out, they should add a better example....

@jusierra
Copy link
Author

@amilcar-andrade : Sorry for the delay.

In your ReactPackage :

@Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Arrays.<ViewManager>asList(
                new CustomViewManager()
        );
    }

And then two classes :

public class CustomViewManager extends SimpleViewManager<CustomView> {
 ....
  @Override
    protected CustomView createViewInstance(ThemedReactContext themedReactContext) {
        return new CustomView(themedReactContext);
    }
...
}

and 

public class CustomView extends LinearLayout {
...
  public void init() {
        inflate(getContext(), R.layout.custom_layout, this);
...
}
...
}

In the res/layout folder of your module, just add the custom_layout.xml .

@helengray
Copy link

I custom layout witch dynamic add child view by RN setState.The first render the view,it just display ok,but when I change the prop 'source'(the view ReactProp) by setState,it can't display the child view.I had set the view width and height.can you help how to solve

@riteshvish
Copy link

@jujumoz Can You share your full code please..

@minhnhatspk
Copy link

@jujumoz Hi Jujumoz , can you share your full code ?

@jbrown400
Copy link

@minhnhatspk, @riteshvish Here is what I have working:

public class MyNativeComponentManager extends SimpleViewManager<View>{
   @Override
    public CustomView createViewInstance(ThemedReactContext context){
        return new CustomView(context);
    }
}

class CustomView extends LinearLayout {
    public CustomView(Context context) {
        super(context);
        init();
    }
    public void init() {
        inflate(getContext(), R.layout.custom_layout, this);
    }
}

House your custom_layout.xml inside main/res/layout and you should be good!
You can convert the linear layout to a constraint layouts if you would like as well.

@kaleai
Copy link

kaleai commented Apr 14, 2018

@helengray Did you fix it?

@facebook facebook locked as resolved and limited conversation to collaborators Jul 21, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

9 participants