Linking to website and ending URL with my textview - android

Eg. "http://www.john.com/" + "textview content from activity"
I've tried a lot of different coding, but I can't make it work. The closest I come finding the solution is a code for Google Search Query, and I've tried to modificate the code for my needs, but it doesn't work.
I have a textview named "article" which value is fetched from a database. Let's say that the output value becomes "4545", so when I click a button I want the browser to open and start at this URL "http://www.john.com/4545".
I would really appriciate the help! Thanks in advance!
MainActivity.java
package com.example.lindstreamerss.androidsqlitesearch;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textViewInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInput = (TextView) findViewById(R.id.article);
public void onSearchClick(View v) {
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = textViewInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}
}
}
layout_item.xml
<TextView
android:id="#+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="28dp"
android:fontFamily="#font/regular"
android:gravity="center_vertical|start"
android:text="4545"
android:textAllCaps="false"
android:textSize="19sp">
<requestFocus />
</TextView>
<Button
android:id="#+id/imageButton"
android:background="#color/white"
android:fontFamily="#font/regular"
android:drawableLeft="#drawable/ic_web"
android:paddingLeft="25dp"
android:textSize="15sp"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onSearchClick"
android:paddingRight="40dp"
android:text="Visa artikel" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lindstreamerss.androidsqlitesearch">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name="com.example.lindstreamerss.androidsqlitesearch.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

This simply works, on button click perform below code, and make necessary change to make it compatible with you.
String term = textViewInput.getText().toString();
String myUrl = "http://www.john.com/4545"+term;
String url = "https://developer.android.com/"+"jetpack";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

Solution if you want to import from other XML:
View inflatedView = getLayoutInflater().inflate(R.layout.YOURLAYOUT, null);
TextView text = (TextView) inflatedView.findViewById(R.id.YOURID); String
escapedQuery = text.getText().toString(); Uri uri =
Uri.parse("https://www.YOURWEBSITE.com/" + escapedQuery); Intent intent = new
Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
Solution if you're using a ViewHolder (in my case RecyclerView:
String example = ((TextView) recyclerView.findViewHolderForAdapterPosition(0).itemView.findViewById(R.id.YOURID)).getText().toString();
Uri uri = Uri.parse("https://www.YOURWEBSITE.com/" + example);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
Happy coding ;)

Related

Button click not moving to the new activity

On pressing the send button in my code, it must move to the new activity which i created. But this is not taking place. I have the code below.
Please help. I am just a beginner in android studio.
.xml file(layout)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myfirstapp.MainActivity"
android:id="#+id/relativeLayout"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="#+id/editText3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="0dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="#+id/button"
android:layout_width="85dp"
android:layout_height="42dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="#string/button_send" />
main activity
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
Please help in resolving the issue. I am new to Android Studio
You have correct code, only thing you are missing is to call the startActivity call.
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
You forgot to call this...
startActivity(intent);
Forget to call
startActivity(intent);
full code should be:
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
here is the Official Documentation
It was a mistake on my part.
I didnt write the statement to invoke the intent.
startActivity(intent);
I know this was just silly on my part. But these mistakes can happen to anyone, I hope this helps other people.
Full Code
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText3);
String message=editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);

Why placing a call programatically gives error message "Internet Calling not Supported"?

This is my code:
public String a_number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_the_call);
//
callButton = (ImageButton)findViewById(R.id.call_button);
aCall = (TextView)findViewById(R.id.number_a);
a_number = aCall.getText().toString();
}
public void makeCallFunction(View view) {
String temp = "";
temp = "tel:"+a_number;
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(temp));
startActivity(callIntent);
}
My XML file contains:
<ImageButton
android:id="#+id/call_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#drawable/dial"
android:onClick="makeCallFunction"/>
I have added the following in my manifest file:
<uses-permission android:name="android.permission.CALL_PHONE"/>
I searched my best for answers but found nothing helpful..
EDIT:
When I give a value directly, the call gets placed now.
Eg:
callIntent.setData(Uri.parse("tel:9876543210"));
But the problem remains when I read the number from my text view and try to place call with that number.
Can someone help me out?
Thanks in advance
If there's some alternate way to implement call, that'd help too.
Disable SIP Internet Calling and VoIP on your emulator or debugging device. This is the steps for Samsung Galaxy S4 :
go to settings > call > disable internet calling(in bottom)
I don't know what you are doing wrong, but see for yourself a working example :
MakeTheCallActivity.java
package com.example.makethecall;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MakeTheCallActivity extends ActionBarActivity {
private EditText mEditText;
private Button mButton;
private final String TAG = "MakeTheCallActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_the_call);
mButton = (Button)findViewById(R.id.call_button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mEditText = (EditText)findViewById(R.id.phone_number);
String phoneNumber = mEditText.getText().toString();
Log.d(TAG, phoneNumber);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(intent);
}
});
}
}
activity_make_the_call.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MakeTheCallActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/phone_number" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="call"
android:id="#+id/call_button" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sauravsamamt.makethecall" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MakeTheCallActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can see I'm not using a hardcoded number. Make sure you enter a valid number. That's it.

Intent to open url results in wrong url on browser

I am trying to open a URL in a browser using an intent.
My code is
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
startActivity(i);
This will result in an error on any browser I have tried since the browser tries to open something like this:
http://http//onair99.com//
As far as I have tried, I only have encountered the problem with this specific URL.
Does anybody know why?
Thanks.
do as mentioned in this post
String url = "http://www.somewebsite.com";
In production level code, you may like to check if the url begins with http or https... Would be better to check
if (!url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Here's the documentation of Intent.ACTION_VIEW.
Try This Code
main.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Go to link" />
</LinearLayout>
MyAndroidAppActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
startActivity(browserIntent);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".MyAndroidAppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Simplify android classes

I'm quite new to android programming and I made my first project yesterday, a cocktail bible app. It runs perfectly but the problem is I'v loaded loads of classes and xml's to store the cocktails data. I was just wondering is there any simpler methods I could use to do away with all my unsuitable classes of the cocktails. Any help would be greatly appreciated.
Menu class
package com.drunktxtapp;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] = {"Bloody_Mary", "Capirinha", "Cosmopolitan", "Cuba_Libre", "Daiquiri", "Mai_Tai", "Manhattan", "Margarita", "Martini", "Mint_Julep", "Mojito", "Old_Fashoned", "Pina_Colada", "Screwdriver", "Singapore_Sling", "Tom_Collins", "Whiskey_Sour", "White_Russian"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cocktailType = classes[position];
try{
Class<?> ourClass = Class.forName("com.drunktxtapp." + cocktailType);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
Example of one of my cocktail classes. I have many of these.
package com.drunktxtapp;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Bloody_Mary extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bloody_mary);
Button b1 = (Button) findViewById(R.id.bYoutube);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=Alt-ehDc3fc")));
}
});
}
}
One of my xml's for which I have many for each cocktail.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/beer"
android:id="#+id/bloody_mary" >
<TextView
android:textStyle="bold"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Bloody Mary"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/bloodymary" />
<TextView
android:textStyle="bold"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Ingredients"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="40ml vodka \n\ 120ml tomato juice \n\ 5ml lemon or lime juice \n\ 5ml worcestershire sauce \n\ 2 dashes tabasco \n\ salt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<TextView
android:textStyle="bold"
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="Preparation"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Squeeze the liquid out of the horseradish, then shake ingredients well with cracked ice in a chilled cocktail shaker, then strain into a Collins glass with 2 or 3 ice cubes in it; add a pinch of salt and a grind or two of fresh pepper, to taste. Garnish, if necessary, with a stalk of celery."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"/>
<Button
android:id="#+id/bYoutube"
android:layout_width="200dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="YouTube Clip"
android:textSize="20dp" />
</LinearLayout>
My manifest with some parts removed
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drunktxtapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CocktailMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.CocktailMenu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Bloody_Mary"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.drunktxtapp.Bloody_Mary" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You will need just one Activity and one xml Layout for all types of cocktails. (Assume that all cocktails has same layout, just the text and image are different.)
Instead of getting the class like your current approach, you will pass a parameter in the start intent.
Intent ourIntent = new Intent(Menu.this, CocktailDetailActivity.class);
ourIntent.putExtra("whichcocktail", "bloodymary");
startActivity(ourIntent);
In the destination activity's onCreate method ( Let's say CocktailDetailActivity):
// Get the param back
String cocktail = getIntent().getStringExtra("whichcocktail");
textView1.setText(cocktail);
Now you have "bloodymary" display on your textview.
In real life, you would want to pass an index to your database / array.
Then in CocktailDetailActivity, you will get the info by this index and display the correct cocktail.
All you need is two activities (ListActivity and Activity to display Cocktail detail) & two xml layouts (one for each activity).
To work with this :
1. Put all your cocktail images in drawable folder(set the name of images same as the cocktail name)
2. Start the cocktail activity when user selects an cocktail from list using Intent and pass the name of the cocktail as extra (or any other detail).
Intent intent = new Intent(Menu.this, CocktailDetailActivity.class);
ourIntent.putExtra("cocktail_name","bloodymary");
startActivity(intent);
3. In CocktailActivity's onCreate() method get the extras from intent object
String cocktailName = getIntent().getStringExtra("cocktail_name");
textView1.setText(cocktailName);
4.To set the image use the cocktailName value (if stored with same name)
ImageView imgView1 = (ImageView)findViewById(R.id.imageView1);
imageView1.setImageDrawable(getResources().getDrawable(R.drawable.cocktailName));

Why is my android application crashing?

So I know it has to do with my "startActivity( myIntent )" line of code. Here is my first Activity file
public class Commander2 extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "AccessCodeEntry";
private static final String LEVEL_ONE_ACCESS_CODE = "derp";
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.accesscodeentry );
}
protected void onDestroy() {
super.onDestroy();
}
public void accessCodeEntered( View v ) {
EditText loginPassword = (EditText) findViewById( R.id.editText1 );
if( loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE ) ) {
Intent myIntent = new Intent( Commander2.this, LevelOne.class );
startActivity( myIntent );
} else {
Intent myIntent = new Intent( Commander2.this, LevelZero.class );
startActivity( myIntent );
}
}
}
Here is the XML file as well.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="accessCodeEntered"
android:text="OK" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText1"
android:layout_alignLeft="#+id/editText1"
android:text="Enter Access Code, or leave blank for Level 0"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
My code just exits as soon as I enter a password and press the OK button, no matter what the output. I have commented out the startActivity( myIntent ) lines and the code finishes without crashing. Does anyone know what I'm doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.eti"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:name=".Commander2"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LevelOne"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LevelZero"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT : i am updating my answer
so finally i got where you were going wrong
check your if condition
in accessCodeEntered() method
of Commander2.java Activity
if(loginPassword.toString().equals(LEVEL_ONE_ACCESS_CODE)){}
here you are not getting the value of password and try to use it
so i added "getText()" method and its working fine like this
if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){}
Commander2.java
package my.eti;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Commander2 extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String TAG = "AccessCodeEntry";
private static final String LEVEL_ONE_ACCESS_CODE = "derp";
private Button btnOK;
private EditText passTxt;
private String strPass;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accesscodeentry);
passTxt = (EditText) findViewById(R.id.editText1);
btnOK =(Button) findViewById(R.id.button1);
btnOK.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==btnOK){
/*... here is the problem we havent added getText() to get value from EditText...*/
if(loginPassword.getText().toString().equals(LEVEL_ONE_ACCESS_CODE)){
Intent myIntent = new Intent( Commander2.this, LevelOne.class );
Toast.makeText(getBaseContext(), "if condition is true..", Toast.LENGTH_SHORT).show();
startActivity( myIntent );
}
else{
Intent myIntent = new Intent( Commander2.this, LevelZero.class );
Toast.makeText(getBaseContext(), "else condition is true..", Toast.LENGTH_SHORT).show();
startActivity(myIntent);
}
}
}
}
try to implement this code and where you going wrong.
still you get any trouble share with me Thanks.
Please post your logcat errors, but my guess is to make sure that LevelOne and LevelZero are in your manifest file. Also, it might help to change this:
loginPassword.toString().equals( LEVEL_ONE_ACCESS_CODE )
to:
loginPassword.getText().toString().equals( LEVEL_ONE_ACCESS_CODE )
have you to register LevelOne and LevelZero in manifest file
Here is a link to a similar SO question that shows how to add the activities to your manifest.
Add a new activity to the AndroidManifest?

Categories

Resources