Android flash image - imageview

I try to flash an image view for 3 seconds and then return it to its original image. here is my code:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imageView).setOnClickListener(this);
}
#Override
public void onClick(View v) {
ImageView pic = findViewById(R.id.imageView);
pic.setImageDrawable(getResources().getDrawable(R.drawable.devil));
//Wait Block
try
{
Thread.sleep(3000);
} catch (InterruptedException e){
Toast.makeText(this,"Problem occurred",Toast.LENGTH_LONG).show();
}
//End of wait block
pic.setImageResource(R.drawable.angel);
Toast.makeText(this,"Done",Toast.LENGTH_LONG).show();
}
So basically I want this code flow:
While the actual flow is the following :
Could you tell me the reason for this behavior as well as how to solve this?

Related

Display AlertDialog extending DialogFragment or from a method in onCreateView()

Hell o,
I would like to know if it's preferrable to inflate a fragment extending DialogFragment class or to display the dialog from a simple method added in onCreateView() ?
Here's a snippet for the second solution from internet (based on an activity and not fragment) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positiveDialog = findViewById(R.id.btnPositiveDialog);
negativeDialog = findViewById(R.id.btnNegativeDialog);
positiveDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_postive_layout);
}
});
negativeDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAlertDialog(R.layout.dialog_negative_layout);
}
});
}
private void showAlertDialog(int layout){
dialogBuilder = new AlertDialog.Builder(MainActivity.this);
View layoutView = getLayoutInflater().inflate(layout, null);
Button dialogButton = layoutView.findViewById(R.id.btnDialog);
dialogBuilder.setView(layoutView);
alertDialog = dialogBuilder.create();
alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
}
My only concern is that i use a fragment among navigationDrawer where i want to display my alertDialog (1 editText and 2 buttons), and i would like to prevent user from closing the dialog via backbutton or clicking outside the dialog.
Thank you very much for helping me out

Send data from Main Activity to Fragment not through intent or bundle but through interface in android?

My question is simple how to send data from Main activity to fragment through interface but not through intent or bundle in android.
Please guide me thanks.
Here is my code from fragment to Activity data send through interface but I want against it Activity to fragment which I don't know how to do.
public interface OnRestaurantSelectedListener {
public void onRestaurantSelected(String s);
}
MainActivity extends AppCompatActivity implements OnRestaurantSelectedListener{
String mPosition;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onRestaurantSelected(String s) {
mPosition =s;
}
}
In fragment:
public class ListFragment extends Fragment {
...
private OnRestaurantSelectedListener mOnRestaurantSelectedListener;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mOnRestaurantSelectedListener = (OnRestaurantSelectedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + e.getMessage());
}
}
mOnRestaurantSelectedListener.onRestaurantSelected("AliResturant");
}
Create interface FragmentCommunicator as,
public interface FragmentCommunicator{
public void passDataToFragment(String someValue);
}
In your main Activity we can use interface and pass data to fragment. Like below,
public class MainActivity extends FragmentActivity{
//interface through which communication is made to fragment
public FragmentCommunicator fragmentCommunicator;
private Button fragmentButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title_viewpager);
fragmentButton = (Button)findViewById(R.id.fragmentButton);
setUIListeners();
}
private void setUIListeners(){
fragmentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(fragmentCommunicator != null)
fragmentCommunicator.passDataToFragment("Hi from FragmentActivity");
}
});
}
}
Access passed data in fragment like below,
public class CustomFragment extends Fragments implements FragmentCommunicator{
public Context context;
private Button activityButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.activity_frag, container, false);
activityButton =(Button)fragmentView.findViewById(R.id.activityButton);
textView =(TextView)fragmentView.findViewById(R.id.textView);
setRetainInstance(true);
return fragmentView;
}
//FragmentCommunicator interface implementation
#Override
public void passDataToFragment(String someValue){
String activityAssignedValue = someValue;
textView.setText(activityAssignedValue);
}
}

Android Code for Refresh Button that destroys and recreates Text MediaPlayer (Hangs)!

viewPager4 Fragment Activities
If I click on the Texts that play short sounds, one after another then after sometimes the mediaplayer hangs and doesn't play any sound. But if I'm able to destroy activity and recreate the same activity with refresh button in Action Bar, I'd be able to click sounds again.
So what to write in the code for R.id.item2?
Or there is any other way that continuous clicking on short sounds by these texts is possible without any hang kind of problem?
Following is the reference code:
public class module1 extends FragmentActivity {
static Context con;
static int length = 0;
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
static MediaPlayer mediaplayer, mediaplayert, m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con = this;
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mains, menu);
// Just .main into .mains [created new for different behavior of Action Bar]
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.item1) {
Intent in = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(in);
}
if (item.getItemId() == R.id.item2) {
//what should i write here? to destroy and recreate the same fragment activity again.
//Problem: After clicking fast on one after another text links, mediaplayert hangs and doesnt play
//Solution: exit app destroy and reopen, then mediaplayer works fine...
//SO, what to write here? kindly help!
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
Fragment ff = new DummySectionFragment1();
switch (arg0) {
case 0:
ff = new DummySectionFragment1();
break;
}
Bundle args = new Bundle();
args.putInt(DummySectionFragment1.ARG_SECTION_NUMBER, arg0 + 1);
ff.setArguments(args);
return ff;
}
#Override
public int getCount() {
return 1;
}
#Override
public CharSequence getPageTitle(int arg0) {
Locale l = Locale.getDefault();
switch (arg0) {
case 0:
return getString(R.string.title_section27).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment1 extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.m01set01, container, false);
// Genius Shot by Stupid vIC//
TextView Text = (TextView) rootView.findViewById(R.id.textView2);
Text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound1);
mediaplayert.start();
}
});
TextView Text1 = (TextView) rootView.findViewById(R.id.textView4);
Text1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound2);
mediaplayert.start();
}
});
TextView Text2 = (TextView) rootView.findViewById(R.id.textView6);
Text2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
mediaplayert = MediaPlayer.create(MainActivity.con,
R.raw.sound3);
mediaplayert.start();
}
});
return rootView;
}
}
#Override
protected void onDestroy() {
if (mediaplayert != null) {
mediaplayert.stop();
mediaplayert= null;
}
super.onDestroy();
}
}

How Stop Sound when back or home button is pressed

My question is how to stop the media player when the user presses the back or the home button? I really need help..Can some one please give a code and tell me where to incorporate it in the activity.
This is my code:
MY JAVA CODE:
public class fbactivity extends Activity {
private MediaPlayer myMediaPlayer;
private MediaPlayer my1MediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myMediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.milyonlarcataraftaryanyana);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.magnumsilah);
myButtonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.Button03);
myButtonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
my1MediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.sevmeksenideligibiyurekister);
//Button related to play btn
Button myButtonOne1 = (Button) findViewById(R.id.Button02);
myButtonOne1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo2 = (Button) findViewById(R.id.Button01);
myButtonTwo2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.pause();
}
});
Button butongeridon=(Button) findViewById(R.id.geridon);
butongeridon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(fbactivity.this,MainActivity.class); // the names of activity as per you program.
startActivity(i);
finish();
}
});
}
}
You inflate two buttons, one from R.id.magnumsilah, and another from R.id.Button03. However, the ClickListener is bound to only one of them, and no listener is bound to the back button.
It may well be worth it to be more thorough in you research before posting questions. Just a thought.

How to play video through FragmentTabHost in android

I made an application that has 4 tabs:
vedio tab, in this tab I want to play video, taking remote url
virtual lab
edit video
help
I have made MainActivity class in which use FragmentTabHost class id for display tab.
public class MainActivity extends FragmentActivity{
private FragmentTabHost mTabHost;
private FragmentTabHost mTabHostabove;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Vedio").setIndicator("Vedio",getResources().getDrawable(R.drawable.ic_launcher)),FragmentTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("Virtual Lab").setIndicator("Virtual Lab"),FragmentTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("Edit Vedio").setIndicator("Edit Vedio"),FragmentTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("Help").setIndicator("Help1"),FragmentTab.class,null);
mTabHostabove = (FragmentTabHost)findViewById(R.id.tabhostabove);
mTabHostabove.setup(this, getSupportFragmentManager(), R.id.tabcontent);
mTabHostabove.addTab(mTabHostabove.newTabSpec("Logo").setIndicator("Logo"),FragmentTab.class,null);
mTabHostabove.addTab(mTabHostabove.newTabSpec("Vedio Url ").setIndicator("Vedio Url",getResources().getDrawable(R.drawable.ic_launcher)),FragmentTab.class,null);
}
}
I have also made FragmentTab class which extends Fragment.
here is code:
public class FragmentTab extends Fragment {
private TextView tv;
private VideoView mVideoView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
tv = (TextView) v.findViewById(R.id.text);
mVideoView = (VideoView)v.findViewById(R.id.vedioview);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
String path1="http://commonsware.com/misc/test2.3gp";
MediaController mc = new MediaController(getActivity());
mc.setAnchorView(mVideoView);
mc.setMediaPlayer(mVideoView);
mVideoView.setMediaController(mc);
mVideoView.requestFocus();
// mVideoView.setVideoURI(Uri.parse("android.resource://" +getActivity().getApplicationContext().getPackageName() +"/"+R.raw.song));
mVideoView.setVideoURI(Uri.parse(path1));
mc.show();
mVideoView.start();
} catch (Exception e) {
}
}
});
String tag = this.getTag();
if (tag == "Vedio") {
tv.setText("play vedio");
}
if (tag == "Edit Vedio") {
tv.setText("want to Edit Vedio !!!!!!!");
}
if (tag == "Help") {
tv.setText("do u want help !!!!!!!");
}
if (tag == "Virtual Lab") {
tv.setText("Enter Virtual lab !!!!!!!");
}
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
I am stuck with to play vedio on click vedio tab
anybody solve it if u can . I have searched for 3 days, but i have not found a solution. vedio sound is coming, but vedio is not playing.
Not all devices support all video codecs.
I had the same problem: I work with mp4-format and a resolution of 1280x720. This video format wasn't support by all devices (e.g. HTC Wildfire S). After changing the resolution to 480x360 the video was shown by all devices.
This link should help you:
http://developer.android.com/guide/appendix/media-formats.html

Resources