I'm new to android developing, so guys i need your help. I've searched all over the internet but couldn't find the solution, so finally i decided to post here... Since I'm a newbie please give me more info as you can.
Here is what i'm doing...
I've few buttons on activity TEST, when the user long presses the buttons it will opens the
(MAIN_ACTIVITY)list of all apps installed. And when the user selects an app, it gets the
respective package name and returns to TEST activity. Now when the user clicks the button it will open the new app(which was selected earliear before returning to TEST activity).
In shortcut user should have the ability to edit the button dynamically like(app link,icon,title).
Now here is what my problem is...
I've abled to achieve the above but it only works on 1 button only. I mean other buttons do the same thing. And this is driving me crazy... If you guys need xml code, please let me know. Thanks in advance guys.
TEST ACTIVITY
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.ListView;
public class Test extends Activity implements OnLongClickListener,
OnClickListener {
final int APPLIST_REQUEST_CODE = 12345;
final int APPLIST_REQUEST_CODE2 = 2;
String pac, pac2;
private ListView mListAppInfo;
Button bBrowser, bCalculator, bExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
bBrowser = (Button) findViewById(R.id.bBrowser);
bExit = (Button) findViewById(R.id.bExit);
bCalculator = (Button) findViewById(R.id.bCalculator);
bBrowser.setOnClickListener(this);
bBrowser.setOnLongClickListener(this);
bCalculator.setOnClickListener(this);
bCalculator.setOnLongClickListener(this);
bExit.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bBrowser:
Intent i = getPackageManager().getLaunchIntentForPackage(pac);
startActivity(i);
break;
case R.id.bCalculator:
Intent i1 = getPackageManager().getLaunchIntentForPackage(pac2);
startActivity(i1);
break;
case R.id.bExit:
finish();
break;
}
}
@Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bBrowser:
Intent a = new Intent("android.intent.action.APP_LIST");
startActivityForResult(a, APPLIST_REQUEST_CODE);
break;
case R.id.bCalculator:
Intent a2 = new Intent("android.intent.action.APP_LIST");
startActivityForResult(a2, APPLIST_REQUEST_CODE2);
break;
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == APPLIST_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
pac = data.getStringExtra("key");
}
} else if (requestCode == APPLIST_REQUEST_CODE2) {
if (resultCode == Activity.RESULT_OK) {
pac2 = data.getStringExtra("key");
}
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// super.onBackPressed();
}
}
MAIN ACTIVITY
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView mListAppInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
mListAppInfo = (ListView) findViewById(R.id.lvApps);
AppInfoAdapter adapter = new AppInfoAdapter(this,
Utilities.getInstalledApplication(this), getPackageManager());
mListAppInfo.setAdapter(adapter);
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
AppInfoAdapter appInfoAdapter = (AppInfoAdapter) parent
.getAdapter();
ApplicationInfo appInfo = (ApplicationInfo) appInfoAdapter
.getItem(pos);
Intent a = new Intent();
a.putExtra("key", appInfo.packageName);
setResult(Activity.RESULT_OK, a);
finish();
}
});
}
}