How to change click event in android - android

Its been few days over to me, learning Android App development :
I have created an APK which is having two buttons (largeButton & smallButton) if you click on these button test will large and small accordingly.
What i am trying to do :
When i click second time button should change in previous mode i.e Vice Versa should happen.
Here is my code :
mainActivity:
package com.firstapk.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button largButton;
Button smallButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
largButton = (Button) findViewById(R.id.largbutton);
smallButton = (Button) findViewById(R.id.smallbutton);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//public void getDisplay(View view){
public void largebuttonclick(View view){
try{
largButton.setTextSize(40);
}catch(Exception e){
Log.d("TestApp", e.getMessage());
}
}
//}
public void smallButtonClick(View view){
try{
smallButton.setTextSize(5);
}catch(Exception e){
Log.d("TestApp",e.getMessage());
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/largbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="96dp"
android:onClick="largebuttonclick"
android:text="#string/button_large" />
<Button
android:id="#+id/smallbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="320dp"
android:onClick="smallButtonClick"
android:text="#string/button_small" />
</RelativeLayout>

As you did now it's right, if he click "Max size" he will see max size;
So maybe you want to use only one button which works like (+ and -)? If yes use one onClick and a boolean variable to save the current status (max size, small size).
private boolean maxSize; // false if small, true if max
public void onChangeSize(View view)
{
if (maxSize)//max->small
{
smallButton.setTextSize(5);
}
else//small->max
{
largButton.setTextSize(40);
}
maxSize = !maxSize;
}
Then android:onClick="onChangeSize" in the two buttons xml.
You could use the same approch for your current code, but it will work in the same way.

Related

button hovering not working in android

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- <TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="24dp"
android:text="#string/name" /> -->
<EditText
android:id="#+id/etname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10" android:inputType="text"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etname"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:text="Login" />
</RelativeLayout>
I have a button, I want when I "hover" over the button it shows a hello message with a toast.
I have a button in the layout. I tried to fetch it by findViewById in the FirstActivity. Then I tried using button.setOnHoverListener but it isn't working.
package com.example.datapass;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
final EditText name ;
Button loginButton ;
//final Context context = this;
/** Called when the activity is first created. */
//name= (EditText) findViewById(R.id.etname) ;
final TextView tv = (TextView) (findViewById(R.id.textView1) );
loginButton = (Button) findViewById(R.id.button1);
loginButton.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("hover", "Bring yor cursor over the button");
if(event.getAction()==MotionEvent.ACTION_HOVER_ENTER)
{
//tv.setText("hi");
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can anyone explain what's wrong?
It would be worth changing your onHover method to be like below and break once it carries out an individual action:
#Override
public boolean onHover(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
I would also advise changing this line as this is not standard format/syntax:
final TextView tv = (TextView) (findViewById(R.id.textView1) );
To be this instead:
final TextView tv = (TextView) findViewById(R.id.textView1);
However I think your actual issue is more relating to the support on mobile devices of hovering as none of your code actually seems like it would break this code section. The majority of devices can only recognise hovers whilst using a stylus and not actually just with a finger. Only some of the higher end devices can actually recognise a hover action with just a finger. This might be something to note and may be advisable to use a swipe or a click instead of a hover to display the Toast.

Imageview gets blurry

This app is supposed to display images in which the students can count up what the money is worth in total, and then input the value in a editText text box, which is then compared against a stored value. Unfortunately, when I try to switch activities past a certain point (there are 11 active activities, with three of them displaying images fine), the images start blurring and the coins are hard to distinguish from each other. I do not know whether this is a Java or XML error, however I have pasted the code below. The following is XML code.
<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=".Ldsm" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
<TextView
android:id="#+id/userQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout"
android:layout_alignLeft="#+id/userAnswer"
android:layout_marginBottom="30dp"
android:text="#string/how_many_coins_total"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/userAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/userQuestion"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="118dp" >
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/userQuestion"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/linearLayout"
android:src="#drawable/ic_coin4" />
That's just one of the 7 screens that look blurry when they display the images. The following is the Java code for the same activity.
package com.example.ldsm3;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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 com.example.ldsm3.Problem5;
public class Problem4 extends Activity
{
private final int COIN3_SCREEN_ANSWER = 95;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_problem4);
Button submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(submitButtonListener);
}
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private OnClickListener submitButtonListener = new OnClickListener()
{
public void onClick(View arg0)
{
EditText editText = (EditText)findViewById(R.id.userAnswer);
int userAnswerValue = Integer.parseInt(editText.getText().toString());
// Build the Alert Dialog
android.app.AlertDialog.Builder alert = new AlertDialog.Builder(Problem4.this);
alert.setTitle("Answer");
alert.setCancelable(false);
if(userAnswerValue == COIN3_SCREEN_ANSWER)
{
alert.setMessage("Congratulations!!!");
}
else
{
alert.setMessage("Sorry, that's not right.");
}
alert.setPositiveButton("OK",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
Intent nextCoinScreenIntent = new Intent(Problem4.this, Problem5.class);
startActivity(nextCoinScreenIntent);
}
});
alert.show();
}
};
}
And this a screenshot when it is running on a Nexus 10:
Please let me know if any more information is needed.
It seems like it was fetching the icons that were not designed for the particular screen size. You can fix this by going to the workspace and deleting every icon that is any other folder besides the "drawable" one. (With the exception of the ic_launcher icon, as that is the one that allows you to display your app in the Android app menu and will not get blurry.)
Better than your solution would be to make sure the proper resolution file is in every folder. The proper resolution is your desired resolution with the following multipliers, as shown on the Android Design documents:

Button not showing up on screen Android Application

I have created an application where users can send and share text that they have input, if they click the send button, the text that they input will be displayed, if they click the share button, the application opens up a list of sharing methods (GMail, Messaging etc..), what I want is though, to allow the users to view there text and then share it, hwoever, when the user clicks send and it goes to the file activity_display, the text shows, but the button does not. Any ideas why? Could you fix this? Here's the code;
activity_display_message.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="shareMessage"
android:layout_below="#+id/text_view"
android:text="test" />
</RelativeLayout>
MAIN PROBLEM: Button Share does not show.
EDIT: I suspect I may need to include something to this file, here's the code, any help?
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Since you are using RelativeLayout you need to specify where to layout your views. Try this for example:
<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=".DisplayMessageActivity" >
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="shareMessage"
android:layout_below="#+id/text_view"
android:text="test" />
you are using relative layout. use linear layout instead. this will solve your problem.
You are using a relative layout. The objects are overlapping because you have not set them up to be LeftOf or Below or something like that.
Just replace your button with:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:onClick="shareMessage"
android:text="#string/button_share" />
And TextView with this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />

Android button returning to activity from layout

I can't find a way how to make it work. So here it goes:
Application starts and I press Option menu and it offers me "Settings" option, and when I click it, it goes to layout called "help.xml" which shows me some text ...And in that layout I created a button which must return me to my activity ( the window which is shown when app starts)
I tried making a back button works but I failed cause I need for user to wait 30 seconds until the next image switch , and by making back button works hw would exploit it..
Sorry for my English, it is not my native language ;)
//** Povratak= return **//
MainActivity
package com.example.ams;
import java.util.Random;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
final Random rnd = new Random();
ImageView img = null;
Button btnRandom = null;
#Override
protected void onCreate(
final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imgRandom);
btnRandom = (Button) findViewById(R.id.btnRandom);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
public void clickHandler(final View v)
{
switch(v.getId())
{
case R.id.btnRandom:
{
if (!btnRandom.isEnabled())
{
return;
}
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(45);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);
btnRandom.setEnabled(false);
new CountDownTimer(30000, 1000) // Wait 30 secs, tick every 1 sec
{
#Override
public final void onTick(final long millisUntilFinished)
{
btnRandom.setText("Pričekaj do sljedeće kartice: " + (millisUntilFinished / 1000));
}
#Override
public final void onFinish()
{
btnRandom.setText("PROMIJENI KARTICU !");
btnRandom.setEnabled(true);
}
}.start();
break;
}
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
setContentView(R.layout.help);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public void goBack(View v){
startActivity(new Intent(this, MainActivity.class));
}
#Override
public void onBackPressed() {
}
}
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#drawable/bgi"
>
<ImageView
android:id="#+id/imgRandom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
<Button
android:id="#+id/btnRandom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imgRandom"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/imgRandom"
android:onClick="clickHandler"
android:text=" Promijeni karticu !"
android:textColor="#ffffff"
android:textSize="25dp" />
help.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" >
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="450dp"
android:fillViewport="true"
android:scrollbars="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:enabled="true"
android:freezesText="false"
android:overScrollMode="always"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
android:text="UVOD:Uz svaki scenario organizator moze odrediti da se koristi "AMS sustav" zbog realnijeg pristupa igri i viseg stupnja MILSIM-a. Organizator bira medice (ili kako se vec odredi) i oni moraju imati prilikom pocetka igre 46 kartica. />
</ScrollView>
<Button
android:id="#+id/button1"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:text="Povratak" />
So I want to "Povratak" button works, it needs to send user to lets called it "main menu" (go back)..
EDITED AND FIXED:
have another question, is there any way to activity remebers the counting, because when you enter the app you click button which random generates a image from drawable and it doesn't let user to press that button for 30 secs.. the problem now is that when you are waiting for counter go to 0 you can easily press option menu, click settings and click "povratak" ,which starts activity all over again and the counter losses its point because user can now again press button that generates image (and I dont want that):/
In your help.xml for your Povratak button, use:
android:onClick="goBack"
Then in your Help.java, use:
public void goBack(View v){
setContentView(R.layout.activity_main);
}
there is two way to fix that problem ,the first one is that you need to call the finish methode in your call back methode of the button like this :
in your help.xml file :
in your class Help.java implement your methode as follow :
public void Povratak(View v)
{
finish();
}
if that does not fix your problem you can start an intent to go to your main activity, you have to change the implimentation of your call back methode :
public void Povratak(View v){
Intent intent=new Intent(this,MainActivity.class);
startactivity(intent);
finish();
}
hope this help ,for more information about activity and intent you can see this tutoriel
click here

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