Is it possible set wrapContent attribute on view without parrent? - android

I have a simple button view, and set it as content view. Is it possible to resize this view programmaticly to "wrap_content"?
Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);

Set your attributes using the following code:
Button button = new Button(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setText("Some text");
setContentView(button);

You can not set LayoutParams for a view without parent.

You have to use LinearLayout.LayoutParams with something like this:
Button button = new Button(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
params.weight = 1.0f;
button.setText("Some text");
setContentView(button);

Related

Android layout dynamically issue

Trying to dynamically add a textView and a button into a LinearLayout, then add this layout into a main layout that setContentView use
So its something like this
T - text view
B - button
Ltb - layout that contains T and B
Lm - Main layout that contains Ltb
Then use this.setContentView(Lm) to show to result
Roles:
T must be on the left.
B must be on right of the screen within the layout
All element above are declared dynamically, without using layout xml
Actual result:
Display fine. but when I type in text that is longer than the screen width, the Button got pushed outside of the screen and gone.
Problem, is it something my dynamic layout doing wrong ?
Code here:
public SearchBar(Context c){
et=new EditText(c);
bt=new Button(c);
et.setHint("added et");
bt.setText("added btn");
ll=new LinearLayout(c);
setLinearLayout();
et.setLayoutParams(flowLeft());
bt.setLayoutParams(flowRight());
ll.addView(et);
ll.addView(bt);
}
private RelativeLayout.LayoutParams flowRight(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
return params;
}
private RelativeLayout.LayoutParams flowLeft(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
return params;
}
private void setLinearLayout(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(params);
}
// try this way, hope this will help you...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ltb = new LinearLayout(this);
TextView T = new TextView(this);
Button B =new Button(this);
LinearLayout.LayoutParams ltbParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams TParms = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1f);
LinearLayout.LayoutParams BParms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
T.setLayoutParams(TParms);
B.setLayoutParams(BParms);
T.setText("Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text");
B.setText("Button");
ltb.addView(T);
ltb.addView(B);
ltb.setLayoutParams(ltbParams);
setContentView(ltb);
}
Do like this. This may help you.
Ltb.setWeightSum(100);
Ltb.setOrientation(LinearLayout.HORIZONTAL);
T.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));
B.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));

how to arrange button at bottom of scrollview programatically

Here is my code to display button at bottom of scroll view programmatically.but only scrollview is displaying.button is not displaying on the screen.how to fix this.
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
ll.setLayoutParams(lp);
ScrollView scrollView = new ScrollView(this);
TableLayout resultLayout = new TableLayout(this);
resultLayout.setStretchAllColumns(true);
resultLayout.setShrinkAllColumns(true);
scrollView.addView(resultLayout);
ll.addView(scrollView);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
//ll.addView(rl1);
ll.addView(btn);
setContentView(ll);
screen of scroll view which contains
Try this approach... Change your ll (Linear Layout) to relative layout then put the properties for button to attach at layout bottom and and for scroll view to attach above button....hope you get my point....
Change your current layout LinearLayout with RelativeLayout and use this params. it will work
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Just Add the Footer to the Scroll View and then get the button as you wish.
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
Then Add the view to your Scroll View.
ll.addFooterView(btn);

Edit Android SFGameView layout programmatically

I'd like to insert a pause button and a Highscore text for my game with the pause button at the upper left of the gameview and the highscore text at the upper right. I'd like to code it programmaticaly, but the highscore and button are not showing up. Here's the code :
gameView = new SFGameView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView textBox = new TextView(SFEngine.context);
textBox.setText("HIGH SCORE");
textBox.setId(1);
Button pauseButton = new Button(SFEngine.context);
pauseButton.setText("PAUSE");
pauseButton.setGravity(Gravity.RIGHT);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, textBox.getId());
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(textBox);
layout.addView(pauseButton,lp);
layout.addView(gameView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
setContentView(layout,rlp);
here's the button listener, the button won't respond to the touch event
pauseButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
try{
Thread.sleep(1000);
}
catch(InterruptedException ex){
}
return true;
}
});
You need to add the LayoutParams correctly. Each View needs LayoutParams of the type of its parent. E.g. your TextView and Button need RelativeLayout.LayoutParams, and your RelativeLayout needs FrameLayout.LayoutParams (as this is the built-in container for anything you add in setContentView).
For the pauseButton set the RelativeLayout.LayoutParams to WRAP_CONTENT & WRAP_CONTENT, with rules ALIGN_PARENT_TOP & ALIGN_PARENT_RIGHT.
For the textBox add another RelativeLayout.LayoutParams with WRAP_CONTENT & WRAP_CONTENT and rules ALIGN_PARENT_TOP & ALIGN_PARENT_LEFT.
Then the RelativeLayout named layout needs FrameLayout.LayoutParams with MATCH_PARENT & MATCH_PARENT.
Example code:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
SFGameView game = new SFGameView(this);
game.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Button btn = new Button(this);
btn.setText("||");
RelativeLayout.LayoutParams btnLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
btn.setLayoutParams(btnLP);
TextView tv = new TextView(this);
tv.setText("Some Text");
RelativeLayout.LayoutParams textLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tv.setLayoutParams(textLP);
layout.addView(game);
layout.addView(btn);
layout.addView(tv);
setContentView(layout);
As for the listener. Use setOnClickListener (not onTouch).

Set Button on Runtime

I am creating button on runtime like below...
Button newCategoryButton = new Button(this);
newCategoryButton.setText(catName);
newCategoryButton.setWidth(30);
newCategoryButton.setBackgroundResource(R.drawable.bnt);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(newCategoryButton, lp);
But my problem is that it disappear when I back from that activity.
I want this permanent. And another problem is that how to set this button below on other button??
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
Button newCategoryButton=new Button(this)
newCategoryButton.setText("Push Me");
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ll.addView(view, newCategoryButton);
You should put this code in the onResume(), not on the onCreate() method ;-)
If you want to use the button under another one, you will need to use a LinearLayout with a vertical orientation!
LinearLayout myll = (LinearLayout) findViewById(R.id.yourLinearLayout);
myll.setLayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
myll.setOrientation(LinearLayout.VERTICAL);
myll.addView(view, Button1);
myll.addView(view, Button2);
setContentView(myll)

fixed button on the linearlayout

A have the button on the bottom of linear layout. The other content in this layout may change its height. Due to this the button moves up. How to make the button to be "fixed" on the layout?
you have to use RelativeLayout....
dynamic of your code
RelativeLayout rl = new RelativeLayout(this);
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rl.setLayoutParams(params);
Button button = new Button(this);
button.setText("Previous");
LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button.setLayoutParams(params1);
rl.addView(button);

Categories

Resources