Got a problem and I cannot solve it : I'd like to crop as circle many pictures coming from a ListFragment. It will be more explicit with the code :
String[] countries = new String[] {
"Amel Mahmuzić",
"Laurent Meyer",
"Philipp Bellé",
"Dennis Pagano",
"Bill Gates",
"Steve Jobs",
"Jean Bonnot",
"Nicolas Sarkozy",
"Bob Marley",
"Thomas Pieronczyk"
};
int[] flags = new int[]{
R.drawable.amel_mahmuzic,
R.drawable.laurent,
R.drawable.phillip_belle,
R.drawable.dennis_pagano,
R.drawable.bill_gates,
R.drawable.steve_jobs,
R.drawable.jean_bonnot,
R.drawable.nicolas_sarkozy,
R.drawable.bob_marley,
R.drawable.thomas_pieronczyk,
};
String[] currency = new String[]{
"MoID GmbH",
"MoID GmbH",
"MoID GmbH",
"MoID GmbH",
"Microsoft",
"Apple",
"Pôle Emploi",
"Elysée",
"Weed Fabric",
"MoID GmbH"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", countries[i]);
hm.put("cur", currency[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.image_item, from, to);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
Ok, I know what you''l say :"it's not flag, not currency..." Example coming from the Internet and working. I just want to know how I can crop my picture as a circle.
And if nobody finds, does somebody know how to do an overlay with a square and a transparent circle whose color change when clicking ?
If you'd like to see what it looks like :
I think you could combine the answers given in the following two posts:
Create a bitmap:
How to crop an image in android?
Crop the bitmap to a circle using canvas.
Android - Cut a circle from a square Bitmap
Kind regards
Related
Using the image of the server instead of native images
I want my server images and text on my list.
Thank you very much
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Country : " + countries[i]);
hm.put("cur", "Currency : " + currency[i]);
hm.put("flag",Integer.toString(R.drawable.abc_ab_share_pack_holo_light) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout1, from, to);
setListAdapter(adapter);
You can store the Image URL as string and then can retrieve the image to your View as you like.You can use some Image Loaders Like Picasso(http://square.github.io/picasso/) to load the image to your view!
I have a listview with multicolumns, where I have several textviews in each row. Including two textview in each row with orientation="vertical".
In the xml file I can set the tag of each textview. However this tag of each textview is equal in each row.
How I can set the tag of each textview? The same problem happens with the id. In the first row it's ok. The problem is in following rows.
I put an image with an example.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_screen_slide_page, container,false);
lv = (ListView)rootView.findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> mylistData =
new ArrayList<HashMap<String, String>>();
String[] columnTags = new String[] {"hour","col1", "col2", "col3","col4", "col5", "col6","col7", "col8", "col9","col10", "col11", "col12","col13","col14"};
int[] columnIds = new int[] {R.id.tv_list_item1,R.id.tv_listRow_item1, R.id.tv_listRow_item2, R.id.tv_listRow_item3,R.id.tv_listRow_item4, R.id.tv_listRow_item5, R.id.tv_listRow_item6,R.id.tv_listRow_item7, R.id.tv_listRow_item8, R.id.tv_listRow_item9,R.id.tv_listRow_item10, R.id.tv_listRow_item11, R.id.tv_listRow_item12,R.id.tv_listRow_item13, R.id.tv_listRow_item14};
for(int i=0; i<24; i++)
{
HashMap<String,String> map = new HashMap<String, String>();
for(int j=0; j<15; j++)
{
if(j==0){
map.put("hour", "0"+i+":00");
if(i<10){
map.put("hour", "0"+i+":00");
}
else
map.put("hour", i+":00");
}
else if(j>0){
map.put(columnTags[j], "row”+i+”col"+j);
}
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter =
new SimpleAdapter(getActivity(), mylistData, R.layout.sechedule_list_row,
columnTags , columnIds);
lv.setAdapter(arrayAdapter);
You have to create your own custom adapter and then in getView set your tags and ids for each text view.
Here is an example how to use getView
android - custom ListView, getView method
This one example help you lot i think http://www.technotalkative.com/android-multi-column-listview/
I have a listview which i want it for displaying a text and corrs image. I have used an arrayadapter for it. I am able to get an arraylist of hashmaps containing the values of the text and the url for the image.
<Arraylist<Hashmap<String,string>> testdata : "name" and "image_url"
Now i am trying to bind it. But no image is shown and the logcat shows resolveuri failed on bad bitmap.
( my url is "/com.example.vocab.MainActivity/res/drawable-hdpi/right_icon.png" ). What am i doing wrong? Thanx in advance for any help.
// Binding resources Array to ListAdapter
this.setListAdapter(new SimpleAdapter(Grammar_tab_all.this, testdata ,
R.layout.list_item, new String[] { "name","img_url"},
new int[] { R.id.module_name_item, R.id.img_recom}));
final ListView lv = getListView();
To show the drawable images in listview, best method is to store only the int id of drawable image.
Try this.
listItems = new ArrayList<HashMap<String,Integer>>();
String fieldName = "image_id";
HashMap<String, Integer> listData1 = new HashMap<String, Integer>();
HashMap<String, Integer> listData2 = new HashMap<String, Integer>();
listData1.put(fieldName, R.drawable.camera_icon_focus_dim);
listData2.put(fieldName, R.drawable.camera_icon_scene_mode);
listItems.add(listData1);
listItems.add(listData2);
SimpleAdapter listItemAdapter = new SimpleAdapter(
this,
listItems,
R.layout.image_list_item,
new String[] { fieldName },
new int[] { R.id.listitem_img });
Instead of this <Arraylist<Hashmap<String,string>> testdata try with this <Arraylist<Hashmap<String,Object>> testdata if you need more refer this link http://developerboards.att.lithium.com/t5/AT-T-Developer-Program-Blogs/Developing-Apps-for-Android-Beyond-quot-Hello-World-quot-Part-I/ba-p/28983/page/2
You have to use a custom list view :
check out this website
http://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/
If you use images from android resources folder then you can use the
// get Drawable from resources folder
Resources res = context.getResources();
Drawable drawable = res.getDrawable( R.drawable.myImage );
ImageView mImageView.setImageDrawable( mDrawable );
// or
ImageView mImageView.setImageBitmap( mBitmap );
The ImageView is the one from your ListItems layout. I wrote for every ListView a own ListAdapter in which i inflate the special layout and set the data to the layout.
You need a custom listadapter if you want to have different images and this one is the best tutorial I have ever found on internet about this topic :)
I'm creating a game and here's the code I'm using to show a list of games with the user names, score, date etc. But how do I get the values of the TextViews tv_playerScore and tv_opponentScore so I can compare them and change the textColors of them? Because what I want is to parseInt and see which has the highest value and set its textcolor to green, and the others textcolor to red.
private void showGames(JSONArray games) throws JSONException {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < games.length(); i++) {
map.put("challenger", games.getJSONObject(i).getString("challengerName"));
map.put("active", games.getJSONObject(i).getString("active"));
map.put("opponent", games.getJSONObject(i).getString("opponentName"));
map.put("date", games.getJSONObject(i).getString("date"));
map.put("gameID", games.getJSONObject(i).getString("gameID"));
map.put("amount", games.getJSONObject(i).getString("amount"));
map.put("playerScore", games.getJSONObject(i).getString("challengerScore"));
map.put("opponentScore", games.getJSONObject(i).getString("opponentScore"));
if (Integer.parseInt(games.getJSONObject(i).getString("active")) == 2) {
mylist.add(map);
}
map = new HashMap<String, String>();
}
SimpleAdapter sadapter = new SimpleAdapter(this, mylist, R.layout.list, new String[]
{"amount", "active", "gameID", "challenger", "opponent", "date", "playerScore", "opponentScore"},
new int[] {R.id.tv_amount, R.id.tv_activte, R.id.tv_gameID, R.id.tv_player, R.id.tv_opponent, R.id.tv_date, R.id.tv_playerScore, R.id.tv_opponentScore});
listView.setAdapter(sadapter);
}
If you want to get the values of a textview must use findViewById function from Activity.
TextView tv_playerScore = (TextView) findViewById (R.id.tv_playerScore);
If the showGames() method is not a class that inherits from Activity (or similiar), you should make a setter injection of the elements of sight to those who want to access.
To compare:
tv_playerScore.getText().toString().compareTo(tv_opponentScore.getText().toString());
Finally, to change the color:
tv_playerScore.setTextColor(Color.CYAN);
Regards.
I think you should have a closer look in how ListViews work ( http://developer.android.com/resources/tutorials/views/hello-listview.html )
In short I guess you'll have to write your own Adpater class (e.g. extend SimpleAdapater) and write over its getView method. Here you can set the color of the textviews depending on the according value. (I think it would make sense to have them sorted before instead of checking them every time a list element is drawn...
I want to set color for particular row in listview.That row will know at runtime. I ahve done list view like this :
ArrayList<SalesRoutes> routeList = getSalesRoute();
ArrayList<HashMap<String, String>> routhPath = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < routeList.size(); i++) {
if(Integer.parseInt(routeList.get(i).getOutlets()) >0){
HashMap<String, String> map = new HashMap<String, String>();
map.put("routeCode",((SalesRoutes) routeList.get(i)).getRouteCode());
map.put("routeName",((SalesRoutes) routeList.get(i)).getDescription());
map.put("outlets", ((SalesRoutes) routeList.get(i)).getOutlets());
routhPath.add(map);
}
}
ListView list = getListView();
sd = new SimpleAdapter(this, routhPath, R.layout.route_path,new String[] {"routeCode","routeName","outlets" },new int[] { R.id.routeCode,R.id.routeName,R.id.outlets});
row = getLayoutInflater().inflate(R.layout.route_path_row, null, false);
getListView().addHeaderView(row);
list.setAdapter(sd);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelected(true);
//list.setSelection(0);
list.setTextFilterEnabled(true);
list.setItemsCanFocus(true);
list.setItemChecked(positions, true);
list.setSelectionAfterHeaderView();
Please tell me how can i do this...
Thanks in advance
One way is to use the index of the row you want to get like
getListView().getChildAt(index).setBackground(#ff0000);
Otherwise you would need to create a custom adapter and overwrite the getView method which is called before rendering each row. You can use that to check any conditions and set the background accordingly.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
The above is a tutorial about that.