Why isn't the TextView working? - eclipse

I'm coding an android program that arranges three words in alphabetical order using two simple activities.But in the second(output) activity,either the TextView or the Intents aren't working.The output isn't getting displayed.The java code for first(input) activity is as follows:
`package com.example.names;
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;
import android.widget.EditText;
import android.widget.Toast;
public class Input extends Activity {
EditText t1,t2,t3;
final Context context=this;
String n[]=new String[100];
String t[]=new String[100];
String a1,a2,a3;
Button alpha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
n[0]=t1.getText().toString();
n[1]=t2.getText().toString();
n[2]=t3.getText().toString();
for(int j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(n[j].compareToIgnoreCase(n[k])>0)
{
String temp=n[j];
n[j]=n[k];
n[k]=temp;
}
}
}
a1=n[0];
a2=n[1];
a3=n[2];
alpha=(Button)findViewById(R.id.button1);
alpha.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Input.this,Output.class);
i.putExtra("x", a1+a2+a3);
Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show();
startActivity(i);
}
});}}`
And the java code for second activity is as follows:
package com.example.names;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class Output extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView p1=new TextView(this);
Bundle extras=getIntent().getExtras();
if (extras!=null){
String ans=extras.getString("x");
p1.setText(ans);
p1.setTextSize(50);
Toast.makeText(Output.this, "All lines have been executed",Toast.LENGTH_LONG).show();
setContentView(p1);
}};}
Why isn't the output getting displayed then???
The application is not crashing also.

Change your code with this below code.
Get edittext value when button is clicked.
Your Input Activity looks like this.
public class Input extends Activity {
EditText t1,t2,t3;
final Context context=this;
String n[]=new String[100];
String t[]=new String[100];
String a1,a2,a3;
Button alpha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
alpha=(Button)findViewById(R.id.button1);
alpha.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
n[0]=t1.getText().toString();
n[1]=t2.getText().toString();
n[2]=t3.getText().toString();
for(int j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(n[j].compareToIgnoreCase(n[k])>0)
{
String temp=n[j];
n[j]=n[k];
n[k]=temp;
}
}
}
a1=n[0];
a2=n[1];
a3=n[2];
Intent i=new Intent(Input.this,Output.class);
i.putExtra("x", a1+a2+a3);
Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show();
startActivity(i);
}
});}}
Your Output Activity is fine it is working no changes in that activity.

Related

Android Studio says "Expression expected"

Android Studio tells there is an error in Java file, in string summonButton2(); Android Studio says "Expression expected".
I want the method summonButton2 to be launched automatically. I undesrtand that I'm doing it wrong. What exactly and is there any other way to start a method, except adding it to onCreate method? Thanks in advance.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int numberOfLinesLeft = 3;
Button secondaryActivityAddButton;
LinearLayout llForSecondaryButton;
LinearLayout llForSecondaryEditText;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
summonButton2();
}
public void summonButton2(View view){
llForSecondaryButton = findViewById(R.id.secondaryButton);
secondaryActivityAddButton = new Button(this);
secondaryActivityAddButton.setText("" + numberOfLinesLeft);
llForSecondaryButton.addView(secondaryActivityAddButton);
secondaryActivityAddButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
plusTextField();
if(numberOfLinesLeft == 0) {
view.setVisibility(View.GONE);
}
}
});
}
public void plusTextField() {
llForSecondaryEditText = findViewById(R.id.linearLayout1);
// add edittext
et = new EditText(this);
et.setText("text" + numberOfLinesLeft );
llForSecondaryEditText.addView(et);
numberOfLinesLeft--;
secondaryActivityAddButton.setText("" + numberOfLinesLeft);
}
}
summonButton2(new View(this));
Helped in my case.

The operator - under defined for the arguement type(s) EditText, int

Im trying to create an activity which adds name, age, and maxhr to sqlite and will be displayed in a listview in Eclipse. name and age is an input text. Maxhr is age(input text) minus 220. But im getting "The operator - under defined for the arguement type(s) EditText, int" error. I can't seem to find the answer for this error. Does anyone know how to fix it?
package com.heartrate.monitoring.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.heartratemonitoringapp.R;
import com.heartrate.monitoring.dao.UserDAO;
import com.heartrate.monitoring.model.User;
public class AddUserActivity extends Activity implements OnClickListener {
public static final String TAG = "AddUserActivity";
private EditText mTxtName;
private EditText mTxtAge;
private Button mBtnAdd;
private UserDAO mUserDao;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_user);
getActionBar().setDisplayHomeAsUpEnabled(true);
initViews();
this.mUserDao = new UserDAO(this);
}
private void initViews() {
this.mTxtName = (EditText) findViewById(R.id.txt_name);
this.mTxtAge = (EditText) findViewById(R.id.txt_age);
this.mBtnAdd = (Button) findViewById(R.id.btn_add);
this.mBtnAdd.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
Editable name = mTxtName.getText();
Editable age = mTxtAge.getText();
if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(age)){
// add the user to database
double mhr;
double maxhr;
mhr = Double.parseDouble(mTxtAge.getText().toString());;
maxhr = mhr - 220;
User createdUser = mUserDao.createUser(
name.toString(), age.toString(),
maxhr.toString());
Log.d(TAG, "added user : "+ createdUser.getName());
Intent intent = new Intent();
intent.putExtra(ListUserActivity.EXTRA_ADDED_USER, createdUser);
setResult(RESULT_OK, intent);
Toast.makeText(this, R.string.user_created_successfully, Toast.LENGTH_LONG).show();
finish();
}
else {
Toast.makeText(this, R.string.empty_fields_message, Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mUserDao.close();
}
}
Remove the extra ; on this line: mhr = Double.parseDouble(mTxtAge.getText().toString());;

Error in creating list

I am new to android and i have a Error creating list....... What is the Error? some one help me out........ I am using api18.........
package com.example.personalinformationmanagement;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
String list[] = {"PersonalTask", "PersonaFileInformation","WeaklyReports", "Remainder"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1));
}
/* (non-Javadoc)
* #see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
*/
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String choice = list[position];
try{
Class ourClass = Class.forName("com.example.personalinformationmanagement." +choice);
Intent i = new Intent(MainActivity.this,ourClass);
startActivity(i);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
First of all ensure that listview id in your activity_main.xml equals to "android:id="#android:id/list".
In code change line
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list));
If you want to manipulate with listView use:
ListView myListView = getListview();

When click button in menu it opens xml file

i am trying to make a menu in my app. when i click the menu button i made called aboutUs it is supposed to open an XML file that explains what this app is about. Except when i run the app and click on the menu button the app just force closes. heres my mainactivity.java
package com.JordanZimmittiDevelopers.BlazeCustomerService1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.JordanZimmittiDevelopers.BlazeCustomerService.R;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mail = (Button)findViewById(R.id.button1);
mail.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"jordanzimmitti#gmail.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "I Have A Question Or Probelm:");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My question or problem is:");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Click Your Defult E-mail To Send Your Message:"));
finish();
break;
}
}
Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.aboutUs:
Intent i = new Intent("com.JordanZimmittiDevelopers.BlazeCustomerService.AboutThisApp");
startActivity(i);
}
return false;
}
}
Make an activity that explain the application then override onCreateOptionsMenu() like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("Help").setIcon(R.drawable.HelpButton).setIntent(new Intent(this,HelpActivity.class));
return super.onCreateOptionsMenu(menu);
}

Capture 5 images after a button click in android

I am developing android application. The aim is to capture the 5 images after click a capture button. I am able to capture a single image by using the following code. But how can I take 5 images after a single button click. Please Can someone help me to do this?
Code snippet:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.camera.facedetectionexample.R;
public class FaceDetectionExample extends Activity {
private static final int TAKE_PICTURE_CODE = 100;
private Bitmap cameraBitmap = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button)findViewById(R.id.take_picture)).setOnClickListener(btnClick);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(TAKE_PICTURE_CODE == requestCode){
processCameraImage(data);
}
}
private void openCamera(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_CODE);
}
private void processCameraImage(Intent intent){
setContentView(R.layout.detectlayout);
((Button)findViewById(R.id.detect_face)).setOnClickListener(btnClick);
ImageView imageView = (ImageView)findViewById(R.id.image_view);
cameraBitmap = (Bitmap)intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
private View.OnClickListener btnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.take_picture:
openCamera();
break;
}
}
};
}
you are going to want to use burst mode, have a look at
How to make burst mode available to Camera
should make it pretty clear.