I've read a few questions and answers regarding this topic, but none of them applied to my issue.
My problem is in the following code:
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.alt);
}
public void buttonPress(View v){
TextView tv1 = (TextView) findViewById(R.id.text1);
TextView tv2 = (TextView) findViewById(R.id.text2);
Button b1 = (Button) findViewById(R.id.button1);
if((tv1 != null) & (tv2 != null) & (b1 != null)){
tv1.setText(R.string.changed);
tv2.setText(R.string.changed);
b1.setText(R.string.changed);
}
else
Toast.makeText(getApplicationContext(), "VIEW ELEMENTS ARE NOT BEING ASSIGNED", Toast.LENGTH_SHORT).show();
}
alt.xml looks like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:name="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/stuff" />
<TextView
android:name="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/stuff2" />
<Button
android:name="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttonText"
android:onClick="buttonPress" />
The IDs are being generated in R.id, but they aren't being assigned to my two TextView elements and my one Button element. (I'm seeing the toast popup every time)
Does anyone have any idea?
Instead of android:name="..." use android:id="#+id/..."
Try replacing
android:name="#+id/text1"
By
android:id="#+id/text1"
Related
I need to add a textView and ImageView at the bottom of the screen
I need to hide the textView and show it again programmatically
here I have this image to explain the Question and how the application should behave
the left rectangle desplays the TextView shown and the second for Hiding Textview:
I tried the code below to do that:
1- I added click listener to ImageView that hide the TextView and Change the LayoutParams of ImageView
2- On Clicking it again it will show the TextView and align the ImageView above it
but the problem is that when I run the code the ImageView is stuck to
the top of the screen
I'm trying to change the LayoutParams of a view pragmatically using this code :
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#id/tv1" />
</RelativeLayout>
and the class MainActivity.java :
public class MainActivity extends Activity {
TextView tv;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.img);
tv = (TextView) findViewById(R.id.tv1);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
if(tv.getVisibility() == MyNavigationBar.GONE){
tv.setVisibility(MyNavigationBar.VISIBLE);
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
image.setLayoutParams(imageParams);
} else {
tv.setVisibility(MyNavigationBar.GONE);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
image.setLayoutParams(imageParams);
}
}
});
}
}
I think the problem is that the function :
imageParams.addRule(RelativeLayout.ABOVE, R.id.bottomNavigationBar);
but its not work
I also tried to set an Id manually to the textView like:
tv.setId(1);
and changing the previous to :
imageParams.addRule(RelativeLayout.ABOVE, tv1.getId());
Also doesn't work for me !!
I wonder that whether the problem in my code or in the android SDK !!
Seems layout_alignWithParentIfMissing would be much easier:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignAbove="#+id/tv1"
android:layout_alignWithParentIfMissing="true" />
</RelativeLayout>
And only switch visibility of tv1 in your code:
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(tv.getVisibility() == View.GONE){
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
})
I have Googled and read several Stack Overflow answers and I am pretty sure the problem is with my layout file here. I also cleaned my project so my R.java file was deleted, but I think after I find the error it will be auto-generated.
Here is my 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"
android:padding="20sp"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/update"
android:textSize="25sp" />
<EditText
android:id="#+id/guess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="26sp" />
<TextView
android:id="#+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="6sp"
android:paddingTop="6sp"
android:text="#string/default"
android:textSize="18sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="evaluate"
android:text="#string/btn" />
</LinearLayout>
Here are the java lines where there is an error shown, in case there's a problem here:
Button button = (Button) view;
TextView log = (TextView)(findViewById(R.id.display));
EditText field = (EditText)(findViewById(R.id.guess));
Update: Here is my evaluate method from the MainActivity.java file
public void evaluate(View view)
{
Button button = (Button) view;
TextView log = (TextView)(findViewById(R.id.display));
EditText field = (EditText)(findViewById(R.id.guess));
if(button.getText().toString().charAt(0) == 'S')
{
int guess = Integer.parseInt(field.getText().toString());
if(guess == num)
{
log.setText("Congratulations! You guessed it in " + guessCount + "moves");
button.setText("New game");
}
else
{
guessCount++;
if(guess < num)
log.setText("Try again. Too low");
else
log.setText("Try again. Too high");
}
}
else
refresh(button, log);
}
R.java is deleted whenever there exists any compile error in res folder or in AndroidManifest.xml file. Assuming you have no compile errors(except the files that should be referencing fields in R.java), my first call is to look at evaluate method to see if it is in the current invoking Activity as follows:
Java Code:
public class MyActivity extends Activity {
Button button;
TextView log;
EditText field;
...
...
#Override
public void onCreate(Bundle savedInstanceState) {
button = (Button) new Button(this);
log = (TextView) findViewById(R.id.display);
field = (EditText) findViewById(R.id.guess);
...
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
evaluate(v);
}
});
}
...
...
// **evaluate** is visible(public) and
// within the same activity where the **button** is
public void evaluate(View v) {
...
...
}
}
`
doesn't eclipse show you any error in the foler of res?
in the line of: android:onClick="evaluate", I haven't seen such value before, is it correct?
Click on text view is not working
My xml is
<TextView
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="3"
android:gravity="center"
android:textColor="#color/white"
android:text="click to download sevenstepstocollege"
android:textSize="15dp"
android:onClick="downloadLink"
android:clickable="true">
</TextView>
and My Activity code is
public void downloadLink(View v)
{
//String requestId = PurchasingManager.initiatePurchaseRequest(skuKye);
//String requestId=PurchasingManager.initiateItemDataRequest("DeveloperSKU-1234");
skuSet.add(skuKye);
final String requestId = PurchasingManager.initiateItemDataRequest(skuSet);
}
But it is not working.I am not able to click that link.please guide me
Well, I use the following code to make a TextView clickable.
First, add this at your activity.xml, to make TextView clikable:
<TextView
android:id=android:id="#+id/button2" <!-- id to get this TextView -->
(...)
android:onClick="onClick" <!-- Add the function onClick() -->
android:clickable="true" <!-- Set the boolean clickable to true -->
/>
Then, at your MainActivity.java, you add:
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the TextView by id
textview = (TextView) findViewById(R.id.button2);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO when clicked on your link(TextView)
}
});
}
As I see now that you are trying to make a link from the TextView clickable, not just be able to click on the TextView I will let a link below to a similar question solved in Stack Overflow that you might find helpful.
The android developer reference to TextView:
https://developer.android.com/reference/android/widget/TextView.html
The similar question in Stack Overflow about how to make a links in a clickable that might be helpful:
How do I make links in a TextView clickable?
You may use like this
<TextView
android:id="#+id/topPaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:clickable="true"
android:text="#string/topPaid"
android:textColor="#000000"
android:textColorLink="#33CC33" />
and At activity
TextView topPaid = (TextView) findViewById(R.id.topPaid);
Linkify.addLinks(topPaid, Linkify.ALL);
topPaid.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//write ur logic
}
}
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(Html.fromHtml("<b>Text:</b> Text with a " + "link " + "Created in the Java source code using HTML."));
XML Code
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
JAVA Code
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(Html.fromHtml("google "));
textView.setMovementMethod(LinkMovementMethod.getInstance());
In your XML as the following:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onTextViewPressed"
/>
In your attached activity
public void onTextViewPressed(View view) {
...
}
I have a simple activity which needs to accept a 4-digit PIN number using a custom pin-pad (buttons on the screen). The digits are stored in four EditTexts.
When I click a button, the text in the button is stored in a char[] (myEditText.getText() shows that this is happening), the focus is moved to the next EditText through an onFocusListener() and the Log output shows that this is all happening correctly.
11-20 10:19:56.969: I/DebugA(17742): Pin1 updated to: 1 // Pin1 is the ID
11-20 10:19:58.289: I/DebugA(17742): Pin2 updated to: 2 // '2' is Pin2.getText()
11-20 10:19:58.849: I/DebugA(17742): Pin3 updated to: 3
11-20 10:19:59.659: I/DebugA(17742): Pin4 updated to: 4
The screen itself is simply not being updated. The EditTexts all appear empty, even though the code executes perfectly.
I have looked through a whole heap of answers on SO and tried many of the suggestions with absolutely no luck, so I am posting this question which I hope someone can shed some light on! Has anyone else had this happen?
Here's some of the layout file:
<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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" >
<EditText
android:id="#+id/Pin1"
android:tag="Pin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="2"
android:gravity="center_vertical|center_horizontal"
android:nextFocusDown="#+id/Pin2"
android:maxLength="1"
android:inputType="textPassword" />
...
...
...
</RelativeLayout>
...and the buttons are:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:layout_below="#+id/layout1" >
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:text="1" />
<Button
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="2" />
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="3" />
</LinearLayout>
The activity is a standard activity (MyActivity extends Activity).
Any thoughts?
UPDATE
Here's some of the java as requested:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.activity_login, null);
EditText Pin1 = (EditText) layout.findViewById(R.id.Pin1);
Pin1.setOnFocusChangeListener(focusListener);
Pin1.setText("1"); // <--THIS HAS NO EFFECT
EditText Pin2 = (EditText) layout.findViewById(R.id.Pin2);
Pin2.setOnFocusChangeListener(focusListener);
EditText Pin3 = (EditText) layout.findViewById(R.id.Pin3);
Pin3.setOnFocusChangeListener(focusListener);
EditText Pin4 = (EditText) layout.findViewById(R.id.Pin4);
Pin4.setOnFocusChangeListener(focusListener);
...
...
...
}
The Onclick method is:
public void btnPinClick(View btnPin) {
String PinNumber = ((Button)btnPin).getText().toString();
RelativeLayout layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_login, null);
ArrayList<View> views = (ArrayList<View>) layout.getFocusables(View.FOCUS_DOWN);
EditText nextItem = null;
for (int i = 0; i < views.size(); i++) {
// THIS IS THE CORRECT EditText FOR EACH OF THESE
EditText textBox = (EditText) views.get(i);
if (textBox.getTag().toString().equals(currentText)) {
textBox.setText(PinNumber);
pin[i] = PinNumber.toCharArray()[0];
Log.i("DebugA", textBox.getTag().toString() + " updated to: " + textBox.getText().toString());
textBox.invalidate();
textBox.refreshDrawableState();
if(i+1 < views.size()) {
nextItem = (EditText) views.get(i+1);
}
else {
doCheckPin();
}
break;
}
}
if(nextItem != null){
nextItem.requestFocus();
currentText = nextItem.getTag().toString();
}
}
UPDATE 2
Here is the code for the OnFocusChangeListener as requested:
protected OnFocusChangeListener focusListener = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//THIS SIMPLY SETS A VARIABLE STRING TO SAY WHICH
//EditText HAS FOCUS.
currentText = v.getTag().toString();
}
}
};
It looks like you're using a layout inflater in addition to setContentView in your activity. And affecting the edittexts referenced by the inflater. Try removing use of the inflater and rely solely on setContentView for this part of the activity. You might be performing in an incorrect view hierarchy.
I have updated to ADT version 20. After that I can't use EditText. For API level 16 it shows some error. For lower API levels there is no error but I can't get the input of EditText. It shows an empty result.
Sample code I used:
java code
public class sample extends Activity {
EditText edt;
Button btn;
TextView txt;
String str;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView2);
str = edt.getText().toString();
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
}
}
XML CODING
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Search"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10pt"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
Actually, you are trying to get text from EditText in onCreate() of Activity, which has no any text contains yet.
So Just put the line, str = edt.getText().toString(); in button's click() then you will get the result,
Like,,
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
str = edt.getText().toString();
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
Your statement str = edt.getText().toString(); should be inside the onClick handler.
The code cannot access the value because after onCreate ends the value found in the EditText abd str is beyond the scope. So put the str = edt.getText().toString(); statement inside the onClick so that when user clicks the button it can access the value.
I think you are trying to get EditText (i.e. edt) value in onCreate() and I think you are entering value after the completion of onCreate() method.So that means you are not getting value for your str.
That's why you are not able to set the value to TextView.
just put text inside your button otherwise it will not work becz whatever u are getting in strings is also working like u are just cre