How can I check if headphones are currently plugged in. I don't want a broadcastreceiver which informs me when they have been connected to the device. I need something like:
if(/*headphone is connected*/)
...
I'm working on an app that allows the user to create notes while rehearsing a play. The user can view the notes they have created in a listview, and edit and delete them if they wish.
Take for example the user creates 3 notes. In the database, the row_id's will be 1, 2 and 3. So when the user views the notes in the listview, they will also be in the order 1, 2, 3 (intially 0, 1, 2 before I increment the values). So the user can view and delete the correct row from the database.
The problem arises when the user decides to delete a note. Say the user deletes the note in position 2. Thus our database will have row_id's 1 and 3. But in the listview, they will be in the position 1 and 2. So if the user clicks on the note in position 2 in the listview it should return the row in the database with row_id 3. However it tries to look for the row_id 2 which doesn't exist, and hence crashes.
I need to know how to obtain the corresponding row_id, given the user's selection in the listview. Here is the code below that does this:
// When the user selects "Delete" in context menu
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case DELETE_ID:
deleteNote(info.id + 1);
return true;
}
return super.onContextItemSelected(item);
}
// This method actually deletes the selected note
private void deleteNote(long id) {
Log.d(TAG, "Deleting row: " + id);
mNDbAdapter.deleteNote(id);
mCursor = mNDbAdapter.fetchAllNotes();
startManagingCursor(mCursor);
fillData();
// TODO: Update play database if there are no notes left for a line.
}
// When the user clicks on an item, display the selected note
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
viewNote(id, "", "", true);
}
// This is where we display the note in a custom alert dialog. I've ommited
// the rest of the code in this method because the problem lies in this line:
// "mCursor = mNDbAdapter.fetchNote(newId);"
// I need to replace "newId" with the row_id in the database.
private void viewNote(long id, String defaultTitle, String defaultNote,
boolean fresh) {
final int lineNumber;
String title;
String note;
id++;
final long newId = id;
Log.d(TAG, "Returning row: " + newId);
mCursor = mNDbAdapter.fetchNote(newId);
lineNumber = (mCursor.getInt(mCursor.getColumnIndex("number")));
title = (mCursor.getString(mCursor.getColumnIndex("title")));
note = (mCursor.getString(mCursor.getColumnIndex("note")));
.
.
.
}
Let me know if you would like me to show anymore code. It seems like something so simple but I just can't find a solution.
Thanks!
i have one problem, my json parser show only last item! But at Logcat i see all item's!
Can you help me?
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
Log.d("Log", result.toString());
JSONObject jsn = result;
try {
JSONArray jarray = jsn.getJSONArray("item");
for (int i = 0; i < jarray.length(); i++){
JSONObject jsno = jarray.getJSONObject(i);
title.setText(Html.fromHtml("<a href=\""+jsno.getString("link")+"\">"+jsno.getString("title")+"</a>"));
cat.setText(jsno.getString("category"));
date.setText(jsno.getString("pubDate"));
desc.getSettings().setJavaScriptEnabled(true);
desc.getSettings().setDefaultTextEncodingName("charset=UTF-8"); desc.loadData(jsno.getString("description"), "text/html; charset=UTF-8", "utf-8");
}
} catch (JSONException e) {
e.printStackTrace();
}
I have a TabLayout containing tabs as intents to activities.
I want to set custom menu items for each tab, but the onCreateOptionsMenu version called is the Host's version.
How can I make the menu items created by each activity on its own.
I'm using the MapView object for my app and it's working really well. But I noticed that it "locks" to zoom levels, even if you don't use them. For instance, if you pinch to zoom and barely do it, the view will "snap" either forward or backward to whatever the closest zoom level is. The native google maps application does not do this at all. You can barely pinch, and it'll accept it (there are no 'hard' zoom levels built in). How can I get my mapview to mirror that?
I have downloaded and installed Android SDK and tried to start android.bat sdk but got following exception:
C:\products\Android\tools>android.bat sdk
Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-win32-3550 or swt-win32 in swt.library.path,
java.library.path or the jar file
at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
at com.android.sdkmanager.Main.showSdkManagerWindow(Main.java:328)
at com.android.sdkmanager.Main.doAction(Main.java:302)
at com.android.sdkmanager.Main.run(Main.java:118)
at com.android.sdkmanager.Main.main(Main.java:101)
I have searched for solution a long time but I cannot get it to work. Is there anything I have to set up first?
I'm having a problem with emulator-5554, it keeps telling me it is offline.
When I do a 'adb devices' from the command line it says
emulator-5554 offline
Even after a fresh restart, I try that command and it still says it is offline.
The problem is when I try to install .apk files to the emulator using 'abd install path' from the command prompt, it tells me that it is offline, if I create another device and run that one, then try to install the .apk files, it says i have too many devices connected. So in other words, I can't install my .apk files.
How in the world can I get rid of that damn emulator-5554? I heard that if you do a restart, it should clear all the devices, but that does not seem to be working. It is like it is getting initialized when my computer starts up. Has anyone run into this issue?
Thanks
Hello Friends,
I am working on horizontal menu that will open on top of the screen.
Layout is something like following,
|| < || Menu Item1 || Menu Item2 || Menu Item3 || ||
I want to put this on top of the screen. It can have more than 3 menu
item and it can traverse through previous and next arrow.
I started with like this,
RelativeLayout ( width - fill_parent)
Gallery View ( Here i appended adapter )
There is two problem,
- Gallery view contains the space before and after
- While scrolling its item gets selected
Though its not ideal solution for this. How can i build custom
component like this ?
Any help appreciated.
Thanks
I have an activity that holds a fragment with Google Map view in it. App adds several dozens of markers to the MapView, using MarkerManager and ClusterRenderer to form clusters.
The problem is that when I have marker's InfoWindow opened and I press hardware Back button, it closes the app. Instead of that, I would like to have the InfoWindow closed.
Is there any straightforward way to achieve this?
Hi
I have an image on a private file.
I read the file, create the drawable, and assign it to an ImageView.
The ImageView has WRAP_CONTENT so the size is automatic.
On 320x480 screens, the image looks good
But on screens with more resolution and high density 480x800 or 480x854 (N1, droid) , when the image is for example 150x150, I see the image as 100x100.
Of course it has something to do with the density but not sure how should I resolve this.
This is my code:
FileInputStream fis = this.openFileInput("icon.png");
icon = Drawable.createFromStream(fis, "icon");
fis.close();
imageView.setImageDrawable(icon);
thanks
I will be releasing two applications soon, one for my company and one for me. Publishing app on my own is straightforward, but I'm not sure which account to use for the company.
What practice do you use in your company?
I only see one solution, creating a special google account like [email protected] shared by the company Android devs.
Problem Definition: We have a fairly large app which has multiple use cases such that they are all independent of each other. For example lets say we have a1, a2, a3 & a4 modules that are independent apps or use cases for our main app 'A'. The independent a1, a2, a3, a4 are all purchasable apps such that the user goes to our website instead of play store and activate either a1 or a2 by paying some fees on our website.
So basically App 'A' is a free app in play-store and is sort of Dashboard with buttons to launch a1, a2, a3, a4. When the user click on lets say a1 button then we will check if a1 is already installed and launch it but if it is not present then give the user a link to download it.
Option 1: Have a main app 'A' and a1, a2, a3, a4 as library project. But with this approach the main app A is too big in size.
Option 2: Have a1, a2, a3, a4 build as separate .apk and then put in the assets folder of main app 'A' and then install them as needed. Again size of main app A is bigger.
Option 3: Upload a1, a2, a3, a4 to a third party website or play store and download from it as needed. This way the main app remains lighter.
Observation: In all these approaches there will be an independent app installed with its own icon on users phone. So basically user can launch from either the Dashboard (which will eventually launch an intent from Activity in a1 app) or user can directly launch app a1.
Follow-up Question: Is there any other solution that anyone can suggest to tackle this kind of problem? Another things is by going this approach app a1, a2, a3, a4 can be developed & tested independently of each other.
I'm trying to create a screen that displays multiples items.
For this, I've created a listview but I'm not sure how to approach how to add/remove items from it.
Since I'm using custom listview items, I've created my own listadapter but now I need a way to add items on a button click.
Is there a way to create listview that can take an indefinite amount of items (instead of passing it in an fixed length array of View objects)?
Also what's the best way to remove items from listviews? setVisibility?
i can implement the QSB on my app using onSearchRequested() method.i have 4 column in my table. when i was type in the QSB. it will give some suggestions on the Suggestion window. how to do that? searchable dictionary example shows the dictionary provider class to retrive the suggestions. but on that no data inserted. then how they getting the suggestions. can you explain me what are the steps we have to follow or tutorials, sample codes are most thankful.
I have an activity with two buttons, start and stop. If the user press the start button a service is created using Context.startService. And the stop button calls Context.stopService.
I want the stop button to be the only way to destroy the service. Now, if i end the activity using a task manager, the service is killed as well. Is there any way to avoid this?
In my app, when one particular image button is clicked and held, i must be able to calculate the time for which the image button was held pressed. Can any one help me by giving some simple guidance or sample code. i am really stuck up here. Is there any specific event listener for this particular requirement.
I have 2 activities in my application.
In activity1, I set some configuration and I modify the state of some component (ex: set button state disabled). And I navigate to activity2.
In activity2 I do some stuff and at end, I finish this activity and come back to activity1.
But In activity1, my components state are the same when I leave this activity.
How to reset the components state with valuesdeclared in XML file (layout) ?
I have 2 Auto Complete Views which i have to pre-populate with some text .However as soon as user clicks on the views , the text should disappear and allow user to enter text . I have written two separate on click listeners to do so . The On click listener for the first one is working fine . However i have to double click for the On click listener of the second one to work. Please find the chunk of code below and come up with some solution .
final AutoCompleteTextView source = (AutoCompleteTextView) findViewById(R.id.source);
ArrayAdapter source_adapter = new ArrayAdapter(this, R.layout.list_item, Model.City);
source.setAdapter(source_adapter);
source.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
source.setText("");
source.setTextSize(14);
}
});
final AutoCompleteTextView destination = (AutoCompleteTextView) findViewById(R.id.destination);
ArrayAdapter destination_adapter = new ArrayAdapter(this, R.layout.list_item, Model.City);
destination.setAdapter(destination_adapter);
destination.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
destination.setText("");
destination.setTextSize(14);
}
});
I have an application that needs the same items [5 buttons acting as tabs] in every screen. Is it possible to create a "base XML layout" that has these 5 buttons and then have all the other XML files extend from the bas layout in some way so that I don't have to have multiple buttons that will ultimately have the same functionality.
Is there a better approach to this problem that can be supported by API 9
In my project i want to draw a graph with dynamically updated data, and data is came from remote device and i update it from my locally sqlite data base.
I have to draw a graph dynamically with having two paremeter as horizontally hours of day from {Mid night, 1, 2, ...., 11, Noon, 1 , 11, Mid Night} and vertically parameter as {One , two, Three, Four}.
At particular hour i get data value from my sqlite and want to draw it on graph, and any particular hour have different value like "One", "two" etc. and i want to draw graph with hour help.
Thanks in advance
I am trying to create app which is match text with appropriate images by pointing with line.
I want to create app exactly same which is shown in the below image:
can any one please give me an idea?
This is my main class:
public class MatchActivity extends Activity {
ArrayAdapter<String> listadapter;
float x1;
float y1;
float x2;
float y2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] s1 = { "smiley1", "smiley2", "smiley3" };
ListView lv = (ListView) findViewById(R.id.text_list);
ArrayList<String> list = new ArrayList<String>();
list.addAll(Arrays.asList(s1));
listadapter = new ArrayAdapter<String>(this, R.layout.rowtext, s1);
lv.setAdapter(listadapter);
GridView gv = (GridView) findViewById(R.id.image_list);
gv.setAdapter(new ImageAdapter(this));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3){
x1=v.getX();
y1=v.getY();
Log.d("list","text positions x1:"+x1+" y1:"+y1);
}
});
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3){
DrawView draw=new DrawView(MatchActivity.this);
x2=v.getX();
y2=v.getY();
draw.position1.add(x1);
draw.position1.add(y1);
draw.position2.add( x2);
draw.position2.add(y2);
Log.d("list","image positions x2:"+x2+" y2:"+y2);
LinearLayout ll=LinearLayout)findViewById(R.id.draw_line);
ll.addView(draw);
}
});
}
}
This is my drawing class to draw a line:
public class DrawView extends View {
Paint paint = new Paint();
private List<Float> position1=new ArrayList<Float>();
private List<Float> position2=new ArrayList<Float>();;
public DrawView(Context context) {
super(context);
invalidate();
Log.d("drawview","In DrawView class position1:"+position1+" position2:"+position2) ;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("on draw","IN onDraw() position1:"+position1+" position2:"+position2);
assert position1.size() == position2.size();
for (int i = 0; i < position1.size(); i += 2) {
float x1 = position1.get(i);
float y1 = position1.get(i + 1);
float x2 = position2.get(i);
float y2 = position2.get(i + 1);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawLine(x1,y1, x2,y2, paint);
}
}
}
Thanks in advance .
I am making a network call in an AsyncTask, but the problem i am facing is the amount of time it is taking to start the doInBackground method.
Here is a part of my code:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Temp:",System.currentTimeMillis()+"");
new Move().execute();
/*some other logic
}
}
And my AsyncTask is:
private class Move extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... temp) {
Log.d("start:",System.currentTimeMillis()+"");
gson.fromJson(Web.Request.get(some_URL),Void.class);
Log.d("end:",System.currentTimeMillis()+"");
return null;
}
}
These are the logs i got:
32658-998/com.example.game D/temp:? 1408923006159
32658-998/com.example.game D/start:? 1408923035163
32658-998/com.example.game D/end:? 1408923035199
So actually it took almost 29 secs to reach the first line in doInBackground method, where as it took just 36 ms to finish the network call. I tried it many times, the time taken is almost in the same order.
Is it possible to start the AsyncTask immediately? Or is there any other way to solve this problem.(other than a running a simple thread?)
Thank you :)