Application won't navigate to next app page - java

I am creating an android app, I won't to navigate from "test1" to "test2" but when I press "btnNext" nothing happens. I am using the same code that I used for other navigations within my app so I don't understand why it won't work. Can someone help please?
"test1" xml code:
<?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" >
<TextView
android:id="#+id/txtSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitle"
android:layout_centerHorizontal="true"
android:text="Please Answer the 9 Following Questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Depression Test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/txtQ1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtSubTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="Q.1. Have you found little pleasure or interest in doing things?"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioButton
android:id="#+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtQ1"
android:layout_below="#+id/radioButton1"
android:text="On some days" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtQ1"
android:layout_below="#+id/txtQ1"
android:text="No, not at all" />
<RadioButton
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/RadioButton01"
android:layout_below="#+id/RadioButton01"
android:text="On more than half the days" />
<RadioButton
android:id="#+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/RadioButton02"
android:layout_below="#+id/RadioButton02"
android:text="Nearly every day" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="#+id/txtQ1"
android:text="Next" />
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RadioButton03"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/txtTitle"
android:text="Next" />
</RelativeLayout>
"Test1.java" code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
openTest2();
}
});
}
//Open test page
public void openTest2() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test2.class);
startActivity(i);
}
}
"Test2.java" code:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//listener call this function
openTest3();
}
});
}
//Open test page
public void openTest3() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test3.class);
startActivity(i);
}
}
I have declared the activities in the Android Manifest and I am getting zero errors in my log cat.

You have two buttons named "btnNext". That's why it doesn't work.
Double check this:
"#+id/btnNext"
You have the same id set twice...

Change buttons code in xml to this
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="#+id/txtQ1"
android:text="Next" />
<Button
android:id="#+id/btnNext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/RadioButton03"
android:layout_marginTop="14dp"
android:layout_toRightOf="#+id/txtTitle"
android:text="Next" />
you are having problem because you are calling button with wrong id. check your button id's in test2.xm as well. it will run then

Related

OnEditorActionListener() working but cant get the text from EditText

I want to get the username from edit text and display it in the second Activity in TextView id-textView2. When I press enter after writing the name and click start button it goes to the second activity but the text is not being displayed . I tried executing other actions like startActivity() inside onEditorAction method it works after i press enter, but this code is not working
String name = nameText.getText().toString();
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
Is it because i declared two times intent, inside onEditorAction() and inside starQuizz method in Activity A. i did this because of scope issues.
Activity A
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText nameText = findViewById(R.id.nameText);
nameText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView nameText, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND || event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
String name = nameText.getText().toString();
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
handled = true;
}
return handled;
}
});
}
//OnClick of a Start button
public void startQuizz(View view){
Intent p1 = new Intent(this, qPage1.class);
startActivity(p1);
}
}
Activity A xml layout
<?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/beautifulcolorgradientsbackgrounds091eternalconstance"
tools:context=".MainActivity">
<EditText
android:id="#+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="181dp"
android:width="300dp"
android:hint="#string/name_input"
android:inputType="text"
android:imeOptions="actionSend"
android:imeActionId="10"/>
<Button
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="182dp"
android:onClick="startQuizz"
android:text="START" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:fontFamily="#font/changa_one"
android:text="Are you up for the challenge?"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="30sp" />
</RelativeLayout>
Activity 2
package com.guesstasif.guesswhat;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View;
import android.widget.TextView;
public class qPage1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_q_page1);
final ProgressBar p1progress = findViewById(R.id.pg1progressBar);
Runnable r = new Runnable() {
#Override
public void run() {
int progressStatus = 0;
while (progressStatus<200){
p1progress.incrementProgressBy(1);
android.os.SystemClock.sleep(50);
progressStatus++;
}
RadioButton q1radiobutton3 = findViewById(R.id.q1radioButton3);
RadioButton q2radiobutton1 = findViewById(R.id.q2radioButton1);
Intent p2 = new Intent(qPage1.this, qPage2.class);
//name==============================================================
String name = getIntent().getStringExtra("user_name");
TextView textView2= findViewById(R.id.textView2);
textView2.setText(name);
//==================================================================
if(q1radiobutton3.isChecked() && q2radiobutton1.isChecked())
{
p2.putExtra("intVariableName", 2);
}
else if (q1radiobutton3.isChecked() || q2radiobutton1.isChecked())
{
p2.putExtra("intVariableName", 1);
}
startActivity(p2);
}
};
Thread progressThread =new Thread(r);
progressThread.start();
}
}
Activity 2 xml layout
<?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=".qPage1">
<TextView
android:id="#+id/q1textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Q1.What is the name of the 7th planet of ou Solar system?"
android:textAlignment="center"
android:textSize="24sp" />
<RadioGroup
android:id="#+id/q1radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1textView"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<RadioButton
android:id="#+id/q1radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Jupiter" />
<RadioButton
android:id="#+id/q1radioButton2"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Saturn" />
<RadioButton
android:id="#+id/q1radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Uranus" />
<RadioButton
android:id="#+id/q1radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Neptune" />
</RadioGroup>
//second Question//
<TextView
android:id="#+id/q2textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="173dp"
android:text="Q2.What is the name of the largest tree in the world?"
android:textAlignment="center"
android:textSize="24sp" />
<RadioGroup
android:id="#+id/q2radioGroup"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/q2textView">
<RadioButton
android:id="#+id/q2radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="General Sherman" />
<RadioButton
android:id="#+id/q2radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sequoia sempervirens" />
<RadioButton
android:id="#+id/q2radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Centurion" />
<RadioButton
android:id="#+id/q2radioButton4"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_weight="1"
android:text="Coast redwood" />
</RadioGroup>
<ProgressBar
android:id="#+id/pg1progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp"
android:max="200" />
<TextView
android:id="#+id/scoreView"
android:layout_width="170dp"
android:layout_height="41dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="89dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="137dp"
android:textAlignment="center" />
</RelativeLayout>
The reason is you start a new Intent in here:
public void startQuizz(View view){
Intent p1 = new Intent(this, qPage1.class);
startActivity(p1);
}
Which does not pass the string and just starts a new Intent-Activity.
Also here:
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
You missed to start Intent so:
Intent p1 = new Intent(MainActivity.this, qPage1.class);
p1.putExtra("user_name",name);
startActivity(p1);
And in the quiz method, start another Intent:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
And finally, in the second Activity:
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("user_name");
Also, you were using Thread instead of runOnUIThread(); and that might cause some UI effects.
Look at the startQuizz method - seems like you are not setting the name as an extra to the intent!
You should use a single function that starts the next activity both in the editor action and the button click listener.
It seems you are setting text of a TextView in a background thread.
The UI cannot be altered in a background thread.
Try wrapping the setText() part in runOnUiThread().
Check this out: How do we use runOnUiThread in Android?
I'll answer the most obvious question here. It seems there's a lot of code work being done, here also.
EditText edittext = (EditText) findViewById(R.id.something);
You must, and I cannot stress this enough, as of Level 30, still, you have to cast it as an EditText. In fact, you have to with everything, essentially. Java doesn't like structural propagating without being handed the stalk, not the beans (seeds). So, please one last thing:
String string = edittext.getText().toString()
That works every time in this combination!

Click a button and be directed to new activity, Android

MainActivity.Java
package com.example.drexsprint.login;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
b2=(Button)findViewById(R.id.button2);
tx1=(TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("1234")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}}
activity_main.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arduino Security"
android:id="#+id/textView"
android:textColor="#ff7aff24"
android:textSize="35dp"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="Enter Name"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:textSize="25dp"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:textColor="#ddec0d"
android:background="#0e17c2"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:background="#df0c0c"
android:textColor="#0dca07"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_centerHorizontal="true" />
<TextView
android:text="Please Login:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:textSize="35dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Second Screen App
screen.Java
package com.example.drexsprint.login;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Screen extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
public void Left(View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button2");
}
public void Right (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button3");
}
public void Temp (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button1");
}
public void Hum (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/button1");
}
public void Photo (View view) {
goToUrl("http://admin:0000000AA#10.0.1.6/image.jpg");
}
private void goToUrl(String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
/** public void Photo (View view) {
// myWebView.loadUrl("http://admin:0000000AA#10.0.1.6/image.jpg");
}**/
}}
screen_layout
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="invisible"
android:layout_above="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Left"
android:id="#+id/button"
android:onClick="Left"
android:backgroundTint="#0f50e8"
android:textColor="#f4d318"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/button5"
android:layout_alignRight="#+id/button4"
android:layout_alignEnd="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Right"
android:id="#+id/button2"
android:onClick="Right"
android:backgroundTint="#0f50e8"
android:textColor="#f4d318"
android:layout_below="#+id/webView"
android:layout_alignRight="#+id/webView"
android:layout_alignEnd="#+id/webView"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Temp."
android:id="#+id/button3"
android:onClick="Temp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:backgroundTint="#369d7c"
android:textColor="#f4d318" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Hum."
android:id="#+id/button4"
android:onClick="Hum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:backgroundTint="#369d7c"
android:textColor="#f4d318" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo"
android:id="#+id/button5"
android:onClick="Photo"
android:backgroundTint="#589d36"
android:textColor="#f4d318"
android:layout_below="#+id/webView"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videoView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/webView"
android:background="#0a0a0a"
android:clickable="true"
android:contextClickable="true"
android:focusable="true" />
</RelativeLayout>
Okay! So I got two aplications, the first one is a login screen and the second one is a main menu of what the application should do, I want to find a way so that when a user insert the correct credentials and click on the Login button the app executes the second screen the main menu, how can i do this, Join the two apps together.
Wat i wanna achieve is on the the click of "Login Button" on the first app it Launches the Second app as a window! Thanks!! Kind of new in android any detailed help would be much appreciated.
Use startActivityForResult() to get the result from another activity.
By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa.
Intent intent=new Intent(this,Class2.class);
startActivityForResult(intent,10);

How to call object from other class to another class and add it?

I'm new for android java coding. I'm try to do menu list where just have tick box, and once tick the items, n press next, it should go to view layout and show items and the total of the item selected, then press next it should open details page where user must put their details n press send button to send via email. I don't know how to call the items from menu to Cart and to confirmation class.
This is menu.java .
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
Button btnorder;
Button btnback;
Button btnlinkcart;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
btnlinkcart = (Button) findViewById(R.id.button1);
btnback = (Button) findViewById(R.id.button2);
// back button click event
btnback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
startActivity(intent);
}
});
// Link to Cart Screen
btnlinkcart.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ViewActivity.class);
startActivity(i);
finish();
}
});
}
}
This is Cart.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ViewActivity extends Activity {
Button btnconfirm;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
btnconfirm = (Button) findViewById(R.id.button1);
// Link to Cart Screen
btnconfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
ConfirmActivity.class);
startActivity(i);
finish();
}
});
}
}
This is cart.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:orientation="vertical"
android:background="#drawable/wallpaper" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Confirm" />
</RelativeLayout>
This is menu.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="#drawable/wallpaper"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Back" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:text="Pizza (Large) RM30.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox1"
android:layout_marginTop="20dp"
android:text="Pizza (Mediume) RM20.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox2"
android:layout_marginTop="20dp"
android:text="Pizza (Personal) RM10.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox3"
android:layout_marginTop="20dp"
android:text="Chicken Wings RM12.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox4"
android:layout_marginTop="19dp"
android:text="Garlic Bread RM6.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox5"
android:layout_marginTop="20dp"
android:text="Soft Drink (Large) RM5.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/checkBox6"
android:layout_marginTop="19dp"
android:text="Soft Drink (Medium) RM4.00"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Make the variable you want to get in the menu class static, then in the cart class you can get them like this:
menu.variableFromMenuClass
public static ArrayList<YourObject> selectItemList= new ArrayList<YourObject>();
on selectItem(){
//each Item you add here
selectedList.add(selectedObject);
}
on disselectItem(){
//remove if exist in list
selectedList.remove(disselectedOject);
}
sendemail(){
send your public static selecteditem list.
}
This is the algorithm you have to write simple.

Unable to run code for Simple Counter App for Android

I am trying to make an application on Android Studio with the help of a tutorial. I managed to get the User Interface right and I think that I have assigned the correct buttons too. I am unable to get where I am going wrong with my code. I am new to Java and so I am unable to pinpoint the error I have committed. I am posting below the code from the files I was asked to edit in the tutorial.
package com.example.to_dolistapplication.app;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;
public class MainActivity extends Activity implements OnClickListener {
Button btn1;
Button btn2;
Button btn3;
TextView textTitle;
EditText scoreText;
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button);
btn2 = (Button)findViewById(R.id.button2);
btn3 = (Button)findViewById(R.id.button3);
scoreText = (EditText)findViewById(R.id.textView);
textTitle = (TextView)findViewById(R.id.editText);
//---set on click listeners on the buttons-----
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
// change font size of the text
textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
}
#Override
public void onClick(View v) {
if (v == btn1){
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.CYAN);
}
if (v == btn2){
counter--;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
}
if (v == btn3){
counter = 0;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
}
}
}
Above is the File from MainActivity.java
<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="com.example.to_dolistapplication.app.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+1"
android:id="#+id/button"
android:onClick="#string/intro"
android:layout_below="#+id/editText"
android:layout_toRightOf="#+id/textView"
android:layout_marginTop="79dp"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-1"
android:id="#+id/button2"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_marginTop="64dp"
android:layout_alignRight="#+id/button2"
android:layout_alignEnd="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textClock"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/textView2"
android:layout_alignBottom="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Score"
android:id="#+id/textView"
android:layout_alignBottom="#+id/editText"
android:layout_toLeftOf="#+id/editText" />
This is from the file activity_main.xml.
The app, when run on emulator, displays Unfortunately Counter App has stopped.
what might be the reason for the app not working? Please help.
**EditText** scoreText = (EditText)findViewById(R.id.textView);
**TextView** textTitle = (TextView)findViewById(R.id.editText);
you missmathed with types: android:id="#+id/editText" is EditText, but in Activity you wrote his id to TextView.
And you missmathed with types: android:id="#+id/textView" is TextView, but but in Activity you wrote his id as EditText.
You've mismatched id's in onCreate() method.
Use the debug mode or just look at your console output

setOnClickListener causes force close in Android

I am trying to create an onClickListener for a button. Everything shows up fine before I create the event listener, but force closes when I add it in.
Here is the code:
package com.austin.mobile;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class TxtLingoActivity extends Activity {
/** Called when the activity is first created. */
Button convert;
EditText inputText, outputText;
String myCopy;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
convert = (Button) findViewById(R.id.inputTextBox);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here is the xml:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/inputTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/convert"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Convert" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/outputTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Log Cat is showing this error:
03-19 18:05:21.726: E/dalvikvm(662): Unable to open stack trace file'/data/anr/traces.txt': Permission denied
Any suggestions?
Looks like you are referring to the incorrect id when 'finding' the Button:
Try to change this:
convert = (Button) findViewById(R.id.inputTextBox);
to this:
convert = (Button) findViewById(R.id.convert);
Hope this helps...
its ClassCastException in this line convert = (Button) findViewById(R.id.inputTextBox);
Because R.id.inputTextBox is EditText and you use as Button .change it in R.id.convert

Categories