Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. package com.lorenzmacht.mobhp;
  2.  
  3. import android.content.ClipData;
  4. import android.content.Intent;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Point;
  8. import android.graphics.drawable.ColorDrawable;
  9. import android.graphics.drawable.Drawable;
  10. import android.support.v7.app.ActionBarActivity;
  11. import android.os.Bundle;
  12. import android.view.DragEvent;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.LinearLayout.LayoutParams;
  17. import android.widget.LinearLayout;
  18. import android.widget.RelativeLayout;
  19. import android.widget.TextView;
  20.  
  21. public class Main extends ActionBarActivity {
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.  
  28.  
  29.  
  30.         }
  31.  
  32.     @Override
  33.     public boolean onCreateOptionsMenu(Menu menu) {
  34.         // Inflate the menu; this adds items to the action bar if it is present.
  35.         getMenuInflater().inflate(R.menu.menu_main, menu);
  36.         return true;
  37.     }
  38.  
  39.     @Override
  40.     public boolean onOptionsItemSelected(MenuItem item) {
  41.         // Handle action bar item clicks here. The action bar will
  42.         // automatically handle clicks on the Home/Up button, so long
  43.         // as you specify a parent activity in AndroidManifest.xml.
  44.         int id = item.getItemId();
  45.  
  46.         //noinspection SimplifiableIfStatement
  47.         if (id == R.id.action_settings) {
  48.             return true;
  49.         }
  50.  
  51.         return super.onOptionsItemSelected(item);
  52.     }
  53.  
  54.     // This method will open a window to create a new mob
  55.     public void openMobActivity(View view) {
  56.  
  57.         // Defining an Intent to open NewMob.class
  58.         Intent intent = new Intent(this, NewMob.class);
  59.         startActivityForResult(intent, 100);
  60.  
  61.     }
  62.  
  63.     //It creates a drag shadow for dragging a TextView as a small gray rectangle
  64.     private static class MyDragShadowBuilder extends View.DragShadowBuilder {
  65.  
  66.         private static Drawable shadow;
  67.         public MyDragShadowBuilder (View v) {
  68.  
  69.             super(v);
  70.             shadow = new ColorDrawable(Color.LTGRAY);
  71.  
  72.         }
  73.         public void onProvideShadowMetrics (Point size, Point touch) {
  74.  
  75.             int width, height;
  76.             width = getView().getWidth();
  77.             height = getView().getHeight();
  78.             shadow.setBounds(0, 0, width, height);
  79.             size.set(width,height);
  80.             touch.set(width/2, height/2);
  81.  
  82.         }
  83.         public void onDrawShadow (Canvas canvas) {
  84.             shadow.draw(canvas);
  85.         }
  86.  
  87.     }
  88.  
  89.     protected void onActivityResult(int requestCode, int returnCode, Intent intent) {
  90.  
  91.         if(requestCode==100) {
  92.  
  93.             String monsterName = intent.getStringExtra("monsterName");
  94.             String monsterMaxHP = intent.getStringExtra("monsterMaxHP");
  95.             if(returnCode == 1001 && monsterName != null && monsterMaxHP != null) {
  96.  
  97.                 // Creating a RelativeLayout inside the layout with 'android:id=@+id/rootlayout'
  98.                 LinearLayout mobsArea = (LinearLayout) findViewById(R.id.mobsarea);
  99.                 final LinearLayout mobArea = new LinearLayout(this);
  100.                 mobArea.setOrientation(LinearLayout.VERTICAL);
  101.                 LayoutParams mobsAreaParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  102.                 mobsArea.addView(mobArea, mobsAreaParams);
  103.                 // Inside of it I create a TextView ('this' is the context, can also getContext(); or smth)
  104.                 TextView textMonsterName = new TextView(this);
  105.                 textMonsterName.setText(monsterName);
  106.                 mobArea.addView(textMonsterName);
  107.                 // Another TextView for the HP
  108.                 TextView textMonsterMaxHP = new TextView(this);
  109.                 textMonsterMaxHP.setText(monsterMaxHP);
  110.                 mobArea.addView(textMonsterMaxHP);
  111.                 mobArea.setTag("Mob");
  112.                 View.OnDragListener mDragListen = new myDragEventListener();
  113.                 mobArea.setOnLongClickListener(new View.OnLongClickListener() {
  114.  
  115.                     public boolean onLongClick(View v) {
  116.  
  117.                         //Guide sait v.getTag(), but since it needs a text type thing, I needed toString() it
  118.                         ClipData.Item item = new ClipData.Item(v.getTag().toString());
  119.                         //Once MIMETYPE_TEXT_PLAIN is called technically this error should stop existing
  120.                         ClipData dragData = new ClipData(v.getTag(), ClipData.MIMETYPE_TEXT_PLAIN, item);
  121.                         //If I don't make mobArea final the following line will give me an error, something tells me I shouldn't be doing this
  122.                         View.DragShadowBuilder myShadow = new MyDragShadowBuilder(mobArea);
  123.                         v.startDrag (
  124.                             dragData,
  125.                             myShadow,
  126.                             null,
  127.                             0
  128.                         );
  129.  
  130.                     }   //Stop giving this error you motherfucker
  131.  
  132.                });
  133.  
  134.  
  135.             }
  136.  
  137.         }
  138.  
  139.     }
  140.  
  141.     protected class myDragEventListener implements View.OnDragListener {
  142.  
  143.         public boolean onDrag(View v, DragEvent event) {
  144.  
  145.         }
  146.  
  147.     }
  148.  
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement