Edit text not receiving text - android

I have tried to solve it for hours but been unable to. What is going on is that I setted this editText, called the layout on the activity, and connect it to my variable on the activity, when you click on the field it opens the visual keyboard, but when you press a key, it takes you to a browser-like search screen, instead of just updating the edittext´s text.
on my xml the editText is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background = "#drawable/splash"
android:orientation = "vertical">
<EditText
android:id="#+id/searchEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/hintGray"
android:textSize="20dp"
android:paddingLeft="50dp"
android:paddingRight="10dp"
android:layout_marginTop="300dp"
android:background="#drawable/a01_text_box"
android:hint="Hint"
android:layout_gravity="center_horizontal" />
<ImageButton
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/a01_boton_buscar"
android:layout_gravity="center_horizontal" />
</LinearLayout>
on the activity I do this
public class SubscribeActivity {
private EditText searchEdit;
private ImageButton searchButton;
private Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe_main);
this.context = this;
findAndInitViews();
}
protected void findAndInitViews() {
searchEdit = (EditText) findViewById(R.id.searchEdit);
searchButton = (ImageButton) findViewById(R.id.searchButton);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String codeText = searchEdit.getText().toString();
Toast.makeText(context, codeText, Toast.LENGTH_LONG).show();
}
});
}
}
I still can't find a way for it not to happen. Can you help me?

Seems like you are not actually playing with activities
This is wrong:
public class SubscribeActivity
It should be something like:
public class SubscribeActivity extends Activity

first I would suggesting to use the getApplicationContext() Method of Activity - instead of casting the Activity down to your Context-Object. Beside you must extend Activity. Im wondering that your code is running... But I assume it's an copy and paste mistake.
Your onCreate(Bundle b) should be public instead of protected.
If the fault is still there I would comment the code not needed for displaying the EditText. And then step by step including the code back again.

Try it different way.
public class SubscribeActivity extends Activity{
private EditText searchEdit;
private ImageButton searchButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subscribe_main);
searchEdit = (EditText) findViewById(R.id.searchEdit);
searchButton = (ImageButton) findViewById(R.id.searchButton);
}
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String codeText = searchEdit.getText().toString();
Toast.makeText(SubscribeActivity.this, codeText, Toast.LENGTH_LONG).show();
}
});
}
}
Please try to do in this way and let me know the result.

Related

How to Move cursor from one EditText to another one and back to the first edit text if a custom button is clicked?

i am basically developing a small mathematics app, in a activity their will be problems like additions subtractions etc. the user has to fill the answers in edittext from the custom buttons from 0-9, a dot, a slash button and a backspace button which i created on the same activity. now i like to add up and down button, so that when the up button is pressed the cursor has to move towards upside edit text and vice versa.
here is a sample code which i used
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView et1;
TextView et2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(android.R.id.et1);
et2 = (EditText) findViewById(android.R.id.et2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) et2.getText(), et1.getSelectionStart());
et2.requestFocus();
}
});
}
}
You could create an array of of your EditTexts and a variable containing your current position, defaulted to 0, meaning the first text box. Then if the user presses the up button, if the variable is greater than 0, set the position -1 and then get the textbox object from the array and call focus(). Below is an example piece of code, its not accurate but should get you started
List<EditText> textfields = null;
int currentTextFieldPosition = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textfields = new ArrayList<EditText>();
textfield1 = (EditText)findViewById(R.id.textfield1);
.....
textfields.add(textfield1);
......
}
protected OnClickListener mBtnUpClickListener = new OnClickListener()
{
public boolean onClick()
{
if (currentTextfieldPosition > 0)
{
currentTextfieldPosition--;
textfields.get(currentTextFieldPosition).focus()
}
}
}

How to show/hide grouped views in Android?

I want to create an activity such as mentioned in photo...
as soon as I press the maximize button I want it to become full screen for the activity and part 1 become minimize, and again when I press the restore button I want it to become in a first state: to be able to see part 1 and part 2 ...
I think if we put two layouts it is possible? Isn't it? Please refer me to a resource to help me about this, or show me the code to achieve a solution.
Part one and two should be in their own layout. After, play with the visilibity property of each layout. Specifically to hide any view without it continues to occupy its space, use the value gone for the visibility property.
Ok, here I go. Below you have a complete example of how to hide/show grouped views.
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" >
<LinearLayout
android:id="#+id/viewsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextBox One" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Two" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="TextBox Three" />
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hide" />
</RelativeLayout>
Activity
public class MyActivity extends Activity implements OnClickListener {
private boolean viewGroupIsVisible = true;
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (viewGroupIsVisible) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
viewGroupIsVisible = !viewGroupIsVisible;
}
I hope this helps ;)
There is a bit simplified solution, than Diego Palomar produced, without using additional variable. I'll take his code to show:
public class MyActivity extends Activity implements OnClickListener {
private View mViewGroup;
private Button mButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewGroup = findViewById(R.id.viewsContainer);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View button) {
if (mViewGroup.getVisibility() == View.VISIBLE) {
mViewGroup.setVisibility(View.GONE);
mButton.setText("Show");
} else {
mViewGroup.setVisibility(View.VISIBLE);
mButton.setText("Hide");
}
}

Android ImageButton not firing the onclick event, what is wrong with my code?

I have very simple code that includes ImageButton with OnClickListener that points to another Activity, the click on the ImageButton doesn't fire the onClick (The same problem was with Button) :
public class ToolsActivity extends Activity {
private ImageButton btnCamera;
final Context context = ToolsActivity.this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
this.btnCamera = (ImageButton) findViewById(R.id.cameraButton);
this.btnCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tools, menu);
return true;
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<ImageButton
android:id="#+id/cameraButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/btncamera"
android:contentDescription="#string/desc" />
I can't see anything wrong with the code. If it works with a regular button my guess is that you maybe have to set android:clickable="true" in the xml (you can also do it in code).
I opened new Android project and copy paste may code, and it works, I guess something with the project was damaged.

How to create radio button without knowing exactly count?

How to create radio button in radioGroup in file layout without knowing exactly count
Dose anyone has any ideas?
I request you to say thing clearly,but as i got ,you want many number of radio Button,
Take a loop and create dynamic RadioButton add these button to Radio Group
May be this is of any help;
public class RadDemoActivity extends Activity {
private RadioGroup radGroup;
private Button addButton;
private RadioButton radButton;
private static int no;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
no=0;
doInflateItems();
setAddButton();
}
private void doInflateItems() {
radGroup = (RadioGroup) findViewById(R.id.radGroup);
addButton = (Button) findViewById(R.id.addRad);
}
private void setAddButton() {
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
radButton = new RadioButton(getApplicationContext());
radButton.setText("Option " + no++);
radGroup.addView(radButton);
}
});
}
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="#+id/addRad" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Add" />
<ScrollView android:id="#+id/vScroll" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioGroup android:id="#+id/radGroup"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
This code is adding radio buttons from a button click, you can do the same from a loop if you want to add multiple buttons at once. Hope this helps.

Update EditText after Button is clicked

This EditText is inside an Activity that is part of a TabHost within the main Activity. It's just supposed to be 4 tabs with an EditText and two Buttons on each, one to increment and one to decrement the EditText field. However, if I ever try to setText() on one of the EditText boxes, the app crashes. So when I call setText() in onCreate() it crashes. Any help would be greatly appreciated!
<EditText
android:label="#+id/LifeForP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoText="true"
android:cursorVisible="false"
android:background="#null"
android:textColor="#999999"
android:color="#null"
android:layout_x="90px"
android:layout_y="0px"
android:textSize="250px"
android:maxLength="3"
android:capitalize="sentences"
android:layout_weight="1"
android:freezesText="true"
android:text="20"
/>
public class ActivityTab1 extends Activity {
private EditText lifeView;
int p1Life = 20;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content1layout);
lifeView = (EditText) findViewById(R.id.LifeForP1);
lifeView.setText(getString(R.string.lifeStart)); //Error here
}
#Override
protected void onResume() {
super.onResume();
}
public void p1GainLifeListener(View view) {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("test gain 1");
alertDialog.show();
//String show = String.format("", Integer.toString(++p1Life));
//lifeView.setText(show);
}
In your xml, change
android:label="#+id/LifeForP1" to
android:id="#+id/LifeForP1"
When you bind your EditBox in code you reference to
findViewById(R.id.LifeForP1);
This means the EditBox needs an ID.
If you change the android:label to an android:id your code will run fine

Categories

Resources