Settings option on click of Menu button - Launcher - android

I am successfully making a launcher, but i need to add another settings option. When i click on the menu button it will show. When you click on it, it goes to an activity.
For example. In the default launcher, when you hit menu on a home screen you can add, or set a wallpaper, ect. When you click on wallpaper it takes you to change the wallpaper.
What i need to do is create a button like that under the menu, and when it is clicked take me to an activity.
Here is part of my Launcher.Java...
private static final int MENU_GROUP_ADD = 1;
private static final int MENU_ADD = Menu.FIRST + 1;
private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
private static final int MENU_MYHOME_SETTINGS = MENU_WALLPAPER_SETTINGS + 1;
private static final int MENU_SEARCH = MENU_MYHOME_SETTINGS + 1;
private static final int MENU_STYLES = MENU_SEARCH + 1;
private static final int MENU_SETTINGS = MENU_STYLES + 1;
And here is the other part
super.onCreateOptionsMenu(menu);
menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add).setIcon(
android.R.drawable.ic_menu_add).setAlphabeticShortcut('A');
menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper).setIcon(
android.R.drawable.ic_menu_gallery).setAlphabeticShortcut('W');
menu.add(0, MENU_MYHOME_SETTINGS, 0, R.string.myhome_settings).setIcon(
R.drawable.ic_menu_customize);
menu.add(0, MENU_SEARCH, 0, R.string.menu_search).setIcon(
android.R.drawable.ic_search_category_default).setAlphabeticShortcut(
SearchManager.MENU_KEY);
menu.add(0, MENU_STYLES, 0, R.string.menu_customize).setIcon(
R.drawable.ic_menu_customize);
Any suggestions??

You can catch the menu clicks in overriden method onOptionsMenuSelected(MenuItem item). It might look like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ADD:
doSomething();
return true;
case MENU_WALLPAPER_SETTINGS:
doSomethingElse();
return true;
// All other buttons here, each in it's own case
}
return super.onOptionsItemSelected(item);
}
private void doSomething() {
Intent intent = new Intent(getBaseContext(), MyAnotherActivity.class);
startActivity(intent);
}
See http://developer.android.com/guide/topics/ui/menus.html#RespondingOptionsMenu for more information about menus and http://developer.android.com/guide/topics/fundamentals/activities.html#StartingAnActivity for Activities and how to start them.

Related

Change background image by pressing a menu item in android

Im creating simple notepad app in android,in options menu i have the "change background" as a menu item,when user clicks it,i just want to change the background image only [for example,i want to change my background image from efil tower to tajmahal],i have 10 images in drawable..how to change it randomly when everytime user clicks the "change background" option.Thank you.
int images[] = {R.drawable.eifel,R.drawable.tajmahal};
LinearLayout backLayout;
onCreate(Bundle savedInstanceState) {
backLayout = (LinearLayout) findViewById(R.id.mybackgroundlayout);
}
private void changeBackground(){
backLayout.setBackgroundResource(images[randInt(0,images.length-1)]);
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.changebackground:
changeBackground();
break;
}
return true;
}

android OptionsMenu click times

Can I get OptionsMenu click times,
I want to get value to use in onReceive, like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SubMenu fileMenu = menu.addSubMenu(0, 7, Menu.NONE, "歌曲");
fileMenu.add(0, 1, Menu.NONE, "A");
fileMenu.add(0, 2, Menu.NONE, "B");
fileMenu.add(0, 3, Menu.NONE, "C");
fileMenu.add(0, 4, Menu.NONE, "D");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "A", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "B", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "C", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this, "D", Toast.LENGTH_SHORT).show();
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(searchDevices, intent);
private BroadcastReceiver searchDevices = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if(OptionsMenu click times == 2)
{
}
}
Because I just have the process only this part,
please help me understand how to solve provide advice thank
Bali gave a good solution for what you asked. But there is a much simpler way to get this done without using a receiver.
Make a new class that has a static container for your click number.
ie:
public class ParamValues{
private static int clickNums= 0;
/**
* #return the clickNumber
*/
public static int getClickNums() {
return clickNums;
}
/**
* Sets the clickNumber
*/
public static int setClickNum(int clickNum) {
clickNums = clickNum;
}
}
Then create an onClickHandler for your OptionsMenu, and inside the click handler add in somecode like.
int count = ParamValues.getClickNum();
count++;
ParamValues.setClickNum(count);
Now you could get this click count anywhere in your code. If you want to make sure it's synchronized you could just add a function to process what you want in the same onClick.
ie:
if(ParamValues.getClickNum() == 2)
{
// Do whatever
}
Then in you can reset the count value in here or wherever you would want to reset it by using the convenient setter in the ParamValues class.
Using the receiver you can't guarantee exactly when the code will execute.
From the detail you have given:
You could create a datamember in your Activity, which will store the number of times, the menukey was pressed, and increase it every time its pressed:
private int menuPressedCount = 0;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_MENU)
menuPressedCount++;
}
And you can pass this to your receiver, puting it in the Intent, which the receiver will get:
Intent intent = ...;
intent.putExtra("menu_pressed_count", menuPressedCount);
And in the onReceive():
int pressedCount = intent.getIntExtra("menu_pressed_count", 0);
Tell me if this isn't what you are looking for!

android textview value

I have some problem
I want to change my textview value. when my code change it will change
here is the code
public class SubMenuActivity extends Activity {
private static final int GALLERY = 0;
private static final int SUBMANU01 = 7;
private static final int MANU01 = 1;
private static final int MANU02 = 2;
private static final int MANU03 = 3;
private static final int MANU04 = 4;
private static final int MANU05 = 5;
TextView tx1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tx1 =(TextView)this.findViewById(R.id.textView1);
if(tx1.toString()=="1".toString())
{
tx1.setText("7");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SubMenu fileMenu = menu.addSubMenu(GALLERY, SUBMANU01, Menu.NONE, "File");
fileMenu.add(GALLERY, MANU01, Menu.NONE, "new");
fileMenu.add(GALLERY, MANU02, Menu.NONE, "open");
fileMenu.add(GALLERY, MANU03, Menu.NONE, "save");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MANU01:
case MANU02:
case MANU03:
final String itemid = Integer.toString(item.getItemId());
tx1.setText(itemid);
return true;
}
return super.onOptionsItemSelected(item);
}
tx1.text value did not show 7 ,where is the problem?
I hope someone could tell me the problem.
compare like this
if(tx1.getText().toString().equals("1"))
{
tx1.setText("7");
}
Strings can not be compared with == operator they can be compared with .equals method
so change your code to this
if(tx1.toString().equals("1"))
{
tx1.setText("7");
}
Instead of
if(tx1.toString()=="1".toString()) {
tx1.setText("7");
}
try this
if(tx1.getText().toString().equals("1")) {
tx1.setText("7");
}
You need to Compare the variable like.equal(Object/String)
if(tx1.toString().equals("1"))
{
tx1.setText("7");
}
in java "==" means the address is the same, instead, you can use .equel() which comes from the
basic class "object".
first of you should print the value of txt1.
System.out.println("value of tx1:"+tx1.getText.toString());
if(tx1.getText().toString().equals("1"))
{
tx1.setText("7");
}

Saving Option Menu Shared Preferences

I have a option menu. When the activity loses focus, the selected option menu item(s) retain state, but when my activity is destroyed, all the options are reset.
How can I save the state of the selected preference after resuming from a destroyed state? Having problem visualizing how to implement Shared Preferences for the code.
(Only needed for the boolean values but I have included the static menu items.)
public final static int MENU_SOMETHING_MODE_ON = 1;
public final static int MENU_SOMETHING_MODE_OFF = 2;
public final static int MENU_FULLSCREEN_ON = 3;
public final static int MENU_FULLSCREEN_OFF = 4;
public final static int MENU_SOUND_ON = 5;
public final static int MENU_SOUND_OFF = 6;
public final static int MENU_FASTER = 7;
public final static int MENU_SLOWER = 8;
public final static int MENU_SOMETHING = 9;
public final static int MENU_EXTRAS = 10;
private static boolean soundOn = true;
private static boolean mFaster = true;
private boolean fullscreen = true;
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_SOMETHING, 0, R.string.menu_new_something);
menu.add(0, MENU_SOMETHING_MODE_ON, 0,
R.string.menu_something_on);
menu.add(0, MENU_SOMETHING_MODE_OFF, 0,
R.string.menu_something_off);
menu.add(0, MENU_FULLSCREEN_ON, 0, R.string.menu_fullscreen_on);
menu.add(0, MENU_FULLSCREEN_OFF, 0, R.string.menu_fullscreen_off);
menu.add(0, MENU_SOUND_ON, 0, R.string.menu_sound_on);
menu.add(0, MENU_SOUND_OFF, 0, R.string.menu_sound_off);
menu.add(0, MENU_FASTER, 0, R.string.menu_faster);
menu.add(0, MENU_SLOWER, 0, R.string.menu_slower);
menu.add(0, MENU_EXTRAS, 0, R.string.menu_extras);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
menu.findItem(MENU_SOUND_ON).setVisible(!getSoundOn());
menu.findItem(MENU_SOUND_OFF).setVisible(getSoundOn());
menu.findItem(MENU_SOMETHING_ON).setVisible(
getMode() == SOMETHING_NORMAL);
menu.findItem(MENU_SOMETHING_OFF).setVisible(
getMode() != SOMETHING_NORMAL);
menu.findItem(MENU_FULLSCREEN_ON).setVisible(!fullscreen);
menu.findItem(MENU_FULLSCREEN_OFF).setVisible(fullscreen);
menu.findItem(MENU_FASTER).setVisible(getFaster());
menu.findItem(MENU_SLOWER).setVisible(!getFaster());
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case MENU_SOMETHING:
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Are you sure?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'YES' Button
something.new();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Something");
// Icon for AlertDialog
alert.setIcon(R.drawable.dialog);
alert.show();
return true;
case MENU_SOMETHING_ON:
setMode(THIS_SOMETHING);
return true;
case MENU_SOMETHING_OFF:
setMode(THIS_NORMAL);
return true;
case MENU_FULLSCREEN_ON:
fullscreen = true;
setFullscreen();
return true;
case MENU_FULLSCREEN_OFF:
fullscreen = false;
setFullscreen();
return true;
case MENU_SOUND_ON:
setSoundOn(true);
return true;
case MENU_SOUND_OFF:
setSoundOn(false);
return true;
case MENU_FASTER:
setFaster(false);
return true;
case MENU_SLOWER:
setSlower(true);
return true;
case MENU_EXTRAS:
startExtras();
return true;
}
return false;
}
private void setFullscreen()
{
if (fullscreen) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
mView.requestLayout();
}
public synchronized static void setMode(int newMode)
{
Mode = newMode;
}
public synchronized static int getMode()
{
return Mode;
}
public synchronized static boolean getSoundOn()
{
return soundOn;
}
public synchronized static void setSoundOn(boolean so)
{
soundOn = so;
}
public synchronized static boolean getFaster()
{
return Faster;
}
public synchronized static void setFaster(boolean dont)
{
Faster = dont;
}
This is a fantastic tutorial on how to create a preference activity along with how to get/set these preferences. It will also show you how to use shared preferences if you choose not to implement a preference activity.
Almost all users are familiar with a preference activity because it is how they adjust settings on their phone.
While not the only way to implement "options," or possibly perhaps not even the best way, it works great, has a ton of flexibility, and is simple and quick to implement.

How do I disabled (dim) an option in onItemLongClick from a ListView in Android?

When an item in my ListView is clicked, I have several options pop up in a dialog for the user to choose. However, there are varying scenarios where I'd like to disable one or more options so the user cannot choose them. Here's some code.
public class MyApp extends ListActivity implements OnItemClickListener, OnItemLongClickListener {
private static final int HOW_MANY = 4;
private static final int ADD = 0;
private static final int EDIT = 1;
private static final int OPEN = 2;
private static final int DELETE = 3;
private String[] myItemClickDialog = null;
...
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mylayout);
this.myItemClickDialog = new String[HOW_MANY];
this.myItemClickDialog[ADD] = "Add";
this.myItemClickDialog[EDIT] = "Edit";
this.myItemClickDialog[OPEN] = "Open";
this.myItemClickDialog[DELETE] = "Delete";
}
#Override
public final boolean onItemLongClick(final AdapterView<?> parent,
final View view, final int position, final long id) {
final Context context = this;
Builder builder = new Builder(context);
builder.setTitle("Options");
String[] items = this.myItemClickDialog;
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, final int which) {
switch (which) {
case ADD:
//Do stuff
break;
case EDIT:
//Do stuff
break;
case OPEN:
//Do stuff
break;
case DELETE:
//Do stuff
break;
}
}
// Rest of code here
I would like to be able to disable (by dimming) a particular option, such as the DELETE option or the OPEN option depending on which list item was clicked (I got that part covered, that is, the detection piece).
Unfortunately this does not seem possible. The AlertDialog.Builder system hasn't taken this into account in its customization options. I looked at the source code for createListView to verify this.
I assume you've considered the simpler approach of just not showing those options? That is, creating a copy of the array with only the valid strings at that particular moment?

Categories

Resources