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).
Related
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);
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);
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);
I declared a RelativeLayout in a xml layout file. Now I want to add Views from code to the existing Layout. I added a Button dynamically to this existing layout as below through code:
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
Now I need to add another Button to the right of the already added Button. I am not able to find the way in which I can add the new one to the right of the previously added button.
Add the rule RelativeLayout.RIGHT_OF for the second added Button LayoutParams:
// first Button
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
// second Button
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv2 = new Button(this);
tv1.setText("Hello2");
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
tv2.setLayoutParams(newParams);
tv2.setId(2);
rLayout.addView(tv2);
may be this can help you, try it.
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TableLayout tl=new TableLayout(this);
rLayout.addView(tl);
TableRow tr1=new TableRow(this);
tl.addView(tr1);
Button btn1 = new Button(this);
btn1.setText("Hello");
btn1.setLayoutParams(lprams);
btn1.setId(1);
tr1.addView(btn1);
TextView tv1 = new TextView(this);
tv1.setWidth(40);
tv1.setHeight(LayoutParams.WRAP_CONTENT);
tr1.addView(tv1);
Button btn2 = new Button(this);
btn2.setText("World");
btn2.setLayoutParams(lprams);
btn2.setId(2);
tr1.addView(btn2);
Create another button:
Button tv2 = new Button(this);
tv2.setText("World");
tv2.setLayoutParams(lprams);
tv2.setId(2);
Add add it into your RelativeLayout:
rLayout.addView(tv2);
I' m at this point stuck : I want to have a scrollview + a fixed button at the bottom, but Programatically Way ! I can' t go with XML for some technical reason.
Actually i have this :
//Is it really usefull Relative View?
RelativeLayout layout = new RelativeLayout(this);
ScrollView sv = new ScrollView(this);
sv.setId(2);
// What is it? RelativeLayout.LayoutParams?
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, sv.getId());
sv.setLayoutParams(new ViewGroup.LayoutParams(480, 800));
layout.addView(saveButton, lp);
layout.addView(sv);
I do the first 3 page Google on "fixed button and scrollview Android programatically"
Im beginner on Android, so, don' t hesitate to comment on my code some hints ;)
Thx for your help.
try this
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(lp);
ScrollView scroll = new ScrollView(this);
LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,0, 1.0f);
scroll.setLayoutParams(slp);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
layout.addView(scroll);
layout.addView(btn);
setContentView(layout);
If you are trying to create a view where the upper portion of the screen is a scrolling list, and the bottom portion is a button, put the scrollview inside a linearlayout, and put the button in the linearlayout below the scrollview.
http://developer.android.com/reference/android/widget/LinearLayout.html