Android onClick start's two animation, should only start one - java

Depending on which animation I click first, the second animation will always set off the first animation. I have no idea what I'm doing incorrectly.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Test extends Activity {
ImageView img_left;
ImageView img_right;
Button left;
Button right;
TranslateAnimation moveup;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
moveup = new TranslateAnimation(0, 0, 0, -900);
moveup.setDuration(2000);
moveup.setFillAfter(true);
left = (Button) findViewById(R.id.left);
right = (Button) findViewById(R.id.right);
img_left = (ImageView) findViewById(R.id.image_left);
img_right = (ImageView) findViewById(R.id.image_right);
right.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_right.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_LONG).show();
}
});
left.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_left.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_LONG).show();
}
});
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context=".First" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000EE"
android:text="" />
<Button
android:id="#+id/right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000EE"
android:text="" />
</LinearLayout>
<ImageView
android:id="#+id/image_left"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/cue1" />
<ImageView
android:id="#+id/image_right"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/cue2" />
</RelativeLayout>

Create two different Animation objects with the same properties, one for the left and one for the right, or call setAnimation(null) on the other view before you start it. In other words:
right.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_left.setAnimation(null);
img_right.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "right", Toast.LENGTH_LONG).show();
}
});
left.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
img_right.setAnimation(null);
img_left.startAnimation(moveup);
Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_LONG).show();
}
});
Animation objects are attached to their views when setAnimation() or startAnimation() is called. By starting both views attached to the same Animation instance, the animation has the possibility of affecting the drawing of both views as it runs. Views only pay attention to the current state of the animation when they are invalidated, so this doesn't necessarily mean they will always animate together. But if your code gets into a situation where both ImageView instances are invalid while the Animation runs, they will both respond to the translation.

try using >> new View.OnClickListener() in place of new OnClickListener()

Related

I want to make a search bar for this code to search by buttons (between btn1 and btn2 and btn3) and hide the non-matched results

I am trying to add a search bar that enables me to search by buttons, search between btn1 and btn2 and btn3. I tried a lot of codes but all of them were focused on TextView and not on buttons. So hope to help me and if I missed details please let me know to describe more.
The two files (.java and .xml) are typed below. thank you.
file.java
package com.example.myapp1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
SearchView sv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("My First App");
Button btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}});
Button btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity3.class);
startActivity(intent);
}});
Button btn3 = findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity4.class);
startActivity(intent);
}});
List<Button> buttonList = new ArrayList<>();
buttonList.add(view.findViewById(R.id.btn1));
buttonList.add(view.findViewById(R.id.btn2));
buttonList.add(view.findViewById(R.id.btn3));
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
for(int i = 0; i < buttonList.size(); i++) {
String buttonText = (String) buttonList.get(i).getText();
if(buttonText.toLowerCase(Locale.ROOT).equals(s.toLowerCase())) {
buttonList.get(i).setVisibility(View.VISIBLE);
} else {
buttonList.get(i).setVisibility(View.INVISIBLE);
}
}
return false;
}
#Override
public boolean onQueryTextChange(String s) {
if(s.isEmpty()) {
for(int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setVisibility(View.VISIBLE);
}
}
return false;
}
});
}
}
file.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="ExtraText"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="vertical"
android:weightSum="2">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:textSize="25dp"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:background="#drawable/gradient"
android:textColor="#color/white"
android:text="Absolute Pressure" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:textSize="25dp"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:background="#drawable/gradient"
android:textColor="#color/white"
android:text="Acoustic Flowmeter" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn3"
android:layout_width="match_parent"
android:layout_height="90dp"
android:textAllCaps="false"
android:textSize="25dp"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:background="#drawable/gradient"
android:textColor="#color/white"
android:text="Bernoulli Number" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
What do you mean by search by buttons? Are you looking to search the text that is held within the button?
Here is a way you can go about this:
Make a list that contains all the instances of your buttons.
List(findViewById(R.id.btn1), etc)
List<Button> buttonList = new ArrayList<>();
buttonList.add(view.findViewById(R.id.button_search1));
buttonList.add(view.findViewById(R.id.button_search2));
buttonList.add(view.findViewById(R.id.button_search3));
We are going to use the listeners available to the search view to do the work.
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
// You can use this for when the submit is clicked
#Override
public boolean onQueryTextSubmit(String s) {
// We loop over the button list
for(int i = 0; i < buttonList.size(); i++) {
// We get the text of the button
String buttonText = (String) buttonList.get(i).getText();
// We turn both the button text and search result to lowercase
// So that results are not case sensitive
// If they match
if(buttonText.toLowerCase(Locale.ROOT).equals(s.toLowerCase())) {
// We make button visible
buttonList.get(i).setVisibility(View.VISIBLE);
} else {
// If it doesn't, we make invisible
buttonList.get(i).setVisibility(View.INVISIBLE);
}
}
return false;
}
// You can use this when the text is changing.
#Override
public boolean onQueryTextChange(String s) {
// If text is empty, reset results
if(s.isEmpty()) {
for(int i = 0; i < buttonList.size(); i++) {
buttonList.get(i).setVisibility(View.VISIBLE);
}
}
return false;
}
});

How to align imageView after change by button

I started learning making Android Apps, and I have stumbled upon a problem.
I have made my app to change an imageView by a button
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring); // changes the image
so, now, when I click on the button, it changes an image.
The problem is, the original image was centered, and the image that replaces it is set on top left. I can't center it, or move it or anything. I guess I am missing a tiny detail somewhere.
MainActivity.java:
package com.maxa.testaplikacija;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
Button button;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button = (Button)findViewById(R.id.bttOK);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/udajSe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bttOK"
android:layout_centerHorizontal="true"
android:text="#string/udaj_se_za_mene" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:adjustViewBounds="false"
android:contentDescription="#string/poklon"
android:foregroundGravity="center_horizontal"
app:srcCompat="#drawable/present" />
<Button
android:id="#+id/bttOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="115dp"
android:text="#string/otvori_me" />
</RelativeLayout>
I am still new to this, but I am a quick learner.
Inside the onclick method align the imageView like this
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.setImageResource(R.drawable.ring);
imageView.setLayoutParams(params);
}
});
Your view seems to have the image set to top left, see these XML tags in your ImageView:
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
Are you sure the original image files aren't spaced differently / have extra padding as part of the image itself?

My android app stopping when I touch the button

This is my code guys when I touch the "saldir" button or another button my app
crashes and this is my app's image https://ibb.co/ce3ATm
This is my error (https://ibb.co/jBxb16):
Dergilik APP has stopped
package com.example.rg.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener
{
int sayac =0;
TextView tv;
**//kind of turkish words don't focus the variables**
TextView tvkarekterOzellikleri;
Button byemekye;
#Override
**//THIS IS MY MAIN_Activity.Java**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.Answer);
//I'm trying very basic app when I touch button this layout'll changed
tvkarekterOzellikleri = (TextView) findViewById(R.id.Question);
Button bsaldir = (Button) findViewById(R.id.saldir);
//comment/i have 3 buttons as you can see on link at top
Button buyu = (Button) findViewById(R.id.uyu);
Button byemekye = (Button) findViewById(R.id.yemekYe);
bsaldir.setOnClickListener(this);
buyu.setOnClickListener(this);
byemekye.setOnClickListener(this);*
//This part useless now because I didn't added this part later I'll add this part
//Character k = new Character();
//k.Movementnum = 10;
//k.Kilos = 10;
//k.FightPower = 10;
//}
#Override
//This part is PROBLEM WHEN I DELETE THIS PART MY CODE IS WORKING
public void onClick(View v){
if (v.getId()==byemekye.getId())
tv.setText("yemek yenildi");
else
tv.setText("Nar" + (sayac++));
}
}
When I delete if.(v.getId....) app is running normal.
This side is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rg.myapplication.MainActivity">
<Button
android:id="#+id/saldir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="saldir" />
<TextView
android:id="#+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|center"
android:padding="3dp"
android:text="#string/Dilektasi"
android:textAppearance="#style/TextAppearance.AppCompat.Display2"
android:layout_above="#+id/saldir"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<TextView
**android:id="#+id/Answer**"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/saldir"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="" />
//I have 3 button for my app as you can see on image
<Button
android:id="#+id/yemekYe"
**im saying twice pls dont focus on variables**
**next time i'll make this variables english**
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toLeftOf="#+id/saldir"
android:layout_toStartOf="#+id/saldir"
android:text="yemek ye" />
smth smth smth smth
<Button
android:id="#+id/uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/saldir"
android:layout_toEndOf="#+id/saldir"
android:layout_toRightOf="#+id/saldir"
android:text="Uyu" />
//dont focus the variable names
</RelativeLayout>
I think your problem is this:
Define Buttons:
private Button bt;
Set id of the button on xml, than on onCrate()
bt= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
code:
public class YourActivity extends AppCompatActivity {
private Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_details);
parent_view = findViewById(android.R.id.content);
btn1= (Button) findViewById(R.id.button1); //button1 is the id of the button
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do actions
}
});
}
on xml
<Button
android:id="#+id/butto1" <!-- the id -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

Unable to make EditText multi-line

I am making an Android app for storing notes. I have made my editText behave like textView before button click. Clicking on edit button makes the editText take value. Now problem arises when i save that value and when it is displayed. EditText doesnt come in multi-line as I want but in a single line.
I declared the inputType property of editText as multi-line in xml file but in the code upon initialising the editText i set its inputType property as null in an attempt to make it both null and multi-line but alas! It doesn't happen.
Below is my xml and java code:
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/AppTheme.AppBarOverlay"
android:background="#color/colorToolBar"
android:id="#+id/idToolbarNoteDetails"
android:padding="10dp"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/idFloatingActionButtonEdit"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
app:srcCompat="#android:drawable/ic_menu_edit"
android:layout_margin="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Colors: "
android:textStyle="italic"
android:visibility="invisible"
android:id="#+id/idTextViewColors"
android:layout_below="#+id/idToolbarNoteDetails"
android:layout_margin="10dp"
android:textSize="15dp"/>
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/idColorRed"
android:visibility="invisible"
android:layout_margin="10dp"
android:layout_below="#+id/idToolbarNoteDetails"
android:layout_toRightOf="#+id/idTextViewColors"
android:background="#drawable/textview_circle_red" />
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:visibility="invisible"
android:id="#+id/idColorBlue"
android:layout_margin="10dp"
android:layout_below="#id/idToolbarNoteDetails"
android:layout_toRightOf="#+id/idColorRed"
android:background="#drawable/textview_circle_blue"/>
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/idColorGreen"
android:visibility="invisible"
android:layout_margin="10dp"
android:layout_below="#id/idToolbarNoteDetails"
android:layout_toRightOf="#+id/idColorBlue"
android:background="#drawable/textview_circle_green"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/idTextViewColors"
android:layout_margin="10dp"
android:inputType="textShortMessage"
android:background="#android:color/transparent"
android:textSize="40sp"
android:hint="Title"
android:textStyle="bold"
android:id="#+id/idEditTextTitle"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/idEditTextTitle"
android:hint="Content"
android:inputType="textMultiLine"
android:lines="5"
android:gravity="top|left"
android:layout_margin="10dp"
android:textSize="20sp"
android:background="#android:color/transparent"
android:id="#+id/idEditTextContent"/>
<EditText
android:layout_width="250dp"
android:id="#+id/idEditTextTag"
android:layout_height="35dp"
android:hint="#TAG"
android:background="#android:color/transparent"
android:layout_alignParentBottom="true"
android:textSize="15dp"
android:textStyle="italic"
android:layout_margin="10dp"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/idFloatingActionButtonSave"
android:layout_above="#+id/idFloatingActionButtonEdit"
android:layout_margin="20dp"
android:visibility="invisible"
app:srcCompat="#android:drawable/ic_menu_save"
android:layout_alignParentRight="true"/>
JAVA
package mynotes.pawanjotsingh.com.mynotes.activities;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import mynotes.pawanjotsingh.com.mynotes.R;
import mynotes.pawanjotsingh.com.mynotes.adapters.RecyclerAdapter;
import mynotes.pawanjotsingh.com.mynotes.dbhelper.DataBaseHelper;
import mynotes.pawanjotsingh.com.mynotes.models.NoteModel;
public class NoteDetailsActivity extends AppCompatActivity {
enum Colors{RED, BLUE, GREEN, WHITE}
DataBaseHelper dataBaseHelper;
FloatingActionButton floatingActionButtonEdit,floatingActionButtonSave;
EditText editTextTitle,editTextContent,editTextTag;
String stringTitle,stringContent,stringTag;
TextView textViewColors,textViewRed,textViewBlue,textViewGreen;
Toolbar toolbar;
int color,colorRed,colorBlue,colorGreen;
String id="";
Colors currentBackgroundColour = Colors.WHITE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_details);
editTextTitle=(EditText) findViewById(R.id.idEditTextTitle);
editTextTitle.setInputType(InputType.TYPE_NULL);
editTextContent=(EditText) findViewById(R.id.idEditTextContent);
editTextContent.setInputType(InputType.TYPE_NULL);
editTextTag=(EditText) findViewById(R.id.idEditTextTag);
editTextTag.setInputType(InputType.TYPE_NULL);
toolbar=(Toolbar) findViewById(R.id.idToolbarNoteDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Edit");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
dataBaseHelper=new DataBaseHelper(this);
textViewColors=(TextView) findViewById(R.id.idTextViewColors);
textViewRed=(TextView) findViewById(R.id.idColorRed);
textViewBlue=(TextView) findViewById(R.id.idColorBlue);
textViewGreen=(TextView) findViewById(R.id.idColorGreen);
colorRed=Color.parseColor("#FE7676");
colorBlue=Color.parseColor("#76FEEC");
colorGreen=Color.parseColor("#99FE76");
stringTitle=getIntent().getStringExtra("text_title");
stringContent=getIntent().getStringExtra("text_content");
stringTag=getIntent().getStringExtra("text_tag");
id = getIntent().getStringExtra("id");
color=getIntent().getIntExtra("color", Color.WHITE);
getWindow().getDecorView().setBackgroundColor(color);
editTextTitle.setText(stringTitle);
editTextContent.setText(stringContent);
editTextTag.setText(stringTag);
floatingActionButtonSave=(FloatingActionButton) findViewById(R.id.idFloatingActionButtonSave);
floatingActionButtonSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
save();
}
});
floatingActionButtonEdit=(FloatingActionButton) findViewById(R.id.idFloatingActionButtonEdit);
floatingActionButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
floatingActionButtonSave.setVisibility(View.VISIBLE);
Animation animationLeft=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_left);
floatingActionButtonSave.startAnimation(animationLeft);
textViewColors.setVisibility(View.VISIBLE);
Animation animationDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_down);
textViewColors.startAnimation(animationDown);
textViewRed.setVisibility(View.VISIBLE);
textViewRed.startAnimation(animationDown);
textViewBlue.setVisibility(View.VISIBLE);
textViewBlue.startAnimation(animationDown);
textViewGreen.setVisibility(View.VISIBLE);
textViewGreen.startAnimation(animationDown);
editNote();
}
});
textViewRed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentBackgroundColour=Colors.RED;
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FE7676"));
}
});
textViewBlue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentBackgroundColour=Colors.BLUE;
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#76FEEC"));
}
});
textViewGreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentBackgroundColour = Colors.GREEN;
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#99FE76"));
}
});
}
#Override
public void onBackPressed() {
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
public void editNote(){
editTextTitle.setInputType(InputType.TYPE_CLASS_TEXT);
editTextTitle.setFocusable(true);
editTextTitle.requestFocus();
editTextContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editTextTag.setInputType(InputType.TYPE_CLASS_TEXT);
}
public void save() {
int colour=Color.WHITE;
switch (currentBackgroundColour){
case RED:
colour = colorRed;
break;
case BLUE:
colour = colorBlue;
break;
case GREEN:
colour = colorGreen;
break;
}
final NoteModel noteModel = new NoteModel();
noteModel.setTitle(editTextTitle.getText().toString());
noteModel.setContent(editTextContent.getText().toString());
noteModel.setTag(editTextTag.getText().toString());
noteModel.setId(id);
noteModel.setDate(getDateTime());
noteModel.setColor(colour);
boolean isModified=dataBaseHelper.editNote(noteModel);
if(isModified==true){
Toast.makeText(this, "Modifications saved", Toast.LENGTH_SHORT).show();
finish();
}
else Toast.makeText(this, "Unable to save modifications", Toast.LENGTH_SHORT).show();
}
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
Links to screenshots
This image is when I click edit button. This works just fine, text goes to another line.
This image is when i click the save button. The text goes back to single line instead of multi-line.
Okay so, I knew that the problem arises when I click on the edit button. Clicking the button made the editText single lined automatically even though I had mentioned the InputType as Multi-line in XML very clearly.
What I did to make the editText multi-line after the button click is that I wrote editTextContent.setSingleLine(false); in my java file inside the onClick button code.
And that is it. :)

Android Stopwatch : Time in Milliseconds not working

I know, I know, there are tons of post on here about this particular problem, however, most of them are very outdated, and none of them comply with the way I wrote this app... I'm fairly new to coding, and I'm doing this on my own, so i need help to show me how to use what I have and get the chronometer to display milliseconds!
Here's my Java:
package com.jackson.eason.stopwatch;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends Activity {
Chronometer mChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get references to views
mChronometer = (Chronometer) findViewById(R.id.chronometer);
Button button1 = (Button) findViewById(R.id.start);
Button button2 = (Button) findViewById(R.id.stop);
Button button3 = (Button) findViewById(R.id.reset);
Button button4 = (Button) findViewById(R.id.set_format);
Button button5 = (Button) findViewById(R.id.clear_format);
// Declare the listeners
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase
(SystemClock.elapsedRealtime());
}
};
// You forgot to declare a listener for set format in your updated code
View.OnClickListener mSetFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat("Formatted time (%s)");
}
};
View.OnClickListener mClearFormatListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setFormat(null);
}
};
//Assign listeners to your buttons
button1.setOnClickListener(mStartListener);
button2.setOnClickListener(mStopListener);
button3.setOnClickListener(mResetListener);
button4.setOnClickListener(mSetFormatListener);
button5.setOnClickListener(mClearFormatListener);
}
}
Also, if needed, Here's my Main XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:background="#ff4f4a53">
<Chronometer android:id="#+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="30dip"
android:paddingTop="30dip"
android:focusableInTouchMode="true"
android:textSize="55sp"
android:textIsSelectable="false"
android:typeface="serif"
android:textColor="#fffcfffa" />
<Button android:id="#+id/start"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="start"
android:background="#ff5effab"
android:elegantTextHeight="false"
android:layout_gravity="center_horizontal">
<requestFocus />
</Button>
<Button android:id="#+id/stop"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="stop"
android:background="#fffe7c5b">
</Button>
<Button android:id="#+id/reset"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="reset"
android:layout_gravity="center_horizontal"
android:textColor="#fffffcfe">
</Button>
<Button android:id="#+id/clear_format"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="clear"
android:layout_gravity="center_horizontal"
android:textColor="#fffefdff">
</Button>
<Button android:id="#+id/set_format"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="format"
android:layout_gravity="center_horizontal"
android:textColor="#ffffffff">
</Button>
</LinearLayout>
Any help would be greatly appreciated!

Categories