Adding views on a custom GLSurfaceView - android

(note that I'm a total beginner in android programming)
I have a class that derives from GLSurfaceView.
What I would like is to place some views (image,text) on top of it.
I managed to get the text view to position properly by using textView.setPadding(300,0,0,0);
The problem is that I cannot get the image view to position properly. I've tried imageView.layout(), imageView.setPadding().
Here is the code:
ImageView imageView=new .... // Create and set drawable
// Setting size works as expected
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200,200);
imageView.setLayoutParams(lp);
surfaceView = new MySurfaceViewDerivedFromOpenGLSurface(...);
setContentView(surfaceView);
addContentView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(textView2, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(imageView, lp); // Add the image view
Is it even possible to position it properly this way without specifing the stuff in the XML file?
I saw an example in the sdk dev website that shows how to create views on the openGL surface view, but the problem is that I have a derived class and I don't know if I can specify it in the XML file (I have 0% experience in the XML, Eclipse handled everything for me so far).

You'll save a ton of headaches later by learning how to use xml layout. Specifying a layout for a custom view tripped me up too. This is how it works:
<view class="complete.package.name.goes.here.ClassName"
android:id="#+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</view>
So a really simple vertical layout for your app would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<view class="package.name.to.MySurfaceViewDerivedFromOpenGLSurface"
android:id="#+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</view>
</LinearLayout>
You can get a reference to anything in your layout files as long as there's an id:
ImageView myImageView = (ImageView ) findViewById(R.id.imageView1);

Related

Android view not visible when adding layout_weight

I'm trying to split my screen into different sizes using four Linear layouts within a linear layout. When I add weights to my project, it shows on the screen that the layout is split into 4 even parts on the layout preview. But when I run the app on a device or emulator, the views are not shown. But when I remove the weight attributes, the views are shown.
I've used code samples which successfully used the weight property but don't work on my program. I've also programmatically got the width and height of all the sub-views on the code. They are not null so they are there but just not visible. I've tried adding properties like visibility = true and focusable = true but to no avail. I've added a drawView to the view using this code
DrawView drawView = new DrawView();
ViewGroup mainLayout = (ViewGroup) findViewById(R.id.main);
mainLayout.addView(drawView);
DrawView is a class that extends View and I call the methods canvas.drawLine() and canvas.drawText() to draw to the screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:weightSum="4">
<LinearLayout
android:visibility="visible"
android:focusable="true"
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorAccent"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:visibility="visible"
android:id="#+id/l2"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorBtnText"></LinearLayout>
<LinearLayout
android:id="#+id/l3"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorBtnBackground"></LinearLayout>
<LinearLayout
android:id="#+id/l4"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorLbl"></LinearLayout>
</LinearLayout>
Nothing I tried above worked. I've spent quite a bit of time on this and would really appreciate some feedback.
I think the DrawView need setLayoutParams(LinearLayout.LayoutParams) before add to LinearLayout, set fixed height or weight by LayoutParams. if you don't, DrawView height is MATCH_PARENT, that will make other view's height is 0.
you can try this:
DrawView drawView = new DrawView();
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0, 1); // or new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,100); to set fixed height
mainLayout.addView(drawView, params);
If it work, i think the best way is DrawView override onMeasure method, and calculate height itself.
As you can see, you set the "android:weightSum" to 4 in your xml file. While it still have 4 childs under the main linear layout, the code shows no error. However, when you run your code, you programmatically add another view into your main linear layout which exceed the weight sum of your main layout.
So, what i would suggest is, you may try to remove the android:weightSum="4" attribute from your xml layout that way it will automatically calculate the layout size by its weight.

Android Button and drawable area

I am novice on Android programming.
My task is simple: I want to create a screen with just two objects:
an action Button;
a drawable area in wich to draw images, text, circles, and so on.
Is it possible so have a working example, or at least a guideline ?
I know how to subclass a view, and to draw int it, using:
MyView d = new MyView(this);
setContentView(d);
But this fills all the screen with MyView and the button is not visible.
Some suggestions ?
You need to define a layout file and specify relative position of button and the drawable area.
Make sure both of them are not specified as fill_parent in layout_width or layout-height.
Set the contentView to this layout file
You go through android Ui for detail understanding.
When you create an android project in eclipse you'll find res/layout/main.xml and this is where your default UI is defined and that is set using setContentView(R.layout.main); in onCreate method.
To put images you can use imageview and textview for Texts in xml. Like that many widgets are there for edit text, Button etc. A simple example including Imageview,textview and Button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO ANDROID"
android:layout_gravity="center"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Click Me"
/>

Is it possible to add views dynamically to an XML based layout?

Lets say that I have a simple XML layout such as the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/my_container"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/leftContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col A - Text 1"
/>
</LinearLayout>
<LinearLayout android:id="#+id/rightContainer"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Col B - Text 1"
/>
</LinearLayout>
</LinearLayout>
Then I want to add a TextView to the rightContainer LinearLayout. I am currently doing this, unsuccessfully:
LinearLayout container = (LinearLayout) findViewById(R.id.rightContainer);
TextView textToAdd = new TextView(this);
textToAdd.setText("Col B - Text 2");
container.addView(textToAdd);
I have looked at LayoutInflater, but am not sure how I would use it here. Any help would be appreciated, thanks! If I try calling setContentView(container), I receive a Force Close error.
Don't call setContentView(), if you are using findViewById() then that view is already inside of your currently set content.
Adding views works fine. All a layout XML is, is a description of the views to create and add to the hierarchy. Make sure you are passing the correct layout params when adding a view -- here since container is a LinearLayout, you want a LinearLayout.LayoutParams.
You don't say in what way your code is "unsuccessful" so it is hard to help further.
Also you can use hierarchyviewer to look at what is actually going on in your view hierarchy.

Reserve a "slot" in a layout XML file for a dynamically created button?

In my app, I have one (and only one) UI element which isn't referenced in the XML layout file.
That element is a button, instantiated and returned at run-time by a 3rd party library (i.e. I don't have control over that).
My problem is that I would like some of the elements (TextViews) in the XML layout file to be placed relative to that button, using RelativeLayout.
Is it possible to "reserve an empty slot" in the XML layout file for that button such that I can do something like the following?
<TextView android:id="#+id/tv_text_under_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_dynamically_created_button"
android:text="" />
Alternatively, if I were to set the layout at run-time using RelativeLayout.LayoutParams.addRule(), what would be the ID of that dynamically created button, if it has no reference at all in the XML layout file?
For example, in the following call:
layoutParams.addRule(RelativeLayout.BELOW, R.id.btn_dynamically_created_button);
What would I put instead of R.id.btn_dynamically_created_button?
Update: Thanks to the answer below, I created a place holder like this:
<LinearLayout android:id="#+id/btn_dynamically_created_button"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
The challenge now is: How to associate the returned object from getDynamicallyCreatedButton() (returned object is subclass of LinearLayout, not Button), with R.id.btn_dynamically_created_button?
EDIT: This thread seem to address a similar issue, but I am not sure that I understand the solution offered.
I'd suggest:
Put a LinearLayout with width/height set to wrap-content, horizontal orientation and zero padding as the placeholder.
Orient all the other things to that LinearLayout.
When its time to put the button, simply stick it into the LinearLayout.
See if that works for you.
EDIT: attempt at a short example:
The layout (suitably shortened): you can place other components relative to the LinearLayout with id LinearLayout01.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_marginTop="2sp" android:layout_marginBottom="2sp" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:gravity="right" style="#style/SimpleButtonBar" android:layout_below="#+id/rootlayout" android:layout_alignParentBottom="true">
</LinearLayout>
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_above="#+id/LinearLayout01" android:fillViewport="true">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/detaillayout">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The code (for example, this would go in onCreate): fetch your button (you need to make sure it has the right Context, but I figure you're doing that alright), fetch the LinearLayout, create a layout parameters object and stick your button into the LinearLayout.
Button b = getButton(); // retrieve your button somehow
LinearLayout l = (LinearLayout)findViewById(R.id.LinearLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
l.addView(b, lp);

android - dynamically changing the position of an imageview

i'm currently using the matrix method to implement drag and pinch zoom for an image (image A)
Now i want a part of image A to be clickable so i put another imageview (image B) over image A and set the layout_margins for it in the xml file.
My question is...is there a way to dynamically change my layout_margins as i'm scaling my image for drag and pinch zoom?
I guess the simpler question is...how do i dynamically set the layout_margins for an imageview?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/map"
android:scaleType="matrix" />
<ImageView
android:id="#+id/dundas_station"
android:layout_width="75px"
android:layout_height="75px"
android:layout_marginTop="337px"
android:layout_marginLeft="373px"
android:clickable="true"
android:src="#drawable/google_maps_icon"
android:scaleType="matrix"
/>
</RelativeLayout>
thanks!
Use following code snippet. Don forget to import
"android.widget.FrameLayout.LayoutParams" or marginal layout params.
ImageView image;
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(100,0, 0,0);
// OR
params.topMargin= 100;
image.setLayoutParams(params);
You use setMargins

Categories

Resources