I am newbie in Android Development and i am developing a android app using recycler view and card view.I created two column using grid layout with 10 rows 20 images Now i want to set a click Listener on every image which starts a new activity that should play a giff animation or video link with that specific image.
public AlphabetRecyclerViewItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Get LayoutInflater object.
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// Inflate the RecyclerView item layout xml
View alphabetItemView = layoutInflater.inflate(R.layout.activity_card_view_item,parent,false);
final TextView alphabetTitleView = (TextView)alphabetItemView.findViewById(R.id.card_view_image_title);
// Get alphabet image view object.
final ImageView alphabetImageView = (ImageView)alphabetItemView.findViewById(R.id.card_view_image);
alphabetImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Get Alphabet title Text
String alphabetTitle = alphabetTitleView.getText().toString();
// Create a snackbar and show it.
Snackbar snackbar = Snackbar.make(alphabetImageView,"You Clicked" + alphabetTitle + "image",Snackbar.LENGTH_LONG);
snackbar.show();
}
});
// Create and return our custom Alphabet Recycler View Item Holder object.
AlphabetRecyclerViewItemHolder ret =new AlphabetRecyclerViewItemHolder(alphabetItemView);
return ret;
}
I want to play different videos on every image user click.
Add your all images and video in arraylist and set arraylist in recycler view
after setting the adapter use the recyclerview item click and pass the video link from that position to next screen which you want to play in your video player.
Related
I have RecyclerView having an ImageView and a TextView for each row layout.In ViewHolder in RecyclerViewAdapter, I have click listener as
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Images i=list.get(getAdapterPosition());
i.setFlag(!i.getFlag());
list.set(getAdapterPosition(),(i));
notifyDataSetChanged();
}
});
In that click listener I am changing Boolean flag such that it can show either an item is selected or not.Depending on it's value I want to Traverse entire ArrayList by checking
for(int i=0; i<images.size(); i++){
if(images.get(i).getFlag()){
// there I want to get view from LayoutManager for that item
//but how to get view for that item and get Bitmap form imageview for that item
//end of geting view
}
}
Please help to get view at specific adapter position from Recyclerview Image to reflect my problem is as follows
Try this use recyclerView.getLayoutManager().findViewByPosition(int position) to get particular row of recyclerView
View row = recyclerView.getLayoutManager().findViewByPosition(0);
TextView textView = row.findViewById(R.id.YourTextviwID);
textView.setText("Nilu");
ImageView textView = row.findViewById(R.id.YourImageViewID);
imageView.setBackgroundResource(R.mipmap.ic_launcher);
NOTE :- only item that is display in screen when you scroll recyclerView than item will removed from memory
EDIT :- you can write a public method in adapter to get your arraylist in your activity where you can check your flag
I don't know how to save the data from these editText(as shown below in the image) and save those data on external storage, and then fetch those data from the saved file to the respective editText boxes, the buttons which I have implemented are for Picking the contact details like name and number from phonebook, which is working properly and there are two more buttons I have included for save and load operation.
Screenshot
If I am not wrong all your controls are there, in ListView or Recyclerview.
Make sure you set tag to each of your EditText properly since based on these tags you can fetch all details from the respected EditText present on each rows.
Here is the how you can set tag to controls
#Override
public View getView(int position, View convertView, ViewGroup parent) {
rowView.setTag(yourdata_object);
return rowView;
}
You can get tag details as mentioned below
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
YourDataObject item = (YourDataObject ) view.getTag();
Intent i = new Intent(GetRota.this, someOtherActivity.class);
i.putsomedata;
startActivity(i);
}
Let me first describe my intention. I have a XML DB with some folklore songs and lyrics. For my family and friends I want to develop an android app that will list the songs in A-Z order and after clicking/touching the song title a text/lyrics will appear (so that we can all sing the same words).
I am not very familiar with Java and Android development, nevertheless I managed to create an App where in the first Activity there is a button "Show all songs" and after pressing it another Activity with ListView lists the songs (using XMLPullHandler parser). Collections.sort() takes care for ordering (although not 100%, because items which start with national characters are pushed to the end). See the screenshot please:
But now I am uncertain how to proceed with the display of the song-text. My ideas:
To achieve, that after clicking an item a new SongActivity will appear and will show the lyrics. But then I should create e.g. 100 intents and activities or can I do it with just one? In the later case - how will the app know which text belongs to the clicked song?
To load the song lyrics text together with the song title and show it in the ListView, but with an attribute invisible (in html it is clear, but in java - I am not sure...)
Example of the song in the xml file:
<songs>
<song>
<id>1</id>
<title>Zvalila sa skala</title>
<lyrics>1. [:Zvalila sa skala z vŕšku do Dunaja,:]
[:prenes ma, má milá, prenes ma, má milá, veď je voda malá.:]
2. [:Ja bych ťa preniesla, hoc by bola väčšia,:]
[:ale že som ti nie, ale že som ti nie, tvoja najmilejšia.:]
3. [:Keby ja vedela, kde môj milý býva,:]
[:zaletela by som, zaletela by som, na jeho biely dom.:]
</lyrics>
<type>Valčík</type>
</song> ...
Could someone please give me a hand and guidance in this? I would be most grateful.
On the picture is the actual status with visible values of variables
To achieve, that after clicking an item a new Song Activity will
appear and will show the lyrics. But then I should create e.g. 100
intents and activities, or can I do it with just one? In the later
case - how will the app know which text belongs to the clicked song?
No you don't have to write 100 intents or activities. You can use just one which is more generalized and convenient.
To load the song lyrics text together with the song title and show it
in the listview, but with an attribute "invisible" (in html it is
clear, but in java - I am not sure...)
Though your question is not very clear, I guess you need to show the title of the song along with some text as a secondary text. You're trying to use invisible attribute gives me sense of hiding the secondary text and once the list item is clicked make the secondary text visible - is that it??
If I got you correctly, then you need to modify the adapter you're using for your RecyclerView. You need to customize your adapter and while binding each element of your list, you need to show/hide the secondary text by yourself by tracking which item is selected.
I would suggest you to use RecyclerView to show the list of songs in a list. To implement a RecyclerView with a custom adapter you'll find plenty of documentation over the internet. Here I'm attaching one example.
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// Define the ViewHolder for list item
public class NormalViewHolder extends ViewHolder {
public NormalViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do whatever you want on clicking the list items
}
});
}
}
// And now in onCreateViewHolder you have to pass the correct view
// while populating the list item.
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_normal, parent, false);
NormalViewHolder vh = new NormalViewHolder(v);
return vh;
}
// Now bind the viewholders in onBindViewHolder
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
try {
if (holder instanceof NormalViewHolder) {
NormalViewHolder vh = (NormalViewHolder) holder;
vh.bindView(position);
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
if (data == null) {
return 0;
}
// data is the ArrayList of items you want to show in your list
return data.size();
}
// Now define getItemViewType of your own.
#Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
// Now set the default ViewHolder for NormalViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
// Define elements of a row here
public ViewHolder(View itemView) {
super(itemView);
// Find view by ID and initialize here
}
public void bindView(int position) {
// bindView() method to implement actions
}
}
}
I am currently creating a basic news aggregator app for Android, I have so far managed to create multiple HorizontalListViews derived from this: http://www.dev-smart.com/archives/34
I am parsing all data from live JSON objects and arrays.
The process goes something like this:
1) Start app
2) Grab a JSON file which lists all feeds to display
3) Parse feed titles and article links, add each to an array
4) Get number of feeds from array and create individual HorizontalListView for each. i.e. "Irish Times".
5) Apply BaseAdapter "mAdapter" to each HorizontalListView during creation.
My baseadapter is responsible for populating my HorizontalListViews by getting each title and thumbnail.
My problem is however that all my feeds seem to contain the same articles and thumbnails. Now I am only new to Android so I'm not 100% sure whats going wrong here. See screenshot below.
Do I need to create a new BaseAdaptor for each HorizontalListview or can I use the same one to populate all my listviews with unique data.
Here's some code to help explain what I mean:
1) OnCreate method to get JSON data, parse it, get number of feeds and create each HorizontalListView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
//--------------------JSON PARSE DATA------------------
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
String json = jParser.getJSONFromUrl(sourcesUrl);
//Parse feed titles and article list
getFeeds(json);
//Create Listviews
for(int i = 0; i < feedTitle.size()-1; i++){
//getArticleImage(i);
addHorzListView(i);
articleArrayCount++;//Used to mark feed count for adaptor to know which array position to look at and retrieve data from.
//Each array position i.e. [1] represents a HorizontalListview and its related articles
}
}
2) addHorzListView method, used to create HorizontalListView and apply adaptor
//Method used to dynamically add HorizontalListViews
public void addHorzListView(int count){
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
View view = getLayoutInflater().inflate(R.layout.listview, mainLayout,false);
//Set lists header name
TextView header = (TextView) view.findViewById(R.id.header);
header.setText(feedTitle.get(count));
//Create individual listview
HorizontalListView listview = (HorizontalListView) view.findViewById(R.id.listviewReuse);
listview.setAdapter(mAdapter);
//add listview to array list
listviewList.add(listview);
mainLayout.addView(view, count);
}
3) Baseadaptor itself:
private BaseAdapter mAdapter = new BaseAdapter() {
private OnClickListener mOnButtonClicked = new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(HorizontalListViewDemo.this);
builder.setMessage("hello from " + v);
builder.setPositiveButton("Cool", null);
builder.show();
}
};
#Override
public int getCount() {
return noOfArticles.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
//Each listview is populated with data here
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
TextView title = (TextView) retval.findViewById(R.id.title);
title.setText(getArticleTitle(position));
new DownloadImageTask((ImageView) retval.findViewById(R.id.ImageView01)) .execute(getArticleImage(position));
Button button = (Button) retval.findViewById(R.id.clickbutton);
button.setOnClickListener(mOnButtonClicked);
return retval;
}
};
The adapter mAdapter is currently displaying the articles from the last HorizontalListView that calls it.
Currently I am using the same BaseAdaptor for each ListView as I figured it populated the listview as soon as its called but i looks as though a BaseAdaptor can only be called once, I really dont know.
I want to dynamically populate feeds though without having to create a new Adaptor manually for each HorizontalListView.
Any help would be much appreciated.
So...you got the same info in 4 listview, right? In that case you only need oneAdapter populating 4 listview.
An adapter just provide the views which are visible in that moment to the listview (if it is implemented in the right way) so you can reuse the adapter if the info contained is the same.
Blatant n00b question: I have several directories of pictures and wish to display randomly pictures from only one, which I select by a set of radio buttons. How do I specify the directory when using :
//"ha" is ha.png, which I would like to be at drawable/1/ha.png
image.setImageResource(R.drawable.ha);
Can I use setImageResource for this? If so how? If not, what should I use and how?
The object of the exercise is a flashcard program with different lessons (hence the dividing up of images) selectable at the first activity.
You cannot have subfolders under res/drawable, if you are referring to the drawables folder in your apk.
If you are referring to a random folder on your sdcard, then it's fine to use subfolders, but then you cannot use R.drawable.* for that approach to refer to the image.
In that case you need to load the image using
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/drawable/1/ha.png");
which returns a bitmap, which you can use like
image.setImageBitmap(bmp)
see http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap)
In order to react on changes made to the radion button, see
How to set On click listener on the Radio Button in android
You can use a GridView to show the images from a directory selected from a radio button (as your requirement says). After creating a GridView, associate a adapter to it. Please refer below for a n example adapter :
public class ImageAdapter extends BaseAdapter {
/** LayoutInflater. */
private LayoutInflater mInflater;
/** The i. */
private ImageView i;
/**
* Instantiates a new image adapter.
*
* #param c
* the c
*/
public ImageAdapter(Context c) {
mInflater = LayoutInflater.from(c);
}
public int getCount() {
// scaled pictures will have the list of
// which you have from the directory
return scaledPictures.size();
}
public Bitmap getItem(int position) {
return scaledPictures.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image, parent, false);
} else {
i = (ImageView) convertView;
}
Bitmap bitmap = getItem(position);
i = (ImageView) convertView.findViewById(R.id.galleryimage);
i.setImageBitmap(bitmap);
bitmap = null;
return i;
}
}