Why doesn't my Send button work? - android

I've created my first app, step by step, as described on
http://developer.android.com/training/basics/firstapp/starting-activity.html
According to this page, after clicking the button, there should appear a default "Hello world" layout, respectively the message typed in the text field.
Unfortunately, none of them appears at all. Absolutely nothing happens. Why?
Thanks in advance...

I suggest using setOnclicklistener. It seems you are new in coding. Google provide a good beginner guide. But still confusing a lot. take a look at this code.
first You need to locate your curret Button.
Button b1=(Button) findViewById(R.id.send);
assuming that your button hase a xml name"Send"
Now starting activity by clicking button
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
Intent in=new Intent(MainActivity.this,secondActivity.class);
startActivity(in);
}
});
}
Assuming that "secondactivity" is your target activity which you want to open by clicking button. Another Putextra and getextra method will be same as described in tutorial. Note this is an alternative method which i have described but you can still use that one.
I suggest that if you are a beginner you should start with youtube or lynda.com video tutorial. Rest is upon you

Have you edited the AndroidManifest.xml file.You should make sure that the following code is in your manifest file:
<activity
android:name="your package name.your main activity name"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>

Related

creating android app shortcut on the home screen

I need to add a shortcut for my app on the home screen (programmatically).
I know that the app store do this by default, but for start, the app won't be on the google app store.
I searched a lot, and found basically the same lines of code over and over, and it doesn't seem to work for me.
the code I used:
in the manifest:
<activity android:name=".MainScreenActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
in the onCreate method I called the function that does the following:
private boolean createShortcut()
{
//create shortcut intent
Intent shortcutIntent = new Intent(getApplicationContext(),MainScreenActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
//create intent to add and define the shortcut
Intent addingIntent = new Intent();
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"SenseGuard");
addingIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.peak_detection_icon));
addingIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addingIntent);
}
I tried switching "getApplicationContext()" to "this".
I tried with an actual tablet and on an emulator but I can't get it to work.
Do like This:
Step 1:
Update your manifest.xml :
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Step 2:
in your MainActivity.java create addShortcut() method and in it`s block put this code :
private void addShourcut(){
Intent shortCutIntent = new Intent(getApplicationContext() ,MainActivity.class);
shortCutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT , shortCutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME , "Convertor");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE ,
Intent.ShortcutIconResource.fromContext(getApplicationContext() , R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate" , false);
getApplicationContext().sendBroadcast(addIntent);
}
Step3:
set onClickListener for your view that be create shortcut :
img_pin = (ImageView) findViewById(R.id.img_pin);
img_pin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addShourcut();
Toast.makeText(MainActivity.this, "shortcut created !", Toast.LENGTH_SHORT).show();
}
});
This is worked for me ...
happy codinngggg...:)
That code isn't guaranteed to work. That broadcast is also sent by ShortcutManagerCompat (which you should probably be using instead of manually sending the broadcast).
However, there are two problems with this.
Your default launcher isn't guaranteed to listen for this broadcast. Nova Launcher, for example, has this behavior disabled by default. Other launchers might not even listen for that action at all.
On Android Oreo (26) and above, this won't work how you expect it to (read the comments on the method I linked for more details).
You can use this logic still and hope that it works for some of your users, but keep in mind that many default launchers no longer even have app drawers, so adding a shortcut could give your users duplicate icons. Also, I know that, at least for me, I have my home screen organized how I want, and if I install an app, it would be really annoying for it to add itself to my home screen.
If you are using the default AOSP launcher (or a close fork), however, and it isn't working, make sure you add this to your manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Unfortunately, <application name> has stopped. (Android)

I am learning android, I am trying to develop game, I have two classes "Starter" and "Board". Starter class contains menu(http://postimg.org/image/dnyvoey2l/). Its Exit and Help buttons are working properly, but when I press "Two Player" option instead of showing board it shows an error (Unfortunately, (Application Name) has stopped). I am sharing code snippet , please suggest solution.
twop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent tow= new Intent(Starter.this, Selector.class);
startActivity(tow);
}
});
You can't show View using startActivity() method
Intent tow= new Intent(Starter.this, Board.class);
startActivity(tow);
Board should be extended from Activity not from View class. Create an BoardActivity.java and extend it from Activity.
You should then add Board View either from XML or programmatically using setContentView(); in your onCreate() method.
Edit
Don't forget to add new Activity in your Manifest.xml file. Like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.application.package.name">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Starter" android:label="#string/app_name"></activity>
<activity android:name=".Selector"></activity>
</application>
</manifest>
Below is my idea.
The code of OnClick means starting a new activity.
Intent tow= new Intent(Starter.this, Board.class);
startActivity(tow);
However, the Board is a View, not an activity.
So, you should make Board inherit from Activity (or create other activity to hold the Board).
When creating any activity, make sure to register in manifest.

Must every activity have a layout?

I'm just getting started with Android and was reading up BroadcastReceiver. Since the MainActivity was being used only to get the alarm time in seconds, it got me thinking whether layout XML files are must for every activity in Android. I mean, is it possible to have an app that when launched, shows no view, but successfully sets up a receiver?
The answer is yes it's possible. Activities don't have to have a UI. It's mentioned in the documentation, e.g.:
An activity is a single, focused thing that the user can do. Almost
all activities interact with the user [...]
(see http://developer.android.com/reference/android/app/Activity.html)
Related SO question: https://stackoverflow.com/a/12817384/534471
To e.g. display a Toast from an Activity without layout you would define the activity in your manifest like so:
<activity
android:name=".MainActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The code would look like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "I'm alive", Toast.LENGTH_LONG).show();
finish();
}
}
You can implement an Activity without a UI. In the manifest you can specify android:theme="#android:style/Theme.NoDisplay". Take a look at this
You can also implement a Service which does not have any UI so you do not need layout inflation. Service just runs in background and shows no views.
Take a look at Android Training and API Guide to learn more about Services

Wikitude personalize view on Android

I am creating an app using Wikitude API, but I haven't been able to customize the view.
I have asked the developers and I know I can't add buttons to the main view in the current release version (for Android), but I am wondering if I can add more buttons to the options menu. Right now when I press it I get just one button that says "Ignore Altitude" can I modify that button and/or add more buttons to that menu?
I have checked other posts but there aren't any answers. The posts are a little bit old so that is why I am asking again.
I haven't found any useful documentation.
Any help is greatly appreciated
if I understood your question correctly, then please try the following: in the method prepareIntent() of your main Activity, you can add up to 3 menu items:
intent.setMenuItem1("Menu 1", YourActivity.CALLBACK_INTENT);
intent.setMenuItem2("Menu 2", YourActivity.ANOTHERCALLBACK_INTENT);
Then you define the callback function as another activity (with dialog, list and stuffs). It works fine this way for me.
I am also playing around a bit with Wikitude, but hard to find something well documented!
Yes , You can Add upto three menu button as if you read doc properly
intent.setMenuItem1("Menu Name", YourActivity.LOCATION_INTENT);
intent.setMenuItem2("Menu Name", YourActivity.LOCATION_THREATS);
intent.setMenuItem3("Menu Name", YourActivity.MAP_INTENT);
With making Intent variable as
public static final String LOCATION_INTENT = "wikitudeapi.mylocationactivity";
Also declare action in manifest as,
<activity android:name=".activities.Your Activity Name"
android:theme="#*android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="wikitudeapi.mylocationactivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Getting error when changing from main to other activity - Android

I have an application using the Google maps - until the moment it works fine. But now when I want to click a button in order to add functionality over the map I have problems.
I managed to visualise the button on the screen, also it works on click - it shows a toast correctly. But my aim is to start a new activity (having his own layout) - looking and reading tones of tutorials and stuff here is what I have :
//the Add Button in the upper right corner
Button addBookmark = (Button) findViewById(R.id.Button);
addBookmark.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View mapView) {
Intent addBookmarkIntent = new Intent(GoogleMapsApp.this, LocationBookmaker.class);
startActivity(addBookmarkIntent);
}
});
Also I've edited the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<activity android:name=".LocationBookmarker"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
...
</manifest>
No matter what I try I always get the "The Application GoogleMapsApp (process google.maps.app) has stopped unexpectedly. Please try again." with the only "Force close" option.
I've been trying since two days now - and in a lot of examples in the Internet other say it should be working like this. I cannot see where could be my mistake.
Maybe in the starting of the intent, or the manifest or where...?
According to the exception, the class it's looking for is 'LocationBookmaker', but in your manifest you have 'LocationBookmarker' (notice the 'r'). That may be your problem.
I don't know what the problem you get is but a tip is to run the "Dalvik Debug Monitor" (ddms) on your computer, with that you can capture all exceptions in your application and see exaclly what the error is (most of the time).
You find the ddms in the tools directory of your android installation, if you run windows its a bat file, ddms.bat, that you just run from cmd.
/Viktor

Categories

Resources