I'm trying to insert a RelativeLayout dynamically. The method works well, but the layout is placed laterally and not under the existing
public void Add(View v) {
EditText et=new EditText(context);
RelativeLayout dynamic_component = new RelativeLayout(this);
dynamic_component.addView(et);
relativo.addView(dynamic_component);
}
If you want your layout "dynamic_component" to be auto-magically placed under the existing views of the parent layout, "relativo" the parent should be a LinearLayout with vertical orientation.
Related
I have Views nested like this
FrameLayout
CardView
LinearLayout
LinearLayout
LinearLayout
ImageButton
In the listener for the ImageButton, I set the top FrameLayout invisible.
What's a better, more dynamic way than this?
public void onButtonClicked(View view) {
((FrameLayout)view
.getParent()
.getParent()
.getParent()
.getParent()
.getParent())
.setVisibility(View.GONE);
}
In the layout file, give an android:id="+#id/yourName" to the view that you want it hidden, and get the view using
View viewToHide = findViewbyId(R.id.yourName);
From there, you could set the viewToHide.setVisibility(View.GONE);
I have some relative layouts in my xml file. I want to add relative layout dynamically on click of add button below last relative layout added in my xml. I have searched n find some articles on stack overflow about it but everyone has added the layout below some dynamic view.I want to add dynamic relative layout below my static relative layout.
Code-
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//get id of relative layout which is in xml
rlOption4=(RelativeLayout)view.findViewById(R.id.rlOption4);
}
if(v.getId()==R.id.imageButtonAdd)
{
Log.e("onclick","add");
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW,rlOption4.getId());
relativeLayout.setLayoutParams(lp);
EditText optNew=new EditText(getActivity());
optNew.setText("Option 5");
relativeLayout.addView(optNew);
}
If you change the line :
relativeLayout.addView(optNew);
to
rlOption4.addView(optNew);
then optNew will be appended as a child of rlOption4 in your layout.
I want to add a scroll view to all the layouts that I have. But dynamically. Because the app will run in different screen sizes, and when I will get a screen size smaller than a specific size, then I want to show the layout in a scroll view.
So I made this method, it will be called on the check that the screen is small. I will pass my activity and I want to change the root layout to scroll view or just add a ScrollView as the root layout. So if the root layout is a LinearLayout, then I want to put that layout in the ScrollView. And I have not named all the layouts, meaning that I didn't give an ID to the layout, so I cannot use findViewById.
public static void SetActivityRoot(Activity c) {
View v = c.getWindow().getDecorView();
// View v = v.getRootView();
ScrollView sv = new ScrollView(c);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
sv.setLayoutParams(lp);
((ViewGroup)v.getParent()).removeView(v);
sv.addView((View) v);
((ViewGroup)v.getParent()).addView(sv);
}
It's giving me an error saying that "you cannot remove view from null" etc. Or that "you cannot add view to layout as it already has parent view". How can I make this work?
Finally solved my problem.
public static void SetActivityRoot(Activity c) {
View v = ((ViewGroup)c.findViewById(android.R.id.content)).getChildAt(0);
ScrollView sv = new ScrollView(c);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
sv.setLayoutParams(lp);
((ViewGroup) v.getParent()).removeAllViews();
sv.addView((View) v);
c.addContentView(sv, lp);
}
I want to create a layout (see class RosterPlayerView below) that comprises an image with text below it and then instantiate that view multiple times in a relative layout. I used relative layout instead of linear as the layout will become more complex.
When I first ran the code below (but without the setId calls) the text appeared above the image. Thanks to this stack overflow article I discovered that relative layout needs unique widget ids to work. But when I added the setId() calls the text view is not displayed at all.
What am I doing wrong?
public class RosterPlayerView extends RelativeLayout {
ImageView imageView;
TextView textView;
static int layoutId = 100;
public RosterPlayerView(Context context, int playerId, Drawable photo) {
super(context);
imageView = new ImageView(context);
textView = new TextView(context);
addView(imageView, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setId(layoutId++);
RelativeLayout.LayoutParams timeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
timeLayoutParams.addRule(RelativeLayout.BELOW, imageView.getId());
addView(textView, timeLayoutParams);
imageView.setImageDrawable(photo);
textView.setId(layoutId++);
textView.setText("0:00");
}
}
a LinearLayout would be an awful lot simpler for what you are trying to do. So would inflating an XML layout, for that matter.
Try to set the Id of you imageView before adding it to the layout.
You can also create a LinearLayout with the imageView and textView inside before adding it to the RelativeLayout
i'm adding programatically 3 textviews into a framelayout that haves a camera view.
The three textviews are writting in the same position, but i want to put each textview bottom to another (using framelayout)
I dont know how to do it, i can't find any examples or info about doing this with framelayout programatically, and also i didnt find the way to do it with setlayoutparams, because that method doesn't have x/y parameters or something like that.
here is the code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
cv = new CustomCameraView(this.getApplicationContext());
FrameLayout rl = new FrameLayout(this.getApplicationContext());
setContentView(rl);
rl.addView(cv);
tv1=new TextView(getApplicationContext());
tv2=new TextView(getApplicationContext());
tv3=new TextView(getApplicationContext());
rl.addView(tv1);
rl.addView(tv2);
rl.addView(tv3);
tv1.setText("Test1");
tv2.setText("Test2");
tv3.setText("Test3");
}
Make a linearLayout, add the textViews to this LinearLayout , and add this linearLayout to your FrameLayout. Use the orientation of LinearLayout vertical.
use LinearLayout.setOrientation(LinearLayout.VERTICAL), method for setting orientation to vertical.