Using Kitkat but also tried on jellybean and im not able to get a custom error icon to appear.
Im using these two overloaded methods for setError(...) from Android doc
Im just doing very simple code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity implements OnCheckedChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.report_issue);
RadioGroup rg = (RadioGroup) findViewById(R.id.myradio_group);
rg.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radio_two) {
EditText et = (EditText) findViewById(R.id.et_city);
// rb.setError("i got a single error");
et.setError("my error",
getResources().getDrawable(R.drawable.ic_launcher));
//as you can see from above im setting a image for the error thru code
et.requestFocus();
}
}
}
heres my mylayout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mycontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/myradio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="radio1" />
<RadioButton
android:id="#+id/radio_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="radio2" />
</RadioGroup>
<EditText
android:id="#+id/et_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter city" />
</LinearLayout>
Here is a screenshot of what im seeing on jellybean and kitkat:
here is a image where its working fine when i remove the custom image icon and only call :et.setError("my error");
I actually got it to work. I had to first set the bounds on the drawable something like this:
Drawable d= getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0,
d.getIntrinsicWidth(), d.getIntrinsicHeight());
et.setError("my error",d);
Android OS bug with some devices running Jelly Bean/4.2.1 - TextView.setError(CharSequence error) Missing icon
i found this and it working like charm, hope this work with you too
Related
I'm brand new to Android Studio and for whatever reason am experiencing really weird errors if I could please get some help.
I have very basic code that is supposed to, on button click change the text of my button from "button" to "clicked!". However everytime I press the button, the app crashes and I get "Appname has stopped" on the emulator.
What is incredibly weird is that in my activity_main.xml Design view, the onClick dropdown shows two functions of the same name (https://puu.sh/t2h5I/42ad4379d6.png)
H owever the code only works when the bottom one is selected. AND each time I run the app, it deselects the bottom one and reselects the top, only to stop working.
Here is my MainActivity:
package com.example.john.ameladay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button melButtonCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonPress(View v){
melButtonCode = (Button) v;
((Button) v).setText("Has been clicked!");
}
}
Here is my activity_main.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:id="#+id/activity_main"
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="com.example.john.ameladay.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mainText"
android:id="#+id/textView" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/mela"
android:id="#+id/melPhoto" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginBottom="43dp"
android:id="#+id/button"
android:onClick="buttonPress (MainActivity)" />
Any help would be greatly appreciated!!
Simple. You should write:
android:onClick="buttonPress"
Why happened
If you wrote buttonPress (MainActivity), Android tries to find buttonPress (MainActivity) method (not MainActivity.buttonPress()), but MainActivity doesn't have buttonPress (MainActivity) method. So the error happened.
Simply replace this Tag in button
Remove this
android:onClick="buttonPress (MainActivity)"
And Paste This
android:onClick="buttonPress"
A better way to do it is, get a reference to the button in your Java code using findViewById() method and set an OnClickListener to the button.
For your current problem, use
android:onClick="buttonPress"
instead of
android:onClick="buttonPress (MainActivity)"
According to me this is the better way to set click on button
public class MainActivity extends AppCompatActivity {
public Button melButtonCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
melButtonCode = (Button).findViewById(R.id.button);//find button by Id
melButtonCode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
melButtonCode.setText("Has been clicked!");
}
});
}
}
For your problem
replace it
android:onClick="buttonPress (MainActivity)"
With
android:onClick="buttonPress"
Make the method public - protected works when you are in instantrun mode, but not when not. No idea why!
I am new to android programming and there are a few things I don't quite know how to do yet. I am doing a course on udemy and was trying to put together everything I have learned up to a certain point.
What I am trying to do is have the user click on a button (i have 12) and have it bring up a textField where they can enter two numbers. I just want to be able to get the user's two numbers and i'm pretty sure I can figure out the rest ( I hope). I just don't understand how to go about doing this.
Any help would be much appreciated. Basically all I want to do is to be able to have the user click on one of the 12 buttons and be asked to enter two values, then take those values and perform a calculation on it.
Your xml could be like this:
<?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" >
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/b_ok"
android:text="Click Me"/>
<EditText android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/et_showme"/>
</LinearLayout>
and your activity could be like this:
package com.william.kinaan.welcomeback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test1 extends Activity implements OnClickListener {
private Button b_ok;
private EditText et_showme;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
initialize();
}
private void initialize() {
this.b_ok = (Button) findViewById(R.id.b_ok);
this.b_ok.setOnClickListener(this);
this.et_showme = (EditText) findViewById(R.id.et_showme);
}
#Override
public void onClick(View v) {
this.et_showme.setVisibility(View.VISIBLE);
}
}
Create an Activity that has 2 EditTexts
When user click a button the Activity is started and user can enter the Numbers
Try this.
<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" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_id"
android:visibility="invisible"
android:text="sample text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/click"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And in your activity, you can do like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button button = (Button) findViewById(R.id.click);
final EditText text = (EditText) findViewById(R.id.tv_id);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setVisibility(View.VISIBLE);
}
});
}
I surdenly noticed that My project starts throwing error anytime I try to access a resources that is a button. It underlines R.id.button. I dont understand why. I even deleted the last xml that I created but problem persist.
This is an example of my xml file
<?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:background="#drawable/layoutborder"
android:orientation="vertical" >
<TextView
android:id="#+id/chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/stepone"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/wine" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:src="#drawable/ai" />
<Button
android:id="#+id/drugdetails"
style="#style/smallButtonStyleBlackpearl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="#string/nextbut" />
</LinearLayout>
My Java code
package com.example.rhemahealthcare;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.actionbarsherlock.app.SherlockActivity;
import com.example.rhemahealthcare.R;
public class SteponeActivity extends SherlockActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.steponeactivity);
final Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent intent = new Intent(SteponeActivity.this,SteptwoActivity.class);
startActivity(intent);
}
});
}
}
i think you change any button1 id buy clicking right click and choose edit id. this option changes all the ids with that name in all the layouts.
As #Aleks G gussed it right in the comment, you don't have any button with id as button1 in your xml file. You've mentioned it :
final Button button = (Button)findViewById(R.id.button1);
Use the appropriate ID or put one in your layout file.
I have figured out the problem. My button ids were automatically change to button1 so they did not reference their previous ids that I gave to them. Thanks alot
i'm a newbie of android :)
I want to create a simple slideshow with a numeric parameter(x) and a button.I have 21 images and, at the click of the button, I want to catch x and "slide" only the first x images of the list.Between one image and the next i want wait 1 second.
At the end i want to know how much time passed during the operation. (sorry for my english)
paste below the code of the activity and the layout:
package com.superpibenchmarknative;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageActivity extends Activity implements OnClickListener {
private Button start;
private EditText iteractions;
private TextView time;
private ImageView display;
private long after,before;
private int images[] = {R.drawable.image1,R.drawable.image2,R.drawable.image3,
R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,
R.drawable.image8,R.drawable.image9,R.drawable.image10,R.drawable.image11,
R.drawable.image12,R.drawable.image13,R.drawable.image14,R.drawable.image15,
R.drawable.image16,R.drawable.image17,R.drawable.image18,R.drawable.image19,
R.drawable.image20,R.drawable.image21,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
setUpUI();
}
private void setUpUI() {
// TODO Auto-generated method stub
start = (Button) findViewById(R.id.bntStartCalcolation);
iteractions = (EditText) findViewById(R.id.etIterations);
time = (TextView) findViewById(R.id.tvTime);
start.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
//Creo le imageview in base al # di iteractions
before = System.currentTimeMillis();
for (int j = 0; j < Integer.parseInt(iteractions.getText().toString());j++){
display = (ImageView) findViewById(R.id.ivDisplay);
display.setImageResource(images[j]);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
display = null;
System.gc();
}
}, 1000);
}
display = null;
System.gc();
after = System.currentTimeMillis();
time.setText(String.valueOf(after-before-1000* Integer.parseInt(iteractions.getText().toString())));
}
}
and this is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ImageActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Benchmark del rendering di immagini" />
<EditText
android:id="#+id/etIterations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:hint="# di iterazioni"
android:inputType="number"
android:padding="5dp" />
<Button
android:id="#+id/bntStartCalcolation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:text="Start Calcolation" />
<ImageView android:id="#+id/ivDisplay"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_weight="50"
android:text="Risultato ottenuto in: " />
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_weight="50" />
</LinearLayout>
this code dosn't work, it create an exception
OutOfMemoryError: bitmap size exceed the vm budget
anyone can help me to solve this problem?? :) thanks at all however :D
great all works :D but i've a little more questions
I don't understand why the time is always negative!!!
anyone of you know why?? [postDelayed not works?!? :( ]
solved with: android.os.SystemClock.sleep(1000);
Another error, the ImageView dosn't change after the setting of an image, any suggestions??
There is a small guide within the developer site regarding loading large bitmap files. The idea is to load lower resolution files so that they should fit in memory: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Add the largeheap to your application in your Manifest file.
<application ... android:largeHeap="true" >
Alternatively, make your photos smaller. You might have a variety of sizes dependent on what size of screen you are using, that will make it much less likely to take all of the memory up. See this page to find Android screen resolutions.
I'm trying to get a simple onClick to fire from an ImageButton - it seems like a simple enough task, but I'm obviously missing something here.
Here is my java file:
package com.jlbeard.android.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class testapp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//handle the button press
ImageButton mainButton = (ImageButton) findViewById(R.id.mainButton);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//show message
Toast.makeText(testapp.this, "Button Pressed", Toast.LENGTH_LONG);
}
});
}
}
Here is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/whereToEat"
android:src="#drawable/where_to_eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8px"
/>
<ImageButton
android:id="#+id/mainButton"
android:src="#drawable/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#null"
android:clickable="true"
android:onClick="mainButtonClick"
/>
</RelativeLayout>
It seems to me that I'm missing something simple... but can't seem to figure it out. Thanks!
You didn't run show() method on Toast object. Very common mistake :-)
You also might have a problem due to the manifest setting onClick
android:onClick="mainButtonClick"
If mainButtonClick exists on post 1.5 devices it may be called instead, overriding the one you're setting in code
In my case, the imageButton was displayed behind a list. Because the list was empty, the ImageButton was seen but onClick was never fired.
Adding android:elevation="5dp" in the screen xml solve my problem
Note that if I use Button instead of ImageButton, elevation is not required.