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");
}
});
}
Related
i am new at android app development. i made a simple app in which text changes whenever button is clicked but whenever i run it the app going to force close. kindly help .
Following is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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="com.progneers.www.app.MainActivity">
<TextView
android:text="#string/txtString"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:id="#+id/myTextView"
android:textAppearance="#style/TextAppearance.AppCompat" />
<Button
android:text="#string/click_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/myTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:id="#+id/myButton" />
</RelativeLayout>
Following is my Java Code
package com.progneers.www.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView myTextView = (TextView) findViewById(R.id.myTextView);
private Button myButton = (Button) findViewById(R.id.myButton);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
myTextView.setText("Button Clicked! Thank You!");
}
}
);
}
}
Error shows after app closed
You have to assign the views in the onCreate, try this
public class MainActivity extends AppCompatActivity {
private TextView myTextView;
private Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.myTextView);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
myTextView.setText("Button Clicked! Thank You!");
}
}
);
}
}
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) {
}
}
I'm very new to Android development and just started to study.
What I'm trying is to add a button and when that button is pressed a text "my first project" to get displayed in the text view.
With the help of some experts I created the button and text view.
So the button is showing in the simulator but when I click that button nothing happens.
Can anyone please help me with how can I display the text when pressing the button?
.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txtView"
android:layout_width="150dp"
android:layout_height="150dp"
android:text="#string/hello" />
<Button
android:id="#+id/mybtn"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="#+id/viewwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/viewheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
String hello;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn= new Button(this);
txtView=new TextView(this);
mybtn.setOnClickListener(this);
printmyname();
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtView = (TextView)findViewById(R.id.viewwidth);
txtView = (TextView)findViewById(R.id.viewheight);
hello="This is my first project";
//setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
txtView.setText(hello);
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, hello, Toast.LENGTH_LONG).show();
}
private void printmyname(){
System.out.println("coming");
}
}
You can do like this:
String hello;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtwidth = (TextView)findViewById(R.id.viewwidth);
hello="This is my first project";
mybtn.setOnClickListener(this);
}
public void onClick(View view){
txtView.setText(hello);
}
Check your textview names. Both are same . You must use different object names and you have mentioned that textview object which is not available in your xml layout file.
Hope this will help you.
First create xml file as follows. Create one textview and a button:
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
The first TextView is created by default. You can leave or remove it if you want.
Next one is to create a button
The next one is TextView where you want to display text.
Now coming to the main activity code...
package com.android.example.simple;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SimpleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView=(TextView)findViewById(R.id.textView1);
final Button button1 = (Button)findViewById(R.id.mybutton);
//Implement listener for your button so that when you click the
//button, android will listen to it.
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
textView.setText("You clicked the button");
} });
}
}
This is because you DON'T associated the OnClickListener() on the button retrieve from the XML layout.
You don't need to create object because they are already created by the Android system when you inflate XML file (with the Activity.setContentLayout( int resourceLayoutId ) method).
Just retrieve them with the findViewById(...) method.
Just check your code in .java class
You had written below line
mybtn.setOnClickListener(this);
before initializing the mybtn object I mean
mybtn = (Button)findViewById(R.id.mybtn);
just switch this two line or put that line "mybtn.setOnClickListener(this)" after initializing your mybtn object and you will get the answer what you want..
Try this
public void onClick(View view){
txtView.setText("hello");
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, "hello", Toast.LENGTH_LONG).show();
}
Also in toast use "Hello"
you use this as txtView.setText("hello");
MainActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
textView1=(TextView)findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView1.setText("TextView displayed Successfully");
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
mybtn .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtView.SetText("Your Message");
}
});
}
Check this:
hello.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
text.setText("hello");
}
});
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.