I'm new to Android Development, and I'm creating a simple app to train.
In this app, I would like to have a button in the Action Bar Menu that, if pressed, would change Action Bar color. I somewhat managed to achieve this in my main activity with a static variable and the switchActionBarColor() method listed above. But my problem is when I go to a new activity : the action bar changes color and I did not press the menu button. And when I go back to my main Activity, it is not coloured anymore. I would like the Action Bar style to persist through the application, and I feel my method is not suited at all. What would be the best way to do this, according to you ? Thanks
public class MainActivity extends Activity {
protected static int color = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the add_contact button */
public void addContact(View view) {
Intent intent = new Intent(this, AddContactActivity.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.change_color:
switchActionBarColor();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void switchActionBarColor() {
ActionBar bar = getActionBar();
if (bar == null)
return ;
switch (color) {
case 0:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_1)));
break ;
case 1:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_2)));
break ;
case 2:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_3)));
break ;
case 3:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_default)));
break ;
}
color++;
if (color == 4)
color = 0;
return ;
}
}
You can use SharedPreferences (https://developer.android.com/reference/android/content/SharedPreferences.html) to save and load the indicator for the color and set the color of the ActionBar in onResume of the Activity.
Related
I am trying to make a spinner that will display a different text view each time an item from the list has been selected. When I run my code, I am able to switch between the different items, but the text is not updating based on the selection. I have looked at a variety of similar questions but none of their solutions have done what I am looking for.
Here is my code from the main activity:
Spinner spinner;
TextView example;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
example = (TextView) findViewById(R.id.example);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.medication_array,android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
}
#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_main, 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;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case 0:
example.setText("Depression");
break;
case 1:
example.setText(R.string.ssriexample);
break;
case 2:
example.setText(R.string.snriexample);
break;
case 3:
example.setText(R.string.tcaexample);
break;
case 4:
example.setText(R.string.moiexample);
break;
case 5:
example.setText(R.string.otherexample);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Any tips would be apprieated as this is my first time coding in android studios and it's taking a little bit of time to get used to.
From the way your code is posted, it looks like your listeners are not attached to the spinner. Call spinner.setOnItemSelectedListener(new listener) or spinner.setOnItemSelectedListener(this)
I am working on an android webview app, i have added share action on action bar which works fine when showAsAction is 'never, but stop working as soon as i change it to 'always' or 'ifRoom'`
my code is:
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareURL();
}
return super.onOptionsItemSelected(item);
}
private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This!"));
}
if someone could help, thank you.
Remove the android:actionProviderClass="android.widget.ShareActionProvider" and add the android:icon="#drawable/image-name"
Are you sure you actually have space to display it?
You could try disabling the title to see is that created enough room:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Update:
Can you try using:
public void setOnMenuItemClickListener (Toolbar.OnMenuItemClickListener listener)
I believe the toolbar has a separate listener than the dropdown menu.
The code could look something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar tb = (Toolbar) findViewById(R.id.toolbar_id);
tb.inflateMenu(R.menu.menu);
tb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Menu item should be returned here (the one you tap on the toolbar)
return false;
}
});
}
When I created an activity it automatically added a back to main activity option in the action bar. It looks like those are the functions that do it:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.app_settings, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Is there a way to make it go back using finish()? Right now it looks like it resets some values that are saved and I want to keep them. When I tried working with finish() it didn't do it but i'm not sure how to use it in the action bar.
This is simpler and it works perfect:
public class BackButtonExample extends Activity {
#SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.duahs);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0B4C5F")));
bar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home {
finish();
return true; }
else{
return super.onOptionsItemSelected(item);
}
}
This is my ActionBar, it has two buttons:
private void showActionBar() {
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.activity_main_actions, null);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
}
I used this method to show buttons. I called this method in onCreate.
Now i want when i click on Any button which is in action bar New activity open.
For example i have AskActivity.java and MessageActivity.java
now when i click on ASK button AskActivity.java opens.
Is this possible?
I have used this but its not working.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
Intent i = new Intent(getApplicationContext(), AskActivity.class);
startActivity(i);
return true;
case R.id.action_message:
Intent ij = new Intent(getApplicationContext(), MessageActivity.class);
startActivity(ij);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I think it's because the onOptionsItemSelected method is related to MenuItem and not the CustomView. The two buttons are not option menu items, they are buttons inside the layout activity_main_actions. You have two choices - either create a new on click listener, as follows:
Button action_ask = (Button) findViewById(R.id.action_ask);
action_ask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
// do something
}
}
Or, use the on click attribute method:
<Button
android:id="#+id/action_ask"
...
android:onClick="actionAskClicked" />
And then inside your Activity:
public void actionAskClicked() {
// do something
}
Same for the other button action_message. Hope this helps.
You need to create a method to open an activity from menu button click:
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_ask:
startActivity(AskActivity.class);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void StartActivity(Class<?> cls) {
Intent intent = new Intent(activity, cls);
activity.startActivity(intent);
}
I am displaying menu action bar at the bottom of the screen. when user click/touch any of the menu item, i want to highlight it (i.e. the way button click highlighting happens). i tried onClickListener and ontouchListener but it doesn't highlight.
can someone tell me which porperty/method i have set.
Here is code i am using.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.landing_page_layout);
ActionBar actionBar = getActionBar();
actionBar.show();
// business logic }
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu);
item1 = menu.findItem(R.id.menu_option1);
item1.getActionView().setOnTouchListener(new OnTouchListener() {
// logic when user touch menu option1 touch
}});
Thanks
Chintan
Check this section in the documentation: http://developer.android.com/guide/topics/ui/menus.html#options-menu
To set the menu up you do this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
Where R.menu.menu points to your res/menu/menu.xml file. This will load the elements from that file
The options menu is listened to in the same way as regular View's with OnClickListeners and such. Instead you onOptionsItemSelected override like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.item1:
// Do something
return true;
case R.id.item2:
// Do something else
return true;
default:
return super.onOptionsItemSelected(item);
}
}