Buttons and TextView in Android - java

I'm trying to learn the ins and outs of Android and I'm finding some real challenges. I am trying to link a Button to a TextView, when you click the Button the value in the TextView changes. I've tried to use some of the sample code I've found online but I get a lot of errors and I'm not sure what to do with them. I'd appreciate any help you could offer.
Thanks so much for your help!
at line 25:
Multiple markers at this line
- main cannot be resolved or is not a field
- R cannot be resolved to a variable
at line 29:
btn1 cannot be resolved or is not a field
at line 32:
Syntax error on token(s), misplaced construct(s)
at line 34:
Multiple markers at this line
- Syntax error on token ")", ; expected
- Syntax error on token "(", ; expected
- void is an invalid type for the variable onClick
at line 36:
txtVw1 cannot be resolved or is not a field
and then here is my code:
package com.demuro1.views;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Button aBut = (Button) findViewById(R.id.btn1);
public void changeText(){
mButton.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.txtVw1);
mTextView.setText("Some Text");
}
};
}
}
and then the xml from the view
<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: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=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="129dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/txtVw1"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press button 1" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button 1"
android:onClick="activateBtn1"/>
</LinearLayout>
<LinearLayout
android:layout_width="140dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/txtVw2"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press button 2" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:onClick="activateBtn2"/>
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Use The following code
package com.demuro1.views;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button aBut = (Button)findViewById(R.id.btn1);
Button bBut = (Button)findViewById(R.id.btn2);
final TextView aText = (TextView)findViewById(R.id.txtVw1);
final TextView bText = (TextView)findViewById(R.id.txtVw2);
aBut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
aText.setText("You Clicked Button 1");
}
});
bBut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
bText.setText("You Clicked Button 2");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

You don't need to import android.R
main cannot be resolved or is not a field: make sure that in res/menu folder there is a file called main.xml
btn1 cannot be resolved or is not a field: initialize btn1 in onCreate() after setting the content
Syntax error on token(s), misplaced construct(s): set the button's click listener inside onCreate
txtVw1 cannot be resolved or is not a field: same as in 3
Change your MainActivity.java like this:
package com.demuro1.views;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView mTextView;
Button aBut;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.txtVw1);
aBut = (Button) findViewById(R.id.btn1);
aBut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setText("Some Text");
}
});
// Do similar thing with TextView (R.id.txtVw2) and Button (R.id.btn2) here
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Related

Error on creating button listener first android app

As this is my first android app; i couldn't figure out what goes wrong.
Error
Error on line "caller.textView.setText(“You clicked the button!”);"
Multiple markers at this line
Syntax error, insert "AssignmentOperator Expression" to complete
Assignment
Syntax error, insert ";" to complete BlockStatements
Line breakpoint:MyOnClickListener [line: 12] - onClick(View)
setText cannot be resolved or is not a field
screenshot of error
MainActivity.java
package com.example.myfirstandroidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new MyOnClickListener(this));
textView = (TextView) findViewById(R.id.textView1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyOnClickListener.java
package com.example.myfirstandroidapp;
import android.view.View;
import android.view.View.OnClickListener;
public class MyOnClickListener implements OnClickListener {
MainActivity caller;
public MyOnClickListener(MainActivity activity) {
this.caller = activity;
}
public void onClick(View view) {
caller.textView.setText(“You clicked the button!”);
}
}
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: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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="70dp"
android:text="Button" />
</RelativeLayout>

the codes are not working in the second xml file

i am just working on a test app.
this is the xml file of the home page.
<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: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=".MainActivityTEST2" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="Button12" />
</RelativeLayout>
i have added a button listener to go to other page. here is my code for that.
package com.example.test2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityTEST2 extends Activity {
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_test2);
but=(Button)findViewById(R.id.button12);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
setContentView(R.layout.activity123);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_test2, menu);
return true;
}
when i add a button listener in the second page to display text in the textfield, nothing works.
xml file of the second page
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="#+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
when i add a button listerner to display text in the textfiled, nothing works.
the app neighter stops working nor does anything after that.
my second window's java content is
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.test2.R;
public class activity_main_activty_test2 extends Activity {
Button but2;
EditText edit1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity123);
edit1=(EditText)findViewById(R.id.edittext1);
but2=(Button)findViewById(R.id.button2);
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit1.setText("hellow");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_test2, menu);
return true;
}
}
am i missing something. do i have to address something. please help i am just a biggener.
i have added a button listener to go to other page No, apparently you haven't.
You cannot go to the other activity through this code
setContentView(R.layout.activity123);
You need to use Intent
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivityTEST2.this,activity_main_activty_test2.class);
startActivity(intent);
finish(); //optional
}
ALSO remove import com.example.test2.R; from your second activity.
if you want to go to the next view, you dont use setContentView(R.layout.activity123); in your button listener it will not transfer you to the next activity, instead make use of intent since you have a 2nd activity.
Intent intent = new Intent(getBaseContext(), activity_main_activty_test2.class);
startActivity(intent)
in your MainActivityTEST2
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), activity_main_activty_test2.class);
startActivity(intent)
}
});
Please Change your first page as following. Android use Intent to jump the activity from one to another.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivityTEST2 extends Activity {
Button but;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_test2);
but = (Button) findViewById(R.id.button12);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(this, activity_main_activty_test2.class);
startActivity(intent);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_test2, menu);
return true;
}
}

Can't get text from editText Android Eclipse

I'm having some issues with my school project. I can't get the text from the textbox. I searched for a solution but nothing found.. I'll be grateful if somebody help me :)
So here is my Java code:
package com.src.vicnote;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class NewNoteActivity extends ActionBarActivity {
Button saveButton;
EditText textData;
Context context;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
saveButton = (Button) this.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textData = (EditText) findViewById(R.id.editText);
text = textData.getText().toString();
//text = "this is sparta!";
Log.d("ADebugTag", "string: \"" + text + "\" end of note text");
new SaveClass(text/*, context.getApplicationContext()*/);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_note, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new_note,
container, false);
return rootView;
}
}
}
And my XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.NewNoteActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/buttonSave"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/buttonSave"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</RelativeLayout>
Also I want to ask you what will happen if the text is cyrillic? Will be there any problem?
Try initializing textData outside of your OnClickListener:
textData = (EditText) findViewById(R.id.editText);
saveButton = (Button) this.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener() { //...
I was also stuck at finding method to get text from edittext. I got resolved this issue by doing the below,
String selQuantity = (((TextView)findViewById(R.id.etxtQuantity)).getText()).toString();
Try this. It works.

How to place an Image Button relatively to the devices screen size

I have a picture with all the Text for the button already right on it, I want to put a transparent button just on top of each word.
I can't use the XML code because the code has to change if I want the button to stay over the text.
Here is the XML code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/backgroundMenue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Nothing"
android:scaleType="centerCrop"
android:src="#drawable/main_menu2" />
<ImageButton
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="92dp"
android:layout_marginRight="218dp" />
</RelativeLayout>
And the Java code :
package com.example.snakesmash;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
ImageButton play;
ImageView background;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (RelativeLayout) RelativeLayout.inflate(this, R.layout.activity_main, null);
play = (ImageButton) layout.findViewById(R.id.play);
background = (ImageView) layout.findViewById(R.id.backgroundMenue);
play.layout((int) Math.round(background.getHeight() * 0.65), (int) Math.round(background.getWidth() * 0.65), (int) Math.round(background.getWidth() * 0.35), (int) Math.round(background.getHeight() * 0.35));
setContentView(layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

App crashing due to FindViewById(), issue with componentInfo

So whenever the these two lines are commented out its fine, but with them it crashes.
EditText convertValue = (EditText) findViewById(R.id.et_value_convert);
TextView convertResult = (TextView) findViewById(R.id.txt_result);
here's my full class code, xml is below.
package com.doubleelite.maxtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class ConverterActivity extends Activity {
EditText convertValue = (EditText) findViewById(R.id.et_value_convert);
TextView convertResult = (TextView) findViewById(R.id.txt_result);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_converter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_converter, menu);
return true;
}
// Custom method to handle button clicks.
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_to_cel:
convertResult.setText(String.valueOf(convertFahrenheitToCelsius(Integer.valueOf(convertValue.getText().toString()))));
}
}
private float convertFahrenheitToCelsius(float f) {
return (f-32) * (5/9);
}
private float convertCelsiusToFahrenheit(float c) {
return c * (9/5) + 32;
}
}
here is the xml:
<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:orientation="vertical"
tools:context=".ConverterActivity" >
<EditText
android:id="#+id/et_value_convert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/text_value_convert"
android:inputType="number"
/>
<Button
android:id="#+id/btn_to_cel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button_celsius"
android:onClick="onClick"
/>
<Button
android:id="#+id/btn_to_fahr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button_fahr"
android:onClick="onClick"
/>
<TextView
android:id="#+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
</LinearLayout>
Currently you are trying to find view's before setting layout for Activity.change your code as :
public class ConverterActivity extends Activity {
EditText convertValue ;
TextView convertResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_converter);
convertValue = (EditText) findViewById(R.id.et_value_convert);
convertResult = (TextView) findViewById(R.id.txt_result);
}
///YOUR CODE HERE

Categories

Resources