onClickListener not working in Fragment - android

trying to run a fragment with a button and getting error after the execution:
Following this S.O. answer How to handle button clicks using the XML onClick within Fragments , but not working with me.
DetalhesFragment.java
package com.example.waitersoriginal;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
public class DetalhesFragment extends Fragment implements OnClickListener{
TextView nomeEntrada,descrEntrada,valorEntrada,nmArm,dsArm,vlArm;
String txtNome, txtDescr;
View view,v;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.detalhes_fragment, container, false);
Button mButton = (Button) view.findViewById(R.id.button1);
nomeEntrada= (TextView) view.findViewById(R.id.textView1);
descrEntrada= (TextView)view.findViewById(R.id.textView2);
valorEntrada= (TextView)view.findViewById(R.id.textView3);
mButton.setOnClickListener((android.view.View.OnClickListener) this);
return view;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3);
array.criaArray(txtNome,txtDescr);
break;
}
}
public void change(String txt, String txt1){
//public void change(String txt, String txt1, String txt2){
//public void change(String txt){
nomeEntrada.setText(txt);
descrEntrada.setText(txt1);
txtNome = txt;
txtDescr = txt1;
//valorEntrada.setText(txt2);
}
}
But this row contains errors:
public class DetalhesFragment extends Fragment implements OnClickListener{
Error: The type DetalhesFragment must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface,int)
I've tried to implement the method above, no success.
Can somebody help me with this?
Ty!
EDIT:
detalhes_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="14dp"
android:textSize="25dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="455dp"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="78dp"
android:textSize="18dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_x="285dp"
android:layout_y="19dp"
android:textSize="22dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="395dp"
android:text="Adicionar aos Pedidos"
android:visibility="visible" />
</AbsoluteLayout>

Remove this code:
mButton.setOnClickListener((android.view.View.OnClickListener) this);
return view;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3);
array.criaArray(txtNome,txtDescr);
break;
}
}
Replace this code...
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3);
array.criaArray(txtNome,txtDescr);
break;
}
});
return view;
}

As the error says your have to implement (add in your class) a method with the name onClick with a parameter of type DialogInterface and another one of type int.
That is because your are implementing an interface which is a kind of contract. All methods of the interface you implement it is mandatory to you to create it on your class even if it has no code at all.
Like:
#Override
public void onClick(DialogInterface dialogInterface, int i){}
EDIT
Sorry, I just saw your code and see the the method is there what you need to do is to add the right parameters. Right now you are passing just a view.

Can you use Linear Layout.
Why did use absolute layout?
<?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" ><TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="14dp"
android:textSize="25dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="455dp"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="78dp"
android:textSize="18dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_x="285dp"
android:layout_y="19dp"
android:textSize="22dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="395dp"
android:text="Adicionar aos Pedidos"
android:visibility="visible" />
</LinearLayout>`
Can You try this...

Related

Access string_array from activity and send selected item to another activity in android

hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .
// Button activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
Button btnOne;
Button btnTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
btnOne = findViewById(R.id.btn1);
btnTwo = findViewById(R.id.button1);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
//textview activity page
package com.Aolai.temeshilai;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lipokchapters extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipokchapters);
textView=findViewById(R.id.textv);
String text= getIntent().getStringExtra("Data");
textView.setText(text);
}
}
Try this :
xml file
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/OKILA"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
Main Class
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.OKILA : {
text = getResources().getStringArray(R.array.testArray)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.testArray)[1];
break;
}
}
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
intent.putExtra("DATA", text);
startActivity(intent);
// Or else you can do whatever you want to do here with that text
}
}
Then in your other activity
String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
This is basics of Android so before answering I suggest you to go
through basic tutorials before posting on Stackoberflow.
Now,
Here is how you can access the items from your string_array.
String[] chapters= getResources().getStringArray(R.array.chapters);
Now,here is pseudo code that might help you.
btn1.setOnClickListener(
your_text_view.setText(chapters[0])
)
//Here 0 means your first item, likewise you can access items by their index in
array

Check the radiobutton from the page fragment

I have a button , I want when I click it goes to the page fragment The radiobutton is checked , If, for example, it was a hello , The word hello is printed on the textview in page MainActivity
page fragment
<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"
tools:context="com.example.java.frag.BlankFragment">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:text="nice to meet you"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton"
android:layout_weight="1" />
<RadioButton
android:text="welcome"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton2"
android:layout_weight="1" />
<RadioButton
android:text="hello"
android:textStyle="bold"
android:layout_gravity="center"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton3"
android:layout_weight="1" />
</RadioGroup>
</LinearLayout>
page MainActivity
package com.example.java.frag;
import android.app.FragmentTransaction;
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView x = (TextView)findViewById(R.id.textView);
}
public void changetext(View view) {
FragmentManager frag = getFragmentManager();
FragmentTransaction tran = frag.beginTransaction();
BlankFragment s = new BlankFragment();
tran.replace(R.id.con,s);
tran.commit();
}
}
How do I do it?
I hope to get an example
First of all, you should be very clear and precise about your query. Next, you should use EventBus here. It really comes handy when callbacks between different components are needed. To use EventBus, you should add following dependency in the app's build.gradle:
compile 'org.greenrobot:eventbus:3.0.0'
Then, you can create a simple POJO class to refer a callback or event. In this case, you can create a class like this:
class OptionItemEvent {
private String option;
public OptionItemEvent(String option){
this.option = option;
}
public String getOption(){
return option;
}
}
In your BlankFragment.java, you've to make a call to the event in the appropriate listener method, for example in this case:
#Override
public void onCheckedChanged(RadioGroup radioGroup, int radioButtonId) {
RadioButton radioButton = (RadioButton)view.findViewById(radioButtonId);
EventBus.getDefault().post(new OptionItemEvent(radioButton.getText().toString()));
}
In your MainActivity.java, add the following code:
#Override
protected void onResume(){
super.onResume();
EventBus.getDefault().register(this);
}
#Override
protected void onPause(){
EventBus.getDefault().unregister(this);
super.onPause();
}
#Subscribe (threadMode = ThreadMode.MAIN)
public void onOptionItemSelected(OptionItemEvent event){
//Set the value for your TextView
x.setText(event.getOption());
}
I hope this solves your problem.

Base Activity Button Click's not working in child activities

I am new to android. In my app, having common header.
I have created common header.xml and include that header.xml in all the activites.
But the button clicks [listener] not working.
How to solve the issue. this is the code.
Header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Home" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Accounts" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="History" />
</LinearLayout>
Home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/header" />
<EditText
android:id="#+id/txtOTPCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/btnVerify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="verify" />
</LinearLayout>
BaseHeaderActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class BaseHeaderActivity extends Activity
{
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
System.out.println("Home button Action");
}
});
}
}
Home Activity.java
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends BaseHeaderActivity
{
Button btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
btnVerify = (Button)findViewById(R.id.btnVerify);
}
}
base activities button clicks not working..!
Kindly suggest the solution.
Thanks in advance,
Arun
You don't have to create two different activities for this. Create one activity and use all buttons (including button in header.xml) similarly
public class MainActivity extends Activity {
Button btn1,btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Home button Action", Toast.LENGTH_SHORT).show();
}
});
btnVerify = (Button)findViewById(R.id.btnVerify);
btnVerify.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Button Verify Action", Toast.LENGTH_SHORT).show();
}
});
}
}
You can not use one layout xml file with single activity only.
Note - If you still want to create two different activities - one base and one child then you need to use setContentView() method in base activity with main layout file (Home.xml in your case) and extend it with child activity. Remember in this case dont use setContentView() method in Child Activity. This scenario is not preferred generally unless we want to create several child of same activity.

How to change textView to a #string and raising

so what i am tring to do is, i am having a book like-app, and want to setup a button when click it change the textview to a string in the #string rec. but in a different way
i will put the code just a moment, lets say i have a string in #string that is NH1,NH2,NH3
i can do for button View.setText(R.string.NH1); that will work but i want to create a counter and make it so every time clicked counter++ and want it to be like this View.setText(R.string.NH+counter); if you know what i mean so each time it clicked to goes to next #string without changing layout every time and without an array because the paragraph are very long
also if i have NH1-10 and the counter did not stop and still goes on what will i do.
if you are sill here thank you for reading.
XML(sheetpaper)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="496dp"
android:background="#drawable/bg1"
android:gravity="top"
android:orientation="vertical"
android:weightSum="100" >
<Button
android:id="#+id/bPrev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="51dp"
android:background="#android:color/transparent"
android:text=" " />
<TextView
android:id="#+id/tvHadith"
android:layout_width="310dp"
android:layout_height="210dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
android:text="#string/NH1"
android:textColor="#android:color/white" />
<Button
android:id="#+id/bNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/bPrev"
android:layout_alignBottom="#+id/bPrev"
android:layout_alignRight="#+id/tvHadith"
android:background="#android:color/transparent"
android:gravity="center"
android:text=" " />
"
</RelativeLayout>
</LinearLayout>
Java (Nawawi)
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 Nawawi extends Activity {
private static final String NH = null;
Button Next;
Button Prev;
TextView View;
int counter = 0;
String H=NH;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sheetpaper);
Next = (Button) findViewById(R.id.bNext);
Prev = (Button) findViewById(R.id.bPrev);
View = (TextView) findViewById(R.id.tvHadith);
Next.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
View.setText(R.string.H);
}
}); Prev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
View.setText(R.string.);
}
});
}
}
You could do this by creating an XML string array, so your strings will still be referenced in resources, but you'll be able to loop through them.
Something like this, in values/strings.xml:
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
And then in Activity:
Resources res = getResources();
final String[] planets = res.getStringArray(R.array.planets_array);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter < planets.length) {
textView.setText(planets[counter]);
counter++;
}
}
});
Be sure to check the counter against arrays length, otherwise you will get an IndexOutOfBoundsException.

Button OnClickListener not working

I am new to Android. I am trying to get input from the EditText and displaying it in TextView with the help of button. When I click the button nothing happens. Please help, here is my MainActivity.java code -
package com.example.addname;
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.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Button b1;
TextView v1;
EditText t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
t1 = (EditText) findViewById(R.id.editText1);
v1 = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
}
});
}
#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;
}
}
and here is my 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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/editText1"
android:layout_marginTop="48dp"
android:text="Button" />
</RelativeLayout>
Instead of
if(t1.getText().toString()=="")
{
v1.setText(t1.getText().toString());
}
You can use
if(t1.getText().isEmpty()) // isEmpty() is only available from API 9 and Above
{
v1.setText(t1.getText().toString());
}
or this
if(t1.getText().trim().length == 0)
{
v1.setText(t1.getText().toString());
}
Don't compare strings using ==. You're actually comparing reference of an object. To compare strings, use equals() method.
I'd recommend comparing strings with .equals() since Objects compared with == usually fails (== compares references, not Object content).
if(t1.getText().toString().equals("")))
in your case though,since you're comparing an empty String, you can also use
if (t1.getText().toString().length == 0)
Then note that once you set the text, you're setting v1's text to an empty string since your logic indicates that only upon an empty string you set v1's text. So I'd recommend getting rid of the if altogether:
#Override
public void onClick(View arg0) {
v1.setText(t1.getText().toString());
}

Categories

Resources