So in my MainActivity, I can change the background of the layout by clicking on the overflow in my actionbar and selecting a background.
But how can I change the layout across all other activities?
This is the code i have now
private int selectedBackgroundId = R.id.defaultTheme;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.defaultTheme:
mainLayout.setBackgroundResource(R.drawable.defaultbackground);
if (selectedBackgroundId == R.id.defaultTheme){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.defaultTheme;
return true;
case R.id.background1:
mainLayout.setBackgroundResource(R.drawable.redpinkgradientbackground);
if (selectedBackgroundId == R.id.background1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.background1;
return true;
Second question i have
How can I save the layout that has been set when app is destroyed?
So when I reopen the app, it's still the layout I clicked in the overflow menu.
Related
The idea is to temporarily hide an option menu for a specific fragment. If the user is signed in, the system should hide the menu.
In all other fragments the menu should be implemented and enabled.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_main_home:
if (MainActivity.isSignedIn() == true){
(signedIn == false)
forwardToWelcomeFragment();
} else {
Toast.makeText(this, "You are not logged in.",
Toast.LENGTH_SHORT).show();
forwardToLoginFragment();
}
return true;
case R.id.menu_main_settings:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_info:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_main_signout:
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
setSignedIn(false);
forwardToLoginFragment();
return true;
default:
return super.onOptionsItemSelected(item);
}
I want to show Toast on actionItem() function inside. But it doesn't now showing Toast.
If I call
Context context = getApplicationContext();
Before actionItem() then the app crashes.
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class ShowWebViewActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_web_view);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_show_web_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
switch (item.getItemId()) {
case R.id.action_settings:
actionItem();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Context context = getApplicationContext() makes app to crash why??
public void actionItem(){
// Toast function should call here. But not working
Context context = getApplicationContext();
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
}
}
It should be,
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
Show() is missing from your code. Kindly refer to my code:
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
should be:
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
You would have got an IDE warning also for this
You missed show()
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
it should be:
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
show() is missing use the following code
public void actionItem(){
// Toast function should call here. But not working
Context context = getApplicationContext();
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
}
You missed the show() function.
Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
I have used ActionBar in my app. I want to get value from an EditText i.e on other Layout. I have tried to get that layout and then getting the EditText value.
Code to get the layout in which EditText is
public View textInputChecks() {
LayoutInflater mLayoutInflater = getLayoutInflater();
View textQrView = mLayoutInflater.inflate(R.layout.text_qr, null);
return textQrView;
}
Code for ActionBar button click
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO This is really needed to make app icon a toggle of nav drawer.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case R.id.action_ok:
View textInputView = textInputChecks();
EditText textInput = (EditText) textInputView.findViewById(R.id.qrTextInput);
String text = textInput.getText().toString();
if (text.length()>0) {
Toast.makeText(getApplicationContext(), "Ok",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "No Text added",
Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
return super.onOptionsItemSelected(item);
}
It always shows "No Text Added" Toast message even if you add text
I'm using ActionBar in my application, and I want when the user clicks a button, the item in the ActionBar should change the text.
This is my code for onOptionsItemSelected():
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
finish();
break;
case R.id.lg:
Toast.makeText(getBaseContext(), "ma", Toast.LENGTH_SHORT).show();
break;
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
break;
case R.id.nerlandais:
Toast.makeText(getBaseContext(), "brahim", Toast.LENGTH_SHORT)
.show();
}
return true;
}
What must I do to change the item title from another item?
Example: When I click in French item I want to change nerlandais item title.
If you want to change an MenuItem's title clicking to another item you can do something like this :
private String mMenuItemTitle;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.nerlandais);
item.setText(mMenuItemTitle);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
mMenuItemTitle = "My New Title";
supportInvalidateOptionsMenu();
break;
}
return true;
}
Try item.setTitle("new title")
Or, if from another item then the Menu has findItem
I have these two options in the menu of an activity
option one starts a music track and option two should stop it, but it isn't.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MediaPlayer mpSoundTrack = MediaPlayer.create(this, R.raw.app_score);
switch (item.getItemId()) {
case R.id.icon: Toast.makeText(this, "Music On!", Toast.LENGTH_LONG).show();
mpSoundTrack.start();
break;
case R.id.icontext: Toast.makeText(this, "Music Off!", Toast.LENGTH_LONG).show();
mpSoundTrack.stop();
break;
}
return true;
}
Each time you create a new mediaPlayer, so you stop a new one, not the old one. You should keep a reference to it:
private MediaPlayer mpSoundTrack = null;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icon:
Toast.makeText(this, "Music On!", Toast.LENGTH_LONG).show();
mpSoundTrack = MediaPlayer.create(this, R.raw.app_score);
mpSoundTrack.start();
break;
case R.id.icontext:
Toast.makeText(this, "Music Off!", Toast.LENGTH_LONG).show();
if(mpSoundTrack != null)
mpSoundTrack.stop();
break;
}
return true;
}