Adding dynamic views in a fragment

Have an empty xml layout with just a RelativeLayout called “mainLayout”(or what ever you want to call it). That way you can append any dynamically generated controls

 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{

    Button myButton = new Button(Builtprofile.context);
    myButton.setText("Press me");
    myButton.setBackgroundColor(Color.YELLOW);

    RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);

    buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);

    View rootView = inflater.inflate(R.layout.q1, container, false);

    RelativeLayout myLayout = (RelativeLayout)rootView.findViewById(R.id.mainLayout);
    myLayout.setBackgroundColor(Color.BLUE);

    myLayout.addView(myButton, buttonParams);

    return rootView;
}
Advertisements

How to add Notification Counter in Android

Recently I had a chance to develop a social networking application, I had to create a notification bubble over friends and messages, So I managed to do it like this.

OZ4qe

This can be achieved with this library. https://github.com/jgilfelt/android-viewbadger

Include library in your project and add below code for your view.

View target = findViewById(R.id.target_view);
BadgeView badge = new BadgeView(this, target);
badge.setText("1");
badge.show();