How to play video through FragmentTabHost in android - android-fragments

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

Related

Android flash image

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?

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

How to open BottomSheetDialog inside fragment

I want to open BottomSheetDialog from fragment. Before it, I tried opening BottomSheetDialog at activity and it worked. I use the same code and replace the getSupportFragmentManager with getChildFragmentManager. I also tried replacing the getSupportFragmentManager with getActivity().getSupportFragmentManager . Is there a solution?
public class MainFragment extends Fragment implements BottomSheetDialog.BottomSheetListener {
private TextView mTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_beranda, container, false);
Button buttonOpenBottomSheet = findViewById(R.id.button_open_bottom_sheet);
buttonOpenBottomSheet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialog bottomSheet = new BottomSheetDialog();
bottomSheet.show(getChildFragmentManager(),"BottomSheet");
}
});
}
}
Try BottomSheetDialogFragment
ModalBottomSheetDialogFragment.Builder()
.add(R.menu.menu_options)
.show(childFragmentManager, "my_bottom_sheet")

Using FirebaseUI for firebase recyclerView in android

i am a new comer in android development and facing some problems using firebase ui.
i wanted to retrieve data from my firebase database and show it on my recyclerView.
but i am constantly getting an error while making FirebaseRecyclerAdapter.
Here is My Code Sample:
public class UsersActivity extends AppCompatActivity {
private Toolbar userToolbar;
private RecyclerView usersList;
private DatabaseReference mUsersDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
userToolbar = (Toolbar) findViewById(R.id.users_toolbar);
setSupportActionBar(userToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Users");
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
usersList = (RecyclerView) findViewById(R.id.users_list);
usersList.setHasFixedSize(true);
usersList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_user_list_layout,
UsersViewHolder.class,
mUsersDatabase
) {
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull Users model) {
holder.setName(model.getName());
}
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
usersList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder
{
View mView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name)
{
TextView userNameView = (TextView) mView.findViewById(R.id.single_user_name);
userNameView.setText(name);
}
}
}
The Error i'm getting is this:
please help!!
Sorry wanted to comment but i ain't got enough points to do so.
Try this first, if it doesnt work try using FirebaseRecyclerOptions
(The latest version (3.x) of FirebaseUI implements a different method ofinitializing a FirebaseRecyclerAdapter than previous versions.)
Make your " firebaseRecyclerAdapter " variable global, so you would have something like this.
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_user_list_layout,
UsersViewHolder.class,
mUsersDatabase
)

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();
}
}

Resources