I have a really long onActivityCreated for one of my fragments because it has to initialize a big gridlayout. Instead of using xml, I did it programmatically because it is easier on my life to code than to create 108 separate views using xml. The only problem: it takes forever to load everything when the fragment is created.
I have tried to use AsyncTaskLoader and that didn't work because you can't change a view using a different thread. Please tell me anyway I can make it run faster or better. This is my first app, so there is a good chance I am just being really dumb. Any help is appreciated.
Also I am doing many things in this fragment, like using mediaplayers and using onclicklisteners. All that works perfectly, its just really slow to make the fragment load. This is my code:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
if (savedInstanceState == null) {
TextViewCompat.setAutoSizeTextTypeWithDefaults(textTranslation, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
GridLayout gridLayout = v.findViewById(R.id.grid_layout);
final MediaPlayer[] sound_resource = {MediaPlayer.create(v.getContext(), R.raw.a_sound),
MediaPlayer.create(v.getContext(), R.raw.b_sound), MediaPlayer.create(v.getContext(), R.raw.c_sound),
MediaPlayer.create(v.getContext(), R.raw.d_sound), MediaPlayer.create(v.getContext(), R.raw.e_sound),
MediaPlayer.create(v.getContext(), R.raw.f_sound), MediaPlayer.create(v.getContext(), R.raw.g_sound),
MediaPlayer.create(v.getContext(), R.raw.h_sound), MediaPlayer.create(v.getContext(), R.raw.i_sound),
MediaPlayer.create(v.getContext(), R.raw.j_sound), MediaPlayer.create(v.getContext(), R.raw.k_sound),
MediaPlayer.create(v.getContext(), R.raw.l_sound), MediaPlayer.create(v.getContext(), R.raw.m_sound),
MediaPlayer.create(v.getContext(), R.raw.n_sound), MediaPlayer.create(v.getContext(), R.raw.o_sound),
MediaPlayer.create(v.getContext(), R.raw.p_sound), MediaPlayer.create(v.getContext(), R.raw.q_sound),
MediaPlayer.create(v.getContext(), R.raw.r_sound), MediaPlayer.create(v.getContext(), R.raw.s_sound),
MediaPlayer.create(v.getContext(), R.raw.t_sound), MediaPlayer.create(v.getContext(), R.raw.u_sound),
MediaPlayer.create(v.getContext(), R.raw.v_sound), MediaPlayer.create(v.getContext(), R.raw.w_sound),
MediaPlayer.create(v.getContext(), R.raw.x_sound), MediaPlayer.create(v.getContext(), R.raw.y_sound),
MediaPlayer.create(v.getContext(), R.raw.z_sound), MediaPlayer.create(v.getContext(), R.raw.one_sound),
MediaPlayer.create(v.getContext(), R.raw.two_sound), MediaPlayer.create(v.getContext(), R.raw.three_sound),
MediaPlayer.create(v.getContext(), R.raw.four_sound), MediaPlayer.create(v.getContext(), R.raw.five_sound),
MediaPlayer.create(v.getContext(), R.raw.six_sound), MediaPlayer.create(v.getContext(), R.raw.seven_sound),
MediaPlayer.create(v.getContext(), R.raw.eight_sound), MediaPlayer.create(v.getContext(), R.raw.nine_sound),
MediaPlayer.create(v.getContext(), R.raw.zero_sound)};
String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
List<ImageButton> imageButtonList = new ArrayList<ImageButton>();
final IsPlaying isPlaying = new IsPlaying(null);
final int[] ind = {0};
for (int i = 0, c = 0, r = 0; i < 36 * 3; i++, c++) {
if (c == 3) {
c = 0;
r++;
ind[0] = r;
}
GridLayout.Spec row = GridLayout.spec(r, GridLayout.CENTER);
GridLayout.Spec col = GridLayout.spec(c, GridLayout.CENTER);
GridLayout.Spec col2 = GridLayout.spec(c, GridLayout.LEFT);
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(row, col);
if (c == 0) {
ImageButton imageButton = new ImageButton(v.getContext());
imageButton.setId(ind[0]);
imageButton.setBackgroundResource(R.drawable.ic_play_sound);
imageButtonList.add(imageButton);
imageButtonList.get(ind[0]).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer player = sound_resource[v.getId()];
if (!isPlaying.somethingIsPlaying) {//if nothing is playing
player.start();
isPlaying.mediaPlayer = player;
isPlaying.somethingIsPlaying = true;
} else if (player.isPlaying()) {//if something is playing and player is already playing
player.seekTo(0);
player.start();
} else {//if something is playing and player is not playing
isPlaying.mediaPlayer.seekTo(0);
isPlaying.mediaPlayer.pause();
player.start();
isPlaying.mediaPlayer = player;
isPlaying.somethingIsPlaying = true;
}
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
isPlaying.somethingIsPlaying = false;
mp.seekTo(0);
}
});
}
});
gridLayout.addView(imageButton, layoutParams);
} else if (c == 1) {
TextView textView = new TextView(v.getContext());
textView.setText(String.valueOf(abc.charAt(r)));
textView.setTextSize(27);
textView.setTextColor(v.getContext().getResources().getColor(R.color.main_text_color));
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
gridLayout.addView(textView, layoutParams);
} else if (c == 2) {
ImageView imageView = new ImageView(v.getContext());
imageView.setImageResource(images_resources[r]);
GridLayout.LayoutParams layoutParams1 = new GridLayout.LayoutParams(row, col2);
layoutParams1.leftMargin = 13;
gridLayout.addView(imageView, layoutParams1);
}
}
}
super.onActivityCreated(savedInstanceState);
}