TextView goes out of screen up and down - java

So I'm creating the custom dialog box which should appear after the required button is pressed. However, when the text is too long, the textView goes out of screen. I tried to find a solution for this, however unsuccessfully.
I hope you will help me, guys. Thanks.
Here is the xml code for the dialog box:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txt_dia"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textSize="16sp"
android:layout_gravity="center"
android:textColor="#color/black"/>
</ScrollView>
<Button
android:id="#+id/btn_back"
android:layout_height="40dp"
android:layout_width="100dp"
android:clickable="true"
android:text="#string/bck"
android:textStyle="bold"
android:layout_gravity="left"/>
</LinearLayout>
Dialog class:
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class CustomDialogClass extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button back;
private TextView strongHeads;
private String text = "";
public CustomDialogClass(Activity a) {
super(a);
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
strongHeads = (TextView) findViewById(R.id.txt_dia);
//strongHeads.setMovementMethod(new ScrollingMovementMethod());
if(text.equalsIgnoreCase("")){
strongHeads.setText(text);
}
strongHeads.setText(text);
back = (Button) findViewById(R.id.btn_back);
back.setOnClickListener(this);
}
#Override
public void onClick(View v) {
dismiss();
}
public void changeText(String txt){
this.text= txt;
}
}

Related

Fragment Does Not Appear In Android

I recently learned about Fragments in Android and was building a notepad application to practice them.
Idea: The idea behind the app is simple. I press the button to add a new note. A CardView Viewgroup turns visible. The fragment will be housed within this CardView.
Problem: When I press the button to add a new note, the fragment does not pop up.
MainActivity.java
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Objects;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CardView fragmentCardView;
private Button newNoteButton;
private FragmentManager fragmentManager = getSupportFragmentManager();
private FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBar();
newNoteButton = findViewById(R.id.new_note_button);
fragmentCardView = findViewById(R.id.fragment_cardView);
}
void setActionBar(){
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.layout_custom_action_bar);
}
void showCreateNoteFragment(){
CreateNoteFragment createNoteFragment = CreateNoteFragment.newInstance();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_cardView, createNoteFragment);
fragmentTransaction.commit();
fragmentCardView.setVisibility(View.VISIBLE);
}
#Override
public void onClick(View v) {
if (v == newNoteButton)
showCreateNoteFragment();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="#F2F2F2"
tools:context=".MainActivity">
<ListView
android:id="#+id/notes_listView"
android:layout_width="match_parent"
android:layout_height="667dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="#+id/new_note_button"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_marginTop="27dp"
android:fontFamily="#font/nunito_bold"
android:text="#string/new_note_button_string"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/notes_listView"/>
<androidx.cardview.widget.CardView
android:id="#+id/fragment_cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:visibility="invisible"/>
</androidx.constraintlayout.widget.ConstraintLayout>
CreateNoteFragment.java
package com.example.notepadapplication;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class CreateNoteFragment extends Fragment implements View.OnClickListener {
private EditText inputEditText;
public CreateNoteFragment() {
// Required empty public constructor
}
static CreateNoteFragment newInstance(){
return new CreateNoteFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_create_note, container, false);
inputEditText = rootView.findViewById(R.id.input_editText);
Button saveButton = rootView.findViewById(R.id.save_button);
saveButton.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
String saveButtonText = inputEditText.getText().toString();
}
}
fragment_create_note.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:background="#F2F2F2"
tools:context=".CreateNoteFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/header_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/display_fragment_header_string"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="20sp"
android:fontFamily="#font/nunito_bold"/>
<View
android:id="#+id/divider_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_below="#id/header_textView"
android:background="#000000" />
<EditText
android:id="#+id/input_editText"
android:layout_width="match_parent"
android:layout_height="475dp"
android:layout_below="#id/divider_view"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:padding="10dp"
android:inputType="textMultiLine"
android:gravity="top"
android:background="#FFFFFF"
tools:ignore="Autofill,LabelFor,TextFields" />
<Button
android:id="#+id/save_button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="#string/save_button_string"
android:fontFamily="#font/nunito_bold"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Could anyone check why the fragment isn't popping up? Thanks for the help, guys.
P.S.: I think that the application might become a little bloated because of all the ViewGroups that I'm using. If you guys have a better way to design this, by all means, I would love any input.
When I press the button to add a new note, the fragment does not pop up.
This is expected since your button doesn't know what to do when pressed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBar();
newNoteButton = findViewById(R.id.new_note_button);
// Button will call activity's onClick whenever it's clicked
newNoteButton.setOnClickListener(this);
fragmentCardView = findViewById(R.id.fragment_cardView);
}

Android Studio RecyclerView wont show my data when i add toolbar at my `layout.xml` file

I created a page where it will show the firebase data in a RecyclerView. It work but I decided to put a toolbar at the top of the page to have a back function. After I put the toolbar in my layout file, the RecyclerView is not showing any data anymore. I think that my layout XML file is wrong. Thanks in advance for anyone helping.
Actity file
package com.example.attendanceappvqyfyp;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.attendanceappvqyfyp.Interface.itemClickListener;
import com.example.attendanceappvqyfyp.Common.Common;
import com.example.attendanceappvqyfyp.Model.LecturerClass;
import com.example.attendanceappvqyfyp.Model.News;
import com.example.attendanceappvqyfyp.Model.Timetable;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.example.attendanceappvqyfyp.Common.Common;
public class LecturerClassList extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference lecturerclass;
DatabaseReference news;
FirebaseRecyclerAdapter<LecturerClass,LecturerClassViewHolder> adapter;
TextView newscourse, newsclass;
EditText newsdescription;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lecturer_class_list);
//Firebase
database = FirebaseDatabase.getInstance();
lecturerclass = database.getReference("Class");
news = database.getReference("News");
recyclerView = (RecyclerView)findViewById(R.id.recycler_lecturerclass);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//Toolbar code
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Class List");
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
loadLecturerClass(Common.currentLecturer.getName());
}
private void loadLecturerClass(String Lecturer) {
adapter = new FirebaseRecyclerAdapter<LecturerClass, LecturerClassViewHolder>(LecturerClass.class, R.layout.lecturerclass_item, LecturerClassViewHolder.class, lecturerclass.orderByChild("lecturer").equalTo(Lecturer)) {
#Override
protected void populateViewHolder(LecturerClassViewHolder lecturerClassViewHolder, LecturerClass lecturerClass, int i) {
lecturerClassViewHolder.lclasstitle.setText("Class :" + adapter.getRef(i).getKey());
lecturerClassViewHolder.ldate.setText("Day :" + lecturerClass.getDay());
lecturerClassViewHolder.ltime.setText("Time :" + lecturerClass.getTime());
lecturerClassViewHolder.lclassroom.setText("Classroom :" + lecturerClass.getClassroom());
lecturerClassViewHolder.lcourse.setText(lecturerClass.getCourse());
lecturerClassViewHolder.setItemClickListener(new itemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
//code later
}
});
}
};
recyclerView.setAdapter(adapter);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals(Common.News))
{
showNewsDialog(adapter.getRef(item.getOrder()).getKey(),adapter.getItem(item.getOrder()));
}
return super.onContextItemSelected(item);
}
private void showNewsDialog(final String course, final LecturerClass item) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(LecturerClassList.this);
alertDialog.setTitle("Make Announcement");
alertDialog.setMessage("Make announcement about this class.");
LayoutInflater inflater = this.getLayoutInflater();
View lecturer_make_news = inflater.inflate(R.layout.activity_lecturer_make_news, null);
newscourse = lecturer_make_news.findViewById(R.id.NewsCourse);
newsclass = lecturer_make_news.findViewById(R.id.NewsClass);
newsdescription = lecturer_make_news.findViewById(R.id.NewsDescription);
newscourse.setText("Course: "+item.getCourse());
newsclass.setText("Subject: " + course);
alertDialog.setView(lecturer_make_news);
alertDialog.setPositiveButton("Post", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int i) {
if(newsdescription.getText().toString().isEmpty()){
Toast.makeText(LecturerClassList.this, "Post cancel, description cannot be empty.", Toast.LENGTH_SHORT).show();
}
else {
News lnews = new News(
item.getCourse(),
course,
newsdescription.getText().toString()
);
news.child(String.valueOf(System.currentTimeMillis())).setValue(lnews);
Toast.makeText(LecturerClassList.this, "Announcement post successfully.", Toast.LENGTH_SHORT).show();
finish();
}
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();;
}
});
alertDialog.show();
}
}
Activity file for viewholder
package com.example.attendanceappvqyfyp;
import android.view.ContextMenu;
import android.view.View;
import android.widget.TextView;
import com.example.attendanceappvqyfyp.Common.Common;
import androidx.recyclerview.widget.RecyclerView;
import com.example.attendanceappvqyfyp.Interface.itemClickListener;
public class LecturerClassViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnCreateContextMenuListener {
public TextView lclasstitle, ldate, ltime, lclassroom, lcourse;
private itemClickListener itemClickListener;
public LecturerClassViewHolder(View itemView) {
super(itemView);
lclasstitle = (TextView)itemView.findViewById(R.id.lclasstitle);
ldate = (TextView)itemView.findViewById(R.id.ldate);
ltime = (TextView)itemView.findViewById(R.id.ltime);
lclassroom = (TextView)itemView.findViewById(R.id.lclassroom);
lcourse = (TextView)itemView.findViewById(R.id.lcourse);
itemView.setOnCreateContextMenuListener(this);
itemView.setOnClickListener(this);
}
public void setItemClickListener(itemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
itemClickListener.onClick(view, getAdapterPosition(), false);
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select");
menu.add(0,0,getAdapterPosition(), Common.News);
}
}
Layout file for recyclerview
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".LecturerClassList">
<android.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:theme="#style/ThemeOverlay.AppCompat.Dark" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_lecturerclass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="#+id/toolbar" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout file for itemview
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="160dp"
app:cardElevation="4dp"
android:layout_marginBottom="8dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/lclasstitle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text=""
android:textColor="#android:color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/ldate"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignBottom="#id/lclasstitle"
android:layout_marginBottom="-40dp"
android:gravity="center"
android:text=""
android:textColor="#android:color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/ltime"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignBottom="#id/ldate"
android:layout_marginBottom="-40dp"
android:gravity="center"
android:text=""
android:textColor="#android:color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/lclassroom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text=""
android:textColor="#android:color/black"
android:textSize="20sp" />
<TextView
android:id="#+id/lcourse"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text=""
android:visibility="invisible"
android:textColor="#android:color/black"
android:textSize="20sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Thanks to ZeePee for giving this link Display Back Arrow on Toolbar, although the toolbar didnt work for me, i change it back to actionbar and use the code from the link and it work.
Try adding horizontal constraints for Toolbar and Recycler View. After that you need to specify where do you want your single item widgets in Relative layout

Android Studio : When ever i run my app, it says Unfortunately app has stopped [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 4 years ago.
I am trying to get an input from user and then covert it into double and divide it by 1000 and then set the value in a textview but i while running the app, the app closes and says Unfortunately the app has stopped.
Here's my MainActivity.java file :
package com.blogspot.techtutorialsajen.androiddevelopmentpractice;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
private Button btn1;
private EditText get;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
result = (TextView)findViewById(R.id.result);
get = (EditText)findViewById(R.id.get);
final double value = Double.parseDouble(get.getText().toString()) / 1000;
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
result.setText(String.valueOf(value));
}
});
}
}
And My activity_main.xml file :
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="#+id/get"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="#string/enter_a_number"
android:layout_marginTop="48dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn1"
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/result"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp" />
<TextView
android:id="#+id/result"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20pt"
android:text="0"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Well, Your app is crashes because of java.lang.RuntimeException:java.lang.NumberFormatException: Invalid double: ""
that means you are trying to convert empty string to Double.
So, Because the EditText is empty your app is crashes and I have re-write your code so that you can copy-paste.
Here is your MainActivity.Java
package com.blogspot.techtutorialsajen.androiddevelopmentpractice;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
private Button btn1;
private EditText get;
private TextView result;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
result = (TextView)findViewById(R.id.result);
get = (EditText)findViewById(R.id.get);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (get.getText().toString().isEmpty()) {
Toast.makeText(MainActivity .this, "Please input something", Toast.LENGTH_LONG).show();
} else {
final double value = Double.parseDouble(get.getText().toString()) / 1000;
result.setText(String.valueOf(value));
}
}
});
}
}
and here is your activity_main.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="#+id/get"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="#string/enter_a_number"
android:layout_marginTop="48dp"
android:inputType="number"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn1"
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/result"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp" />
<TextView
android:id="#+id/result"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20pt"
android:text="0"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I have added android:inputType="number" property to your EditText so that, now on, it will only take Number from the user.
From your xml file and activity code, it clearly says your edittext is empty while launching app.
And you are using this code in oncreate()
final double value = Double.parseDouble(get.getText().toString()) / 1000;
which throws java.lang.NumberFormatException: empty String
You can change your code like,
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!get.getText().toString().isEmpty)
final double value = Double.parseDouble(get.getText().toString()) / 1000;
result.setText(String.valueOf(value));
}
});
}

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. :)

Button click force closes the activity

My problem is when I click bSA button I encounter error and the activity closes.
java.lang.runtimeexception unable to start activity componentinfo ...
here is my code
Data.java :
package com.example.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Data extends Activity implements View.OnClickListener {
Button start, startFor;
EditText sendET;
TextView gotAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
initialize();
}
private void initialize() {
start = (Button) findViewById(R.id.bSA);
startFor = (Button) findViewById(R.id.bSAFR);
sendET = (EditText) findViewById(R.id.etSend);
gotAnswer = (TextView) findViewById(R.id.tvGot);
start.setOnClickListener(this);
startFor.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bSA:
String bread = sendET.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
startActivity(a);
break;
case R.id.bSAFR:
break;
}
}
}
get.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etSend" android:layout_gravity="center_horizontal"/>
<Button
android:layout_below="#+id/etSend"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity"
android:id="#+id/bSA"/>
<Button
android:layout_toLeftOf="#+id/bSA"
android:layout_alignTop="#+id/bSA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity for Results"
android:id="#+id/bSAFR"/>
<TextView
android:layout_below="#+id/bSAFR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:id="#+id/tvGot"/>
</RelativeLayout>
=============================================================
OpenedClass.java
package com.example.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
public class OpenedClass extends Activity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
TextView question, test;
Button returnData;
RadioGroup selectionList;
String gotBread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
initialize();
Bundle gotBasket = getIntent().getExtras();
gotBread = gotBasket.getString("key");
question.setText(gotBread);
}
private void initialize() {
question = (TextView) findViewById(R.id.tvQuestion);
test = (TextView) findViewById(R.id.tvText);
returnData = (Button) findViewById(R.id.bReturn);
returnData.setOnClickListener(this);
selectionList = (RadioGroup) findViewById(R.id.rgAnswers);
selectionList.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View view) {
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rCrazy:
break;
case R.id.rFun:
break;
case R.id.rBoth:
break;
}
}
}
send.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Hosein is ..."
android:id="#+id/tvQuestion"/>
<RadioGroup
android:id="#+id/rgAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crazy"
android:id="#+id/rCrazy"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Super Fun"
android:id="#+id/rFun"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Both"
android:id="#+id/rBoth"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Return"
android:id="#+id/bReturn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tvText"/>
</LinearLayout>
Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);
You cannot put a inside itself. This causes infinite recursion and stack overflow.
You probably wanted
a.putExtras(basket);

Categories