[Q] Explanation of code (super function) - Java for Android App Development

Code:
@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);
}
}
Doesn't the default case create an infinite loop if item is not action search or action settings?

saliceman said:
Code:
@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);
}
}
Doesn't the default case create an infinite loop if item is not action search or action settings?
Click to expand...
Click to collapse
super.onOptionsItemSelected will execute the onOptionsItemSelected function with parameter item in the "super" class your class extends from, eg Fragment/Activity/ActionBarActivity/FragmentActivity etc

Jonny said:
super.onOptionsItemSelected will execute the onOptionsItemSelected function with parameter item in the "super" class your class extends from, eg Fragment/Activity/ActionBarActivity/FragmentActivity etc
Click to expand...
Click to collapse
Thanks for your response.
With the code below what does that mean? and could you provide an example of what would happen if I selected an option other than search or settings? I am having trouble with what this all means in the grand scheme of things. I might be missing something basic so try to keep that in mind - I have little knowledge of java.
Again, thanks for your help
Code:
package com.salmanshahid.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp" +
".MESSAGE";
@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) {
// 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);
}
}
private void openSettings() {
// TODO Auto-generated method stub
}
private void openSearch() {
// TODO Auto-generated method stub
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
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);
}
}

saliceman said:
I have little knowledge of java.
Click to expand...
Click to collapse
No problem, you just need to know programming in general to understand.
saliceman said:
With the code below what does that mean? and could you provide an example of what would happen if I selected an option other than search or settings?
Click to expand...
Click to collapse
replace
Code:
super
with
Code:
this
and you will get infinite loop.

Related

last tempt at asking for help

Ok all i have tried for weeks now to get a simple webview app sorted. i have managed to get this up on the market but it needs a few more features like menu for refresh back forward etc.
It seems nobody really wants to helps so how much will it cost me for you to help with it?
explain a little more, you after someone to code it for you or design the ui ?
Well basically I got a basic webview made from tutorials, but that is about as far as my skills go. I need a few more features like options menu, stop the back button exiting app and go back a page instead and also if possible but not essential change loading bar for loading circle which spins in middle if screen while loading. I have tried to ask for help with what code I need and where to put it but no luck. I pretty much need it coded or at least to be told what code and exactly where in my code I need to fit it in.
Sent from my Desire HD using XDA App
Where have you tried asking? Tackle one issue at a time, and post some example code.
StackOverflow is a great site for programming questions and you should get some good answers there.
But before posting make sure you search first.
The back button shouldnt exit the app if there is a activity that was displayed before it.
cyberpedz said:
Ok finally i have a running webview in the market what i would like to do now is add a soft menu for a back, refresh and forward button what code do i need and where would i put it in my java below?
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity
{
final Activity activity = this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.windowcleaningforums.co.uk");
}
}
Click to expand...
Click to collapse
Sent from my Desire HD using XDA App
Sent from my Desire HD using XDA App
I found this tutorial after a quick google search - http://kahdev.wordpress.com/2008/11/25/building-a-menu-for-your-android-v10-r1-app/
It seems quite simple to follow. Let me know if you have any issues and I try give you an example for your code tonight.
Ok I have edited your code to add a menu, and also to set the back button of the phone to go back to the previous page in the webview, instead of previous activity.
I found the code for this here
I have commented bits of the code so hopefully its understandable.
If you have any questions or it doesn't work let me know.
Don't just take what I have done and use it (although you can). Go through it and make sure you understand what I have done. That's the only way you will learn app development.
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
final Activity activity = this;
//DECLARE webview variable outside of onCreate function so we can access it in other functions (menu)
public WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.windowcleaningforums.co.uk");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//Add menu items, second value is the id, use this in the onOptionsItemSelected
menu.add(0,1, 0, "Back");
menu.add(0, 2, 0, "Forward");
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
//If the ID equals 1 , go back
case 1:
webView.goBack();
return true;
//If the ID equals 2 , go forward
case 2 :
webView.goForward();
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I have seen these tutorials but my problem is I don't know how to fit it into my webview where should I add this in my code
Sent from my Desire HD using XDA App
Have a look at the code I posted.
What we are basically doing is adding new functions (or methods) inside our activity class. When I first started app development I also got confused as to where to add things.
These new methods override methods built into the activity, so we don't want to put them inside our onCreate method (where everything goes for setting up the activity, and where all your current code resides).
So we put them at any position after the closing brace of our onCreate. I've put them at the end.
I also moved the declaration of the webView variable to outside the onCreate method, so we can access it in the other functions we added.
Thank you so much I will look at this properly when I next get chance on my pc. thanks again for the help and taking the time to explain.
Sent from my Desire HD using XDA App
Thank you have have looked over the code and even added another button!
I have also added icons for each menu item after looking over some other code but not sure if i have done it correctly. they dont show untill the menu item has been clicked once. how can i get them to show right away? i put the images in res/drawable and called then in the code below, is this the correct way to do it?
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
final Activity activity = this;
//DECLARE webview variable outside of onCreate function so we can access it in other functions (menu)
public WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.windowcleaningforums.co.uk");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//Add menu items, second value is the id, use this in the onOptionsItemSelected
menu.add(0,1, 0, "Back");
menu.add(0, 2, 0, "Forward");
menu.add(0, 3, 0, "Refresh");
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()){
//If the ID equals 1 , go back
case 1:
webView.goBack();
item.setIcon(R.drawable.back);
return true;
//If the ID equals 2 , go forward
case 2 :
webView.goForward();
item.setIcon(R.drawable.forward);
return true;
//If the ID equals 3 , go back
case 3:
webView.reload();
item.setIcon(R.drawable.refresh);
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Comments below (marked -->), I'm not going to give you the answer, you need to understand why more that how.
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
final Activity activity = this;
//DECLARE webview variable outside of onCreate function so we can access it in other functions (menu)
public WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://www.windowcleaningforums.co.uk");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//Add menu items, second value is the id, use this in the onOptionsItemSelected
menu.add(0,1, 0, "Back");
menu.add(0, 2, 0, "Forward");
menu.add(0, 3, 0, "Refresh");
//--> Here you have finished configuring the menu
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
// -->This is called when you select/tap a menu item (look at the method name)
switch (item.getItemId()){
//If the ID equals 1 , go back
case 1:
webView.goBack();
// --> Hence this happens _after_ you have tapped on the back menu item
item.setIcon(R.drawable.back);
return true;
//If the ID equals 2 , go forward
case 2 :
webView.goForward();
item.setIcon(R.drawable.forward);
return true;
//If the ID equals 3 , go back
case 3:
webView.reload();
item.setIcon(R.drawable.refresh);
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
As SilentMobius' comments pointout, you need to set your icons in the onCreateOptionsMenu, not the onOptionsItemSelected.
That's why they are only showing when you select the button.
Ah great thanks i figured it, didn't want to be given correct code just you giving the correct info to sort my self is good i know these are most likely simple things but nobody learns unless they ask questions thanks again.
Check my apps out in the market Luvdroid and window cleaning forums
Would you mind sharing your back, forward and refresh icons with me? I'm looking for something very clean and standardized for use on 12 webview apps for the some of the local municipalities.
SilentMobius said:
Comments below (marked -->), I'm not going to give you the answer, you need to understand why more that how.
Code:
package com.windowcleaningforums;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class windowcleaningforums extends Activity {
final Activity activity = this;
//DECLARE webview variable outside of onCreate function so we can access it in other functions (menu)
public WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.WebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// Handle the error
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("m.xxx.co.jp");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//Add menu items, second value is the id, use this in the onOptionsItemSelected
menu.add(0,1, 0, "Back");
menu.add(0, 2, 0, "Forward");
menu.add(0, 3, 0, "Refresh");
//--> Here you have finished configuring the menu
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
// -->This is called when you select/tap a menu item (look at the method name)
switch (item.getItemId()){
//If the ID equals 1 , go back
case 1:
webView.goBack();
// --> Hence this happens _after_ you have tapped on the back menu item
item.setIcon(R.drawable.back);
return true;
//If the ID equals 2 , go forward
case 2 :
webView.goForward();
item.setIcon(R.drawable.forward);
return true;
//If the ID equals 3 , go back
case 3:
webView.reload();
item.setIcon(R.drawable.refresh);
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Click to expand...
Click to collapse

Switching to and from layouts using buttons

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Focusedrelaxaation87 said:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Click to expand...
Click to collapse
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Code:
[user=439709]@override[/user]
public void onClick(View v) {
switch (v.getId()){
case R.id.btnOne:
mLayoutFirst.setVisibility(View.GONE);
mLayoutSecond.setVisibility(View.VISIBLE);
break;
case (R.id.btnTwo):
mLayoutFirst.setVisibility(View.VISIBLE);
mLayoutSecond.setVisibility(View.GONE);
break;
}
}
Zatta said:
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Click to expand...
Click to collapse
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
octobclrnts said:
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
Click to expand...
Click to collapse
Thanks, never heard of before. I use that method for hiding/unhiding complete ViewGroups, Buttons and what not but I'll look into that, might be more easy.
Try naming the activity other than the "main" attribute, that is only for the first or "parent" activity. You need it to be a child activity.
Sent from my HUAWEI-M835 using xda app-developers app
Cool, learned something new

Need help with my app

Hi I'm working on a custom Android launcher I'm running into a little problem I have an image button that indicates show all applications but the problem is I don't know the code syntax to show all installed applications when that button is clicked!!
Please help I tried googling for the last four hours and getting very frustrated
Thanks in advance
Rapsong11
Sent from my Nexus 4 using xda app-developers app
Maybe you should use PackageManager to query the installed applications for you.
For example, when you want the list of launchable activities. You can do like this:
Code:
// create a intent for the query below
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
// add the intent category you want, if need
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// get the instance of the package manager
PackageManager packageManager = context.getPackageManager();
// these are the activies you want
List<ResolveInfo> activities = packageManager.queryIntentActivities(mainIntent, 0);
Hope can help you
Code:
Here is my code:
package com.d4a.SchoolLauncher;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.content.Context;
import com.d4a.SchoolLauncher.R;
public class LauncherActivity extends Activity{
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//scroller
final HorizontalScrollView scroller= (HorizontalScrollView) findViewById(R.id.scroller);
scroller.setSmoothScrollingEnabled(true);
//apps
final ImageButton apps= (ImageButton) findViewById(R.id.all_apps); //my all apps button
apps.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mainIntent, 0);
}
});
final ImageButton wbrowser= (ImageButton) findViewById(R.id.wbrowser);
wbrowser.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
final ImageButton osettings= (ImageButton) findViewById(R.id.osettings);
osettings.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
startActivity(new Intent(Settings.ACTION_SETTINGS));
}
});
//remove from home screen button
//apps
final ImageButton delapps= (ImageButton) findViewById(R.id.delapps);
delapps.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
apps.setVisibility(View.GONE);
delapps.setVisibility(View.GONE);
}
});
//web browser
final ImageButton delwbrowser= (ImageButton) findViewById(R.id.delwbrowser);
delwbrowser.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
wbrowser.setVisibility(View.GONE);
delwbrowser.setVisibility(View.GONE);
}
});
//web browser
final ImageButton delosettings= (ImageButton) findViewById(R.id.dellosettings);
delosettings.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
osettings.setVisibility(View.GONE);
delosettings.setVisibility(View.GONE);
}
});
//add button
final ImageButton addshortcut= (ImageButton) findViewById(R.id.addshortcut);
addshortcut.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
startActivity(new Intent("com.PhysicsPhantom.Launcher"));
}
});
//done button
final ImageButton done= (ImageButton) findViewById(R.id.done);
done.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
delwbrowser.setVisibility(View.GONE);
delosettings.setVisibility(View.GONE);
delapps.setVisibility(View.GONE);
done.setVisibility(View.GONE);
addshortcut.setVisibility(View.GONE);
}
});
//edit button
final ImageButton edit= (ImageButton) findViewById(R.id.edit);
edit.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
if (apps.getVisibility()==View.VISIBLE){
delapps.setVisibility(View.VISIBLE);
}
if (wbrowser.getVisibility()==View.VISIBLE){
delwbrowser.setVisibility(View.VISIBLE);
}
if (osettings.getVisibility()==View.VISIBLE){
delosettings.setVisibility(View.VISIBLE);
}
done.setVisibility(View.VISIBLE);
addshortcut.setVisibility(View.VISIBLE);
}
});
//code to open editor for all delete buttons
//apps
apps.setOnLongClickListener(new View.OnLongClickListener() {
[user=439709]@override[/user]
public boolean onLongClick(View v) {
if (delapps.getVisibility()==View.GONE){
delapps.setVisibility(View.VISIBLE);
}
else{
delapps.setVisibility(View.GONE);
}
return true;
}
});
//web browser
wbrowser.setOnLongClickListener(new View.OnLongClickListener() {
[user=439709]@override[/user]
public boolean onLongClick(View v) {
if (delwbrowser.getVisibility()==View.GONE){
delwbrowser.setVisibility(View.VISIBLE);
}
else{
delwbrowser.setVisibility(View.GONE);
}
return true;
}
});
//settings
osettings.setOnLongClickListener(new View.OnLongClickListener() {
[user=439709]@override[/user]
public boolean onLongClick(View v) {
if (delosettings.getVisibility()==View.GONE){
delosettings.setVisibility(View.VISIBLE);
scroller.fullScroll(ScrollView.FOCUS_RIGHT);
}
else{
delosettings.setVisibility(View.GONE);
}
return true;
}
});
}
}
I get a error at: PackageManager packageManager = context.getPackageManager();
The error is as follows: context cannot be resolved
please help!!
[ code] tags would be helpful
out of ideas said:
[ code] tags would be helpful
Click to expand...
Click to collapse
just added code tags
sorry about that lol
You dont have startActivity in your launcher button
Can you please give me a code example fairly new to this
Sent from my Nexus 4 using xda app-developers app
Well, if you just want a activity list and launches when clicked, you can simply create a LauncherActivity.
Can you please show me using my code example where I would place that at? Thank you so much
Rapsong11
Sent from my Nexus 4 using xda app-developers app
rapsong11 said:
Can you please show me using my code example where I would place that at? Thank you so much
Rapsong11
Sent from my Nexus 4 using xda app-developers app
Click to expand...
Click to collapse
1. create new class MyLauncherActivity
2. override the method "getTargetIntent()" in LauncherActivity
Code:
...
import android.app.LauncherActivity;
public class MyLauncherActivity extends LauncherActivity {
 [user=439709]@override[/user]
protected Intent getTargetIntent () {
// just a example, you should replace the method yourself
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
}
3. replace the code inside your onClick method to the R.id.all_apps button :
Code:
Intent intent = new Intent(this, MyLauncherActivity.class);
startActivity(intent);
4. don't forget to declare MyLauncherActivity in your AndroidMenifest.xml
5. try to do it yourself with others' experience, but not just ask for the code, that's not good for a coder. Wish you can understand this.

How to use Navigation Drawer inside Tabs menu.

Hi guys.
I've been developing an app which has Swipeable menu in bottom and one of the pages , should have a Navigation Drawer to change its own Fragments.
MainActivity.java
Code:
package com.example.elizehprotowithshlkandvpis;
import com.example.elizehprotowithshlkandvpis.adapter.MainMenuAdapter;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private ViewPager mViewPager;
private FragmentPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MainMenuAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
}
@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;
}
public static String[] getCONTENT() {
return CONTENT;
}
}
MainMenuAdapter.java
Code:
package com.example.elizehprotowithshlkandvpis.adapter;
import com.example.elizehprotowithshlkandvpis.MainActivity;
import com.example.menuclasses.AboutUs;
import com.example.menuclasses.CustomerClub;
import com.example.menuclasses.Maps;
import com.example.menuclasses.Neccesseries;
import com.example.menuclasses.Tours;
import com.example.menuclasses.UrgentCall;
import com.example.menuclasses.Resturants;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MainMenuAdapter extends FragmentPagerAdapter {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private final int HOME_INDEX = 0;
private final int ABOUTUS_INDEX = 1;
private final int MAPS_INDEX = 2;
private final int RESTURANTS_INDEX = 3;
private final int TOURS_INDEX = 4;
private final int CUSTOMERCLUB_INDEX = 5;
private final int NECCESSERIES_INDEX = 6;
private final int URGENTCALL_INDEX = 7;
private final int NUMBEROFMENUS = CONTENT.length;
public MainMenuAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public CharSequence getPageTitle(int position) {
String[] CONTENT = MainActivity.getCONTENT();
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override
public Fragment getItem(int index) {
switch(index) {
case HOME_INDEX:
return new AboutUs();
case ABOUTUS_INDEX:
return new AboutUs();
case MAPS_INDEX:
return new Maps();
case RESTURANTS_INDEX:
return new Resturants();
case TOURS_INDEX:
return new Tours();
case CUSTOMERCLUB_INDEX:
return new CustomerClub();
case NECCESSERIES_INDEX:
return new Neccesseries();
case URGENTCALL_INDEX:
return new UrgentCall();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return NUMBEROFMENUS;
}
}
The Fragment that should contain a Navigation Drawer is Resturants , also drawer's mainactivity is implemented with Sherlock Library(SherlockFragmentActivity).
I'm puzzled how to call the SherlockFragmentActivity from the SherlockActivity
Thanks
Pouya_am said:
Hi guys.
I've been developing an app which has Swipeable menu in bottom and one of the pages , should have a Navigation Drawer to change its own Fragments.
MainActivity.java
Code:
package com.example.elizehprotowithshlkandvpis;
import com.example.elizehprotowithshlkandvpis.adapter.MainMenuAdapter;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private ViewPager mViewPager;
private FragmentPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MainMenuAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
}
@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;
}
public static String[] getCONTENT() {
return CONTENT;
}
}
MainMenuAdapter.java
Code:
package com.example.elizehprotowithshlkandvpis.adapter;
import com.example.elizehprotowithshlkandvpis.MainActivity;
import com.example.menuclasses.AboutUs;
import com.example.menuclasses.CustomerClub;
import com.example.menuclasses.Maps;
import com.example.menuclasses.Neccesseries;
import com.example.menuclasses.Tours;
import com.example.menuclasses.UrgentCall;
import com.example.menuclasses.Resturants;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MainMenuAdapter extends FragmentPagerAdapter {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private final int HOME_INDEX = 0;
private final int ABOUTUS_INDEX = 1;
private final int MAPS_INDEX = 2;
private final int RESTURANTS_INDEX = 3;
private final int TOURS_INDEX = 4;
private final int CUSTOMERCLUB_INDEX = 5;
private final int NECCESSERIES_INDEX = 6;
private final int URGENTCALL_INDEX = 7;
private final int NUMBEROFMENUS = CONTENT.length;
public MainMenuAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public CharSequence getPageTitle(int position) {
String[] CONTENT = MainActivity.getCONTENT();
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override
public Fragment getItem(int index) {
switch(index) {
case HOME_INDEX:
return new AboutUs();
case ABOUTUS_INDEX:
return new AboutUs();
case MAPS_INDEX:
return new Maps();
case RESTURANTS_INDEX:
return new Resturants();
case TOURS_INDEX:
return new Tours();
case CUSTOMERCLUB_INDEX:
return new CustomerClub();
case NECCESSERIES_INDEX:
return new Neccesseries();
case URGENTCALL_INDEX:
return new UrgentCall();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return NUMBEROFMENUS;
}
}
The Fragment that should contain a Navigation Drawer is Resturants , also drawer's mainactivity is implemented with Sherlock Library(SherlockFragmentActivity).
I'm puzzled how to call the SherlockFragmentActivity from the SherlockActivity
Thanks
Click to expand...
Click to collapse
That last sentence does not make sense I assume you mean from the Fragment?! Well to access the holding activity you need to add an interface to the Fragment which the activity will implement.
Am I understanding you correctly that have a horizontal ViewPager and one of the Fragments should have a Navigation drawer? To accomplish that you would still need the whole activity to hold the drawer and figure out a way to disable it when the user is on some other Fragment. But my question is why you want to use a navigation drawer in that case, because the drawer is meant for navigating on the same hierarchical level, but the different items are more independent than with the ViewPager. It doesn't really make sense to have both a drawer and horizontal swiping, even the YouTube app really bothers me with its swiping between suggestions and feed. I think what you need to use here is a spinner in the ActionBar, replacing the title of the Fragment.
SimplicityApks said:
That last sentence does not make sense I assume you mean from the Fragment?! Well to access the holding activity you need to add an interface to the Fragment which the activity will implement.
Am I understanding you correctly that have a horizontal ViewPager and one of the Fragments should have a Navigation drawer? To accomplish that you would still need the whole activity to hold the drawer and figure out a way to disable it when the user is on some other Fragment. But my question is why you want to use a navigation drawer in that case, because the drawer is meant for navigating on the same hierarchical level, but the different items are more independent than with the ViewPager. It doesn't really make sense to have both a drawer and horizontal swiping, even the YouTube app really bothers me with its swiping between suggestions and feed. I think what you need to use here is a spinner in the ActionBar, replacing the title of the Fragment.
Click to expand...
Click to collapse
Yeah you're right , I just want to use the drawer in order to change contents.
So as you suggest , I just have to use a simple tricks to make the drawer enable in desire Fragment.
I don't want to , I am asked to make it possible....

[Q] Open another screen on button click

Hello, I'm extremely new to both android application developing, and this forum.
I am trying to open another screen ( activity ) when a button ( readyButton ) on the splash screen is pressed. I've tried at least ten different times with different tutorials to no avail, this is my current code which didn't work, and instead forces the app to crash.
Code:
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
Please help.
The button's name is 'readybutton'
the second activity name is 'SecondActivity'
also, where am I supposed to put this code into the java class? Here is how it is currently set up:
Code:
package com.unchat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
// Default Items
public class FirstActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/** New button code start */
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
/** new button code end */
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
[user=439709]@override[/user]
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);
}
}
// End of Default Items
incorrectly announces intent
Try like this.
Code:
Intent intent = new Intent(this, SecondActivity.class) ;
startActivity(intent);
and check whether your Listener
1. you need to use the full name of your activity, including the package name.
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
rhmsoft said:
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
Click to expand...
Click to collapse
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
bornander said:
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
Click to expand...
Click to collapse
I thought you had to declare all your activities in the manifest?
Log
Post the error log please

Categories

Resources