Not Performing Any action On Button Click : Android - android

I need a button in android button.I need to show that when button was click it show the character 1 in the TextView field textView.
So here is my java code.
package com.example.kartikeya;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonOnClick(){
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<Button
android:text="#string/button"
android:id="#+id/button"
android:textColor="#0A0A0A"
android:textSize="25sp"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:clickable="true"
android:contentDescription="#string/click1"
android:contextClickable="true" />
<Button

You forget to call your method. buttonOnClick();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOnClick();
}

Try this way
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}

You have missed the text view in your activity.xml file.

you never called this fuction... buttonOnClick()

In MainActivity write following code.
public class MainActivity extends AppCompatActivity {
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.clickBtn);
TextView textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(textView.getText() + "" + 1);
}
});
}
}
activity_main.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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/textView"
android:textSize="30sp"/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/clickBtn"
android:layout_centerInParent="true"
android:text = "click me"/>
</RelativeLayout>

Related

Android app is not running on android studio and going to force close

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!");
}
}
);
}
}

Android code not working

Can anyone guide me please?
When I run this, it stops before running.
I dunno what's wrong in this.
Can anyone guide me please?
When I run this, it stops before running.
I dunno what's wrong in this.
MainActivity.java-
package gangster.cookies;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView textView = (TextView) findViewById(R.id.textView);
ImageView imageView = (ImageView) findViewById(R.id.bc);
public void eat(View view) {
textView.setText(R.string.changed_shit);
imageView.setImageResource(R.drawable.after_cookie);
}
public void uneat(View view) {
textView.setText(R.string.changed_again);
imageView.setImageResource(R.drawable.before_cookie);
}
}
Strings.xml-
<resources>
<string name="app_name">Cookies</string>
<string name="changed_shit">I am full bitch</string>
<string name="changed_again">Feed me again Motherfucker</string>
</resources>
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:background="#BC8F8F"
android:orientation="vertical"
tools:context="gangster.cookies.MainActivity">
<ImageView
android:id="#+id/bc"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="I am Hungry Bitch"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:textColor="#fff"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Eat Cookie"
android:id="#+id/button"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:onClick="eat"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uneat"
android:id="#+id/button2"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="100dp"
android:onClick="uneat"/>
</LinearLayout>
</LinearLayout>
Change your onCreate View like This:
TextView textView;
ImageView imageView;
Button eat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.bc);
eat = (Button) findViewById(R.id.button);
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
eat();
}
});
// Calling member function
uneat();
}
public void eat() {
textView.setText(R.string.changed_shit);
imageView.setImageResource(R.drawable.after_cookie);
}
public void uneat() {
textView.setText(R.string.changed_again);
imageView.setImageResource(R.drawable.before_cookie);
}
you have initialized the TextView and ImageView outside of any method.. put that code inside onCreate(). method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
ImageView imageView = (ImageView) findViewById(R.id.bc);
}
Replace your MainActivity.java with following code.
package gangster.cookies;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.bc);
}
public void eat(View view) {
textView.setText(R.string.changed_shit);
imageView.setImageResource(R.drawable.after_cookie);
}
public void uneat(View view) {
textView.setText(R.string.changed_again);
imageView.setImageResource(R.drawable.before_cookie);
}
}

displaying a string on the textview when clicking a button in android

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");
}
});

my first application android

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");
}
});
}

Android : EditText.getText() returns "" (Empty) String on Android 1.6

I am trying to fetch the EditText value on click of a button.
String ETValue = ((EditText)findViewById(R.id.ETID)).getText().toString().trim();
Every things works fine on other android versions, but on 1.6 I am getting ""(Empty) String.
Whats going wrong on Android 1.6, how this is happening?
Thanks
Screen Shots:
Code Reference :
main.xml
<?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_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="#+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="#+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
<Button android:text="Click" android:id="#+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
Etext.Java
public class EText extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword)).getText().toString().trim();
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
Even this way doesn't works
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
ETextt1 = ((EditText) findViewById(R.id.keyword1));
}else{
ETextt2 = ((EditText) findViewById(R.id.keyword2));
}
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(null == ETextt1){
ETValue = ETextt2.getText().toString().trim();
}else if(null == ETextt2){
ETValue = ETextt1.getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
This Works Perfectly Fine :-)
package com.test.et;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class EText extends Activity {
/** Called when the activity is first created. */
EditText ETextt1 = null;
EditText ETextt2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Integer.parseInt(Build.VERSION.SDK) < 7){
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
}
//ETextt = ((EditText) findViewById(R.id.keyword1));
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = null;
if(Integer.parseInt(Build.VERSION.SDK) < 7){
ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
}else{
ETValue = ((EditText) findViewById(R.id.keyword1)).getText().toString().trim();
}
Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
I have found the problems source. You declareted two EditTexts with the same id. To resolve problem just rename your EditText as keyword1 and keyword2. Then, get text from second EditText.
public class EdText extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
Toast.makeText(EdText.this, ETValue, Toast.LENGTH_SHORT).show();
}
});
} }
layout:
`
<TextView
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:text="Type you text here"
android:id="#+id/TextView02"
android:layout_height="wrap_content"
android:textColor="#333333"
android:layout_width="fill_parent"
android:textSize="16dip">
</TextView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID1">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword1"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="250dip"/>
<Button android:id="#+id/btnID"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:background="#drawable/icon"
android:gravity="bottom"
android:paddingBottom="9dip"
android:layout_marginLeft="3dip"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ID2"
android:visibility="gone">
<EditText android:layout_marginLeft="5dip"
android:id="#+id/keyword2"
android:hint="e.g. Text here"
android:textSize="17dip"
android:singleLine="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginRight="5dip" />
</LinearLayout>
`
I have just finished struggling with EditText returning an empty string ("").
The reason was that setContentView(R.layout.main); was invoked twice:
private EditText editText1;
private EditText editText2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /// !!!
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
// some really long and untrivial initialization stuff
setContentView(R.layout.main); /// !!!
}
It looks like editText1 and editText2 pointed into the parts of the previous incarnation of R.layout.main...
Here is my test example for your case:
public class EdText extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// string initialised here will always have initial value and never changes
final String ETValue = ((EditText) findViewById(R.id.EditText01)).getText().toString().trim();
Button but = (Button) findViewById(R.id.Button01);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(EdText.this, ((EditText) findViewById(R.id.EditText01)).getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
});
} }
Everything works fine for API 4. Maybe your probles connected with the moment when you read the string value. see my comment.
public class EditTextDemo extends Activity {
/** Called when the activity is first created. */
Button btnClick;
String ETValue;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ETValue = ((EditText)findViewById(R.id.editText1)).getText().toString().trim();
btnClick.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
//assign a default value
ETValue.equals("Welcome!!");
Toast.makeText(getBaseContext(), "Value is:"+ETValue, Toast.LENGTH_SHORT).show();
Log.i("EditTextDemo","-----Value of EditText is:----------"+ETValue);
}
});
}}
You can also give value at runtime.. Also that value can be used for further coding.
Hope this will help!!

Categories

Resources