Trying an intent and crash the app - android

I'm trying to create an intent that connect a button with a class (definition of intent). The problem is that, when I press this button, everything crash. I think that the problem is in the intent class. Can you see what's wrong? I'm trying to open an alert dialog when button bet is pressed. I have already create it:
Chips class, the class aI want to use after the button is pressed:
public class Chips extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
private AlertDialog.Builder dialog;
public void scommessa () {
dialog = new AlertDialog.Builder(this);
final EditText txtInput = new EditText (this);
final String stringInput = txtInput.getText().toString();
dialog.setTitle("Welcome to poker");
dialog.setMessage("Player 2, place your bet!");
dialog.setView(txtInput);
dialog.setPositiveButton("Bet", new Dialog.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
int bet1 = 100;
int intInput = Integer.parseInt(stringInput);
int newPlayer1Bet = bet1 - intInput;
String total1 = "" + newPlayer1Bet;
}
});
dialog.setNegativeButton("Pass", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which){
Toast.makeText(getApplicationContext(), "Player 2 won", Toast.LENGTH_SHORT).show();;
}
});
AlertDialog dialogPoker = dialog.create();
dialogPoker.show();
}
public void onClick(View v){
switch (v.getId()){
case R.id.Cover1:
scommessa();
break;
}
}
}
Then here is the class where I defined the button and then try to call the class Chips:
final Button Cover1 = (Button) findViewById(R.id.Cover1);
final Button button1 = (Button) findViewById(R.id.A);
final Button button2 = (Button) findViewById(R.id.B);
final Button button3 = (Button) findViewById(R.id.C);
final Button button4 = (Button) findViewById(R.id.D);
final Button button5 = (Button) findViewById(R.id.E);
final Button Bet = (Button) findViewById(R.id.button1);
Cover1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
if(v.equals(Cover1)){
button1.setBackgroundResource(R.drawable.back);
button2.setBackgroundResource(R.drawable.back);
button3.setBackgroundResource(R.drawable.back);
button4.setBackgroundResource(R.drawable.back);
button5.setBackgroundResource(R.drawable.back);
}
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View x){
if(x.equals(Bet)){
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
}
Intent intent1 = new Intent(GameActivity.this, Chips.class);
//startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
}
});
Here is the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pokeroriginal"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.pokeroriginal.FullscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="GameActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.pokeroriginal.GameActivity"
android:label="#string/title_activity_game"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name= "com.example.pokeroriginal.Chips"
android:label= "#string/Bet"
android:screenOrientation= "landscape"
android:theme= "#android:style/Theme.NoTitleBar">
</activity>
</application>
</manifest>
Also the activity_game.xml where all the buttons are defined:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/green_table_cloth"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".GameActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal" >
<Button
android:id="#+id/Cover1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cover"
android:onClick="onClickChips" />
<Button
android:id="#+id/A"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/B"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/C"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/D"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</View>
<Button
android:id="#+id/E"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:onClick="onClick"
android:background="#drawable/back"
/>
<View
android:layout_width="0dp"
android:layout_height="0dp" >
</View>
</LinearLayout>
<ImageSwitcher
android:id="#+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick(View x)"
android:text="#string/Bet" />
<Button
android:id="#+id/Button01"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Bet" />
</ImageSwitcher>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="horizontal" >
<Button
android:id="#+id/Cover2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cover" />
<Button
android:id="#+id/A2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="#+id/B2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_weight="1" />
<Button
android:id="#+id/C2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="#+id/D2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="#+id/E2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/back2" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<!-- <ImageView
android:id="#+id/Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView> '
-->
</LinearLayout>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Bet" />
</LinearLayout>

It's difficult to determine what the error is without seeing the logcat - you should really include it in the future.
Looking at your code, I notice this in the button1 OnClickListener:
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
Chips chip = new Chips();
chip.scommessa();
To start another activity, all you need to do is this:
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
I believe that trying to instantiate the Activity using it's constructor is causing problems. When you do this, the Activity's Views have not been setup properly, and when you try to show a dialog, you will encounter errors.
Try removing these lines:
Chips chip = new Chips();
chip.scommessa();
Your OnClickListener should look like this:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View x) {
if (x.equals(Bet)) {
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
}
});

I think you're getting NullPointerException here.
if(x.equals(Bet))
and your xml onClick() implemented wrongly.
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick(View x)"
android:text="#string/Bet" />
java code should be like this,
final Button Cover1 = (Button) findViewById(R.id.Cover1);
final Button button1 = (Button) findViewById(R.id.A);
final Button button2 = (Button) findViewById(R.id.B);
final Button button3 = (Button) findViewById(R.id.C);
final Button button4 = (Button) findViewById(R.id.D);
final Button button5 = (Button) findViewById(R.id.E);
final Button Bet = (Button) findViewById(R.id.button1);
Cover1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
button1.setBackgroundResource(R.drawable.back);
button2.setBackgroundResource(R.drawable.back);
button3.setBackgroundResource(R.drawable.back);
button4.setBackgroundResource(R.drawable.back);
button5.setBackgroundResource(R.drawable.back);
}
});
Bet.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Chips chip = new Chips();
chip.scommessa();
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}
});
Or if you're xml implementation should be like this.
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="NavigateToChips"
android:text="#string/Bet" />
and you should implement onClick() method in activity like this,
public void NavigateToChips(View v) {
Chips chip = new Chips();
chip.scommessa();
Intent intent1 = new Intent(GameActivity.this, Chips.class);
startActivity(intent1);
}

In your xml layout file, I see an attribute android:onClick="onClickChips" for the component having id as android:id="#+id/Cover1". Hence the java code would typically look for the method with name onClickChips(), whereas you've used an setOnClickListener() for Cover1 button.
Solution:
Either remove the attribute (android:onClick="onClickChips") from your XML or remove the setOnClickListener() for that button. Keep only one.

Related

Loading a URL by using a webview by passing URL in a second activty

I want to pass URL string from main activity to second activity and load the URL in second activity.... but when I click the go button at main activity it goes to second activity but it shows nothing but blank.
here is my code ..
public class MainActivity extends AppCompatActivity {
EditText editText;
Button go;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.urltext);
go=findViewById(R.id.button6);
final String link=editText.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Webview.class);
intent.putExtra("link",link);
startActivity(intent);
}
});
}
}
Second Activity:
public class Webview extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
}
Use the following in your manifest :
<uses-permission android:name="android.permission.INTERNET"/>
Refer these examples Please
Web View Android Developers
Can someone give one exact example of webview implementation in android
webView android
your code seems to be fine, just add internet permission in your manifest file
<uses-permission android:name="android.permission.INTERNET"/>
The problem is in this line
final String link=editText.getText().toString();
You have made the variable "link" final that means this variable can be set value only once. Try putting some logs in your code and see what values are passed in the intent extra into the second activity.
Also you have to set WebViewClient to your WebView instance.
Try below code. It worked for me:
private EditText editText;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.urltext);
button = (Button) findViewById(R.id.btn_go);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
String link = editText.getText().toString();
intent.putExtra("link",link);
startActivity(intent);
}
});
}
In the second activity -
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;
Log.d(WebViewActivity.class.getSimpleName(), url);
mWebView = (WebView)findViewById(R.id.wv_url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
I have several buttons those I have not used yet. I have used button 6.
Activity mainXML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#drawable/backgroundone"
tools:context="com.example.rakib.webbrowser.MainActivity">
<EditText
android:id="#+id/urltext"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/button"
android:layout_marginTop="20dp"
android:text=""
android:layout_marginRight="15sp"
android:textColor="#FFFF" />
<Button
android:id="#+id/button"
android:background="#drawable/buttonshapehome"
android:text="Facebook"
android:textColor="#fdf900"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/urltext"
android:layout_marginStart="24dp"
android:layout_marginTop="93dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:text="Youtube"
android:textColor="#fdf900"
android:background="#drawable/buttonshapehome"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_marginStart="23dp"
android:layout_toEndOf="#+id/button"
/>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google"
android:textStyle="italic"
android:textColor="#fdf900"
android:background="#drawable/buttonshapehome"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginStart="23dp"
android:layout_toEndOf="#+id/button2"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:background="#drawable/buttonshapehome"
android:text="linkdin"
android:textColor="#fdf900"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/button"
android:layout_below="#+id/button"
android:textStyle="italic"
android:layout_marginTop="72dp"
/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshapehome"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:text="gmail"
android:textStyle="italic"
android:textColor="#fdf900"
android:layout_alignStart="#+id/button2"
/>
<Button
android:id="#+id/newage"
android:layout_width="wrap_content"
android:background="#drawable/buttonshapehome"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:text="new age"
android:textStyle="italic"
android:textColor="#fdf900"
android:layout_alignBottom="#+id/button4"
android:layout_alignStart="#+id/button5" />
<Button
android:id="#+id/button6"
android:text="GO"
android:layout_width="50dp"
android:textStyle="bold"
android:layout_height="35dp"
android:layout_alignBaseline="#+id/urltext"
android:layout_alignBottom="#+id/urltext"
android:layout_toEndOf="#+id/urltext"
android:background="#039337"
/>
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/button3"
android:layout_marginBottom="53dp"
android:background="#drawable/settings_icon"
android:text=""
android:textColor="#ffaa00" />``
<TextView
android:id="#+id/textView"
android:text="settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button8"
android:layout_alignParentBottom="true"
android:layout_marginBottom="27dp"
android:layout_marginEnd="16dp"
android:textColor="#ffff" />
</RelativeLayout>
Activity webviewXML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.rakib.webbrowser.Webview">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="470dp">
</WebView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_below="#id/webview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#00ffffff"
>
<ImageView
android:id="#+id/home"
android:src="#drawable/homeicon"
android:clickable="true"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="36sp"
tools:ignore="OnClick" />
<ImageView
android:id="#+id/reload"
android:layout_weight="1"
android:clickable="true"
android:src="#drawable/reload"
android:layout_height="36sp"
android:layout_width="wrap_content"
/>
</LinearLayout>
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rakib.webbrowser">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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=".Webview">
</activity>
</application>
</manifest>

Android Buttons dont work click on click

I'm new new to Android and am working on a simple project that needs login and sign up.
When I click on the links to link to another activity, The buttons do not work.
here is my code....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg_login"
android:gravity="center"
android:padding="10dp"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:weightSum="1">
<ImageView android:src="#drawable/logo_bg"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="100dp"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/Zilwacom_Logo" />
<TextView android:id="#+id/login"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:text="#string/welcome_to_zilwacom"
android:gravity="center"
android:textSize="24sp"
android:layout_weight="0.14" />
<!-- Email Label -->
<EditText
android:id="#+id/loginemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/Email" />
<!-- Password Label -->
<EditText
android:id="#+id/loginpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/Password"/>
<Button
android:id="#+id/btnlogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="#string/Login"/>
<TextView
android:id="#+id/linksignup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="#null"
android:text="#string/btn_link_to_register"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="15dp"
android:clickable="true"/>
</LinearLayout>
</LinearLayout>
and my login code
When the user clicks to sign up, the app shd open the signup activity
public class LoginActivity extends AppCompatActivity {
private EditText email;
private EditText password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Get reference to buttons and texts
//buttons
Button loginbtn =(Button) findViewById(R.id.btnlogin);
TextView signuplink=(TextView) findViewById(R.id.linksignup);
//Texts
email=(EditText) findViewById(R.id.loginemail);
password=(EditText) findViewById(R.id.loginpassword);
signuplink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(LoginActivity.this, SignupActivity.class);
startActivity(i);
}
});
}
}
I managed to do it guys,
The problem was in the manifest...
I made the change as below..
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Add/change in your code:
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(LoginActivity.this, SignupActivity.class);
startActivity(i);
}
});
}
Set onClickListener to your Button [loginbtn] not TextView [signuplink].
In your xml layout file add to TextView android:clickable = "true"

pass intent with extra to defined layout text view

I am at the beginning of an app, I'm an absolute amateur and what i want to do is simple.
I am trying to pull Extras from an intent on my first activity into a textview on my second activity
First page: what is your name? (type a name and push a button)
On the next page: okay so your name is ...(the name they entered)
I had an answer to this but had a little trouble understanding it, in the code i have, when i click the okay button just the word okay shows up in the textview rather than the edited text
here is my code....
main.java
Button ok;
EditText name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText);
ok=(Button)findViewById(R.id.button);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String a=ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a);
startActivity(intent);
}
});
}
main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:textStyle="bold|italic"
android:textSize="23sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignBottom="#+id/button"
android:hint="#string/text"
android:textSize="25sp"
android:textStyle="bold|italic"
android:layout_toLeftOf="#+id/button"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:src="#drawable/start"
android:contentDescription="#string/startimage"
android:layout_below="#+id/button"
android:paddingBottom="20dp"
android:paddingTop="20dp"/>
</RelativeLayout>
secondactivity.java
public class secondactivity extends Activity {
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t = (TextView)findViewById(R.id.textView3);
String n = this.getIntent().getStringExtra("NAMEDATA");
t.setText(n);
}
activity_second.xml
<RelativeLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_sent"
android:textSize="23sp"
android:id="#+id/textView2"
android:textStyle="bold|italic"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_to_do"
android:id="#+id/textView"
android:layout_marginTop="13dp"
android:textSize="23sp"
android:layout_below="#+id/textView2"
android:textStyle="bold|italic"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bath_time"
android:id="#+id/button"
android:layout_marginTop="29dp"
android:layout_below="#+id/textView"
android:layout_alignRight="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:textSize="25sp"
android:textStyle="bold|italic"
android:onClick="viewimage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/school"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignRight="#+id/button"
android:layout_alignLeft="#+id/button"
android:textSize="25sp"
android:textStyle="bold|italic"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bed"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignRight="#+id/button2"
android:textSize="25sp"
android:textStyle="bold|italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:paddingStart="4dp"
android:layout_alignParentRight="true"
android:textSize="23dp"
android:textStyle="bold|italic" />
</RelativeLayout>
androidmainfest.xml
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".main"
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=".secondactivity"
android:label="#string/title_activity_second" >
</activity>
<activity
android:name=".bathactivity"
android:label="#string/title_activity_bath"
android:parentActivityName="com.example.myapplication.SecondActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapplication.SecondActivity" />
</activity>
</application>
can anyone tell me where I've gone wrong? Think its in my onCreate method on either Activity but really stuck,
many thanks
Actually - you don't pass anything from the EditText widget, you are only passing from the Button
Here lies the problem:
String a=ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a);
You could add the contents of the EditText onto the end of this string in the same way.
Try something like this:
String nameStr = name.getText().toString();
String a = ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a+nameStr);

Webview won't load on button click?

I have been trying to resolve this now for a number of days, I have searched the web and have also scoured through the many related questions provided on here but I am yet to resolve this issue.
The code works fine when I create a separate project, but the some reason it is not working here.
I have several activities that are accessible buy a click of a button from the main page (This part is working fine) and then from each of those several activities I have buttons that when clicked should open a website, but it is not happening.....
I have added all activities to the Manifest ( what I can see) have looked at it several times. lol
I've redone the code several times but to no avail, the only thing I have not done is start the project from scratch (Last resort).
Appreciate your help.
**
MainActivity.java (Main Page Working Fine)
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton imagebutton1, imagebutton2,imagebutton3, imagebutton4,
imagebutton5, imagebutton6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
imagebutton1 = (ImageButton) findViewById(R.id.imageButton1);
imagebutton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Fashion.class);
startActivity(intent);
}
});
imagebutton2 = (ImageButton) findViewById(R.id.imageButton2);
imagebutton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Electrical.class);
startActivity(intent);
}
});
imagebutton3 = (ImageButton) findViewById(R.id.imageButton3);
imagebutton3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Kids.class);
startActivity(intent);
}
});
imagebutton4 = (ImageButton) findViewById(R.id.imageButton4);
imagebutton4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Pets.class);
startActivity(intent);
}
});
imagebutton5 = (ImageButton) findViewById(R.id.imageButton5);
imagebutton5.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Food.class);
startActivity(intent);
}
});
imagebutton6 = (ImageButton) findViewById(R.id.imageButton6);
imagebutton6.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Web.class);
startActivity(intent);
}
});
}
}
**
MainActivityWebview.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivityWebView extends Activity {
private Button button1, button2;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.fashion);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivitySelfridges.class);
startActivity(intent);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivityVery.class);
startActivity(intent);
}
});
}
}
fashion.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
>
<ScrollView
android:id="#+id/tabscrvie"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="#drawable/selfridges" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Store" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="#drawable/very" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="#drawable/nelly" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/george" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/redoute" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/fraser" />
<Button
android:id="#+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/lncc" />
<Button
android:id="#+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="#drawable/selfridges" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Store" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="#drawable/very" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="#drawable/nelly" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/george" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/redoute" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/fraser" />
<Button
android:id="#+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
<TableRow
android:id="#+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:baselineAlignBottom="false"
android:cropToPadding="true"
android:src="#drawable/lncc" />
<Button
android:id="#+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="1"
android:text="Visit Website" />
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
WebViewActivitySelfridges.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivitySelfridges extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.selfridges.com/");
}
}
WebViewActivityVery.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivityVery extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.very.co.uk/");
}
}
webview.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="store.front"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="store.front.MainActivity"
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:label="#string/app_name"
android:name="store.front.WebViewActivitySelfridges" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.WebViewActivityVery" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.Fashion" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.Electrical" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.Kids" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.Food" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.Pets" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.Web" >
</activity>
<activity
android:label="#string/app_name"
android:name="store.front.MainActivityWebView" >
</activity>
</application>
</manifest>
Not completely sure if this is correct.
#Override is missing in onCreate of all the webview activities defined by you.
Check if the onCreate() is getting triggered at all.

Transfer data between EditText and Text View in different activities

I am novice for android app development. I have an issue here.
In 1st activity, i have created 10 rows. Each row contain a next button which links to second activity.
In 2nd activity,i have edittext field to input user details such as account name, password and etc. Each time I update my account name, when i press android back button, the row should contain the updated name. But I am not able to pass the account name to the 1st activity.
Below is my code for 1st activity:
public class AccountSetup extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_main);
this.initViews();
}
private void initViews(){
TextView user1 = (TextView)findViewById(R.id.user1);
Button iconNext1 = (Button)findViewById(R.id.iconNext1);
iconNext1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent1 = new Intent(AccountSetup.this, AccountSettingActivity1.class);
Intent1.putExtra("rowid","1");
startActivityForResult(Intent1, 100);
}
});
Button iconNext2 = (Button)findViewById(R.id.iconNext2);
iconNext2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent2 = new Intent(AccountSetup.this, AccountSettingActivity2.class);
startActivity(Intent2);
finish();
}
});
Button iconNext3 = (Button)findViewById(R.id.iconNext3);
iconNext3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent3 = new Intent(AccountSetup.this, AccountSettingActivity3.class);
//onNewIntent((Intent) v.getTag());
Intent3.putExtra("rowid","3");
startActivity(Intent3);
finish();
}
});
Button iconNext4 = (Button)findViewById(R.id.iconNext4);
iconNext4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent4 = new Intent(AccountSetup.this, AccountSettingActivity4.class);
//onNewIntent((Intent) v.getTag());
Intent4.putExtra("rowid","4");
startActivity(Intent4);
finish();
}
});
Button iconNext5 = (Button)findViewById(R.id.iconNext5);
iconNext5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent5 = new Intent(AccountSetup.this, AccountSettingActivity5.class);
//onNewIntent((Intent) v.getTag());
Intent5.putExtra("rowid","5");
startActivity(Intent5);
finish();
}
});
Button iconNext6 = (Button)findViewById(R.id.iconNext6);
iconNext6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent6 = new Intent(AccountSetup.this, AccountSettingActivity6.class);
//onNewIntent((Intent) v.getTag());
Intent6.putExtra("rowid","6");
startActivity(Intent6);
finish();
}
});
Button iconNext7 = (Button)findViewById(R.id.iconNext7);
iconNext7.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent7 = new Intent(AccountSetup.this, AccountSettingActivity7.class);
//onNewIntent((Intent) v.getTag());
Intent7.putExtra("rowid","7");
startActivity(Intent7);
finish();
}
});
Button iconNext8 = (Button)findViewById(R.id.iconNext8);
iconNext8.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent8 = new Intent(AccountSetup.this, AccountSettingActivity8.class);
//onNewIntent((Intent) v.getTag());
Intent8.putExtra("rowid","8");
startActivity(Intent8);
finish();
}
});
Button iconNext9 = (Button)findViewById(R.id.iconNext9);
iconNext9.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent9 = new Intent(AccountSetup.this, AccountSettingActivity9.class);
//onNewIntent((Intent) v.getTag());
Intent9.putExtra("rowid","9");
startActivity(Intent9);
finish();
}
});
Button iconNext10 = (Button)findViewById(R.id.iconNext10);
iconNext10.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Intent10 = new Intent(AccountSetup.this, AccountSettingActivity10.class);
//onNewIntent((Intent) v.getTag());
Intent10.putExtra("rowid","10");
startActivity(Intent10);
finish();
}
});
}
#Override
public void onBackPressed() {
Intent i = new Intent(AccountSetup.this, WelcomeActivity.class);
startActivity(i);
finish();
super.onBackPressed();
}
#Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
if (requestCode == 100) {
if (resultCode == RESULT_OK) {
String accountName1 = data.getStringExtra("accountName1");
}
}
}
}
And the following is the code for 2nd activity.
public class AccountSettingActivity1 extends Activity{
private EditText etAccountName;
private EditText etWanIp;
private EditText etLocalIp;
private EditText etPort;
private EditText etPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_tab_content_setting);
this.initViews();
}
private void initViews(){
etAccountName = (EditText)this.findViewById(R.id.etAccountName);
etWanIp = (EditText)this.findViewById(R.id.etWanIp);
etLocalIp = (EditText)this.findViewById(R.id.etLocalIp);
etPort = (EditText)this.findViewById(R.id.etPort);
etPassword = (EditText)this.findViewById(R.id.etPassword);
// Assigns value
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
etAccountName.setText(sp.getString("accountName1", ""));
etWanIp.setText(sp.getString("wanIp1", ""));
etLocalIp.setText(sp.getString("localIp1", ""));
etPort.setText(sp.getString("port1", ""));
etPassword.setText(sp.getString("password1", ""));
etWanIp.setOnFocusChangeListener(new OnFocusChangeListener(){
#Override
public void onFocusChange(View arg0, boolean hasFocus) {
if(!hasFocus){
System.out.println("lost focus");
AccountSettingActivity1.this.saveSettings();
}
}
});
}
private void saveSettings(){
String accountName1 = etAccountName.getText().toString();
String wanIp1 = etWanIp.getText().toString();
String localIp1 = etLocalIp.getText().toString();
String port1 = etPort.getText().toString();
String password1 = etPassword.getText().toString();
accountName1 = (accountName1.trim().length() == 0)? "User": accountName1;
wanIp1 = (wanIp1.trim().length() == 0)? "0.0.0.0": wanIp1;
localIp1 = (localIp1.trim().length() == 0)? "0.0.0.0": localIp1;
port1 = (port1.trim().length() == 0)? "8000": port1;
password1 = (password1.trim().length() == 0)? "xxxx": password1;
etAccountName.setText(accountName1);
etWanIp.setText(wanIp1);
etLocalIp.setText(localIp1);
etPort.setText(port1);
etPassword.setText(password1);
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
editor.putString("accountName1", etAccountName.getText().toString());
editor.putString("wanIp1", etWanIp.getText().toString());
editor.putString("localIp1", etLocalIp.getText().toString());
editor.putString("port1", etPort.getText().toString());
editor.putString("password1", etPassword.getText().toString());
editor.commit();
}
/* #Override
public void onBackPressed() {
saveSettings();
Intent i = new Intent(AccountSettingActivity1.this, AccountSetup.class);
startActivity(i);
finish();
super.onBackPressed();
}*/
public void onBackPressed() {
saveSettings();
//final EditText Eclass1;
EditText et = (EditText)findViewById(R.id.etAccountName);
String s= et.getText().toString();
Intent i = new Intent(AccountSettingActivity1.this, AccountSetup.class);
i.putExtra("accountName1" ,s);
setResult(RESULT_OK, i);
finish();
super.onBackPressed();
}
#Override
protected void onPause() {
// When user leaves this tab, saves the values
this.saveSettings();
super.onPause();
}
}
1st activity xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:AccountSetup="http://schemas.android.com/apk/res/com.example.play"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Account Toolbar -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#324F85" >
<Button
android:layout_width="54dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:background="#drawable/ic_btn_done"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/selectAccount"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textStyle="bold" />
<Button
android:id="#+id/btnAdd"
android:layout_width="54dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:background="#drawable/ic_btn_add_account"
android:layout_centerVertical="true" />
<ImageView
android:layout_width="4dp"
android:layout_height="30dp"
android:layout_alignTop="#+id/btnDone"
android:layout_toRightOf="#+id/btnDone"
android:src="#drawable/toolbar_seperator" />
<ImageView
android:layout_width="4dp"
android:layout_height="30dp"
android:layout_alignTop="#+id/btnAdd"
android:layout_toLeftOf="#+id/btnAdd"
android:src="#drawable/toolbar_seperator" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#drawable/logo_small_white" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="#+id/tlStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
style="#style/tableRow"
android:id="#+id/row1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:id="#+id/user1"
style="#style/textSettingLabel"
android:layout_weight="1" />
<Button
android:id="#+id/iconNext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:id="#+id/user2"
style="#style/textSettingLabel"
android:layout_weight="1" />
<Button
android:id="#+id/iconNext2"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:id="#+id/user3"
style="#style/textSettingLabel"
android:layout_weight="1" />
<Button
android:id="#+id/iconNext3"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
android:id="#+id/user4"
style="#style/textSettingLabel"
android:layout_weight="1" />
<Button
android:id="#+id/iconNext4"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
style="#style/textSettingLabel"
android:id="#+id/user5"
android:layout_alignBaseline="#+id/etZone_1_1"
android:text="User 1" />
<Button
android:id="#+id/iconNext5"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
style="#style/textSettingLabel"
android:id="#+id/user6"
android:layout_alignBaseline="#+id/etZone_1_1"
android:text="User 1" />
<Button
android:id="#+id/iconNext6"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
style="#style/textSettingLabel"
android:id="#+id/user7"
android:layout_alignBaseline="#+id/etZone_1_1"
android:text="User 1" />
<Button
android:id="#+id/iconNext7"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
style="#style/textSettingLabel"
android:id="#+id/user8"
android:layout_alignBaseline="#+id/etZone_1_1"
android:text="User 1" />
<Button
android:id="#+id/iconNext8"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
style="#style/textSettingLabel"
android:id="#+id/user9"
android:layout_alignBaseline="#+id/etZone_1_1"
android:text="User 1" />
<Button
android:id="#+id/iconNext9"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
<TableRow
style="#style/tableRow"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical" >
<TextView
style="#style/textSettingLabel"
android:id="#+id/user10"
android:layout_alignBaseline="#+id/etZone_1_1"
android:text="User 1" />
<Button
android:id="#+id/iconNext10"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:scaleType="fitEnd"
android:background="#drawable/icon_next" />
</TableRow>
<View style="#style/tableRowBorder" />
</TableLayout>
</ScrollView>
</RelativeLayout>
</LinearLayout>
2nd activity's xml code:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ToolBar="http://schemas.android.com/apk/res/com.example.play"
xmlns:TextViewMyRiadPro="http://schemas.android.com/apk/res/com.example.play"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ViewSwitcher
android:id="#+id/switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/settingsView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/logo_small_white" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="565dp"
android:padding="15dp" >
<TextView
android:id="#+id/tvAccount"
android:text="Name"
android:layout_alignBaseline="#+id/etAccountName"
style="#style/textSettingLabel" />
<EditText
android:id="#+id/etAccountName"
style="#style/textSettingEdit"
android:layout_alignLeft="#+id/etWanIp"
android:inputType="text" />
<TextView
android:id="#+id/tvAccount"
android:layout_below="#+id/etAccountName"
android:text="User Account Name"
style="#style/textSettingHint" />
<TextView
android:id="#+id/tvWan"
android:text="Wan IP"
android:layout_alignBaseline="#+id/etWanIp"
style="#style/textSettingLabel" />
<EditText
android:id="#+id/etWanIp"
android:layout_below="#+id/tvAccount"
android:layout_toRightOf="#+id/tvWan"
style="#style/textSettingEdit" android:inputType="text"/>
<TextView
android:id="#+id/tvWanHint"
android:layout_below="#+id/etWanIp"
android:text="Port forwarding is required in order to establish connection from the internet"
style="#style/textSettingHint" />
<TextView
android:id="#+id/tvLocal"
android:text="Local IP"
android:layout_alignBaseline="#+id/etLocalIp"
style="#style/textSettingLabel" />
<EditText
android:id="#+id/etLocalIp"
android:layout_below="#+id/tvWanHint"
android:layout_toRightOf="#+id/tvLocal"
style="#style/textSettingEdit" android:inputType="text"/>
<TextView
android:id="#+id/tvLocalHint"
android:layout_below="#+id/etLocalIp"
android:text="The IP address of the alarm system in the local area network. 192.168.1.234 is the default address."
style="#style/textSettingHint" />
<TextView
android:id="#+id/tvPort"
android:text="Port"
android:layout_alignBaseline="#+id/etPort"
style="#style/textSettingLabel" />
<EditText
android:id="#+id/etPort"
android:layout_below="#+id/tvLocalHint"
android:layout_toRightOf="#+id/tvPort"
android:inputType="number"
style="#style/textSettingEdit" />
<TextView
android:id="#+id/tvPortHint"
android:layout_below="#+id/etPort"
android:text="The connection port of the alarm system. 8000 is the default port."
style="#style/textSettingHint" />
<TextView
android:id="#+id/tvPassword"
android:text="Password"
android:layout_alignBaseline="#+id/etPassword"
style="#style/textSettingLabel" />
<EditText
android:id="#+id/etPassword"
android:layout_below="#+id/tvPortHint"
android:layout_toRightOf="#+id/tvPassword"
style="#style/textSettingEdit" android:inputType="textPassword"/>
<TextView
android:layout_below="#+id/etPassword"
android:id="#+id/tvPasswordHint"
android:text="Your 4 digits password to access the alarm system."
style="#style/textSettingHint" />
</RelativeLayout>
</ScrollView>
</ViewSwitcher>
</LinearLayout>
use startActivityForResult() to start your 2nd activity
startActivityForResult(intent, requestcode)
When the second activity is finished do the following, the intent can hold the values that you need to pass back to activity1
setResult(RESULT_OK, intent)
finish();
now on Activity1, override onActivityResult()
onActivityResult()
{
//update your textview here.
}
deepdroid's Answer is true but you have one other alternative
Set public static String value=null; So , its will not depend on whole activity.

Categories

Resources