Android Layout question - android

I need to create a map with items on it (the map consists of a drawable object, which represents a room) and I thought about using buttons with background images for the items so that they are clickable.
I guess the AbsoluteLayout fits here the best, but unfortunately it's deprecated.
What layout would you recommend me for this kind of application ? Is there another layout which supports X/Y coordinates ?

Relative Layout supports X,Y coords -- and that would probably be the best, since you can set the layout relative to the map instead of the screen.

Cool. THanks so much !
Here is the code, if somebody needs it too.
RelativeLayout RL = new RelativeLayout(this);
RL.addView(V, RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
Button t = new Button(this);
t.setText("text");
t.setBackgroundResource(R.drawable.test);
int top = 200; //y
int left = 100; //x
RelativeLayout.LayoutParams LP= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LP.setMargins(left, top, 0, 0);
t.setLayoutParams(LP);
RL.addView(t);
setContentView(RL);

Related

How can i move items programmatically on the screen?

I have an image with an item in the lower left. I would like at the bottom center, and not attached at the lower end but slightly raised. How can I do? Working with some of the calculations on the size of the screen? Obviously I mean in code, not xml. thanks
have a look at LayoutParams.
for exmaple
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);
you can do all differnt alignments, params, margins and padding as in xml

How to create button at run time on specified location inside frame layout android?

I am trying to create button at runtime.I am getting the coordinate and height,width of button from backend and I have to create button at same location on run time.I have used following code.
Button btn=new Button(getApplicationContext());
btn.setText("CLICK ME");
#SuppressWarnings("deprecation")
AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams( 121, 58, 50, 50);
btn.setLayoutParams(param);
mParentView.addView(btn);
My xml is
<FrameLayout
android:id="#+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ViewFlipper>
<fragment
android:id="#+id/header_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.sdei.presentationapp.activity.PresentationModeTopBar"
tools:layout="#layout/presentation_top_bar" />
</FrameLayout>
here the parentview is framelayout.
I am able to create the button but the problem is it is created always at top left corner,no matter what coordinate we pass.Please help.
Thanks in advance.
You cannot set button at your desired position in framelayout only possible in absolute layout. but you can use margin with respect to your left and top which will work like your (x, y) coordinates.
// First create your button:
Button test_button = new Button(getApplicationContext());
test_button.setText("test");
// Then create layout params for you buttons.
FrameLayout.LayoutParams lp = new LayoutParams(100, 100); // Button width and button height.
lp.leftMargin = 200; // your X coordinate.
lp.topMargin = 300; // your Y coordinate.
// Then find layout and add button on it.
FrameLayout layout = (FrameLayout) findViewById(R.id.FrameLayout1);
layout.addView(test_button, lp);
Hope this can help you.
I have not compile this code, so change as per your layout.
LinearLayout ll = new LinearLayout(this);
fl .setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setId(ok_id)
okButton.setText("some text");
See one reference link here
The absolute layout class is deprecated, you are encouraged to use Frame Layout or Relative layout instead.
The reason of that is it won’t be compatible with all the android phones as they have different screen sizes and resolutions.
Absolute layout lays widgets by specifying their exact X and Y positions. In android the origin (0,0) coordinate is located at the top left of the screen. By default, if you define any control in absolute layout without defining it’s x,y coordinates, it will be placed in the origin point at (x,y)=(0,0);
Thus your defined position for AbsoluteLayout.LayoutParams is not working properly.
As you are using FrameLayout then you can try the following way-
// declare and initialize LayoutParams for the framelayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
// decide upon the positioning of the button //
// you will likely need to use the screen size to position the
// button anywhere other than the four corners
params.setMargins(.., .., .., ..);
// use static constants from the Gravity class
params.gravity = Gravity.CENTER_HORIZONTAL;
btn.setLayoutParams(params);
mParentView.addView(btn);

Android TranslateAnimation Overwrites Other Layout Views

I have a RelativeLayout (vertical) defined to include 3 Views. The top and bottom views are 16 pixels high, and display text. The middle view displays graphics for a game application.
At various points during game play, the app runs a TranslateAnimation that effectively "scrolls" the middle view up or down. This keeps the game character in the game "centered" in the middle view.
My problem is that when the animation runs, the translation causes the top or bottom views to be overwritten. This lasts only for the duration of the animation. When the animcation completes, everything is fine and the top/bottom views display their text as expected. What is surprizing to me is that the translation animation displays content outside of the (middle) view in which it is running. My desire is that when the translation slides the display up/down, that the animation only affects the middle view and does not obscure content in the top or bottom views.
The following is the code I am using to create the layout. The custom views derive directly from View and display content in their onDraw(Canvas) methods.
RelativeLayout rl = new RelativeLayout(this);
topView = new MyTextView(this);
topView.setId(1);
middleView = new MyGameView(this);
middleView.setId(2);
bottomView = new MyTextView(this);
bottomView.setId(3);
final int textSize = 16;
RelativeLayout.LayoutParams lpTop = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, textSize);
lpTop.addRule(RelativeLayout.ALIGN_PARENT_TOP);
RelativeLayout.LayoutParams lpBottom = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, textSize);
lpBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
RelativeLayout.LayoutParams lpMiddle = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lpMiddle.addRule(RelativeLayout.BELOW, 1);
lpMiddle.addRule(RelativeLayout.ABOVE, 3);
rl.addView(topView, lpTop);
rl.addView(middleView, lpMiddle);
rl.addView(bottomView, lpBottom);
setContentView(rl);
The code in middleView that launches the animation is as follows:
TranslateAnimation ta = new TranslateAnimation(0, 0, 0, yDistance);
ta.setDuration(500);
startAnimation(ta);
I have tried a number of things to get this animation to behave as desired, but have been unable to do so. Hopefully someone out there can help. Thanks in advance.

Dynamic interface using TableRows

I am developing an app that creates a dynamic interface according to a string set by the user.
The only thing I've got in my XML file, is a ScrollView, the rest is in java code:
ScrollView sv = (ScrollView)findViewById(R.id.sv);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
Then I decipher a string, which will tell me how many buttons I need to add. Next to each Button should be a TextView. In a for-loop, I create the Buttons and the TextViews and add them to the TableRow, which I add to the TableLayout, which I add to the LinearLayout which I add to the ScrollView.
My problem is: I want the Buttons to be, say 200dp wide, followed by the TextView, but currently it takes the width from WRAP_CONTENT. Setting the size of the Button with RelativeLayout and setLayoutParams, does not work when I add it to the TableRow.
final float scale = getBaseContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_btn.height = pixels;
rel_btn.width = pixels;
btn.setLayoutParams(rel_btn);
tr[i].addView(btn);
tr[i].addView(tv);
tl.addView(tr[i]);
Does anyone have an idea on how to fix my problem with the TableLayout?
I've read that nested LinearLayouts might be a different approach, but I cant seem to find any java examples, only XML - and I need it to be dynamic.
What is the reason for using RelativeLayout.LayoutParams when your Button and TextView are the children of a TableRow? It should be like this:
TableRow.LayoutParams rel_btn = new TableRow.LayoutParams(pixels, pixels);
btn.setLayoutParams(rel_btn);
Also, do you really need the LinearLayout that wraps the TableLayout? If you're not using that LinearLayout for something else you should remove it to improve the views hierarchy.

How to draw two images on the bottom right corner of the screen programatically? (without using XML files)

i need to draw two imageviews, (+) and (-) symbols, on the bottom right corner of the screen, similar of the zoom objects from googlemaps.
I need to do it programatically, with Java, and without using XML files.
I'm trying to do with relativelayout, but i dont know how to do it. They must to be on the bottom right corner of the screen, with 5 or 10 pixels of separation between them.
How to do it?
Also will be cool if someone can tell me how to detect when a user has pressed each image.
You can use gravity.
http://developer.android.com/reference/android/widget/RelativeLayout.html#setGravity(int)
this might do it:
image.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
This should work
ImageView plusImage = new ImageView(this);
RelativeLayout.LayoutParams pp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
pp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
pp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
pp.leftMargin = 5;
plusImage.setId(501);
plusImage.setLayoutParams(pp);
plusImage.setImageResource(R.drawable.icon);
ImageView minusImage = new ImageView(this);
RelativeLayout.LayoutParams mp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mp.addRule(RelativeLayout.LEFT_OF,plusImage.getId());
mp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
mp.rightMargin = 5;
minusImage.setId(502);
minusImage.setLayoutParams(mp);
minusImage.setImageResource(R.drawable.icon);
//Add the images to the outer layout
((RelativeLayout)findViewById(R.id.outerlayout)).addView(plusImage);
((RelativeLayout)findViewById(R.id.outerlayout)).addView(minusImage);

Categories

Resources