i want change my overlayedButton (always on top) to overlayedLayout and in this layout put instlled app icon for lunch.
i do not want a widget layout. i want a layout like widget that every where is showing and always on top
this image...
my HUD class
public class HUD extends Service implements OnTouchListener, View.OnClickListener {
private View topLeftView;
private Button overlayedButton,overlayedButton2;
private float offsetX;
private float offsetY;
private int originalXPos;
private int originalYPos;
private boolean moving;
private WindowManager wm;
private String TAG = "GET_APP";
private LayoutInflater li;
private ViewGroup myview;
private int deviceWidth;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
overlayedButton = new Button(this);
overlayedButton.setBackgroundResource(R.drawable.main);
overlayedButton.setOnTouchListener(this);
overlayedButton.setAlpha(0.0f);
overlayedButton.setBottom(100);
overlayedButton.setOnClickListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(overlayedButton, params);
topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams);
}
#Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (WindowManager.LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, params);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
return true;
}
}
return false;
}
#Override
public void onClick(View v) {
Toast.makeText(this, "Overlay button click event", Toast.LENGTH_SHORT).show();
Intent dialogIntent = new Intent(this, ArcMenuActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
}
ArcMenuActivity
public class ArcMenuActivity extends Activity {
private int state = 0;
public static final String FAVORITES = "Product_Favorite";
private ArrayList<Intent> Ar;
private Drawable icon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
state = 1;
switch (state){
case 1:{
setContentView(R.layout.activity_ray);
final RayMenu rayMenu = (RayMenu) findViewById(R.id.ray_menu);
Ar = getFavorites(getApplicationContext());
final int itemCount = Ar.size();
for (int i = 0; i < itemCount; i++) {
ImageView item = new ImageView(this);
try {
icon = getPackageManager().getApplicationIcon(Ar.get(i).getPackage());
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
item.setImageDrawable(icon);
final int position = i;
rayMenu.addItem(item, new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ArcMenuActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
startActivity(Ar.get(position));
finish();
}
});
}
break;}
case 2:{
break;}
case 3:{
break;}
}
Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_LONG).show();
Intent svc = new Intent(this, HUD.class);
startService(svc);
}
public ArrayList<Intent> getFavorites(Context context) {
SharedPreferences settings;
List<Intent> favorites;
settings = context.getSharedPreferences("INTENT",
Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
Gson gson = new Gson();
Intent[] favoriteItems = gson.fromJson(jsonFavorites,
Intent[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList<Intent>(favorites);
} else
return null;
return (ArrayList<Intent>) favorites;
}
private void initArcMenu(ArcMenu menu, int[] itemDrawables) {
final int itemCount = itemDrawables.length;
for (int i = 0; i < itemCount; i++) {
ImageView item = new ImageView(this);
item.setImageResource(itemDrawables[i]);
final int position = i;
menu.addItem(item, new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ArcMenuActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
}
}
this my xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
style="#style/custom_toolbar"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/layout_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#ffffff" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
>
<RelativeLayout
android:id="#+id/ec_rltabselected"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<AbsoluteLayout
android:id="#+id/relative1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/border_rounding" >
<ImageView
android:id="#+id/ivCardView"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
</AbsoluteLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/llBottomLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="5dp"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="0" >
</LinearLayout>
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:layout_weight="1"
android:layout_marginBottom="10dp"
android:id="#+id/recyclerview_up"
android:layout_width="match_parent"
android:layout_height="150dp"
/>
</LinearLayout>
And my class :
public class clothe_codi extends AppCompatActivity {
private Toolbar toolbar;
ArrayList<String> arr_id_list;
Clothe_DBHelper helper;
SQLiteDatabase db;
List<clothe_codi_mini_item> codi;
RecyclerView recyclerView_mini_codi;
//경계선
private Button m_btnSelectImage, m_btnSDeleteImage, m_btnZoom;
private Context m_context;
private LinearLayout m_llTopLayout;
private ImageView m_ivImage, m_ivtmpImage;
private Display m_screen;
private int m_DisplayWidth, m_ImageCount, m_viewsAddedHeightEmotions = 0,
m_height, m_absHeight = 0, m_AddedViewsHeightText = 0,
m_deleteEditHeightwidth;
private Dialog m_dialog;
private View.OnTouchListener m_touchImagListener, m_strecthArrowListener;
private AbsoluteLayout m_absolutelayout, m_absTextlayout, m_absZoomlayout;
private int m_widthDelete = 0, m_totalTextViewCount = 0;
private float m_oldDist = 1f, m_scale, m_oldX = 0, m_oldY = 0, m_dX, m_dY,
m_posX, m_posY, m_prevX = 0, m_prevY = 0, m_newX, m_newY;
ViewTreeObserver m_vtoTree;
private AbsoluteLayout.LayoutParams m_layoutparams, m_layoutparamsDelete,
m_layoutParamsEdit;
private ArrayList<ViewsVo> m_arrSignObjects;
private Bitmap m_bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clothe_codi);
//id데리고 오기
arr_id_list = new ArrayList<String>();
//db만들기
helper = new Clothe_DBHelper(getApplicationContext());
db = helper.getWritableDatabase();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ref_back);
//toolbar상단에 글씨설정
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("코디하기");
recyclerView_mini_codi=(RecyclerView)findViewById(R.id.recyclerview_up);
LinearLayoutManager layoutManager_up=new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView_mini_codi.setHasFixedSize(true);
recyclerView_mini_codi.setLayoutManager(layoutManager_up);
codi=new ArrayList<>();
//listview에 뿌려주기 위하여
ClotheSelect();
recyclerView_mini_codi.setAdapter(new clothe_codi_mini_item_RecyclerAdapter(getApplicationContext(),codi,R.layout.activity_main));
recyclerView_mini_codi.addOnItemTouchListener(new RecyclerViewOnItemClickListener(this, recyclerView_mini_codi,
new RecyclerViewOnItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View v, int position) {
final Integer selectedPos = position;
String popo = arr_id_list.get(selectedPos);
Cursor search;
search = db.rawQuery("SELECT * FROM clothe_list where _id = '" + popo + "';", null);
while (search.moveToNext()){
byte[] byteimage = search.getBlob(3);
Bitmap bitmapimage = DbBitmapUtility.getBitmap(byteimage);
getImageLayout(bitmapimage);
}
search.close();
}
#Override
public void onItemLongClick(View v, int position) {
}
}
));
//I made Drag and Drop to Move Objects on Finger Touch in Android
m_context = clothe_codi.this;
m_ivImage = (ImageView) findViewById(R.id.ivCardView);
m_absolutelayout = (AbsoluteLayout) findViewById(R.id.relative1);
m_llTopLayout = (LinearLayout) findViewById(R.id.llBottomLayout);
m_arrSignObjects = new ArrayList<ViewsVo>();
// Set the layout parameters to the Absolute layout for adding images.
RelativeLayout.LayoutParams rl_pr = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
rl_pr.addRule(RelativeLayout.ABOVE, R.id.llBottomLayout);
rl_pr.addRule(RelativeLayout.BELOW, R.id.layout_title);
m_absolutelayout.setLayoutParams(rl_pr);
m_screen = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
m_DisplayWidth = m_screen.getWidth();
m_AddedViewsHeightText = m_llTopLayout.getHeight();
// Get the absoulte layout height according to the device screen density
// to set the layout.
m_vtoTree = m_absolutelayout.getViewTreeObserver();
m_vtoTree.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
m_absHeight = m_absolutelayout.getHeight();
m_absolutelayout.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
m_dialog = new Dialog(this, R.style.Dialog);
m_dialog.setCancelable(true);
}
public void ClotheSelect() {
Cursor search;
search = db.rawQuery("SELECT * FROM clothe_list", null);
while (search.moveToNext()){
String up_down = search.getString(1);
String clothename = search.getString(2);
byte[] byteimage = search.getBlob(3);
Bitmap bitmapimage = DbBitmapUtility.getBitmap(byteimage);
codi.add(new clothe_codi_mini_item(up_down, clothename, bitmapimage));
//id를 저장
arr_id_list.add(search.getString(0));
}
search.close();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
// NavUtils.navigateUpFromSameTask(this) 를 사용할 경우 모든 Activity를 Destroy 시키고 부모 Activity로 돌아간다.
// NavUtils.navigateUpFromSameTask(this);
// finish() 를 사용할 경우 현재 Activity를 Destroy하고 부모 Activity로 돌아간다.
finish();
return true;
case R.id.setting:
Intent timesetting = new Intent(clothe_codi.this, ref_time_setting.class);
startActivity(timesetting);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (requestCode == 0 && resultCode == RESULT_OK && null != data) {
m_bitmap = null;
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
m_bitmap = BitmapFactory.decodeFile(picturePath);
getImageLayout(m_bitmap);
}
break;
}
}
/**
* Method to set the view's height dynamically according to screen size.
*/
private void setViewsHeightDynamically() {
if (m_absHeight <= 500) {
m_layoutparamsDelete = new AbsoluteLayout.LayoutParams(20, 20, 110,0);
m_layoutParamsEdit = new AbsoluteLayout.LayoutParams(20, 20, 110,110);
m_deleteEditHeightwidth = 20;
} else if (m_absHeight >= 900) {
m_layoutparamsDelete = new AbsoluteLayout.LayoutParams(40, 40, 130,0);
m_layoutParamsEdit = new AbsoluteLayout.LayoutParams(40, 40, 130,130);
m_deleteEditHeightwidth = 40;
} else {
m_layoutparamsDelete = new AbsoluteLayout.LayoutParams(35, 35, 140,0);
m_layoutParamsEdit = new AbsoluteLayout.LayoutParams(35, 35, 120,120);
m_deleteEditHeightwidth = 35;
}
}
/**
* Method to add the image by setting and creating the views dynamically
* with delete and zoom option.
*/
#SuppressWarnings("deprecation")
private void getImageLayout(Bitmap p_bitmap) {
ViewsVo m_signVo;
//Check for images count .Set the count for limiting the number of images to add on screen.
if (m_ImageCount < 1) {
m_viewsAddedHeightEmotions = m_viewsAddedHeightEmotions + 90;
m_ImageCount++;
}
m_btnSDeleteImage = new Button(m_context);
m_btnZoom = new Button(m_context);
m_ivtmpImage = new ImageView(m_context);
setViewsHeightDynamically();
m_btnSDeleteImage.setLayoutParams(m_layoutparamsDelete);
m_btnSDeleteImage.setBackgroundDrawable(getResources().getDrawable(
R.drawable.ic_deletered));
m_btnSDeleteImage.setId(0);
m_btnSDeleteImage.setOnClickListener(new ImageDeleteListener());
m_btnZoom.setLayoutParams(m_layoutParamsEdit);
m_btnZoom.setBackgroundDrawable(getResources().getDrawable(
R.drawable.right_arrow));
m_btnZoom.setId(0);
m_absTextlayout = new AbsoluteLayout(m_context);
m_absZoomlayout = new AbsoluteLayout(m_context);
//500여기 숫자를 늘리면 이미지 자체 화질이 좋아진다.
m_ivtmpImage.setImageBitmap(Bitmap.createScaledBitmap(p_bitmap, 500, 500, true));
m_absTextlayout.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 0));
m_absZoomlayout.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT, 0, 0));
if (m_absHeight >= 900)
//setLayoutParams를 이용하여 위의 이미지 크기를 지정한다.
m_ivtmpImage.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
else
m_ivtmpImage.setLayoutParams(new FrameLayout.LayoutParams(80, 80));
m_ivtmpImage.setBackgroundColor(Color.TRANSPARENT);
m_absTextlayout.addView(m_btnSDeleteImage);
if (m_absHeight >= 900)
m_absZoomlayout.setPadding(20, 20, 15, 15);
else
m_absZoomlayout.setPadding(15, 15, 15, 15);
/*m_absZoomlayout.setBackgroundResource(R.drawable.dashedbordersmall);*/
m_absZoomlayout.addView(m_ivtmpImage);
m_absTextlayout.addView(m_absZoomlayout);
m_absTextlayout.addView(m_btnZoom);
m_absTextlayout.setDrawingCacheEnabled(true);
m_absTextlayout.setClickable(true);
m_absTextlayout.setId(0);
m_ivtmpImage.setId(0);
m_vtoTree =m_absTextlayout.getViewTreeObserver();
m_vtoTree.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
m_absTextlayout.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
/**
* Add all the views into arraylist which are added into the screen for
* further to perform deletion of each views.
*/
m_signVo = new ViewsVo();
m_arrSignObjects.add(0, m_signVo);
m_absolutelayout.addView(m_absTextlayout);
// Image touch listener to move image onTouch event on screen.
m_touchImagListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
m_oldX = event.getX();
m_oldY = event.getY();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
m_dX = event.getX() - m_oldX;
m_dY = event.getY() - m_oldY;
m_posX = m_prevX + m_dX;
m_posY = m_prevY + m_dY;
if (m_posX > 0 && m_posY > 0&& (m_posX + v.getWidth()) < m_absolutelayout.getWidth()&& (m_posY + v.getHeight()) < m_absolutelayout.getHeight())
{
v.setLayoutParams(new AbsoluteLayout.LayoutParams(v.getMeasuredWidth(), v.getMeasuredHeight(),(int) m_posX, (int) m_posY));
m_prevX = m_posX;
m_prevY = m_posY;
}
break;
}
return false;
}
};
// Listener for the arrow ontouch of arrow ZoomIn and ZoomOut the image.
m_strecthArrowListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
View view;
view = v;
v.setClickable(true);
v.setDrawingCacheEnabled(true);
AbsoluteLayout m_absLayout = null;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
m_oldX = event.getX();
m_oldY = event.getY();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
m_newX = event.getX();
m_newY = event.getY();
float newDist = m_newX - m_oldX;
if (m_newX > m_oldX && m_newY > m_oldY) {
if (newDist > 0.0f) {
m_scale = 1;
m_absLayout = (AbsoluteLayout) v.getParent();
int m_hightOfImage = (int) (m_scale + (((ImageView) ((AbsoluteLayout) m_absLayout.getChildAt(1)).getChildAt(0)).getHeight()));
int m_widthOfImage = (int) (m_scale + (((ImageView) ((AbsoluteLayout) m_absLayout.getChildAt(1)).getChildAt(0)).getWidth()));
m_widthDelete = (int) (m_scale + ((((AbsoluteLayout) m_absLayout.getChildAt(1))).getWidth()));
if (m_absLayout.getBottom() <= (m_ivImage.getBottom())
&& m_absLayout.getRight() <= (m_DisplayWidth)) {
m_layoutparams = new AbsoluteLayout.LayoutParams( m_widthOfImage, m_hightOfImage, 0, 0);
((ImageView) ((AbsoluteLayout) m_absLayout.getChildAt(1)).getChildAt(0)).setLayoutParams(m_layoutparams);
m_layoutparams = new AbsoluteLayout.LayoutParams( AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,m_absLayout.getLeft(), m_absLayout.getTop());
m_absLayout.setLayoutParams(m_layoutparams);
((Button) m_absLayout.getChildAt(0)).setLayoutParams(new AbsoluteLayout.LayoutParams(m_deleteEditHeightwidth,m_deleteEditHeightwidth,m_widthDelete, 0));
((Button) m_absLayout.getChildAt(2)).setLayoutParams(new AbsoluteLayout.LayoutParams(m_deleteEditHeightwidth,m_deleteEditHeightwidth,m_widthDelete, m_widthDelete));
m_hightOfImage = (int) (m_scale + (((AbsoluteLayout) m_absLayout.getChildAt(1)).getHeight()));
m_widthOfImage = (int) (m_scale + (((AbsoluteLayout) m_absLayout.getChildAt(1)).getWidth()));
m_layoutparams = new AbsoluteLayout.LayoutParams( m_widthOfImage, m_hightOfImage,((AbsoluteLayout) m_absLayout .getChildAt(1)).getLeft(),((AbsoluteLayout) m_absLayout .getChildAt(1)).getTop());
((AbsoluteLayout) m_absLayout.getChildAt(1)).setLayoutParams(m_layoutparams);
}
}
}
if (m_newX < m_oldX && m_newY < m_oldY) {
m_absLayout = (AbsoluteLayout) view.getParent();
int m_hightOfImage = (int) (((ImageView) ((AbsoluteLayout) m_absLayout.getChildAt(1)).getChildAt(0)).getHeight() - m_scale);
int m_widthOfImage = (int) (((ImageView) ((AbsoluteLayout) m_absLayout.getChildAt(1)).getChildAt(0)).getWidth() - m_scale);
m_widthDelete = (int) (((AbsoluteLayout) m_absLayout.getChildAt(1)).getWidth() - m_scale);
m_layoutparams = new AbsoluteLayout.LayoutParams( m_widthOfImage, m_hightOfImage, 0, 0);
((ImageView) ((AbsoluteLayout) m_absLayout.getChildAt(1)).getChildAt(0)).setLayoutParams(m_layoutparams);
m_layoutparams = new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
m_absLayout.getLeft(), m_absLayout.getTop());
m_absLayout.setLayoutParams(m_layoutparams);
((Button) m_absLayout.getChildAt(0)).setLayoutParams(new AbsoluteLayout.LayoutParams(m_deleteEditHeightwidth,
m_deleteEditHeightwidth, m_widthDelete,0));
((Button) m_absLayout.getChildAt(2)).setLayoutParams(new AbsoluteLayout.LayoutParams(m_deleteEditHeightwidth,
m_deleteEditHeightwidth, m_widthDelete,m_widthDelete));
m_hightOfImage = (int) ((((AbsoluteLayout) m_absLayout
.getChildAt(1)).getHeight()) - m_scale);
m_widthOfImage = (int) ((((AbsoluteLayout) m_absLayout
.getChildAt(1)).getWidth()) - m_scale);
m_layoutparams = new AbsoluteLayout.LayoutParams(
m_widthOfImage, m_hightOfImage, ((AbsoluteLayout) m_absLayout.getChildAt(1)).getLeft(),((AbsoluteLayout) m_absLayout.getChildAt(1)).getTop());
((AbsoluteLayout) m_absLayout.getChildAt(1))
.setLayoutParams(m_layoutparams);
}
break;
}
return false;
}
};
m_absTextlayout.setOnTouchListener(m_touchImagListener);
m_btnZoom.setOnTouchListener(m_strecthArrowListener);
}
// Delete button listener to show the alert and confirmation for deleting
// the items.
private class ImageDeleteListener implements View.OnClickListener {
#Override
public void onClick(final View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
m_context);
alertDialogBuilder.setTitle("Drag & Drop");
alertDialogBuilder
.setMessage("Are you sure you want to delete ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
m_ImageCount--;
for (int counter = 0; counter < m_arrSignObjects
.size(); counter++) {
if (v.getId() == m_arrSignObjects.get(counter).getViewId()) {
if (m_totalTextViewCount <= 0) {
m_AddedViewsHeightText = m_AddedViewsHeightText
- m_arrSignObjects.get(counter).getViewHeight();
} else {
m_totalTextViewCount--;
}
m_absolutelayout.removeView((View) v
.getParent());
m_arrSignObjects.remove(counter);
break;
}
}
}
}).setNegativeButton("No",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
}
I made Drag and Drop to Move Objects on Finger Touch in Android
Reference site this : http://grishma102.blogspot.kr/2013/10/drag-and-drop-functionality-to-move.html
But, my activity's zoom in, zoom out don't function
When I move objects on Finger Touch, the objects just zoom out and screen out.
please help me..
Try overriding OnDrag() listener instead of onTouch()
view.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View view, DragEvent dragEvent) {
return true;
}
});
I'm basically trying to achieve drag&drop feature..
What i'm trying is that i provides sequence of images on the screen, if i click on any image available in images row that will be added to the Mains screen. But i'm getting problem that when i add new view into the Main Screen then all the other views also moved to top left corner.
Can you please tell me what is the problem...? Or kindly suggest me a tutorial or link where i can find solution.... or how to achieve this ?
I'm using Framelayout, So that i also achieve images overlapping...
This is the class in which all code is working:
public class drag extends Activity implements OnClickListener, OnTouchListener
{
ImageView img1;
Button btn,btn2;
FrameLayout layout;
LayoutParams params;
ImageView im , im2, im3 ,im4;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.vg);
layout.setDrawingCacheEnabled(true);
im = (ImageView)findViewById(R.id.img1);
im.setDrawingCacheEnabled(true);
im.setOnTouchListener(this);
im.setOnClickListener(this);
btn = (Button)findViewById(R.id.btn1);
btn.setDrawingCacheEnabled(true);
btn.setOnClickListener(this);
btn.setOnTouchListener(this);
btn2 = (Button)findViewById(R.id.btn2);
btn2.setDrawingCacheEnabled(true);
btn2.setOnClickListener(this);
btn2.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
im2 = new ImageView(drag.this);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
im2.setImageBitmap(bm);
im2.setOnTouchListener(drag.this);
im2.setOnClickListener(drag.this);
layout.addView(im2, params);
}
});
btn2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
saveImage(bm);
}
});
}
public void saveImage(Bitmap myBitmap)
{
MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, "mmsImage" , "mmsimage");
}
int l, t, r, b;
int oldLeft, oldTop;
PointF p, curr;
#Override
public boolean onTouch(View view, MotionEvent me)
{
if (me.getAction() == MotionEvent.ACTION_DOWN)
{
//status = START_DRAGGING;
Log.i("status"," AAA dOWN");
img1 = new ImageView(this);
view.setDrawingCacheEnabled(true);
Bitmap mmsImage = Bitmap.createBitmap(view.getDrawingCache());
img1.setImageBitmap(mmsImage);
img1.setOnTouchListener(drag.this);
img1.setOnClickListener(drag.this);
oldLeft = (int)view.getLeft();
oldTop = (int)view.getTop();
p = new PointF(me.getRawX(), me.getRawY());
}
if (me.getAction() == MotionEvent.ACTION_MOVE)
{
Log.i("status"," AAA draging");
int xDiff = (int)(me.getRawX() - p.x);
int yDiff = (int)(me.getRawY() - p.y);
p.x = me.getRawX();
p.y = me.getRawY();
l = view.getLeft();
t = view.getTop();
r = view.getRight();
b = view.getBottom();
view.layout(l + xDiff, t + yDiff , r + xDiff, b + yDiff);
}
if (me.getAction() == MotionEvent.ACTION_UP)
{
Log.i("status"," AAA UP");
//captureUserMove(view);
}
return false;
}
}
Here is the XML :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
You set params, but don't specify how to position the added view. Try this:
In onCreate()
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT | Gravity.TOP; // you need this for margins to work
In the click listener:
// your x and y where you want the new view
params.leftMargin = x;
params.topMargin = y;
Putting
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
inside
#Override
public void onClick(View v)
{
}
will work.
I have a 5x5 grid (I ve drawn a matrix in canvas). I ve arranged the textviews in the the grid
in the order of a array..ie (5X5 ) array. The thing is I want to change the values on of the text view when I swipe horizontally or vertically.
Say suppose I am using a touch movement on the screen continuously from [4][0] to [4][2] I want to modify the values of the textviews in those places.
Any ideas?
This piece of code might help you:
public class MyClass extends Activity implements OnGestureListener{
ArrayList<TextView> tvs = new ArrayList<TextView>();
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.mygrid);
}
public void onResume() {
super.onResume();
GestureOverlayView gd = (GestureOverlayView)findViewById(R.id.gd);
LinearLayout g = (LinearLayout)findViewById(R.id.mygrid);
gd.addOnGestureListener(this);
g.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 5; i++) {
LinearLayout row = new LinearLayout(this);
g.addView(row);
row.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rowlp = (LayoutParams) row.getLayoutParams();
rowlp.width = LayoutParams.MATCH_PARENT;
rowlp.weight = 1;
rowlp.height = LayoutParams.WRAP_CONTENT;
row.setLayoutParams(rowlp);
for (int j = 0; j < 5; j++) {
TextView tv = new TextView(this);
row.addView(tv);
LinearLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
lp.height = LayoutParams.MATCH_PARENT;
lp.width = 0;
lp.weight = 1;
tv.setLayoutParams(lp);
tv.setText(String.valueOf(i * 5 + j));
tv.setTag(String.valueOf(i * 5 + j));
tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
tvs.add(tv);
if ((i * 5 + j) % 2 == 0)
tv.setBackgroundColor(0xff888888);
}
}
}
private boolean isPointInsideView(float x, float y, View view){
int location[] = new int[2];
view.getLocationOnScreen(location);
int viewX = location[0];
int viewY = location[1];
//point is inside view bounds
if(( x > viewX && x < (viewX + view.getWidth())) &&
( y > viewY && y < (viewY + view.getHeight()))){
return true;
} else {
return false;
}
}
#Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
float x = event.getRawX();
float y = event.getRawY();
for (TextView v : tvs) {
if (isPointInsideView(x, y, v)) {
v.setText("HERE");
}
}
}
#Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
}
#Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
}
#Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.gesture.GestureOverlayView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gd"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/mygrid"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.gesture.GestureOverlayView>
Hope it helps.
Edit2: replace the methods above with these for highly increased performance:
private TextView getAbove(float x, float y) {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (isPointInsideView(x, y, tvs[i][j]))
return tvs[i][j];
return null;
}
#Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
float x = event.getRawX();
float y = event.getRawY();
if (last == null || !isPointInsideView(x, y, last)) {
last = getAbove(x,y);
draw();
}
}
private void draw() {
if (last == null)
return;
last.setText("+");
}
private void reset() {
last = null;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
tvs[i][j].setText(String.valueOf(i * 5 + j));
}
#Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
reset();
}
#Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
reset();
}
#Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
last = getAbove(event.getRawX(), event.getRawY());
draw();
}
TextView[][] tvs = new TextView[5][5];
TextView last = null;
And make the arraylist into an TextView[5][5]
I made two nested RelativeLayouts, filled one with some TextViews and made reaction on finger drag guesture.
But it works badly:
1) moving group leaves garbage traces as if background does not redraw
2) the column with leftMargin==800 does not draw
The XML layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/stator"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:id="#+id/mover"
android:background="#android:color/darker_gray"
>
</RelativeLayout>
</RelativeLayout>
The java code:
public class SymbolPadActivity extends Activity {
private RelativeLayout mover;
private RelativeLayout stator;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mover = (RelativeLayout) findViewById(R.id.mover);
RelativeLayout.LayoutParams labelParams;
TextView textView;
for(int leftMargin = 0; leftMargin<1500; leftMargin += 200) {
for(int topMargin=0; topMargin<800; topMargin += 80) {
// I can't omit these 3 lines
labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
labelParams.leftMargin = leftMargin;
labelParams.topMargin = topMargin;
textView = new TextView(this);
textView.setText("(" + leftMargin + "," + topMargin + ")");
mover.addView(textView, labelParams);
}
}
stator = (RelativeLayout) findViewById(R.id.stator);
stator.setOnTouchListener(new View.OnTouchListener() {
private int startleft, starttop;
private float startx, starty;
private boolean started;
#Override
public boolean onTouch(View v, MotionEvent event) {
if( event.getActionMasked() == MotionEvent.ACTION_DOWN ) {
started = true;
startx = event.getX();
starty = event.getY();
startleft = mover.getLeft();
starttop = mover.getTop();
return true;
}
else if( event.getActionMasked() == MotionEvent.ACTION_UP ) {
started = false;
startx = starty = 0;
return true;
}
else if( event.getActionMasked() == MotionEvent.ACTION_MOVE ) {
mover.setLeft( startleft + (int)(event.getX() - startx) );
mover.setTop( starttop + (int)(event.getY() - starty) );
return true;
}
else {
return false;
}
}
});
}
}
How to implement my goal correctly (in brief)?