Skip to content

Instantly share code, notes, and snippets.

@li2
Last active March 20, 2018 11:48
Show Gist options
  • Save li2/97800cadf7f13195023653bd4cd3eb0b to your computer and use it in GitHub Desktop.
Save li2/97800cadf7f13195023653bd4cd3eb0b to your computer and use it in GitHub Desktop.
[How to add views dynamically to a RelativeLayout already declared in the xml layout?] #tags: android-view
private void addPageTurnButton() {
View view = getLayoutInflater().inflate(R.layout.widget_page_turn, null);
/*
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
view.setLayoutParams(params);
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.rootLayout);
rootLayout.addView(view);
*/
// android.support.v7.widget.ContentFrameLayout cannot be cast to android.widget.RelativeLayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
view.setLayoutParams(params);
view.bringToFront();
FrameLayout rootLayout = (FrameLayout) findViewById(R.id.contentFragmentContainer);
rootLayout.addView(view);
Button pageUpButton = (Button) view.findViewById(R.id.widgetPageUp);
pageUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Button pageDownButton = (Button) view.findViewById(R.id.widgetPageDown);
pageDownButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment