I am trying to change the background image of my fragment after some seconds but I still have trouble with it! I used fragment because I used tab bar. the app will work correctly until 2.5s which is the time for changing background! and I am sure the problem is caused from the codes not application!
here are my codes :
public class LogInFragment extends android.support.v4.app.Fragment {
LinearLayout login_layout;
public LogInFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
login_layout = (LinearLayout) getActivity().findViewById(R.id.linear1);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
change();
}
}, 2500);
}
private void change()
{
Random random = new Random();
int image_change = random.nextInt(5 - 1) + 1;
switch (image_change)
{
case 1:
login_layout.setBackgroundResource(R.drawable.blue1);
break;
case 2:
login_layout.setBackgroundResource(R.drawable.yellow1);
break;
case 3:
login_layout.setBackgroundResource(R.drawable.red1);
break;
default:
login_layout.setBackgroundResource(R.drawable.green1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_log_in, container, false);
}
}
I would be happy if anyone can help me please!