How to destroy and recreate objects in Android during runtime? - java

I'm developing an Android app that has to simulate a sort of Pokédex.
For now, what I want to do is simply have all 151 Pokémon printed on my device, so I can scroll them up and down.
The problem is that when I try this thing with such as 9 or 12 images there are no problems, but when I load all the 151 images (all .png), Android kills the app because it's draining too much system resources.
I've heard that there are Java methods that can (don't know how) "destroy" an object when it goes out of the display and then recreate it when it returns in the screen. Anyway if you have different suggestions on how to resolve my problem, every idea is welcome!
Here is my MainActivity:
package com.example.thefe.newsmartkedex;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
};
}
And here is my ImageAdapter class I use for Gridview:
package com.example.thefe.newsmartkedex;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.pkmn1, R.drawable.pkmn2,
R.drawable.pkmn3, R.drawable.pkmn4,
R.drawable.pkmn5, R.drawable.pkmn6,
R.drawable.pkmn7, R.drawable.pkmn8,
R.drawable.pkmn9, R.drawable.pkmn10,
R.drawable.pkmn11, R.drawable.pkmn12,
R.drawable.pkmn13, R.drawable.pkmn14,
R.drawable.pkmn15, R.drawable.pkmn16,
R.drawable.pkmn17, R.drawable.pkmn18,
R.drawable.pkmn19, R.drawable.pkmn20,
R.drawable.pkmn21, R.drawable.pkmn22,
R.drawable.pkmn23, R.drawable.pkmn24,
R.drawable.pkmn25, R.drawable.pkmn26,
R.drawable.pkmn27, R.drawable.pkmn28,
R.drawable.pkmn29, R.drawable.pkmn30,
R.drawable.pkmn31, R.drawable.pkmn32,
R.drawable.pkmn33, R.drawable.pkmn34,
R.drawable.pkmn35, R.drawable.pkmn36,
R.drawable.pkmn37, R.drawable.pkmn38,
R.drawable.pkmn39, R.drawable.pkmn40,
R.drawable.pkmn41, R.drawable.pkmn42,
R.drawable.pkmn43, R.drawable.pkmn44,
R.drawable.pkmn45, R.drawable.pkmn46,
R.drawable.pkmn47, R.drawable.pkmn48,
R.drawable.pkmn49, R.drawable.pkmn50,
R.drawable.pkmn51, R.drawable.pkmn52,
R.drawable.pkmn53, R.drawable.pkmn54,
R.drawable.pkmn55, R.drawable.pkmn56,
R.drawable.pkmn57, R.drawable.pkmn58,
R.drawable.pkmn59, R.drawable.pkmn60,
R.drawable.pkmn61, R.drawable.pkmn62,
R.drawable.pkmn63, R.drawable.pkmn64,
R.drawable.pkmn65, R.drawable.pkmn66,
R.drawable.pkmn67, R.drawable.pkmn68,
R.drawable.pkmn69, R.drawable.pkmn70,
R.drawable.pkmn71, R.drawable.pkmn72,
R.drawable.pkmn73, R.drawable.pkmn74,
R.drawable.pkmn75, R.drawable.pkmn76,
R.drawable.pkmn77, R.drawable.pkmn78,
R.drawable.pkmn79, R.drawable.pkmn80,
R.drawable.pkmn81, R.drawable.pkmn82,
R.drawable.pkmn83, R.drawable.pkmn84,
R.drawable.pkmn85, R.drawable.pkmn86,
R.drawable.pkmn87, R.drawable.pkmn88,
R.drawable.pkmn89, R.drawable.pkmn90,
R.drawable.pkmn91, R.drawable.pkmn92,
R.drawable.pkmn93, R.drawable.pkmn94,
R.drawable.pkmn95, R.drawable.pkmn96,
R.drawable.pkmn97, R.drawable.pkmn98,
R.drawable.pkmn99, R.drawable.pkmn100,
R.drawable.pkmn101, R.drawable.pkmn102,
R.drawable.pkmn103, R.drawable.pkmn104,
R.drawable.pkmn105, R.drawable.pkmn106,
R.drawable.pkmn107, R.drawable.pkmn108,
R.drawable.pkmn109, R.drawable.pkmn110,
R.drawable.pkmn111, R.drawable.pkmn112,
R.drawable.pkmn113, R.drawable.pkmn114,
R.drawable.pkmn115, R.drawable.pkmn116,
R.drawable.pkmn117, R.drawable.pkmn118,
R.drawable.pkmn119, R.drawable.pkmn120,
R.drawable.pkmn121, R.drawable.pkmn122,
R.drawable.pkmn123, R.drawable.pkmn124,
R.drawable.pkmn125, R.drawable.pkmn126,
R.drawable.pkmn127, R.drawable.pkmn128,
R.drawable.pkmn129, R.drawable.pkmn130,
R.drawable.pkmn131, R.drawable.pkmn132,
R.drawable.pkmn133, R.drawable.pkmn134,
R.drawable.pkmn135, R.drawable.pkmn136,
R.drawable.pkmn137, R.drawable.pkmn138,
R.drawable.pkmn139, R.drawable.pkmn140,
R.drawable.pkmn141, R.drawable.pkmn142,
R.drawable.pkmn143, R.drawable.pkmn144,
R.drawable.pkmn145, R.drawable.pkmn146,
R.drawable.pkmn147, R.drawable.pkmn148,
R.drawable.pkmn149, R.drawable.pkmn150,
R.drawable.pkmn151
};
}
Finally, this is the XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.thefe.newsmartkedex.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
Thanks for help!

First of all, I don't see any violation in your code so let's focus the pictures.
The problem is that when I try this thing with such as 9 or 12 images there are no problems, but when I load all the 151 images (all .png), Android kills the app because it's draining too much system resources.
What do you mean by 'load all the 151 images'? At the same time in the same screen? Or you just make quite a small numbers of them(like 9,12,16 etc.) seen in the view and others out of the screen?
I've heard that there are Java methods that can (don't know how) "destroy" an object when it goes out of the display and then recreate it when it returns in the screen. Anyway if you have different suggestions on how to resolve my problem, every idea is welcome!
You can't simply destroy an object by yourself and the Jvm will handle this for you when your objects are invalid or unused. As to this problem the recreation of objects that you implements in 'getView' seems no harm.
My question is: how many images did you show in one whole screen? And what size of them?
If you can provide your demo here, it will be the best to work on.

Display limited images that can fit your screen and load the other images when scrolling Gridview.
That way system wont have do to do lots of work at once. Your application gets faster also.
You can refer this link:
i want grid view with loading by scroll i have image fetch from sever but i want only 10 images view other can load when scrolling grid view

Related

How to keep scrolling position in ScrollView after rotating the screen in fragment with LiveDate object loaded

I have a fragment which is to display a lyrics of a song which is loaded from room databse based on its given id.
I'd like to preserve scrolling position after rotating the screen. Now after rotating the song is loaded again from db and the view is on the very top regardless of the scrolling position befor the rotating.
I thought that I can save scrolling position in onSaveInstanceState some bundle in onCreateView() use command on mSongDisplayScrollView.scrollTo(x, y)
The fragment code:
public class SongDisplayFragment extends Fragment {
private Song mSongToDisplay;
private ScrollView mSongDisplayScrollView;
private TextView mSongTitleTextView;
private RecyclerView mSongLyricsRecyclerView;
private GuitarSongbookViewModel mGuitarSongbookViewModel;
public static final String SONG_ID_KEY = "SONG_ID_KEY";
public SongDisplayFragment() {
}
public static SongDisplayFragment newInstance(Long songId) {
SongDisplayFragment songDisplayFragment = new SongDisplayFragment();
Bundle arguments = new Bundle();
arguments.putLong(SONG_ID_KEY,
songDisplayFragment.setArguments(arguments);
return songDisplayFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_song_display, container, false);
mSongDisplayScrollView = view.findViewById(R.id.song_display_scroll_view);
mSongLyricsRecyclerView = view.findViewById(R.id.lyrics_rv_);
mSongTitleTextView = view.findViewById(R.id.
mGuitarSongbookViewModel = ViewModelProviders.of(this).get(GuitarSongbookViewModel.class);
final SongDisplayAdapter songDisplayAdapter = new SongDisplayAdapter(getContext());
Long songId = null;
if (getArguments().containsKey(SONG_ID_KEY)) {
songId = getArguments().getLong(SONG_ID_KEY);
}
if (songId != null) {
final Long finalSongId = songId;
mGuitarSongbookViewModel.getSongById(songId).observe(this, new Observer<Song>() {
#Override
public void onChanged(#Nullable final Song song) {
mSongToDisplay = song;
mSongTitleTextView.setText(mSongToDisplay.getMTitle());
songDisplayAdapter.setSong(song);
}
});
}
mSongLyricsRecyclerView.setAdapter(songDisplayAdapter);
mSongLyricsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
}
The fragment 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.SongDisplayFragment">
<ScrollView
android:id="#+id/song_display_scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/autoscroll_bar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:layout_editor_absoluteX="138dp">
<LinearLayout
android:id="#+id/son_display_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/displayed_song_title_txt_"
style="#style/TitleOfDisplayedSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="#string/title_placeholder" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/lyrics_rv_"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="12dp"
android:nestedScrollingEnabled="false" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
The fragment displays a tiltle and lyrics of a song in a textView and RecyclerView which adapter class code is:
package com.example.guitarsongbook.adapters;
import android.content.Context;
import android.graphics.Rect;
import android.text.Html;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.guitarsongbook.R;
import com.example.guitarsongbook.daos.SongChordJoinDao;
import com.example.guitarsongbook.model.Chord;
import com.example.guitarsongbook.model.Song;
import java.util.ArrayList;
import java.util.List;
public class SongDisplayAdapter extends RecyclerView.Adapter<SongDisplayAdapter.SongViewHolder> {
private Context context;
private final LayoutInflater mInflater;
private Song mSong;
private ArrayList<String> mLyrics;
public SongDisplayAdapter(Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
}
#NonNull
#Override
public SongViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.song_lyrics_rv_item, parent, false);
return new SongDisplayAdapter.SongViewHolder(itemView);
}
public void setSong(Song song){
mSong = song;
mLyrics = mSong.getMLyrics();
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(#NonNull SongViewHolder holder, int position) {
holder.bindTo(position);
}
#Override
public int getItemCount() {
if (mLyrics != null)
return mLyrics.size();
else return 0;
}
public class SongViewHolder extends RecyclerView.ViewHolder {
private final TextView mLyricsLineTextView;
public SongViewHolder(#NonNull View itemView) {
super(itemView);
mLyricsLineTextView = itemView.findViewById(R.id.song_lyric_line_txt_);
}
public void bindTo(int position) {
if (mSong != null) {
mLyricsLineTextView.setText(Html.fromHtml(mLyrics.get(position)));
} else {
mLyricsLineTextView.setText(context.getString(R.string.no_song_label));
}
}
}
}
RecyclerView item 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="wrap_content">
<TextView
android:id="#+id/song_lyric_line_txt_"
style="#style/LyricOfDisplayedSong"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="24dp"
app:layout_constraintEnd_toStartOf="#+id/song_chord_line_txt_"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="placeholder text" />
</androidx.constraintlayout.widget.ConstraintLayout>
I think that the problem is that after rotating the screen song has to be loaded again by observer's methon onChange() so there is a short moment that there is nothing to display for scrollView. I'd like to find some solution how to return to the old position after again loading the song.
The first step is to persist the values of the objects through your Fragment's onSaveInstanceState method.
More of my explanations would come from the comments within each code block, so you can understand better on how it all works.
Values to be persisted: mSongToDisplay and mScrollViewPosition
//new variable introduced
int mScrollViewPosition = 0;
#Override
protected void onSaveInstanceState(Bundle outState) {
//If screen orientation changes, android redraws all views
//and non persistent data would be lost.
//Nevertheless, before this happens android informs the app via this method
//So to prevent state loss we save the data to a temporary storage
outState.putParcelable("song_data", mSongToDisplay);
//save ScrollView current position
outState.putInt("position_data", mSongDisplayScrollView.getScrollY());
//call super to commit your changes
super.onSaveInstanceState(outState);
}
Next is to recover the data after orientation change is completed
Add the following block in your fragments onCreateView and make sure the if statement block comes after initializing your adapter
...
//...add adapter initialization block here before checking for saved data
//Check for saved data
if (savedInstanceState != null) {
// Retrieve the data you saved
mSongToDisplay = savedInstanceState.getParcelable("song_data");
mScrollViewPosition = savedInstanceState.getInt("position_data");
//Re-initialize and reload adapter record
mSongToDisplay = song;
mSongTitleTextView.setText(mSongToDisplay.getMTitle());
songDisplayAdapter.setSong(song);
}
else {
//No data to retrieve
//initialize data model here.
}
//Assign the adapter to recyclerView
mSongLyricsRecyclerView.setAdapter(songDisplayAdapter);
mSongLyricsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
try{
//Finally we set the scrollview position at this point just to make sure
//recyclerview has its data loaded and full length of scrollview is restored
mSongDisplayScrollView.setScrollY(mScrollViewPosition);}
catch(Exception ex){
//position isn't right
}
return view;
At this point the required data has been persisted and on every screenOrientationChange the app would load the previously saved song object into the adapter preventing a false call to the db or api, and would also scroll to the expected position using the saved integer value mScrollViewPosition.

Adding tasks to RecyclerView

I'm new to Android and have been learning about Android app development for about a month just through a book, and right now, I'm working on my own project, which is a basic app that manages user tasks and schedule. There is this one problem I am having. I am trying to add a new task to a RecyclerView list that contains the list of main tasks, but I keep getting errors. Here is what I have (I don't want to show everything because it is a project):
The RecyclerView list (main_task_window.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/MainTaskList"
android:layout_width="match_parent"
android:layout_height="449dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="61dp" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/AddaMainTask"
android:onClick="addnewtask"
android:layout_width="54dp"
android:layout_height="51dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="13dp"
android:layout_marginBottom="10dp"
android:clickable="true"
app:srcCompat="#android:drawable/ic_input_add" />
</RelativeLayout>
The window that allows users to add new main tasks as well as additional notes (add_new_main_task_window.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">
<EditText
android:id="#+id/maintasks"
android:layout_width="match_parent"
android:layout_height="62dp"
android:hint="Main Task"/>
<EditText
android:id="#+id/notes"
android:layout_width="match_parent"
android:layout_height="62dp"
android:hint="Additional notes"/>
<Button
android:id="#+id/addButton"
android:onClick="sendButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to main task" />
</LinearLayout>
Now here are the following Java files:
1. MainTasks.java (contains the get and set methods for the main task and additional notes)
package com.ricstudios.daymanager;
public class MainTasks
{
private String mt, an; //'mt' stands for "main tasks". 'an' stands for "additional notes"
//MainTasks class constructor
public MainTasks(String mt, String an)
{
//mt and an in the parameters is equal to the mt and an variables above
this.mt = mt;
this.an = an;
}
public String getMainTasks() //this get method obtains the main task string input)
{
return mt;
}
public void setMainTasks(String MainTasks) //this set methods stores the main task string input)
{
this.mt = MainTasks;
}
public String getAdditionalNotes() //this get method obtains the main task string input
{
return an;
}
public void setAdditionalNotes(String AddNotes) //this set method stores the main task string
{
this.an = AddNotes;
}
}
MainTaskAdapter.java (contains the adapter to render the data)
package com.ricstudios.daymanager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import java.util.List;
public class MainTaskAdapter extends RecyclerView.Adapter<MainTaskAdapter.ViewHolder>
{
private List<MainTasks> maintasklist; //will contain the main task and additional notes string inputs
public class ViewHolder extends RecyclerView.ViewHolder
{
public EditText maintask, addnotes;
//provides a reference to the views for each data item
public ViewHolder(View view)
{
super(view);
maintask = (EditText)view.findViewById(R.id.maintasks);
addnotes = (EditText)view.findViewById(R.id.notes);
}
}
//MainTaskAdapter class constructor
public MainTaskAdapter(List<MainTasks> maintasklist)
{
this.maintasklist = maintasklist;
}
//create new view (invoked by the layout manager)
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_new_main_task_window, parent, false);
return new ViewHolder(itemView);
}
//replaces the contents of the main task view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position)
{
MainTasks obj = maintasklist.get(position); //MainTasks class obj called 'obj', which allows access to the MainTasks class
//obtains the main task and additional notes from the MainTask class
holder.maintask.setText(obj.getMainTasks());
holder.addnotes.setText(obj.getAdditionalNotes());
}
//returns the size of the main task list (invoked by the layout manager)
#Override
public int getItemCount()
{
return maintasklist.size();
}
}
AddNewMainTask.java (adds the tasks and additional notes to the RecyclerView)
package com.ricstudios.daymanager;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/*This class handles the window for adding new main tasks to the main task list (this window is the add_new_main_task_window.xml layout)*/
public class AddNewMainTask extends AppCompatActivity
{
private Button addbutton; //Button element from the add_new_main_task_window.xml layout
private EditText maintask, addnotes; //EditText elements from the add_new_main_task_window.xml layout
private List<MainTasks> maintasklist = new ArrayList<>();
private RecyclerView ListofMainTasks; //RecyclerView list element from the add_new_main_task_window.xml layout (containing the Main Task list)
private MainTaskAdapter TAdapter; //MainTaskAdapter class object 'TAdapter', allows access to the MainTaskAdapter class
/*sendButton method from the Button element in the add_new_main_task_window.xml and adds the main task and additional notes to the RecyclerView main task list*/
public void sendButton(View view)
{
maintask = (EditText)view.findViewById(R.id.maintasks);
addnotes = (EditText)view.findViewById(R.id.notes);
ListofMainTasks = (RecyclerView)findViewById(R.id.MainTaskList);
TAdapter = new MainTaskAdapter(maintasklist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
ListofMainTasks.setLayoutManager(mLayoutManager);
ListofMainTasks.setItemAnimator(new DefaultItemAnimator());
ListofMainTasks.setAdapter(TAdapter);
/*Passes the EditText element values into the MainTask.java parameters*/
MainTasks mnt = new MainTasks(maintask.getText().toString(), addnotes.getText().toString());
maintasklist.add(mnt);
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_new_main_task_window); //displays the window that allows users to add new main tasks (which is the add_new_main_task_window.xml layout)
addbutton = (Button)findViewById(R.id.addButton);
/*When user clicks button, the values the user puts in the EditText fields will be added to the RecyclerView list of the main_task_window.xml layout*/
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
sendButton(v);
}
});
}
}
Whenever I press the "Add to Main Task" button, the app suddenly stop. Please help. Oh, and about the floatingactionbutton in the main_task_window.xml, don't worry about that. That just sends the user to the add_new_main_task_window.xml
Update: 3/5/19
I'm getting this error from the logcat:
03-05 15:22:49.298 10335-10335/com.project.daymanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.daymanager, PID: 10335
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.project.daymanager.AddNewMainTask.sendButton(AddNewMainTask.java:39)
at com.project.daymanager.AddNewMainTask$1.onClick(AddNewMainTask.java:63)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10887)
at android.view.View$PerformClick.run(View.java:22541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Replace
view.findViewById();
with
findViewById();
In the MainTasks.java Don't use a constructor
public MainTasks(String mt, String an)
Just keep the set And get methods and do
MainTasks mt = new MainTasks();
mt.setMainTasks("Title");
mt.setAdditionalNotes("Note");
list.add(mt);
If it still not working, please post the error log so we can know whats the error.
Update: 3/5/19
You are trying to access the recyclerView of the MainTaskActivity in
ListofMainTasks = (RecyclerView)findViewById(R.id.MainTaskList);
from out side the activity (From another Activity) and that's not possible this way,
you can set the RecyclerView in the MainTaskActivity to static and use it in AddNewMainTask Activity by calling it
MainTaskActivity.myRecyclerView.
Problem is in this line:
ListofMainTasks = (RecyclerView)findViewById(R.id.MainTaskList);
program is unable to find recycler view and its null. and you can't set layout manager to a null object. I've reviewed your code and thing you did wrong is that in you onCreate method you are setting layout setContentView(R.layout.add_new_main_task_window); and you're trying to find recylerview which is not in this layout.
Your recycler view is in main_task_window.xml. use setContentView(R.layout.main_task_window.xml); and your activity will find recyclerview easily.
Note: if you want button/controls also which are in this layout add_new_main_task_window.xml then please move them in layout with recyclerview.

Programmatically setting TextView to RelativeLayout.CENTER_OF_PARENT

I am just learning Java and XML and am trying to set a TextView to be in the center of its parent RelativeLayout. My App only loads when I comment out the last 3 lines before the setContentView(homeScreen)
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
</RelativeLayout>
Here is my Java:
package com.example.android.testerapp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView homeScreen = new TextView(this);
homeScreen.setText("Welcome to Test App 001" + "\nThis TextView was created dynamically in Java!");
homeScreen.setTextSize(24);
homeScreen.setTextColor(Color.CYAN);
homeScreen.setCursorVisible(true);
homeScreen.setPadding(16,56,16,56);
homeScreen.setBackgroundColor(Color.BLACK);
homeScreen.setGravity(Gravity.CENTER);
//dynamically set width to dp (converted to pixels ~600) and height to 'wrap content'
// convert dp amount to pixels for size
final float scale = getResources().getDisplayMetrics().density;
int pixelWidth = (int) (2000 / scale + 0.5f);
homeScreen.setLayoutParams(new ViewGroup.LayoutParams(pixelWidth , ViewGroup.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)homeScreen.getLayoutParams();
params.addRule(RelativeLayout.CENTER_IN_PARENT);
homeScreen.setLayoutParams(params);
setContentView(homeScreen);
}
}
I have seen this sort of post about 10 times now and they all have the same solution which I can't seem to implement correctly, it may be another part of my code? Possibly where I set the width and height usingsetLayoutParamsalso?
The setContentView() call is supposed to be used to set the layout of the full screen. What you're doing currently in your Activity code is setting just a TextView as the full view of the screen, so the Activity has no reference to the XML layout that you created. This is why your 3 lines of code at the end fail, because the TextView is trying to setup its LayoutParams for how its parent should place and measure it, however it has no parent in this context. What I would recommend doing is giving an id attribute to the RelativeLayout in the XML to get a reference to it in Activity code like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="home_screen_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
Then in your Activity code, adjust it so that you call with the resource id of your XML file. If we assume it's called act_main.xml in the layout folder of your resources directory (i.e. in src/main/resources/layout/act_main.xml), you would call setContentView(R.layout.act_main) as the first line in onCreate() after the super() call so that the framework has an opportunity to parse your XML and inflate it (i.e. instantiate, make calculations on the size and
determine placement of its components among other things). After that, use findViewById(R.id.home_screen_layout) to get a reference to that RelativeLayout so that you may create a new TextView and add it to your already inflated layout.
package com.example.android.testerapp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// make your view components private members as findViewById calls are expensive for the framework
private RelativeLayout homeScreenLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Have the activity inflate the XML file with your RelativeLayout
setContentView(R.layout.act_main);
// Now that it is inflated, get a reference to that parent
homeScreenLayout = (RelativeLayout) findViewById(R.id.home_screen_layout);
// Dynamically create a TextView associated with this Activity's context
TextView homeScreen = new TextView(this);
homeScreen.setText("Welcome to Test App 001" + "\nThis TextView was created dynamically in Java!");
homeScreen.setTextSize(24);
homeScreen.setTextColor(Color.CYAN);
homeScreen.setCursorVisible(true);
homeScreen.setPadding(16,56,16,56);
homeScreen.setBackgroundColor(Color.BLACK);
homeScreen.setGravity(Gravity.CENTER);
//dynamically set width to dp (converted to pixels ~600) and height to 'wrap content'
// convert dp amount to pixels for size
final float scale = getResources().getDisplayMetrics().density;
int pixelWidth = (int) (2000 / scale + 0.5f);
// Adjust the placement in the parent
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(pixelWidth , RelativeLayout.LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); // make sure to use the function which takes a boolean value for rules like CENTER_IN_PARENT
homeScreen.setLayoutParams(params); // Add these parameters to the textview
// Let the layout know about your newly created textview so that it can re-draw its canvas
homeScreenLayout.addView(homeScreen);
}
}
As a note, I will add that a of what you're doing can be done in the XML with relative ease, but since you asked about setting it programmatically specifically, I won't go into detail on that aspect. But if you're interested in some structured resources, I would recommend checking out the Android Developer Guide, specifically the section on XML layouts and how they interact with Activities
EDIT: Note the changes I made to the code for the Activity. The major pieces are first inflating the empty RelativeLayout xml with setContentView(int id), and then adding the other TextView to the given layout. There was a minor error in the code I presented concerning the CENTER_IN_PARENT line. According to the [docs](https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int, int)), you must use the addRule(int, int) version of the function when adding rules that use a boolean value.
You can set width and height on the constructor and then use it
Relative.LayoutParams(int width, int height)
so you need to do like:
homeScreen.setLayoutParams(width , height);

How to show query result raws as a table in an activity?

I am querying some raws from the database in Android studio. When I have the result table as a cursor, I want to show that cursor in an activity where columns are written at the top and rows are seen one under the other like a normal table. So, this activity's column names will change according to the result query's columns.
Any idea how to implement this, or is there a template I can use? I am new to Android so it might be an easy question for some of you, sorry for that.
So I recently have as well the same question. Then I found a really good tutorial with custom list items.
First, make sure that you save your records from the database into an object.
So first you have to create a row view. So create a simple XML file insert for example the following code and save it as myobject_list_item in your res/layout folder.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="#+id/column1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/column2"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="Column1"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
<TextView
android:id="#+id/column2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/gewichtung"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:text="Column2"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
</RelativeLayout>
After that you have to create a custom list adapter. So create a new Java file with the name MyObject_ListAdapter and insert the following code:
package net.example.app;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import net.example.app.R;
import java.util.ArrayList;
public class MyObject_ListAdapter extends ArrayAdapter<MyObject> {
// Source:
// http://hmkcode.com/android-custom-listview-items-row/
private ArrayList<MyObject> objects;
private Context context;
public MyObject_ListAdapter(Context context, ArrayList<MyObject> objects) {
super(context, R.layout.myobject_list_item, objects);
this.objects = objects;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.fach_list_item, parent, false);
// 3. Get the two text view from the rowView
TextView column1 = (TextView) rowView.findViewById(R.id.column1);
TextView column2 = (TextView) rowView.findViewById(R.id.column2);
RelativeLayout item = (RelativeLayout) rowView.findViewById(R.id.item);
// 4. Set the text for textView
column1.setText(objects.get(position).getName());
column2.setText(objects.get(position).getSecondName());
// 5. return rowView
return rowView;
}
}
Add in your activity now an simple ListView and add for this an id like lv.
Then in your Java Activity you can insert the following code:
package net.example.app;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import net.example.app.MyObject;
import net.example.app.MyObject_ListAdapter;
import java.util.ArrayList;
public class MyActivity extends AppCompatActivity {
private ArrayAdapter arrayAdapter;
private ArrayList<MyObject> myObjects = new ArrayList<>();
private SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
db = openOrCreateDatabase("database.db", MODE_PRIVATE, null);
ListView lv = (ListView) findViewById(R.id.lv);
arrayAdapter = new MyObject_ListAdapter(this, myObjects); //Define the custom list adapter with the activity and arraylist
lv.setAdapter(arrayAdapter); //Connect your listview with the adapter
displayData();
}
private void displayData() {
Cursor c = db.rawQuery("SELECT * FROM my_table", null);
while (c.moveToNext()) { //Loop through all the records
//Now on the variable 'c' there is one record.
int column_a_name = c.getColumnIndex("my_column1"); //Get the index of the column from your table.
String column_a_value = c.getString(column_a_name); //Get the value from the column from the current record.
int column_b_name = c.getColumnIndex("my_column2");
String column_b_value = c.getString(column_b_name);
//Now you can do with the value what you want.
myObjects.add(new MyObject(column_a_value, column_b_value));
}
arrayAdapter.notifyDataSetChanged(); //Notify, that you have changed some data in the array list.
}
}
I hope this tutorial may help you.
When you get a Cursor from a SQL execution, then you can use the following script:
Cursor c = db.rawQuery("SELECT * FROM my_table", null);
while (c.moveToNext()) { //Loop through all the records
//Now on the variable 'c' there is one record.
int column_a_name = c.getColumnIndex("my_column"); //Get the index of the column from your table.
String column_a_value = c.getString(column_a_name); //Get the value from the column from the current record.
//Now you can do with the value what you want.
System.out.println(column_a_value);
}
I hope this might be helpful for you.
Android has different layouts that could be of use for what you state.
You can use TableLayout of GridLayout. At this post you can find some discussion about which one choose: Grid Layout Vs. Table Layout
From Android docs, you can check some examples like: https://developer.android.com/guide/topics/ui/layout/grid.html
and https://developer.android.com/guide/topics/ui/layout/gridview.html
Some additional recommendation:
Use AsyncTask for interact with the database, don't do that from the UI thread.
I choose callback methods so, when the data access is completed, you inform the calling activity who is in charge of showing the data on the screen (using the layout of your choice).
Do not create big long classes doing everything. use one class for the activity, another for the adapter, another for the async task.
Hope this helps you.

ViewPager app not working (Eclipse)

I need your help on this. I am going to make an app using ViewPager and I since I never been programming for android before I thought it would be good to make a sample app first. I want to use the ViewPager a little bit differently than the classic list-of-items-style so I made an app that will show all the colors (or every 10th color) from #000000 to #FFFFFF.
It doesn't work. I've started the app on the emulator but I just get a white screen. If the default position of the ViewPager when starting is 0 then the color should be black. And when I try to make a breakpoint the program doesn't stop, or it's never reaching the point. I'm using eclipse.
These are the files of the project
MainActivity.java
package com.example.colorswipe;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
private ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager=(ViewPager)this.findViewById(R.id.pager);
mPager.setAdapter(new MyAdapter());
}
private class MyAdapter extends PagerAdapter {
#Override
public int getCount() {
return 0xFFFFFF/10;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ColorView view=new ColorView(container.getContext());
view.setBackgroundColor(android.graphics.Color.parseColor(String.format("#%06X", position*10)));
view.setText(position);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
ColorView.java
package com.example.colorswipe;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ColorView extends LinearLayout {
private TextView tv;
public ColorView(Context context) {
super(context);
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
TextView tv=new TextView(context);
this.tv=tv;
this.addView(tv);
}
public void setText(int position) {
tv.setText(Integer.toString(position).toCharArray(), 0, Integer.toString(position).length());
}
}
EDIT - the view pager is there , but I think you don't resolve the color correctly.
I found the problem. isViewFromObject must be implemented as return arg0==arg1.

Categories