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());
}
Related
<?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.
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.
Iam doing an android application. In that i want to create dynamic controls(edittext) like android core contacts application. After entering data in the ediitext i want to save the data to sqlite database by clicking a button named save. As Iam new to android i dont have any idea to create dynamic controls and storing its row values. Please help me if anybody knows.
My code:
package com.xiaochaoyang.dynamicviews;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" button
private Button mAddButton;
// There always should be only one empty row, other empty rows will
// be removed.
private View mExclusiveEmptyView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_container);
mContainerView = (LinearLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btnAddNewItem);
// Add some examples
inflateEditRow("Xiaochao");
inflateEditRow("Yang");
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// TODO: Handle screen rotation:
// encapsulate information in a parcelable object, and save it
// into the state bundle.
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
// Inflate a new row and hide the button self.
inflateEditRow(null);
//v.setVisibility(View.GONE);
}
// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
// Helper for inflating a row
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row, null);
final ImageButton deleteButton = (ImageButton) rowView
.findViewById(R.id.buttonDelete);
final EditText editText = (EditText) rowView
.findViewById(R.id.editText);
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
//deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
//mAddButton.setVisibility(View.GONE);
//deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.VISIBLE);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() );
}
}
My layouts:
Row Cointainer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical" >
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnAddNewItem"
android:layout_width="21dp"
android:layout_height="wrap_content"
android:background="#drawable/transparent_background"
android:gravity="center_vertical"
android:onClick="onAddNewClicked"
android:layout_gravity="right"
android:paddingLeft="5dp"
android:text="+"
android:textColor="#android:color/black" />
</ScrollView>
</LinearLayout>
Row.xml
<?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="50dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="true"
android:ems="10"
android:hint="" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/spinnerCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:entries="#array/categories"
android:gravity="right" />
<ImageButton
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/content_remove"
android:background="#drawable/transparent_background"
android:contentDescription="#string/button_delete_row_description"
android:onClick="onDeleteClicked"/>
</LinearLayout>
First of all, while posting your question in SO, please show what have you tried so far, post your logcat if your app is getting force closed.
To work with controls, please refer to http://developer.android.com/reference/packages.html
There you find the methods available to work with controls.
Check this link: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
It helps to learn and perform CRUD operations on SQLite Database.
At last, what you left with is, get data (or input) from controls, and insert into database.
Also, please go for googling.
does anybody has any idea to increase or decrease the word spacing in the text set on the textview in android. I want to do the word spacing and line spacing by providing any integer value...
May be this could help
By setting this property to your xml you can get it done
android:lineSpacingExtra
hi i had solved the problem of word spacing by replacing the single space by multiple spaces according to requirement. I am giving an example in which there are one textview and two button one is for increasing the space and one is for decreasing the space. the code is given below and used android:lineSpacingExtra for line spacing as suggested above.
package com.example.wordspacingexample;
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 MainActivity extends Activity implements OnClickListener {
private TextView mTextView;
private Button mIncrease;
private Button mDecrease;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
mIncrease = (Button) findViewById(R.id.btn_increase);
mDecrease = (Button) findViewById(R.id.btn_decrease);
mTextView.setTag(" ");
mIncrease.setOnClickListener(this);
mDecrease.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_increase) {
String space = (String) mTextView.getTag();
String text = mTextView.getText().toString();
mTextView.setText(text.replace(space, (space += " ")));
mTextView.setTag(space);
} else if (v.getId() == R.id.btn_decrease) {
String space = (String) mTextView.getTag();
String text = mTextView.getText().toString();
if (space.length() > 2) {
mTextView.setText(text.replace(space,
space = space.substring(0, space.length() - 2)));
mTextView.setTag(space);
} else if (space.length() == 2) {
mTextView.setText(text.replace(space, " "));
mTextView.setTag(" ");
}
}
}
#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;
}
}
This the code of the activity with the implementation.
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/btn_increase"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="Click to increase word space" />
<Button
android:id="#+id/btn_decrease"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_increase"
android:layout_centerHorizontal="true"
android:text="Click to decrease word space" />
Code of the xml file used. ScreenShots are given as:
I am new to android app development on eclipse IDE.
I am trying to make a basic app in which, on clicking the button both the image in ImageView as well as the text in EditText replaces with another image and text respectively.
But on doing so, primarily when the app starts, a button is displayed on the top, then the ImageView and then the EditText, they work fine, but on clicking the button, only image is replaced successfully while my EditText disappeared. Can you help me out why this happened and how to rectify it?
xml file:
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/click_it" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:src="#drawable/prac" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/imageView1"
android:layout_below="#+id/imageView1"
android:layout_marginTop="30dp"
android:ems="10"
android:text="hello!" >
<requestFocus />
</EditText>
</RelativeLayout>
java file:
package com.example.practicetwo;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
Button b1;
ImageView iv1;
EditText et1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(new R.id().button1);
iv1 = (ImageView)findViewById(new R.id().imageView1);
et1 = (EditText)findViewById(new R.id().editText1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv1.setImageResource(R.drawable.prac1);
et1.setText("how r u?");
}
});
}
#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;
}
}
Your image prac1 might be greater than the size of the screen, and as you replace prac with prac1 the EditText is pushed down. Try using LinearLayout instead of RelativeLayout.