I am having problem with limiting edittext length in android. Everything works perfect except when delete message.
My edit text control has max length of 16 characters and it will limit it for recognizing, displaying and reading first 16 characters but when I continue to type, it memorizes text somewhere in background and if I want to delete text it doesn't start to delete backwards from 16th character but from last one I have entered.
See below code.
I have even tried to add TextChangedListener it doesn't trigger 16 character is entered as there isn't any action.
I am using Nexus 7 for testing this functionality
LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/background"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/send_new_message_lblTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/send_new_message_to"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/send_new_message_lblToValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/send_new_message_lblTo"
android:ems="10"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<EditText
android:id="#+id/send_new_message_txtMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/send_new_message_lblToValue"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/send_new_message_hint"
android:inputType="textMultiLine"
android:lines="2"
android:maxLength="16"
android:scrollbarStyle="outsideOverlay" />
<Button
android:id="#+id/send_new_message_btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/send_new_message_txtMessage"
android:layout_marginTop="10dp"
android:text="#string/send" />
</RelativeLayout>
ACTIVITY
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
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 NewMessageSendActivity{
private TextView to;
private EditText message;
private Button send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
getViewInstances();
setControlActions();
}
private void getViewInstances() {
to = (TextView) findViewById(R.id.send_new_message_lblToValue);
message = (EditText) findViewById(R.id.send_new_message_txtMessage);
send = (Button) findViewById(R.id.send_new_message_btnSend);
to.setText(AppSettings.getInstance().getSelectedDevice().toString());
}
private void setControlActions() {
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String smsMessage = message.getText().toString();
Toast.makeText(this, smsMessage, Toast.LENGTH_SHORT).show();
}
});
}
}
I cannot reproduce your behavior. It seems everything is ok, and the text is correctly limited to 16 characters.
The only place where the keyboard keeps appending text, is in the field above it, but even when you select the suggested word, it is truncated before being inserted in the text field. That's why you don't see the event fired, because it's an internal event in the IME, not an Android one inside the EditText.
Related
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
findViewById returns null
(4 answers)
Null pointer Exception - findViewById()
(12 answers)
Closed 3 years ago.
I am new in android development. I'm trying to make a simple login and registration form. I have a TextView 'Sign up' which basically takes the user to the registerActivity when clicked on, but when everytime when i click on the sign up textView it crashes my app. I have searched for the solutions but i can't seem to figure out whats causing the crash.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#drawable/bg"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginTop="70dp"
app:srcCompat="#drawable/logo"
/>
<EditText
android:id="#+id/edittext_username"
android:layout_width="249dp"
android:layout_height="71dp"
android:layout_marginTop="20dp"
android:drawableLeft="#drawable/user"
android:drawablePadding="10dp"
android:textColorHint="#f8f8f8"
android:paddingLeft="15dp"
android:hint="#string/mobile"
android:textColor="#f8f8f8"
android:background="#drawable/rounded_edittext" />
<EditText
android:id="#+id/edittext_password"
android:layout_width="249dp"
android:layout_height="71dp"
android:layout_marginTop="20dp"
android:drawableLeft="#drawable/password"
android:drawablePadding="10dp"
android:textColorHint="#f8f8f8"
android:paddingLeft="15dp"
android:hint="#string/password"
android:textColor="#f8f8f8"
android:background="#drawable/rounded_edittext"/>
<Button
android:id="#+id/button_login"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#drawable/rounded_buttons"
android:text="#string/login"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Not Registered?"
android:textColor="#000000" />
<TextView
android:id="#+id/textview_register"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="5dp"
android:textStyle="bold"
android:textColor="#e8170c"
android:text="#string/register"/>
</LinearLayout>
JAVA:
package com.example.careerhunts.raw;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
EditText mTextUsername;
EditText mTextPassword;
Button mButtonLogin;
TextView mTextViewRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full
screen
setContentView(R.layout.activity_login);
mTextUsername = findViewById(R.id.edittext_username);
mTextPassword = findViewById(R.id.edittext_password);
mButtonLogin = findViewById(R.id.button_login);
mTextViewRegister = findViewById(R.id.textview_register);
mTextViewRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this,
RegisterActivity.class);
startActivity(registerIntent);
}
});
}
}
I tested same code, Its's working fine, by guessing i can tell that you may misspelled name of activity_login (may be you assigned different layout which doesnot have text view named textview_register) or may be you dont have activity named RegisterActivity.class.
I am using ViewSwitcher to switch between EditText and TextView. When I click the TextView for the first time, it changes to EditText and then I have to click EditText again so that the keyboard appears and I to be able to edit the text.
The behavior I want is to be able to edit the text from the first click on the TextView. I don't want the user to click twice each time they want to edit the text.
Is there any way to do that ?
Here is a sample of the code to make it more clear:
XML code
<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=".Trial">
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/user_name_switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/username_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="12dp"
android:paddingTop="8dp"
android:text="User Name"
android:textColor="#android:color/black"
android:textSize="18sp" />
<EditText
android:id="#+id/username_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="User Name"
android:inputType="textPersonName" />
</ViewSwitcher>
<Button
android:id="#+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="24dp"
android:layout_marginTop="24dp"
android:padding="12dp"
android:text="save" />
</LinearLayout>
Java code
package com.example.test
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class Trial extends AppCompatActivity {
TextView userNameTV;
EditText userNameET;
Button saveBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trial);
userNameTV = findViewById(R.id.username_tv);
userNameET = findViewById(R.id.username_et);
saveBtn = findViewById(R.id.save_btn);
saveBtn.setEnabled(false);
userNameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textViewClicked();
userNameET.setText(userNameTV.getText().toString());
}
});
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
switcher.showNext();
saveBtn.setEnabled(false);
}
});
}
public void textViewClicked() {
ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
switcher.showNext();
saveBtn.setEnabled(true);
}
}
I create a static expandaletextview in this example i used two Textview and two ImageButton so now how to i make this dynamic expandable..Any one can help me please because i m very new in android... Here below is my full static source code thanks in advance
MainActivity class
package com.example.atextalignment;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView descText;
ImageButton show, hide;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
descText = (TextView) findViewById(R.id.description_text);
show = (ImageButton) findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Show button");
show.setVisibility(View.INVISIBLE);
hide.setVisibility(View.VISIBLE);
descText.setMaxLines(Integer.MAX_VALUE);
}
});
hide = (ImageButton) findViewById(R.id.hide);
hide.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Hide button");
hide.setVisibility(View.INVISIBLE);
show.setVisibility(View.VISIBLE);
descText.setMaxLines(5);
}
});
}
}
my Xml layout
<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="com.example.atextalignment.Second" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:maxLines="5"
android:text="#string/desc_content" />
<ImageButton
android:id="#+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/description_text"
android:background="#android:color/transparent"
android:clickable="true"
android:src="#drawable/ic_launcher"/>
<ImageButton
android:id="#+id/hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/description_text"
android:clickable="true"
android:src="#drawable/ic_launcher"
android:visibility="invisible" />
</RelativeLayout>
String.xml
<string name="desc_content"> Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It\'s the largest installed base of any mobile platform and growing fast—every day another million users power up their Android devices for the first time and start looking for apps, games, and other digital content.
Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.
</string>
I am implementing a program that uses database and interacts with different layouts of entries and editing users.
I'm working with RelativeLayout on all screens. In one of thelayouts, I insert a perfectly aligned button and give the command android:visibility="gone" for him to show upon request.
The problem is that when I need to use it at command editarBt.setVisibility(View.VISIBLE), the button appears out of alignment and overlaps the fields for entering information.
Is there any way to keep the position of command by button?
I will not put the entire code because it has 7 classes, so I'll just put the classes that interest.
EnterPatientActivity Class
package br.luizhmu.aulas_android_sqlite;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by LuizHMU on 2/17/15.
*/
public class EnterPatientActivity extends Activity {
private Paciente paciente = new Paciente();
private EditText nomeEt;
private EditText emailEt;
private EditText senhaEt;
private Button salvarBt;
private Button editarBt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inserir_paciente);
nomeEt = (EditText) findViewById(R.id.editTextNome);
emailEt = (EditText) findViewById(R.id.editTextEmail);
senhaEt = (EditText) findViewById(R.id.editTextSenha);
salvarBt = (Button) findViewById(R.id.buttonSalvar);
editarBt = (Button) findViewById(R.id.buttonEditar);
Intent intent = getIntent();
if(intent != null){
Bundle bundle = intent.getExtras();
if(bundle != null){
paciente.setId(bundle.getLong("id"));
paciente.setNome(bundle.getString("nome"));
paciente.setEmail(bundle.getString("email"));
nomeEt.setText(paciente.getNome());
emailEt.setText(paciente.getEmail());
senhaEt.setVisibility(View.GONE);
salvarBt.setVisibility(View.GONE);
editarBt.setVisibility(View.VISIBLE);
}
}
}
public void salvar(View view){
paciente.setNome(nomeEt.getText().toString());
paciente.setEmail(emailEt.getText().toString());
paciente.setSenha(senhaEt.getText().toString());
DataBase bd = new DataBase(this);
bd.inserir(paciente);
Toast.makeText(this, "Paciente inserido com sucesso!", Toast.LENGTH_SHORT).show();
}
public void editar(View view){
paciente.setNome(nomeEt.getText().toString());
paciente.setEmail(emailEt.getText().toString());
DataBase bd = new DataBase(this);
bd.atualizar(paciente);
Toast.makeText(this, "Paciente \""+paciente.getNome()+"\" atualizado com sucesso.", Toast.LENGTH_SHORT).show();
}
}
activity_inserir_paciente.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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#ffffea0a"
tools:context=".EnterPatientActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Novo paciente"
android:id="#+id/textView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ff1727ff"
android:textSize="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="*Nome"
android:ems="10"
android:id="#+id/editTextNome"
android:layout_below="#+id/textView3"
android:layout_alignRight="#+id/buttonSalvar"
android:layout_alignEnd="#+id/buttonSalvar" />
<EditText
android:hint="Telefone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/editTextTelefone"
android:layout_below="#+id/editTextNome"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:hint="*E-mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editTextEmail"
android:layout_below="#+id/editTextTelefone"
android:layout_alignLeft="#+id/editTextTelefone"
android:layout_alignStart="#+id/editTextTelefone" />
<EditText
android:hint="*Senha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editTextSenha"
android:layout_below="#+id/editTextEmail"
android:layout_alignLeft="#+id/editTextEmail"
android:layout_alignStart="#+id/editTextEmail" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Salvar"
android:id="#+id/buttonSalvar"
android:onClick="salvar"
android:layout_below="#+id/textView4"
android:layout_alignRight="#+id/editTextSenha"
android:layout_alignEnd="#+id/editTextSenha" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Editar"
android:id="#+id/buttonEditar"
android:layout_alignTop="#+id/buttonSalvar"
android:layout_toLeftOf="#+id/buttonSalvar"
android:layout_toStartOf="#+id/buttonSalvar"
android:visibility="gone"
android:onClick="editar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="* Campos de preenchimento obrigatório"
android:textColor="#000000"
android:id="#+id/textView4"
android:layout_below="#+id/editTextSenha"
android:layout_alignLeft="#+id/editTextSenha"
android:layout_alignStart="#+id/editTextSenha" />
</RelativeLayout>
In a RelativeLayout, you can prevent views from overlapping by using the layout_toLeftOf, layout_toRightOf, layout_above and layout_below attributes, which expect a view id as value.
Also, you might want to use View.INVISIBLE instead of View.GONE: The former will consider the view during layouting, but hide it. The latter will pretend the view does not exist, therefore altering your layout result.
i want to have a set of buttons and the focus on buttons should change periodically first i want to try it with 2 buttons.this is my main.xml file.
<?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/number_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.72"
android:focusable="true"
android:gravity="center"
android:text="#string/number"
android:textSize="10pt" android:clickable="true"
android:focusableInTouchMode="true"/>
<Button
android:text="#string/contact"
android:textSize="10pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contact_button"
android:layout_weight="1" android:layout_gravity="center"
android:focusable="true" android:gravity="center"
android:focusableInTouchMode="true"/>
</LinearLayout>
in the source code i have used FOCUS_LEFT,RIGHT and all other options ,but i am getting a run time exception. the orientation is set as vertical in linear layout.Please help me to sort out this problem, if anybody know how to set the timer option as well post it here
here is the code
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class HelloandroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Object o=null;
// o.toString();
// TextView tv = new TextView(this);
// tv.setText("Hello Andriod");
setContentView(R.layout.main);
Button mybtn= (Button)findViewById(R.id.number_button);
mybtn.requestFocus();
try
{
Button mybtn2= (Button)mybtn.focusSearch(View.FOCUS_DOWN);
mybtn2.requestFocus();
}
catch(Exception e)
{
Log.e("focus change","focus failed",e);
}
}
}
The problem is that you is finding a control in mybtn. Inside your activity you should call focusSearch in Activity context. Try this:
Button mybtn2 = (Button) focusSearch(View.FOCUS_DOWN);
if (mybtn2 != null)
mybtn2.requestFocus();