I'm quite new to this, I have checked for an answer at the forums here but I didn't find any answer that can really help me out. I'm trying to play a video from the res/raw folder. I have set up this code so far:
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
ListView l1 = (ListView) findViewById(R.id.listview);
l1.setAdapter(new EfficientAdapter(this));
l1.setOnItemClickListener(new OnItemClickListener() {
//Starts up a new activity, based on what listitem you press.
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(VideoButtonActivity.this, com.example.norskattrack.VideoActivity.class);
if(position==0){
System.out.println("Item 0");
intent.putExtra("video", 1);
startActivity(intent);
}
if(position==1){
System.out.println("Item 1");
intent.putExtra("video", 2);
startActivity(intent);
}
}
});
}
Uri video = Uri.parse("android.resource://com.package.app/raw/videoname");
That should let you play the video from the raw folder !
have a look at :
How to play videos in android from assets folder or raw folder?
Play Video From Raw Folder
Trying to play video from raw folder (VideoView)
All have excellent answers !
Related
I developed an application that receives a YOUTUBE URL from other users and plays it in the youtube player. But I want that URL to play in youtube Webview which I have in my Application. I don't have an extensive background in android and Java. This is my third time experimenting with android. So, please help me so I can make some changes in the Onclickmethod of the listview.
Here is the code that I have for playing in the youtube application or either it will ask to choose between youtube and Google chrome.
scanListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TextView textView = findViewById(R.id.textView);
String text = ((TextView) view).getText().toString();
// textView.setText(text);
String launchUrl = url.concat(getUrlMethod(text,"|"));
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(launchUrl)); //*************************************************************
startActivity(browserIntent);
}
}
);
I have made a Song List using ArrayList, and I got all the information and it can be passed to the second activity and open it on a new page.
What I want to have is when I click the song list, then it opens in a new page, and the new page shows the details and the image of the song.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView_Main=findViewById(R.id.listView_Main);
final Song song1 = new Song("Love Story","Taylor Swift","September 12,2008", "$1.29");
Song song2 = new Song("Lover","Taylor Swift","August 23,2019", "$1.29" );
Song song3 = new Song("I Forgot That You Existed", "Taylor Swift", "August 23,2019", "$1.29");
Song song4 = new Song("The Archer", "Taylor Swift", "August 23,2019", "$1.29");
Song song5 = new Song("I Think He Knows","Taylor Swift", "August 23,2019", "$1.29");
final List<Song> songs = new ArrayList<>();
songs.add(song1);
songs.add(song2);
songs.add(song3);
songs.add(song4);
songs.add(song5);
MainAdapter adapter = new MainAdapter(this, songs);
listView_Main.setAdapter(adapter);
listView_Main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent=new Intent(view.getContext(),SongDetailActivity.class);
intent.putExtra("currentSong",(Serializable) songs.get(i));
startActivity(intent);
}
});
}
But my problem is in the second activity, I don't know how to get the detail of the song, and the images by using the setText.
So, I have made the song details in the String.xml file.
How can I get those details and the image in the second activity?
Also, I don't know how to get the position of the song list in the second activity, I mean the position of which song that I click, and It show the detail of that song I had clicked.
//Here is the second activity of the Song Details.//////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song_detail);
textViewSongDetail=findViewById(R.id.textView_SongDetail);
imageViewSongDetail=findViewById(R.id.image_SongDetail);
Song currentSong=(Song)getIntent().getSerializableExtra("currentSong");
}
First of all make sure you've implemented Serializable in your model class Song. Than your code will work fine.
And to receive the position you can send -
intent.putExtra("position", i); // to send
getIntent().getIntExtra("position", -1); //to receive (-1 is default value)
I am currently working on a movie review app. I am trying to pass data from a Recyclerview to another recyclerview in a new activity. Currently I know how to pass data from Recyclerview to textview and imageview. However I looked online and also tried codes but still I could not pass data from recyclerview to recyclerview. What I am trying to pass is a String ArrayList that contains urls. How could I pass the data? Please help me TY! I just recently started on android studio
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.asBitmap()
.load(mImageUrls.get(position))
.into(holder.image);
holder.name.setText(mNames.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Clicked on an image: " + mNames.get(position));
Toast.makeText(mContext, mNames.get(position), Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(v.getContext(), MovieDetails.class);
//v.getContext().startActivity(intent);
Intent intent = new Intent(mContext, MovieDetails.class);
intent.putExtra("image_url", mImageUrls.get(position));
intent.putExtra("image_name", mNames.get(position));
intent.putExtra("director_name", mDirector.get(position));
intent.putStringArrayListExtra("movieTrailer", mTrailer);
mContext.startActivity(intent);
}
});
}
private void setImage(String imageUrl, String imageName, String directorName, ArrayList trailerUrl) {
Log.d(TAG, "setImage: setting the image and name to widgets.");
TextView name = findViewById(R.id.text_movie_original_title);
name.setText(imageName);
ImageView image = findViewById(R.id.image_movie_detail_poster);
Glide.with(this)
.asBitmap()
.load(imageUrl)
.into(image);
TextView director = findViewById(R.id.text_movie_director_name);
director.setText(directorName);
RecyclerView trailer = findViewById(R.id.movie_videos);
trailer.set;
bundle.putParcelableArrayList("urlList", list)
send it via intent
get arraylist by
getParcelableArrayList("urlList")
i think these steps would help:
1. create arrays
put the same values to the arrays, you put on the recyclerView.
pass the arrays using intent. Example:
Bundle bundle = new Bundle();
bundle.putStringArray("arrayOfName", new String[]{name1, name2});
bundle.putStringArray("arrayOfImageUrls", new String[]{url1, url2});
Intent intent=new Intent(context, SecondActivity.Class);
intent.putExtras(bundle);
then in the second activity:
Bundle bundle = this.getIntent().getExtras();
String[] names=bundle.getStringArray("arrayOfName");
String[] urls=bundle.getStringArray("arrayOfImageUrls");
add this arrays to the recyclerView adapter in the SecondActivity
set the Adapter to your recyclerView
Can I ask some help. we have smart android T.V box and already set up. we have 20 T.V channels set up. Now we want to create an android application that can select channel and display it to the screen just like the other cable provider that they have menu or list of their channels and you can select each of them to view on screen.The android app is installed in our smart android T.V box.
I have this simple running code but the problem is that I cannot show it again the list of my channels in order to select other channel I need to press the back button in order to show the list (discouraging I use listview). how can I achieve just like the same other cable provider that they can show the menu of channels on the left side and only will show if they press the menu.
this is what I have so far.
MyActivity.class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
String[] values = new String[] { "Nickelodeon", "Animal Planet", "CNN",
"MTV", "History Channel", "Discovery Channel", "National Geographic Wild", "MovieMAX HD",
"Science Channel", "TLC" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.videolist,R.id.label,values);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Intent intent = new Intent(this, DisplayVid.class);
intent.putExtra("channelextra", item);
startActivity(intent);
}
Showvideo.class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url = "url chanell here"; // static for my demo purpose only
video = (VideoView) findViewById(R.id.myVideo);
video.setVideoURI(Uri.parse(url));
mediacontroller = new MediaController(this);
mediacontroller.setMediaPlayer(video);
video.setMediaController(mediacontroller);
video.requestFocus();
video.start();
Bundle extras = getIntent().getExtras();
String item = extras.getString("channelextra");
}
Thank you in advance.
Ive searched for this all over the internet and there seems to be no simple explanation or tutorial on how to do this.
Basically, I want a layout that has a ListView where the user can click on an object and it will take them to the next layout.
In other words, using the listview as links to other layouts.
Everything Ive found on the internet has the end result of using a Toast... However I dont want a toast, i want to link to the next page.
Your question is slightly confusing so I'm going to make an assumption.
Is [LinearLayout1 LinearLayout2 Breadcrumb] suppose to be navigation or tabs that when selected insert their corresponding content into the Main Content?
If so I would suggest using fragments for each piece of content. Then when you click the navigation/tab, perform an animation of the fragment which slides the content in and out.
See the google docs for how to use fragments: http://developer.android.com/guide/components/fragments.html
See another stackoverflow answer for how to do the slide animation: Android Fragments and animation
or
http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html
Here is some code that outlines how to invoke an activity following a click on a list row. Hopefully you can adapt the Toast example you mention to make this work for you.
The basic idea is that you launch a new Activity with a new Intent. You can pass any data you need from the listView row as an extra in the Intent.
final static String[] months = new String[] {"Jan","Feb","Mar"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row_layout, R.id.text1, months);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int intItem = (int)id;
Intent intent= new Intent(this, SecondaryActivity.class);
intent.putExtra("MONTH", intItem);
startActivity(intent);
}
Why using ListView for it?
Each row must lead to different layout?
Its main benefits is in displaying dynamically changing data, but in your case data is constant, right?
Use vertical LinearLayout, fill it programmatically with "list elements", and add
leListComponent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(YourActivity.this, TargetActivity.class));
}
});
to each.
If i didn't get it, and you feel good of using some adapter, it can be like this:
public class LeWrapper {
private String caption;
private Class<? extends Activity> target;
...POJO here...
}
v.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//get leWrapper object from adapter
startActivity(new Intent(MenuActivity.this, leWrapper.getTarget()));
}
});
but its kinda overkill
Thanks for all the help guys. Sorry ive took my time replying. My solution is below :)
public class FP_WL1_ListView extends Activity {
private ListView lv1;
private String lv_arr[]={"Exercise Bike", "Treadmill", "Cross Trainer", "Squats", "Lunges"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fitnessprograms_wlday_one);
lv1=(ListView)findViewById(R.id.list);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final TextView mTextView = (TextView)view;
switch (position) {
case 0:
Intent newActivity0 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_sp.class);
startActivity(newActivity0);
break;
case 1:
Intent newActivity1 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Treadmill.class);
startActivity(newActivity1);
break;
case 2:
Intent newActivity2 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Crosstrainer.class);
startActivity(newActivity2);
break;
case 3:
Intent newActivity3 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Squats.class);
startActivity(newActivity3);
break;
case 4:
Intent newActivity4 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Lunges.class);
startActivity(newActivity4);
break;
}
}
});
} }