I have a problem with my android project. I cannot get access to my button. I have attach my class code==http://pastebin.com/A5ZTBkhd. Please anyone help me.
Here is my code -
package com.droid.androiddoctor;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidDoctorMainActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_android_doctor_main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnSubmit:
String e="hello";
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
break;
}
}
}
.Here is my code.when i push submit nothing happens. and the xml file is====
<?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/btnSubmit"
androi:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit" />
</LinearLayout>
You did not set the listener for your widgets at all. Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
button1.setOnClickListener(this)
}
I would suggest you to go through Android's UI Guide.
I wouldn't implement OnClickListener like that, try to add it like this in your class:
public class AndroidDoctorMainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
Button button2 = (Button) findViewById(R.id.btnExit);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for click on this button
}});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for this button
//or if you just want to exit from activity, just call:
finish();
}});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
}
}
Related
I'm building an app which has 3 functions a converter, a calculator and a notes section. When I click on the converter button on the home page it brings me to the converter activity / page. But when I click on the calculator button on the home page it won't open. Here is the code below. Any reason as to why? Thanks in advance.
MainActivity
package com.qub.buildersbuddy;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button buttonConverter;
Button buttonCalculator;
Button buttonNotePad;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ConvertBtn = (Button) findViewById(R.id.butonConverter);
ConvertBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CentInch.class);
startActivity(intent);
}
});
}
public void setupConverterButton(){
buttonConverter = (Button) findViewById(R.id.butonConverter);
// Button messageButton = (Button) findViewById(R.id.butonConverter);
}
public void CentToInch(){
buttonConverter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//opening the
try{
Class centClass = Class
.forName("com.qub.buildersbuddy.CentInch");
Intent myintent = new Intent(MainActivity.this,centClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
});
}
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button CalcBtn = (Button) findViewById(R.id.buttonCalc);
CalcBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Calculator.class);
startActivity(intent);
}
});
}
public void setupCalculatorButton(){
buttonCalculator = (Button) findViewById(R.id.buttonCalc);
// Button messageButton = (Button) findViewById(R.id.butonConverter);
}
public void Calculator(){
buttonCalculator.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//opening the
try{
Class calcClass = Class
.forName("com.qub.buildersbuddy.Calculator");
Intent myintent = new Intent(MainActivity.this,calcClass);
startActivity(myintent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
});
}
}
activity_main:
<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=".CentInch" >
<Button
android:id="#+id/butonConverter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="Converter" />
<Button
android:id="#+id/buttonCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/butonConverter"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Calculator" />
<Button
android:id="#+id/buttonNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonCalc"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="Notes" />
</RelativeLayout>
Is this the exact code you're using? Your #onCreate1 method will never get called. #onCreate gets called because it overrides a method from the class you extended (Activity), and something in Activity calls the method when the activity first starts. Move your calculator button logic into the first #onCreate method.
calculated.java: (this has to command to show the calculated.xml layout)
public class Calculated extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculated);
}
}
older:
main class:
package com.barth.appie;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
Button button1;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button1 = (Button) findViewById(R.id.buttoncalculate);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText)findViewById(R.id.editText1);
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(), Calculated.class);
startActivityForResult(myIntent, 0);
}
});
}
#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_main, menu);
return true;
}
}
calculated.xml: (this is what it has to show after the button press)
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Furthermore I have <string name="text">Value</string> at strings.xml, and in the other class called calculated.class, I have it so that it outputs the calculated.xml ( which works fine)
What I want: I want to display text in calculated.xml, which is a string made in main.class, and i want the string to be the text filled in textfield called "editText1"
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
Button button1;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button1 = (Button) findViewById(R.id.buttoncalculate);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText)findViewById(R.id.editText1);
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(),Calculated.class);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
}
});
}
#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_main, menu);
return true;
}
}
Calculated.java
public class Calculated extends Activity {
TextView mTextview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculated);
mTextview = (TextView)findViewById(R.id.textView1);
mTextview.setText(getIntent().getStringExtra("mytext"));
}
calculated.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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
To send the data from one activity to another activity
MainActivity.java is first activity from where you want to send data to another activity.
Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("text", text);
startActivity(myIntent);
Calculated.java is second activity which receive the intent data whatever you pass from MainActivity.java
String text = myIntent.getStringExtra("text");
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(text);
hello i try to use simple application in android but i have many problems ;
i need when i click in button text change in "hh";
my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
my class
package com.my.Hello;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class HelloActivity extends Activity{
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button1);
button.setText("hh");
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
button.setText("hh");
}
});
setContentView(R.layout.main);
}
}
i have good resultat in UI but when i click in button not thing happen ?
You're setting the text to "hh" in the onCreate() so clicking it would not change it.
Also, you are calling setContentView() twice, so the second time just invalidates everything you've already coded.
Try this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setText("hh");
}
});
}
I am looking for a way to close a custom dialog with a button that is inside the xml used in the dialog, alternatively closing it by pressing anywhere on the dialog. What I have is this; a layout with a Image Button that brings up the custom dialog with the content. I have setCanceledOnTouchOutside(true); and that works, but I need the dialog to fill up most of the screen and it can be hard for the user to click in the small space that is available. So how do I do this?
My java code:
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class Rose extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.rose);
ImageButton b = (ImageButton) findViewById(R.id.imageButton1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Dialog d1 = new Dialog(Rose.this);
d1.setContentView(R.layout.tariquet);
d1.setCanceledOnTouchOutside(true);
d1.show();
}
});
}
}
And my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:isScrollContainer="true"
android:minHeight="1100dp"
android:minWidth="650dp">
<ImageView
android:src="#drawable/rose_tariquet"
android:id="#+id/imageView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageView>
<Button android:text="X"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:layout_width="55dp"
android:layout_gravity="right"></Button>
</FrameLayout>
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_diolog_main);
tv=(TextView) findViewById(R.id.content);
tv.setText(Stringcontent);
close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == close)
dismiss();
}
}
called:
CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this,"clickme");
customizeDialog.show();
I have recently started on android.
i have written this piece of code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArrangeMe extends Activity {
private Button button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.button1 = (Button)findViewById(R.id.buttonOne);
this.button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
setContentView(R.layout.main);
}
}
and my main.xml looks as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="ArrangeMe"/>
<Button android:text="Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/buttonOne"></Button>
</LinearLayout>
but when i pass through this line
this.button1 = (Button)findViewById(R.id.buttonOne);
i observe button1 = null. but when i type R.id. eclipse does suggest an auto complete buttonOne (that suggests layout xml is correct !)
where am I going wrong ?
edit:
interstingly, i tried the following code,
still the button does not appear. it has stopped crashing, but the button does not appear !
button1 = new Button(getContext());
button1.setText("1");
addView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
this was giving error
i changed them to
button1 = new Button(getBaseContext());
button1.setText("1");
addContentView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
I think you should call setContentView(R.layout.main); After that you activity class should be aware there to find views.
You are assigning the button1 before setting the ContentView in your Activity. Change your code as follows,
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArrangeMe extends Activity {
private Button button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // you misplaced this line in your code
this.button1 = (Button)findViewById(R.id.buttonOne);
this.button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
}
}
you must use small letter for id of your view and second one build your project and clean that may generate id for button in R.java.