• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps image overlay

Hi,

I trying to implement a image as background and put images on top of it.
this is my layout.
Code:
<FrameLayout android:id="@+id/my_ph" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<ImageView  
    android:id="@+id/image"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/sketch" />
</FrameLayout>

and my class
Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.Toast;

public class myActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        FrameLayout rv =(FrameLayout)findViewById(R.id.my_ph);
       
        ImageButton im1 = new ImageButton(this);
        im1.setBackgroundResource(R.drawable.lamp_off_x);
        im1.setMinimumHeight(50);
        im1.setMinimumWidth(50);
        im1.setOnClickListener(OnLampClickListener());
        im1.layout(50,50, 0, 0);
       
        rv.addView(im1);
     }
    
    OnClickListener OnLampClickListener(){
      Toast.makeText(getBaseContext(), "Lamp",Toast.LENGTH_LONG);
      return null;
    }
}

my questions:
1. how to put the images im1 in specific place(x,y) on top of my ImageView.
2. how to implement the click event on the image im1 (i will have more images like that)
3. if i will dynamic load images on my ImageView how to know which image i clicked (where and how to save the image id that came from db).

Eyal
 
1)Use absolute layout Learn Android: Absolute layout . You can put view on absolute layout so:
Code:
AbsoluteLayout.LayoutParams params=new AbsoluteLayout.LayoutParams(
                        width, height, x, y
);
mView.setLayoutParams(params);
AbsLay.addView(mView)
It layout is deprecated, because in some screens your app will poor like - sizes and coordinates already same. But if you add views in code, you can calculate coordinates and sizes - depending screen params:
Code:
protected void GetScreenSize(){
        Display display = getWindowManager().getDefaultDisplay(); 
        screen_x = display.getWidth();
        screen_y = display.getHeight();
 }
2 and 3)Create ImageView array and assign id to every image. Next setOnClickListener(this) on every image and parse image id in click listener code.
 
It layout is deprecated, because in some screens your app will poor like - sizes and coordinates already same. But if you add views in code, you can calculate coordinates and sizes - depending screen params:

2 and 3)Create ImageView array and assign id to every image. Next setOnClickListener(this) on every image and parse image id in click listener code.

1) i want to use deprecated layouts
2,3) the event isn't working i click on the image but nothing appand.
 
Back
Top Bottom