android menu which open a webview or a slide image - android

I need to create a menu which opens a webview page or a simple page with images slide.
I'm more proficient in web programming, not so much in programming for Android. So if it is easier to do it in a WebView I will use it. I will use the images slide otherwise.
So for now, I need 2 items when I press the menu button of the phone. Upon clicking they should open 2 different links in the app (not in the browser).
I've done only menu.xml for now:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_howto" android:title="#string/howto"
android:icon="#android:drawable/ic_menu_howto" />
<item android:id="#+id/menu_about" android:title="#string/about"
android:icon="#android:drawable/ic_menu_info" />
</menu>

in your main class you must #override the menu like:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
and then to make the menu buttons clickable you have to add this after
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.exit:
finish();
return true;
case R.id.loadanotherwebpage:
loadUrl("www.google.com");
// inside the menu button you don't need to call Super.loadUrl, LoadUrl its enough
return true;
case R.id.share:
share(); // here i'm calling the share method !
default:
return super.onOptionsItemSelected(item);
}
}
edit : if you want to load the html file from assets file then do this
super.loadUrl("file:///android_asset/index.html");
hope that what you asking for :)

Related

Add icon to fragment actionbar and get reference to it

I have no custom bar, I just set a delete icon to the actionbar, but now I need to set OnClickListener to this icon. how can I do that without custom bar is this possible. Also the icon not apears on the left side, can I set it on the rightside?
activity.getSupportActionBar().setIcon(R.drawable.ic_delete);
I use Navigation Drawer, when I use custom bar the toggle icon despairs.
Looks like you want to set the ActionBar's home button as your delete button. I would suggest not to do it as that is a bad design decision in my opinion. And moreover you also want to show the button on the right hand side which can be done in a much intuitive manner by using a menu.
Please have a look at the official documentaion for adding ActionBar actions here
Basically you need to add an XML menu resource and declare your actions like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_delete"
android:icon="#drawable/ic_delete"
android:title="#string/action_delete"
app:showAsAction="always"/>
</menu>
Then in your Activity override the OnOptionsItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete:
// Do your stuff here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
actionBar.setDisplayHomeAsUpEnabled(true);
Then you need to override activity method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onIconClicked();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
To create an item in the right side, you need to create custom menu, actually, it is simple.
Here is example how to do this

set app icon to right side in activity tool bar

I am working with the 'AppCompact' library and faced some problems with layout/positioning. I want to position the App icon on the right hand side of the 'ActionBar'. One method is to define a button in the toolbar, but is there a standard method to set the App icon and Up button on the right hand side of the ActionBar?
As you can see in the above image, the icon is positioned on the left, I want it to be on the right. Any help would be appreciated.
P.s:For people who may face my problem it can be fixed easily using this code
Add this code to manifest:
<application android:supportsRtl="true">
and then write this code on Oncreate:
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
There is no way android provides to set app icon at right side of actionbar but you can still do that.
Create a menu, say main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_item"
android:icon="#drawable/your_app_icon"
android:title="#string/menu_item"
app:showAsAction="always"/> //set showAsAction always
//and this should be the only menu item with show as action always
</menu>
Now just override onCreateOptionsMenu in your activity class.
add this in MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
It's done! Your app icon will be visible on right side of ActionBar now.
If you have more than one item in menu then override onPrepareOptionsMenu in activity class and set setEnabled(false) for menu item having app icon, doing this prevents your icon to be clickable.
#Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.findItem(R.id.menu_item).setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
Now your MainActivity.java file would look like
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu_item: //this item has your app icon
return true;
case R.id.menu_item2: //other menu items if you have any
//add any action here
return true;
case ... //do for all other menu items
default: return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.findItem(R.id.menu_item).setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
This is the only trick you can use to set app icon on right side.
There is no other way than set the layout to RTL or implement it yourself. Even if you subclass Toolbar and try to change onLayout, you would need to recalculate position of all views.
I just added android:layout_marginStart="300dp" in the xml where the imagebutton is declared.

How to create Settings page on Android?

I am trying to add a page in "Settings" menu option on top of the android app. The app by default had the 3 dots on top right corner and when I click on it, it doesn't go anywhere and now I am trying to implement settings to my app. What I have done so far is following,
Created xml file in /res/xml folder called preferences.xml with following content;
<?xml version="1.0" encoding="utf-8"?>
<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android">
<preference android:key="sample"
android:summary="can open other activity"
android:title="click here">
</preference>
<edittextpreference android:key="Name"
android:summary="Add user Name."
android:title="Your Name">
</edittextpreference>
</preferencescreen>
Then I have created a new java class called Preferences in /src/main/java/package/ with following content;
package com.domain.mobile.tipcalculator.app;
import android.preference.PreferenceFragment;
import android.os.Bundle;
public class Preferences extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Then to my understanding this is all I need. I m not getting any errors on compilation time but when I click on the 3 dots on top right and click on "Settings" I still don't see the page come up. Any ideas? Thank you.
P.S. I am using PreferenceFragment because my min sdk requirement is 11.
To use menus like that, you have to create a menu xml file then inflate it and then switch between the menu item ids so that you can handle click events like the following:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.preferences:
startActivity(new Intent(this, PreferenceActivity.class));
return true;
case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You should be able to achieve what you need this way and I hope this helped.

android actionbar for multiple activities

I have application that have multiple screens ( ViewPager, Activities ).
My question is how can I display different action bar in each of this screens?
multiple xml files ?
one xml file and add / replace existing one ?
Thanks
If you don't want to change theme and you only need different action item for each activity you can do it simply by inflating different menu item for each activity
create new file under src/menu/activity_one.xml (if menu directory does not exist create one)
and put menu items on it. here is an example
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_flip"
android:title="#string/flip"
android:showAsAction="always"/>
</menu>
Then in ActivityOne add this code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_one,menu);
return super.onCreateOptionsMenu(menu);
}
This way you inflated that especific menu items for ActivityOne. If you have another Activity lets say ActivityTwo just inflate different menu file!
And here is how you can define witch action selected by user, just in case!
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_flip:
flipCard("left");
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Android Actionbar with Tabs

I am very new to Android and Java programming. What I am trying to do is to get Actionbar with couple of tabs (11 in final). I have managed to install ActionBar Sherlock but then I am stuck with adding tabs to actionbar. From what I read it looks that also I would need to add separate Fragment for each tab. Is there a simple solution how to make that work.
You don't need to have a fragment per tab in the action bar. It should just be as simple as defining your menu in xml, then in your activity initialising the action bar with your menu.
For instance your menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/my_menu_item"
android:showAsAction="always|withText"
android:title="Tab name here"/>
</menu>
Then in your activity override onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Then you'll need to override onOptionItemSelected to respond to when a tab is pressed:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_menu_item:
//do something
}
return super.onOptionsItemSelected(item);
}

Categories

Resources