Android Studio: Set dropped TextView to whatever was dragged - java

I am implementing a Drag and Drop interface with 6 Drag TextViews and 6 Drop TextViews.
Any 1 of 6 Drag views can be dropped onto any 1 of 6 Drop views.
An example would be if tvNum1=13, tvNum2=16, tvNum3=9, then I can drop any one of them onto tvSTR, tvDEX, or tvINT and set the respective Drop text to whatever was dragged. So if tvNum2 was dragged onto tvSTR, then tvSTR should show 16. Or let's say that instead you decided to drag tvNum3 onto tvSTR, then tvSTR should show 9. Each Drag view can only be used once.
Here's my current code. I can't find a way to make it so that the text of whatever was dragged is the text of the Drop view. I can only make (a 1-to-1 correlation) it so that if tvNum1 was dragged onto tvSTR, then tvSTR=13. Or if tvNum2 was dragged onto tvDEX, then tvDEX=16.
public class setCharacterRolls extends AppCompatActivity
{
// Variable declarations
private TextView tvNum1, tvNum2, tvNum3, tvNum4, tvNum5, tvNum6;
private TextView tvSTR, tvDEX, tvINT, tvWIS, tvCHA, tvCON;
String roll1, roll2, roll3, roll4, roll5, roll6;
// String roll1Num, roll2Num, roll3Num, roll4Num, roll5Num, roll6Num;
private Button bSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_character_rolls);
// Get the previous intent
Intent intent = getIntent();
// Save the rolls from the previous screen onto a String to use later.
roll1 = intent.getStringExtra("roll1");
roll2 = intent.getStringExtra("roll2");
roll3 = intent.getStringExtra("roll3");
roll4 = intent.getStringExtra("roll4");
roll5 = intent.getStringExtra("roll5");
roll6 = intent.getStringExtra("roll6");
// Get TextView id's
tvNum1 = (TextView) findViewById(R.id.tDrag1);
tvNum2 = (TextView) findViewById(R.id.tDrag2);
tvNum3 = (TextView) findViewById(R.id.tDrag3);
tvNum4 = (TextView) findViewById(R.id.tDrag4);
tvNum5 = (TextView) findViewById(R.id.tDrag5);
tvNum6 = (TextView) findViewById(R.id.tDrag6);
tvSTR = (TextView) findViewById(R.id.tvSTR);
tvDEX = (TextView) findViewById(R.id.tvDEX);
tvINT = (TextView) findViewById(R.id.tvINT);
tvWIS = (TextView) findViewById(R.id.tvWIS);
tvCHA = (TextView) findViewById(R.id.tvCHA);
tvCON = (TextView) findViewById(R.id.tvCON);
// Set TextView to the roll numbers
tvNum1.setText(roll1);
tvNum2.setText(roll2);
tvNum3.setText(roll3);
tvNum4.setText(roll4);
tvNum5.setText(roll5);
tvNum6.setText(roll6);
// Set TextView to be draggable
tvNum1.setOnTouchListener(onTouch);
tvNum2.setOnTouchListener(onTouch);
tvNum3.setOnTouchListener(onTouch);
tvNum4.setOnTouchListener(onTouch);
tvNum5.setOnTouchListener(onTouch);
tvNum6.setOnTouchListener(onTouch);
// Targets to be dropped on
tvSTR.setOnDragListener(dragListener);
tvDEX.setOnDragListener(dragListener);
tvINT.setOnDragListener(dragListener);
tvWIS.setOnDragListener(dragListener);
tvCHA.setOnDragListener(dragListener);
tvCON.setOnDragListener(dragListener);
}
View.OnTouchListener onTouch = new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent mEvent)
{
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuild = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuild, v, 0);
return true;
}
};
View.OnDragListener dragListener = new View.OnDragListener()
{
#Override
public boolean onDrag(View v, DragEvent event)
{
int dragEvent = event.getAction();
switch(dragEvent)
{
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
final View view = (View) event.getLocalState();
switch(view.getId())
{
// This is where I have the 1-to-1 correlation.
// I want to be able to drag any draggable texview and set it to whatever it was dropped on.
case R.id.tDrag1:
tvSTR.setText(roll1);
break;
case R.id.tDrag2:
tvDEX.setText(roll2);
break;
default:
break;
}
break;
case DragEvent.ACTION_DROP:
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
};
}

Related

Why is the text in TextViews changing two times when I am calling it only once at a time?

I know setText just changes the text only once but I can't seem to find the reason why the text in it changes before I move on to the next question in the quizActivity
What I have made is an app with one activity in it which has a quiz in it, a question is displayed along with 4 options. When the user selects an option, if that option is correct then it becomes green and red otherwise and additionally, I then open a dialog box showing whether the answer was right or wrong and then the question is changed when the user clicks Next on the dialog box.
But what is happening that when the user selects an option, in between the process of clicking the option and then clicking next on the dialog box, the text in the questions and the options changes and I can't seem to figure out why is that happening. In total, the question and options change two times when they should change only once, the unexpected change is when the user clicks on an option and the dialog box opens.
Here's the code:
#Override
public void onClick(View view) {
int selectedOption = 0;
switch (view.getId()) {
case R.id.option_1_tile:
selectedOption = 1;
break;
case R.id.option_2_tile:
selectedOption = 2;
break;
case R.id.option_3_tile:
selectedOption = 3;
break;
case R.id.option_4_tile:
selectedOption = 4;
break;
default:
}
checkAnswer(selectedOption, view);
}
Here's the function which checks the answer:
private void checkAnswer(int selectedOption, View view) {
if (selectedOption == selected_questions.get(quesNum).getAnswer()) {
//Right Answer
(view).setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
quizReference.child(selected_questions.get(quesNum).getId()).child("correct_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getCorrect_attempts()) + 1));
quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));
score++;
correctDialog();
} else {
//Wrong Answer
(view).setBackgroundTintList(ColorStateList.valueOf(Color.RED));
quizReference.child(selected_questions.get(quesNum).getId()).child("total_attempts").setValue(String.valueOf(Integer.valueOf(selected_questions.get(quesNum).getTotal_attempts()) + 1));
switch (selected_questions.get(quesNum).getAnswer()) {
case 1:
options[0].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 2:
options[1].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 3:
options[2].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
case 4:
options[3].setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
break;
}
wrongDialog ();
}
}
Here's the function which changes the question:
private void changeQuestion() {
resetColor ();
if (quesNum < selected_questions.size() - 1) {
quesNum++;
playAnim(question, 0, 0);
playAnim(option1_text, 0, 1);
playAnim(option2_text, 0, 2);
playAnim(option3_text, 0, 3);
playAnim(option4_text, 0, 4);
qCount.setText(String.valueOf(quesNum + 1) + "/" + String.valueOf(selected_questions.size()));
} else {
// Go to Score Activity
Intent intent = new Intent(quizActivity.this, scoreActivity.class);
intent.putExtra("SCORE", String.valueOf(score) + "/" + String.valueOf(selected_questions.size()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
Here's the function which sets the text and animation:
private void playAnim(final View view, final int value, final int viewNum) {
view.animate().alpha(value).scaleX(value).scaleY(value).setDuration(500)
.setStartDelay(100).setInterpolator(new DecelerateInterpolator())
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
if (value == 0) {
switch (viewNum) {
case 0:
((TextView) view).setText(selected_questions.get(quesNum).getQuestion());
break;
case 1:
((TextView) view).setText(selected_questions.get(quesNum).getOption1());
break;
case 2:
((TextView) view).setText(selected_questions.get(quesNum).getOption2());
break;
case 3:
((TextView) view).setText(selected_questions.get(quesNum).getOption3());
break;
case 4:
((TextView) view).setText(selected_questions.get(quesNum).getOption4());
break;
}
if (viewNum != 0)
(view).setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#E99C03")));
playAnim(view, 1, viewNum);
}
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
Here's the code for the dialog boxes:
public void wrongDialog() {
final Dialog dialogWrong = new Dialog(quizActivity.this);
dialogWrong.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogWrong.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogWrong.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogWrong.setContentView(R.layout.dialog_wrong);
dialogWrong.setCancelable(false);
dialogWrong.show();
TextView wrongText = (TextView) dialogWrong.findViewById(R.id.wrongText);
Button buttonNext = (Button) dialogWrong.findViewById(R.id.dialogNext);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogWrong.dismiss();
//reset the color of buttons back to white
resetColor();
//Change question
changeQuestion();
}
});
}
public void correctDialog() {
final Dialog dialogCorrect = new Dialog(quizActivity.this);
dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (dialogCorrect.getWindow() != null) {
ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
}
dialogCorrect.setContentView(R.layout.dialog_correct);
dialogCorrect.setCancelable(false);
dialogCorrect.show();
TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
Button buttonNext = (Button) dialogCorrect.findViewById(R.id.dialogNext);
//OnCLick listener to go next que
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//This will dismiss the dialog
dialogCorrect.dismiss();
//reset the color of buttons back to white
resetColor();
//it will increment the question number
changeQuestion();
}
});
}
I have tried to explain it to my best ability though I would be glad to answer any additional information/code you may want. Also, this is the link for the project if you have the time to run it and understand the problem better.
I have checked your code. you have placed an addValueEventListener in setUpdates method. When you select an option, you update the firestore database by setting fields like total attempts. As a result, eventListener gets triggered and "selectQuestionSet" function is called.
Hence, every time you select an option, selectQuestionSet function is called. You should make sure that its called only once at the start.

Drag and Drop using images

I have this drag and drop on android studio, it works using image views. Once I pick an image view up using a OnLongListener and drop it to a blank image view which is the target, the target image view should change to the image view I have grabbed.
My Code
img6.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
DragShadow dragshadow = new DragShadow(v);
ClipData data = ClipData.newPlainText("", "");
v.startDrag(data, dragshadow, v, 0);
return false;
}
});
droptarget.setOnDragListener(new View.OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
int dragEvent = event.getAction();
int ImageViewID = v.getId();
switch (dragEvent) {
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
droptarget.setImageResource(ImageViewID);
break;
}
return true;
}
});
The setImageResource comes up as an error.
setImageResource sets a drawable as the content of an ImageView. But you are trying to pass the id of a view to it
int ImageViewID = v.getId();
droptarget.setImageResource(ImageViewID);
Instead you can try something:
ImageView imageView = (ImageView)v;
droptarget.setImageResource(imageView.getDrawable());
You can send the Drawable code(int) (For example int value of R.id.some_image_view) in the ClipData of the View getting dragged. Then use that code to retrieve the correct Drawable to the Dropped-on-View.
droptarget.setImageDrawable(getResources().getDrawable(Integer.parseInt(code)));
Then force a redraw with:
droptarget.invalidate();
This do require you to override the OnLongClick listener though with your own custom one.

adding ImageViews to Relative Layout using Drag and Drop

I am pretty new to Android and I am making an app with a Vertical Linear Layout at the bottom of the screen with a list of imageViews going across the screen. Each of these are draggable onto the main part of the screen, however, if I drag one of the imageviews on the far right of the layout, it will drop the view far to the left of the actual drop point. What is strange is that if I try dragging the first imageview in the linearlayout (the one on the left side) it will land right under where I drop it.
My onDrag and onTouch Listeners...
class MyDragListener implements OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
float X = event.getX();
float Y = event.getY();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
container = (RelativeLayout) v;
container.setLayoutParams(findViewById(R.id.drawingbackground).getLayoutParams());
container.addView(view);
view.setX(X-(view.getWidth()/2));
view.setY(Y-(view.getHeight()/2));
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
and the OnTouch...
final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return true;
} if(motionEvent.getAction()== MotionEvent.ACTION_UP){
view.performClick();
}
return false;
}
}
Any assistance would be greatly appreciated!
Change your xml.Use Relative Layout Instead of LinearLayout and Fix their height and width.It would work pretty fine.

Setting text color of EditText

I have a dialog that triggers on EditText's context menu. Dialog sets the amount of red, green and blue of EditText. The problem is that runtime error occurs when I click positive button. Items above works fine, they set the color of text in EditText as it should, but rgbDialog don't.
I think that the problem occurs here:
etRed = (EditText)findViewById(R.id.etRed);
etGreen = (EditText)findViewById(R.id.etGreen);
etBlue = (EditText)findViewById(R.id.etBlue);
But I don't know how to initialize etRed, etGreen and etBlue right. I think that I should place something before findView like
etRed = (EditText)Dialog.findViewById(R.id.etRed);
But don't know what, because everything is happening in the same method (onContextItemSelected)
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.white:
temp.setTextColor(Color.WHITE);
break;
case R.id.red:
temp.setTextColor(Color.RED);
break;
case R.id.green:
temp.setTextColor(Color.GREEN);
break;
case R.id.rgb:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("choose the amount of red green and blue");
LayoutInflater inflater = this.getLayoutInflater();
dialogBuilder.setView(inflater.inflate(R.layout.colors, null));
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
etRed = (EditText)findViewById(R.id.etRed);
etGreen = (EditText)findViewById(R.id.etGreen);
etBlue = (EditText)findViewById(R.id.etBlue);
red=etRed.getText().toString();
green = etGreen.getText().toString();
blue = etBlue.getText().toString();
intRed = Integer.parseInt(red);
intGreen = Integer.parseInt(green);
intBlue = Integer.parseInt(blue);
temp.setTextColor(Color.rgb(intRed, intGreen, intBlue));
}
});
dialogBuilder.setNegativeButton("CANCEL", null);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
EditText temp is set like this, and works fine for items above rgb
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context, menu);
temp = (EditText)findViewById(v.getId());
super.onCreateContextMenu(menu, v, menuInfo);
}
you can have something like below to call findViewById from correct layout:
View dialogView = inflater.inflate(R.layout.colors, null);
dialogBuilder.setView(dialogView );
and when you are calling findViewById in onClick you can have like:
etRed = (EditText)dialogView.findViewById(R.id.etRed);
etGreen = (EditText)dialogView.findViewById(R.id.etGreen);
etBlue = (EditText)dialogView.findViewById(R.id.etBlue);
Check if this helps.

Error in updating the main content by replacing fragments

In my sample project, I implemented the code using activity, running fine.
After then I implemented that code in my main App which is using Fragment.
For that I extends Fragment, made a default constructor, made onCreateView & inside that inflated the layout, then changed getApplicationContext() to getActivity(). Thats how I managed to remove errors in my main app.
An in my MainActivity this is how we update the main content by replacing fragments by calling constructor & assigning it to fragments:
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new IntroductionFragment();
break;
case 1:
fragment = new PrefaceFragment();
break;
case 2:
fragment = new PreambleFragment();
break;
case 3:
fragment = new ContentsFragment();//ERROR here: because I modified this class
break;
case 4:
fragment = new SchedulesFragment();
break;
case 5:
fragment = new AppendixFragment();
break;
case 6:
fragment = new AmendmentFragment();
break;
case 7:
fragment = new PhotoFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
That line shows me Type mismatch: cannot convert from ContentsFragment to Fragment
I hope the problem is clear. Please tell me how can I avoid this error...
This is ContentsFragment.java
public class ContentsFragment extends Fragment// implements OnTouchListener
{
public ContentsFragment() {
// TODO Auto-generated constructor stub
}
public static ArrayList<GS> q = new ArrayList<GS>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBAdapter db = DBAdapter.getDBAdapter(getActivity());
if (!db.checkDatabase())
db.createDatabase(getActivity());
db.openDatabase();
q = db.getData();
// setContentView(R.layout.sample_activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.sample_activity, container, false);
final FloatingGroupExpandableListView list = (FloatingGroupExpandableListView) v.findViewById(R.id.sample_activity_list);
// final LayoutInflater inflater = getLayoutInflater();
// Even though the child divider has already been set on the layout file, we have to set it again here
// This prevents a bug where the background turns to the color of the child divider when the list is expanded
list.setChildDivider(new ColorDrawable(Color.BLACK));
final ArticlesAdapter adapter = new ArticlesAdapter(getActivity());
final WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(adapter);
list.setAdapter(wrapperAdapter);
for(int i = 0; i < wrapperAdapter.getGroupCount(); i++) {
list.collapseGroup(i);//expandGroup(i);
}
list.setOnScrollFloatingGroupListener(new FloatingGroupExpandableListView.OnScrollFloatingGroupListener() {
#Override
public void onScrollFloatingGroupListener(View floatingGroupView, int scrollY) {
float interpolation = - scrollY / (float) floatingGroupView.getHeight();
// Changing from RGB(162,201,85) to RGB(255,255,255)
final int greenToWhiteRed = (int) (0 + 3 * interpolation);
final int greenToWhiteGreen = (int) (0 + 6* interpolation);
final int greenToWhiteBlue = (int) ( 0+ 20 * interpolation);
final int greenToWhiteColor = Color.argb(200, greenToWhiteRed, greenToWhiteGreen, greenToWhiteBlue);
// Changing from RGB(255,255,255) to RGB(0,0,0)
final int whiteToBlackRed = (int) (255 - 1 * interpolation);
final int whiteToBlackGreen = (int) (255 - 1 * interpolation);
final int whiteToBlackBlue = (int) (255 - 1 * interpolation);
final int whiteToBlackColor = Color.argb(255, whiteToBlackRed, whiteToBlackGreen, whiteToBlackBlue);
final View background = floatingGroupView.findViewById(R.id.sample_activity_list_group_item_background);
background.setBackgroundColor(greenToWhiteColor);
final TextView text = (TextView) floatingGroupView.findViewById(R.id.sample_activity_list_group_item_text);
text.setTextColor(whiteToBlackColor);
final ImageView expanded = (ImageView) floatingGroupView.findViewById(R.id.sample_activity_list_group_expanded_image);
final Drawable expandedDrawable = expanded.getDrawable().mutate();
expandedDrawable.setColorFilter(whiteToBlackColor, PorterDuff.Mode.SRC_ATOP);
}
});
return v;
}
}
I think the problem is because of importing Fragment from different sources in two different classes ie. Your_Activity and ContentsFragment.java. One from android.support.v4.app.Fragment and one from android.app.Fragment.
Please make sure you are importing Fragment either from support library ie.android.support.v4.app.Fragment or from android.app.Fragment.

Categories

Resources