Box2D how to detect touching elements - android

I am trying to build a game using Box2D.
My problem is: on each update of the engine, I need to check each element and update the state of the element according to the state of touching elements. So I really need a function to detect which element is touching which element.
How can I do that?
Edit:
Example-> i have 5 boxes and box1-box2-box3 are touching to each other and box4-box5 are touching each other. And without any change on collusions if state of box2 changes to blue, box1 and box3 should also become blue

You'll need to implement ContactListener in a class and use it's methods :
public class CollisionListener implements ContactListener {
#Override
public void beginContact(Contact contact) {
}
#Override
public void endContact(Contact arg0) {
// TODO Auto-generated method stub
}
#Override
public void postSolve(Contact arg0, ContactImpulse arg1) {
// TODO Auto-generated method stub
}
#Override
public void preSolve(Contact arg0, Manifold arg1) {
// TODO Auto-generated method stub
}
}
You can set the listener like this
world.setContactListener(new CollisionListener());
Every time two item contact, beginContact(Contact contact) will be called and you will get info in the Contact object about which two items are colliding. You can access them like this
if ( contact.getfixtureA.getBody().getUserData() == "element1" &&
contact.getfixtureB.getBody().getUserData()=="element2" ){
Colliding = true;
}

Related

is there a way to get the number of children of a node without loading all the node data in Android?

My datastructure (userdetails)
-KCV32vWQECRlMvlgkGO
Name: "asdf"
Phoneid: "1zlkflakfhkf0e8"
Phoneno: "9478567899"
-KCV3s-lwv5i-VvFBaxq
Name: "asas"
Phoneid: "1c584jbascjasc8"
Phoneno: "9999999999"
My method
queryRef.addChildEventListener(new ChildEventListener() {
public void onCancelled(FirebaseError arg0) {
// TODO Auto-generated method stub
}
public void onChildAdded(DataSnapshot arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println("Size "+arg0.getChildrenCount());
}
How to get a count of 2? I am getting 3 two times because each time it goes inside and get child separately.
The problem is that the snapshot that a child_added event gives you is the new child data. So getChildrenCount() counts how many properties (children) the new child has (and it has 3).
To do what you want you should add a ValueEventListener event like so:
queryRef.addValueEventListener(new ValueEventListener() {
public void onCancelled(FirebaseError arg0) {
}
public void onDataChanged(DataSnapshot arg0, String arg1) {
System.out.println("Size "+arg0.getChildrenCount());
}
});
This should return all the children at queryRef location every time the value changes (e.g new child).
Tell me if this works as I can't try it myself.

How to onClickListen MP Android line Chart Circles?

I am trying to listen the click on the circles at the Line Chart .. but I could not. So please help me.
chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
}
#Override
public void onNothingSelected() {
}
});
I tried this but it did not work!.
It's hard to tell from the code you provided but I ran into this issue when I called chart.setHighlightPerTapEnabled(false)
From the names "highlight" versus "selected" if wasn't clear at first sight that the setHighlightPerTapEnabled method also disables the OnChartValueSelectedListener
My solution was to replace the OnChartValueSelectedListener with a OnChartGestureListener. Here's a snippet of what I implemented:
private class BarChartGestureListener implements OnChartGestureListener {
private int _lastTappedIndex = -1;
/* ... */
#Override
public void onChartSingleTapped(MotionEvent me) {
final Entry entry = _barChart.getEntryByTouchPoint(me.getX(), me.getY());
if (entry != null && _lastTappedIndex != entry.getXIndex()) {
final Object data = entry.getData();
// TODO: Insert your magic here...
}
}
/* ... */
}
In the onChartSingleTapped you can get the Entry that was tapped and programmatically highlight it or whatever else you want.
Implement OnChartValueSelectedListener this interface and override these two methods as below
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
Toast.makeText(getContext(),entries.indexOf(e)+"",Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected() {
}
and add listener on lineChart lineChart.setOnChartValueSelectedListener(this);
Note:
Entries is list of Entry objects.
Toast will show clicked circle index value.

Contacts are listing in one row of listview?

I make an app in which phone contact show in listview. but i want to convert it into widget. ie, I want to show contact list on the homescreen in a listview. I searched on google and found some example but none worked. any one has any link. I am not posting my code because it not worked. any help will be appriciated
Update :-
I am able to show list on homescreen. how to bind this listview to contact list
I tried to add this method in my viewfactory class
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
ids = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
aa.add(new ContactStock(info.name,info.phone));
}
phone.close();
//Collections.sort(aa);
adapt=new ContactListAdapter(MainActivity.this, aa);
//listcontact.setAdapter(new ContactListAdapter(MainActivity.this, aa));
//adapter = new ArrayAdapter<Info>(this, android.R.layout.simple_list_item_1);
listcontact.setAdapter(adapt);
}
But it doesn't work, it is working in my app but not in homescreen widget
Update 2 :-
I am showing contact list in widget but all contacts are showing in one row.My remote view class is
public class DialerViewFactory implements RemoteViewsFactory {
private static final int mCount = 2;
private List<Info> mWidgetItems = new ArrayList<Info>();
private Context mContext;
private int mAppWidgetId;
public DialerViewFactory(Context context,Intent intent){
mContext=context;
mAppWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mCount;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
#Override
public RemoteViews getViewAt(int position) {
// TODO Auto-generated method stub
//ContentResolver cr=mContext.getContentResolver();
//getnumber(cr);
RemoteViews rv=new RemoteViews(mContext.getPackageName(),R.layout.row);
rv.setTextViewText(R.id.textrow1, mWidgetItems.toString());
rv.setTextViewText(R.id.textrow2, "Kya");
Bundle extras = new Bundle();
extras.putInt(DialerWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.textrow1, fillInIntent);
return rv;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
ContentResolver cr1=mContext.getContentResolver();
getnumber(cr1);
}
#Override
public void onDataSetChanged() {
// TODO Auto-generated method stub
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
}
public void getnumber(ContentResolver cr) {
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phone.moveToNext()) {
Info info = new Info();
info.phone=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
info.name=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
info.picture=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out.println("...........name");
mWidgetItems.add(info);
}
phone.close();
}
class Info {
public String name;
public String phone;
public String picture;
#Override
public String toString() {
return name;
}
}
}
What you are looking for is AppWidgets using Collections. This can accept a ListView as the widget for presenting your data.
This will basically consist of
an AppWidgetProvider, as in any widget
a RemoteViewsService / RemoteViewsFactory, responsible for
producing the views in your ListView, much like an Adapter would do.
Since you are not sharing your code, it's hard to say what is not working for you, but you can find working examples in the sdk samples. Note that the samples have apparently been removed from latest sdk versions, so you should probably download the samples for older sdk's, if I remember correctly. You can also browse the source here.
EDIT :
adapt=new ContactListAdapter(MainActivity.this, aa);
listcontact.setAdapter(adapt);
This will not work in app-widgets, for 2 reasons :
You don't have an Activity available. For widgets, you can use the Context provided as argument of the onUpdate callback in your AppWidgetProvider, although in your case it probably won't be needed.
You don't instantiate an Adapter for app-widgets. To bind data to your ListView on your homescreen, you call setRemoteAdapter on the main RemoteView inside your AppWidgetProvider (in the onUpdate method), like this :
Example :
// Setup the intent which points to the StackViewService which will
// provide the views for this collection.
Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Creating the main RemoteView and binding data to it.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
rv.setRemoteAdapter(R.id.listview, intent);
Basically, the RemoteViewsFactory's getViewAt method acts much like the getView in an Adapter would, you just have to copy and adapt the code of your adapter to make it work in the RemoteViewsFactory. The sample of StackWidget contains a RemoteViewsFactory where all the method are commented to let you know where to perform which tasks.
EDIT 2 :
#Override
public int getCount() {
// This method tells the factory how many rows it has to produce
// you should be returning the size of your data collection
// so in your case :
return mWidgetItems.size();
}
Also, in you getViewAt method you are setting the text as the toString() of the array (this is why you have all contacts in one row), whereas you should read the fields of the individual Info object for the position given by the method:
// get the current Info object by position :
Info currentInfo = mWidgetItems.get(position);
// then use that object to fill each row, like in getView for an Adapter, for example :
rv.setTextViewText(R.id.textrow1, currentInfo.phone);
rv.setTextViewText(R.id.textrow2, currentInfo.name);

how to access facebook friends list using social auth in android?

I am using social auth library for facebook integration. I am able to share/post text or images but i want to access my friend list.
Is it possible to access friend list via social auth in android?
Thanks in advance...
I am using socialauth to get a list of friends in Facebook. They have more up to date information on their github page wiki. The code below is my adaptation of the examples in the wiki. I am getting Name, profile image and Facebook id.
in my Fragment:
public class Friends extends SherlockFragment {
private static SocialAuthAdapter facebookAdapter;
I set up the adapter
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
facebookAdapter = new SocialAuthAdapter(new FacebookListener());
later in onCreateView I authorize the user
facebookAdapter.authorize(getSherlockActivity(), Provider.FACEBOOK);
Now I implement the first Listener (did they authorize)
private final class FacebookListener implements DialogListener
{
public void onComplete(Bundle values) {
Log.d("SocialAuth","Successful");
facebookAdapter.getUserProfileAsync(new FacebookProfileDataListener());
Toast.makeText(getSherlockActivity(),"Connection Successful",Toast.LENGTH_LONG).show();
}
#Override
public void onError(SocialAuthError error) {
Log.d("Custom-UI" , "Error");
}
public void onCancel() {
Log.d("Custom-UI" , "Cancelled");
}
#Override
public void onBack() {
// TODO Auto-generated method stub
}
}
Now I get the profile
public final class FacebookProfileDataListener implements org.brickred.socialauth.android.SocialAuthListener<Profile> {
#Override
public void onError(SocialAuthError arg0) {
// TODO Auto-generated method stub
}
#Override
public void onExecute(String arg0, Profile profile) {
if (profile.getProviderId().equalsIgnoreCase(Provider.FACEBOOK.name())) {
//store some of the profile information here
}
facebookAdapter.getContactListAsync(new FacebookContactDataListener());
}
}
Now the Contact List
public final class FacebookContactDataListener implements SocialAuthListener<List<Contact>> {
#Override
public void onError(SocialAuthError arg0) {
// TODO Auto-generated method stub
}
#Override
public void onExecute(String arg0, List<Contact> t) {
List<Contact> contactList = t;
JSONArray listOfIds = new JSONArray();
for (Contact c: contactList) {
listOfIds.put(c.getId());
}
AppDataManager manager = AppDataManager.getInstance(getSherlockActivity());
manager.setFacebookFriendsIds(listOfIds);
manager.setFacebookFriends(contactList);
}
}
At this point I have a List that I can work with.
https://developers.facebook.com/tools/
Use Graph Api Search .
https://graph.facebook.com/YOUR_ID?fields=friends , will show you whole list of friends .
https://graph.facebook.com/YOUR_ID?fields=friendlists , will show you only 25 friends .
This is the correct answer: add this to your oauth_consumer.properties file, under the line containing graph.facebook.com.consumer_secret:
graph.facebook.com.custom_permissions = user_friends
or read_friendlists depending on what permissions you truly need.
Check https://developers.facebook.com/docs/facebook-login/permissions/v2.0 for more info about permissions.

detect maps v2 zoom by user not device(automatic)

I am using the code below to detect zoom by user but its in loop even if I dont touch anything .How do I detect human touch on device and check if he zoomed ? I really appreciate any help.Thanks in Advance.
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
// TODO Auto-generated method stub
if (position.zoom < 12 ) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
});
}
The endless loop is obvious.
Either you have a zoom level of 20, then you will always end up in the second if condition.
Or you have a zoom level less than 20, then you will always end up in the third if condition.
I would try something like this:
boolean changeTriggeredByProgram = false;
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
if (changeTriggerByProgram) {
changeTriggeredByProgram = false;
return;
}
/* Check, whether you want to adapt the position by program,
* and if so, do the following: */
changeTriggeredByProgram = true;
googleMap.animateCamera(newPosition, new CancelableCallback() {
#Override
public void onFinish() {
}
#Override
public void onCancel() {
changeTriggeredByProgram = false;
}
};
}
}
No guarantee that this works. I am using a slightly different solution, as my problem is slightly different.

Categories

Resources