Android Layout Parameters. Can i change them at runtime? - android

I am new to android need bit help..
Is it possible to implement or make layout parameter change according to user's click? or can assign twice
Frankly Can I have
RelativeLayout layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
listView1.setLayoutParams(layoutParams);
and then after user click's
layoutParams = new RelativeLayout.LayoutParams(150, 150);
listView1.setLayoutParams(layoutParams);
Like this structure.
I'm getting an error.

Your code is creating layout parameters of type relative layout and adding it to listview...
layout params you create should be of type as of parent...so create them for listview as below
listview01.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
hope this helps.

Related

android - How to set the button params in the Listview

I added the Button has footer view of the List view pro-grammatically. Now i want to set the params of the button and set the button gravity as center vertical. I did in the following way but it's not working.How to implement this?
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
You currently have
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
I assume this is an oversight and you meant CENTER_VERTICAL? This will vertically align the text inside the button which it should already be doing so your question seems confusing. Are you instead asking how to align the button itself vertically? If so you need to set the gravity on the buttons parent.
As for the params, you are already setting layout params. What else are you trying to do? You should aim to be more specific in your questions so it is easier to identify what you are trying to do.
Try this:
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
btnMoreUpcomingExits = new Button(this);
btnMoreUpcomingExits.setText("More Upcoming Exits");
btnMoreUpcomingExits.setBackgroundResource(R.layout.moreupebtn_widget);
btnMoreUpcomingExits.setLayoutParams(params);
btnMoreUpcomingExits.setTextColor(Color.WHITE);
btnMoreUpcomingExits.setTypeface(null, Typeface.BOLD);
btnMoreUpcomingExits.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(btnMoreUpcomingExits);
btnMoreUpcomingExits.setOnClickListener(btnMoreUpcomingExitsListener);
getListView().addFooterView(btnMoreUpcomingExits);
setListAdapter(new UpcomingExitsResultsListViewAdapter(this));
Check this this it might help you.
Check this link
you might use WindowManager.LayoutParams to set attribute of button in listview.

How can i set margin to a view in another class?

I do a constructer and set some view inside programmatically and add some rule. so When i call constructer i see default view.
Now i want to reach someof this view and change set margin.It is basic i know but i dosent work for me.
Can anybody help?
Part of my constructer:
_photoImgVw = new ImageView(getContext());
_photoImgVw.setImageResource(R.drawable.pic);
_photoImgVw.setAdjustViewBounds(true);
_photoImgVw.setMaxWidth(100);
_photoImgVw.setId(2);
lp2=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp2.setMargins(0,0,0,0);
addView(_photoImgVw,lp2);
LayoutParams lp3=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.RIGHT_OF,_photoImgVw.getId());
lp3.setMargins(0,0,0,0);
addView(_nameTxt,lp3);
Call in the activity for change:
facebookPersonView.lp2.setMargins(10,300,100,20);
Make sure that the layout params lp2 are an instance of the class RelativeLayout.LayoutParams instead of MarginLayoutParams, or any other subclass of it named LayoutParams (there are quite a few, see the top of the documentation).
If the params are an instance of the latter, they won't contain any rules for the RelativeLayout. Which means the parent layout doesn't know where to place the view¹ and therefore can't make any sense out of the margins.
It's best to declare that explicitly in a way like this:
RelativeLayout.LayoutParams lp2
= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp2.setMargins(0,0,0,0);
addView(_photoImgVw,lp2);
¹ It will still make it visible in the top-left corner, but it's kind of an undefined state.

Android, how to add to parameters to LayoutParams

I want to add two parameters to LayoutParams:
View bar1 = new View(this);
bar1.setBackgroundResource(R.drawable.blue_bar);
RelativeLayout.LayoutParams relativeParams1 = new RelativeLayout.LayoutParams(68, (int(aValue));
relativeParams1.bottomMargin=50;
Log.i(TAB, relativeParams1.debug(OUTPUT));
linearLayout.addView(bar1, relativeParams1);
but only width and height is changing. The included logcat show me that
ViewGroup.LayoutParams={ width=68, height=300 }
How do I change the margins too?
What is the type of layout you are adding bar1 to, is it a LinearLayout ?
In that case, you can't use RelativeLayout.LayoutParams, you have to use LinearLayout.LayoutParams and, if I remember correctly, you don't have margins there.
So change the containing layout

Added Views appear at top of screen

I've been trying to add multiple textviews and buttons when onClick, and this is the best code I have found that actually works:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.RelativeLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(lp);
tv.setText("Shift" + mShiftCount);
EditText edittv = new EditText(getApplicationContext());
edittv.setLayoutParams(lp);
relative.addView(tv);
relative.addView(edittv);
This seems to be the best code to add additional items that are alike to that of what I already have in my main.XML file.
My issue is that when these are added, they appear at the top of the screen and I am unsure how to add further parameters to the objects.
How would I go about placing the textview and edittext below my other elements specified in the XML?
What you need to do is create a RelativeLayout.LayoutParams (refer here) when you add the views like tv and edittv to the RelativeLayout. In your RelativeLayout.LayoutParams, you need to define the same parameters you would give to your other RelativeLayout objects. Here is an example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.idOfWhatYouWantToBeBelowOf);
addView(tv, params);
First off there are definitely some static components to your view and some dynamic components. can you please specify what is your layout file so that i can give you a much more detail in your application.

Android FrameLayout and TextView - why does LayoutParams work where setGravity() does not?

I'm using a FrameLayout to display (on demand) some text on the screen. I want the text to be in a certain place, so I thought setGravity() would do the job... but no, it seems to have no effect whatsoever on where the text goes (and other objects like ImageView don't even have this method).
So first, what exactly is TextView.setGravity() used for? (Edit: I understand this much better now, thanks! Still not clear on the following part of the question though.)
Second, it seems the only way to update a FrameLayout in this way is to create a new FrameLayout.LayoutParams object with the settings you want, and then use the setLayoutParams() method to apply it. (This seems to automatically update the view so is a requestLayout() call necessary?) And is there a simpler / more straightforward way to achieve this for a FrameLayout... say, without creating a new LayoutParams object as I'm doing now?
Thanks! For reference, below is a (working) code snippet showing how I'm setting up this FrameLayout and TextView.
FrameLayout fl1 = new FrameLayout(this);
FrameLayout.LayoutParams flp1 = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
fl1.setId(9001);
fl1.setLayoutParams(flp1);
.....
tv1 = new TextView(this);
FrameLayout.LayoutParams tvp1 = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
tv1.setId(9006);
tv1.setLayoutParams(tvp1); // This works
tv1.setBackgroundColor(Color.GRAY);
tv1.setTextColor(Color.BLACK);
tv1.setText("Dynamic layouts ftw!");
tv1.setGravity(Gravity.CENTER); // This does NOT work
.....
fl1.addView(tv1);
1) view.setGravity means hows the view should positions if children.
In the case of the textview it refers to the positioning of the text.
When in linearlayouts or viewgroups it refers to its child views.
2) I checked your code. You are already using textview.setGravity method. In that case you dont need to specify gravity parameters in the FrameLayout.LayoutParams constructor.
Other thing I noticed is that you gave the textview the width and height as wrap content which will only take the size of the text. So there is no meaning in giving gravity as the textview has no extra area to position the text to the center. You need to give the width of the textview as fill_parent. That should make your gravity property work.
Btw this is a very good article about Gravities. It explains both about gravity and the layout_gravity attribute.
If you want your textview to wrap the content then you should add your textview to a linearlayout and setgravity to the linear layout.
This should give what your are trying to do
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
FrameLayout fr = new FrameLayout(this);
fr.setLayoutParams(lp);
fr.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(lp2);
l.setGravity(Gravity.CENTER);
l.setBackgroundColor(Color.BLUE);
ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView t = new TextView(this);
t.setLayoutParams(vp);
t.setText("Blessan Mathew");
t.setBackgroundColor(Color.CYAN);
l.addView(t);
fr.addView(l);
tv1.setGravity(Gravity.CENTER);
belongs to the gravity of the TEXT inside the TextView.
As documentation states:
Sets the horizontal alignment of the text and the vertical gravity
that will be used when there is extra space in the TextView beyond
what is required for the text itself.

Categories

Resources