I'm creating an app for Android and I wanted to make items in the context menu.
This wasn't a Problem and they were shown. But when I click on them, nothing happens.
I configured the things I needed to configure, but I really am not able to find the Problem. Do you see something? Here my complete code of the Main-Java-Code.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("ABC");}
#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, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem about) {
//respond to menu item selection
switch (R.menu.optionsmenu) {
case R.id.about:
startActivity(new Intent(this, SecondActivity.class));
return true;
case R.id.download:
startActivity(new Intent(this, DownloadActivity.class));
return true;
case R.id.impressum:
startActivity(new Intent(this, ImpressumActivity.class));
case R.id.license:
startActivity(new Intent(this, LicenseActivity.class));
}
return false;
}
I want them to Show the Activitys, but nothing happens.
Thanks for your help
Thanks to Phil, the item selection is now working. Here my other codes, the app breaks down every time I select the others.
Here is LicenseActivity:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
/**
* Created by Florent on 16.08.13.
*/
public class LicenseActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView = (WebView) findViewById(R.id.licenseview);
myWebView.loadUrl("URL");
}
}
Second Activity is only a design activity, the other activitys are the sames as the License Activity.
I see your misstake. You need to get the menuitems Id inside the switch using MenuItem.getItemId(), and return return super.onOptionsItemSelected();:
public boolean onOptionsItemSelected(MenuItem about) {
//respond to menu item selection
switch (about.getItemId()) { // call this here
case R.id.about:
startActivity(new Intent(this, SecondActivity.class));
return true;
case R.id.download:
startActivity(new Intent(this, DownloadActivity.class));
return true;
case R.id.impressum:
startActivity(new Intent(this, ImpressumActivity.class));
case R.id.license:
startActivity(new Intent(this, LicenseActivity.class));
}
return super.onOptionsItemSelected(about); // return this instead of false
}
Also, do not forget to register your Activities inside your Manifest file.
And make sure you are calling setContentView(...) inside your Activity's onCreate() method.
public class LicenseActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whateveryourlayoutis); // DONT FORGET THIS
WebView myWebView = (WebView) findViewById(R.id.licenseview);
myWebView.loadUrl("URL");
}
}
Related
I have create a menu with couple options, but I want to get a separate page when I click on them in MainActivity. As in: if I click on Movies, I would have other page showing lists of movies and so on.
Which functions should I call for that?
package com.example.popupmenu.popupmenu;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements PopupMenu.OnMenuItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
public void showPopUp(View v){
PopupMenu popup=new PopupMenu(this,v);
popup.setOnMenuItemClickListener(MainActivity.this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu. main_menu, popup.getMenu() );
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.Movies:
Toast.makeText(getApplicationContext(), "movies Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.MeloDramas:
Toast.makeText(getApplicationContext(), "Melodramas Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.Songs:
Toast.makeText(getApplicationContext(), "Songs Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
//return false;
}
}
On Your menu Click do this thing to StartActivity.
case R.id.Movies:
Intent launchNewIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(launchNewIntent);
return true;
this will open new Activity.
Create a movie list activity. Then in your menuitemclick function in MainActivity, call intent to MovieActivity.
For eg;
Intent intent = new Intent(MainActivity.this, MovieActivity.class);
startActivity(intent);
You can start a new activity by using startActivity. To learn more about starting activities please visit this link.
Intent intent = new Intent(MainActivity.this, MovieActivity.class);
startActivity(intent);
first of all you didn't called showPopUp(View v) you need to call that for inflating menu. and if you want regular menu then why you are using popup menu.. just use this for inflating menu...
#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_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Movies) {
Intent i = new Intent(MainActivity.this,SplashScreen.class);
startActivity(i);
return true;
}
if (id == R.id.MeloDramas) {
Intent i = new Intent(MainActivity.this,melodramas.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
In this case you don't need to call any method just put these two method..
i would advise you to use fragments based on MenuItem selected
public boolean onMenuItemClick(MenuItem item)
I'm trying to create the Action Bar by following the Android Studio tutorial. I am familiar with VBA, C++, and Python, but this is the first time I've ventured into an App, which uses java, xml, etc. in one. I say that because I think I've misplaced (or misnamed) something in my code, as when I try to run this, I get the error error: duplicate class: com.example.batman.myfirstapp.MyActivity.
I don't understand how it's a duplicate, I only have one "MyActivity.java", so am thinking that I can't do more than one public class MyActivity extends ... per .java file?
Here's what I have (please let me know if I need to include other code):
MyActivity.java
package com.example.batman.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.support.v7.app.*;
public class MyActivity extends ActionBarActivity{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.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.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";
/**
* Called when the user clicks the Send button
*/
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myfirstapp="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
myfirstapp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
myfirstapp:showAsAction="never" />
</menu>
Again, I'm not sure what other files (strings.xml, DisplayMessageActivity.java, etc.) would be helpful to debug the above, so let me know what else I can include.
Thank you for any advice/help!
You're intuition is correct! You can only have one MyActivity class in your file, here's what it might look like:
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Your assumption is correct. In your .java file you have two classes named MyActivity. You can only have one.
I'm using Studio 0.8.9
I have an android-sdks\extras\android\support\v4\android-support-v4.jar
but I still run into the import issue.
I'm using NavUtils.navigateupfromsametask method.
what I'm trying to achieve is simply to use the back button to get to my previous activity.
is it the right way of doing?
when I'm using my email app (for ex yahoo) I'm using the back button to get back to my inbox when I'm viewing an email.
how do they do it? using activities and the back button?
I'm using the standard code:
package com.example.bernard.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.NavigableMap;
import android.support.v4.app.NavUtils;
public class Statistic extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistic);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.statistic, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id)
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed(){
super.onBackPressed();
}
}
I am using actionbar compat in my application first time. I want to add another activity to the application. When i press a SETTINGS from actionbar, i like to start the second activity.
How is possible ?
codes for my application as follows
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.action_settings:
Toast.makeText(getBaseContext(), "You selected Settings", Toast.LENGTH_SHORT).show();
startActivity( new Intent().setClass(this, SecondActivity.class));
break;
}
return true;
}
}
mainactivity image
Secondactivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
// TODO Auto-generated method stub
}
}
secondactivity image
thanks in advance
Just use a
Intent intent = new Intent(this, YourActivityToStart.class);
startActivity(intent);
on the position where you pop up a Toast.
Using 'app' instead of 'android' namespace prefix for 'actionLayout' attribute fixed it for me.
So, basically, use
app:actionLayout="#layout/yourLayout"
instead of
android:actionLayout="#layout/yourLayout"
I am assuming you are using app_compat library to get the theming engine.
I've been racking my brains over this for ages, but think I'm too close to the woods to see the trees. Can anyone tell me why item1 in the menu below doesn't work to refresh the webview?
(The exit button (item2) works just fine, if that matters.)
package com.my.project;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
public class MyProjectActivity extends Activity
{
final Activity activity = this;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
WebView WebView = (WebView) findViewById(R.id.webview);
WebView.getSettings().setJavaScriptEnabled(true);
WebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
WebView.loadUrl("http://www.mydomain.php");
}
// Create Menu Buttons
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
// Set Menu Button Actions
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
reload();
return true;
case R.id.item2:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
try this just minor changes:::
public class MyProjectActivity extends Activity
{
final Activity activity = this;
WebView webView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl("http://www.mydomain.php");
}
// Create Menu Buttons
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
// Set Menu Button Actions
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
webView.reload();
break;
case R.id.item2:
finish();
break
}
return super.onOptionsItemSelected(item);
}
}