changing string value displayed in scrollView - android

first I would like to say thank you to every one out here as i am a nOOb and have learned a lot just by reading questions and answers that you post. I am trying to pass a great deal of text to the end users and while being able to do this with new classes and .xml files this is becoming cumbersome. i thought of stream lining the app by just having a single xml layout for a particular set of text strings and just change the #string/????? via button onclick and setText but have learned that I can not change the initial value of #string in an xml file. question is that TRUE? and is there a more efficient way to do this ie (setting android:text to a var and setting var in java to a particular string) or do i need a new xml layout for each string? (that's a lots of waste if you ask me) and a little insight, there are at this time approx 250 different strings with min 5 paragraphs and growing.
here is my code thus far.
snippet of first java
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Monlt extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.monolt);
final MediaPlayer buttonsound = MediaPlayer.create(Monlt.this, (R.raw.buttonclick));
Button button1 = (Button) findViewById(id.button1);
Button button2 = (Button) findViewById(id.button2);
Button button3 = (Button) findViewById(id.button3);
Button button4 = (Button) findViewById(id.button4);
Button button5 = (Button) findViewById(id.button5);
Button button6 = (Button) findViewById(id.button6);
Button button7 = (Button) findViewById(id.button7);
Button button8 = (Button) findViewById(id.button8);
Button button9 = (Button) findViewById(id.button9);
Button button11 = (Button) findViewById(id.button11);
Button button12 = (Button) findViewById(id.button12);
Button button13 = (Button) findViewById(id.button13);
Button button14 = (Button) findViewById(id.button14);
Button button15 = (Button) findViewById(id.button15);
Button button16 = (Button) findViewById(id.button16);
Button button17 = (Button) findViewById(id.button17);
Button button18 = (Button) findViewById(id.button18);
Button button19 = (Button) findViewById(id.button19);
Button button21 = (Button) findViewById(id.button21);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("mono1"); //this was my first string to pass
startActivity(new Intent("com.nvar.Sorders.Mono.ASO1"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonsound.start();
startActivity(new Intent("com.nvar.Sorders.ASO1"));
final TextView mview = (TextView) findViewById(R.id.solayout2);
mview.setText("#string/mono2");/this is the second string to pass
}
});
`
now this code works when i remove the 2 text view lines in the onclick
so then it call another class Aso1 that I would like to keep in place for later use.
Aso1 java code
`
package com.nvar.Sorders.Mono;
import com.nvar.Sorders.R;
import android.app.Activity;
import android.os.Bundle;
public class Aso1 extends Activity {
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.solayout2);
}
}
`
and then the first xml
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/solayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mview"/>
<!-- android:text="#string/monoaso1"/>
-->
<!-- this is were i was playing with the strings />
-->
</LinearLayout>
</ScrollView>
`
any help in this matter would be greatly appreciated, and remember "noob" to java / android! so if there is a sample of what i could or should be looking at, don't hesitate to smack me in the head and point me in the right direction. i don't mind reading :)
thanks again.

When displaying the Strings, What you could use is a TextView and have multiple lines and then just update the text value of it in code. It doesn't even need default text
<ScrollView
...some params...>
<TextView
android:id="#+id/my_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="10"/>
</ScrollView>
This will give you a TextView 10 lines long. Then in code you can do something like this to update the value:
String longText = getVeryLongText();
((TextView)findViewById(R.id.my_text)).setText(longText);
Then you'll have something that looks like a scrollable paragraph of text

*#string/string_name* in xml are not designed to change the value. They are there to help you with localization. Currently that xml is located here http://developer.android.com/guide/topics/resources/localization.html
res/values/string.xml
you can have another string xml with different language like in the following location, for example france
res/values-fr/strings.xml
you can read more about that over here.
Now, let's move on to how to reference that string_name from java
Resources res = Monlt.this.getResources();
mview.setText(res.getString(R.string.string_name));
//do something
mview.setText(res.getString(R.string.mono2));

Related

how to prevent a user from clicking on a button after another button has been clicked?

I am new to android programming and am building a quiz app in which a question has 4 options and if the user clicks on one of the options the other options should be unclickable. I am currently able to only make a single button unclickable. Here is the java code.
package com.example.android.quiz;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//This method is called when option 1 of question 1 is selected
public void verifyQuestion1Option1(View view) {
Button QuestionOption1 = (Button) findViewById(R.id.question1_option1);
QuestionOption1.setBackgroundColor(getResources().getColor(R.color.solid_red));
question1Answer();
}
public void verifyQuestion1Option2(View view) {
Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
Question1Option2.setBackgroundColor(getResources().getColor(R.color.solid_red));//solid red is not a predefined colour. It is declared in colors.xml
question1Answer();
}
public void verifyQuestion1Option3(View view) {
Button Question1Option3 = (Button) findViewById(R.id.question1_option3);
Question1Option3.setBackgroundColor(getResources().getColor(R.color.solid_green));
question1Answer();
}
public void verifyQuestion1Option4(View view) {
Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
Question1Option4.setBackgroundColor(getResources().getColor(R.color.solid_red));//We call the getResources() method because R.colour.solid_red passed the id of the color not the actual colour value.
question1Answer();
}
public void question1Answer() {
TextView q1Answer = (TextView) findViewById(R.id.question1_answer);
String answer = "Rajinish Kumar is the current Chairman of SBI who took over after Arundhati Bhattacharya retired on 6 October.Shikha Sharma is the Managing Director and CEO of Axis Bank and Chanda Kochhar is the managing director and CEO of ICICI Bank";
q1Answer.setText(answer);
}
}
Either you can use a buttongroup which will have only 1 active button at any point of time or else, you need to disable other button programatically.
To disable the button you can use the following code:
Button button = (Button) findViewById(R.id.button);
button.setEnabled(false);
You can use .setEnabled(false); to disable a button. That button will grey out and does not respond to click events any more.
To disable all buttons, get the handle to each button and set them to disabled.
Button Question1Option1 = (Button) findViewById(R.id.question1_option1);
Button Question1Option2 = (Button) findViewById(R.id.question1_option2);
Button Question1Option3 = (Button) findViewById(R.id.question1_option3);
Button Question1Option4 = (Button) findViewById(R.id.question1_option4);
Question1Option1.setEnabled(false);
Question1Option2.setEnabled(false);
Question1Option3.setEnabled(false);
Question1Option4.setEnabled(false);
That way, all buttons of this question become disabled.
You can also come up with a solution where you save that the button is already pressed and ignore further click events. You could introduce some sort of variable bool question1answered = false; that is set to true as soon as the onClick event is fired.
public void verifyQuestion1Option4(View view) {
if (question1Answered == true) {return;}
question1Answered =true;
//Do the rest of your checks here
}
Two tips for for programming Java:
Java (in contrast to i.e C#) used lower letter variables as convention.
Button question1Option1 = (Button) findViewById(R.id.question1_option1); would be a better way.
If you have more questions, it would make sense to put them in some sort of array and reuse the same four buttons multiple times. That would save you much programming overhead and code rewrites if you have to change something. And it keeps the code cleaner.

Asdroid SDK Eclipse : Simple App gave Black Screen and Stopped?

First of all I'm totally new with Eclipse so my Code maybe full of errors *
Second I wrote a simple App about two buttons (Add,Sub) with One TextView , the idea is about click one of these buttons and then the TextView changes and gives a number (Add=+1 , Sub = -1) , I'm Sure Compiler gave No Errors
But I don't know why I have a lot of problems :
1-The Apk file is not generated in the bin folder .
2-I tried to Export it using Android tools , and then tried it on my mobile and it gave black Screen then stopped working.
3-I got 3 warnings in The Activity_Main.xml about buttons and textview "Hardcoded String Should use #String Resource "
I'm totally Confused , I don't know how to fix it i tried a lot and searched on the internet but i couldn't
Here is the Main_Activity.Java :
package com.example.counter;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int Counter = 0;
Button A = (Button) findViewById(R.id.Add);
Button S = (Button) findViewById(R.id.Sub);
TextView C = (TextView) findViewById(R.id.Calculate);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter ++ ;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter -- ;
C.setText("Your Total Counter Is " + Counter);
}
});
}}
And here is the Activity_main.xml :
<TextView
android:id="#+id/Calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Waiting To Calculate"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Add"
android:layout_alignBottom="#+id/Add"
android:layout_alignRight="#+id/Calculate"
android:text="Sub" />
<Button
android:id="#+id/Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Calculate"
android:layout_below="#+id/Calculate"
android:layout_marginTop="66dp"
android:text="Add" />
The app suppose work even on API1 and Targeted API 17 (Jelly Bean) I tested it on Galaxy S3 Mini (have Android 4.2) and it crashed , tried simple app before with same settings and worked
*Any Help is Appreciated , thanks for your time and Sorry for make it Long *
The 2nd problem is on the View (e.g. Button, TextView) initialization.
Since they are declared and initialized at class scope, they are initialized before onCreate() (and thus setContentView()) is called. Because there is no layout inflated at the moment, the initialization will return null. Then, when setOnClickListener() is called, it will throw NullPointerException and crashes the app.
The solution is to move the initialization after setContentView() is called.
public class MainActivity extends Activity {
int Counter = 0;
Button A;
Button S;
TextView C;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
A = (Button) findViewById(R.id.Add);
S = (Button) findViewById(R.id.Sub);
C = (TextView) findViewById(R.id.Calculate);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter++;
C.setText("Your Total Counter Is " + Counter);
}
});
S.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Counter--;
C.setText("Your Total Counter Is " + Counter);
}
});
}
}
For the 3rd problem, just as Juwee posted, you can define the String resources to be used in XML layout file (it can be used in the code too!).
Create an XML file in /values folder with <resources> root element (or just use strings.xml if it's already there). Then, you can declare a string resource with String value
Example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="add">Add</string>
</resources>
Then, on the XML layout, you can refer it as android:text="#string/add" (Or in code as setText(R.string.add))
Since this is just only a warning, you can actually ignore it. But if you decide your app to support localization, or if you use the same String quite often, then it is better to define it as app's resource.
Third problem : About this property android:text="Sub" ,you can define the text in the resource file "string.xml",like <string name='subString'>sub</string>,then make reference to it.android:text="#string/R.string.subString" . Have a try .

Calling a EditText field into focus

I have a EditText that I have set to invisible by default. I would like to make this box visible onclick of a ImageView, but cant find any documentation online to help me, how would I go about doing this?
In your xml
<EditText
android:id="#+id/my_edit_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:inputType="number"
android:visibility="gone"
android:paddingRight="8dp" />
In your Activity Class
import android.widget.Button;
import android.widget.EditText;
public class MyActivity extends Activity {
private EditText editTxt = null;
private Button myBtn = null;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_xml_layout);
editTxt = (EditText) findViewById(R.id.my_edit_text);
myBtn = (Button) findViewById(R.id.my_button);
myBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTxt.setVisibility(View.VISIBLE);
}
});
}
}
well instead of invisible you can make it disable. This way you can prevent it from user input. And it is very easy to enable or disable EditTex.
You can use JQuery to toggle display (display:none vs. display:block) on your EditText field. You probably want to start out with display:none on the EditTextBox. You will also need to have JQuery loaded in your page. Can't make it a detailed tutorial here so giving you enough to get to next level.
$jq(document).ready(function() {
$jq("#yourImageView").click(function(e) {
$jq("#yourEditTextBox").fadeToggle("slow", "linear");
});

xml button link to for loop

I am trying to get a button in an XML doc to add new textfields and checkboxes, I have the for loop that does what I want, but I don't understand how I am suposed to link my button to access a specific part of a java file.
How do i implement?
edit.
Here is my forloop that I want my button to access when pressed (generate textfields)
And Generally I want to know if it is possible to link a xml button to a loop in java and
If not, what can I do in order to get my button to generate textfields?
for(int i = 0; i <5; i++){
CheckBox cb = new CheckBox(this);
cb.setText("I'm an egg!");
EditText et1 = new EditText(this);
et1.setText("Listitemz!");
ll.addView(et1);
ll.addView(cb);
in XML:
<Button android:id="#+id/button" ..... />
in java file:
Button button = (Button)findViewById(R.id.button)
Now you can use button object from Button class.
Is it what you mean by linking?
If I'm understanding you correctly you want the button to access and run the for loop?
Step 1:
First create the button in xml.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Step 2:
Create the link in your activity.
Button button = (Button) findViewById(R.id.button);
Step 3:
Assign an onClickListener to your button and put the for-loop inside.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i <5; i++){
CheckBox cb = new CheckBox(this);
cb.setText("I'm an egg!");
EditText et1 = new EditText(this);
et1.setText("Listitemz!");
ll.addView(et1);
ll.addView(cb);
}
}
});
Now whenever the button is pressed your for-loop will run, if you have any questions please let me know.
NOTE: You may need the following imports -
import android.view.View;
import android.widget.Button;

Image button changes image

I have a image button and i want that image button when pressed it changes the text in a textbox and it changes a image to a different image how do i do this?
You need an OnClickListener: http://developer.android.com/reference/android/view/View.OnClickListener.html
When clicked your text can be changed with (something like) text.setText("new text");
I'll find a link in a minute which will help more.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class TestesetetActivity extends Activity {
/** Called when the activity is first created. */
TextView textview = null;
ImageButton buttonResume = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonResume = (ImageButton) findViewById(R.id.imageButton1);
textview = (TextView) findViewById(R.id.textView1);
buttonResume.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textview.setText("test");
buttonResume.setImageResource(R.drawable.push_pin);
}
});
}
}
In the xml file, you can give the image an onClick attribute, which calls a method in the java class, which calls the xml resource, and passes it the id of the ImageView.
In that java method, you can use
((ImageView) view).setImageResource(int id)
or
setImageDrawable(Drawable d)
to change the image.
Likewise, you can identify the TextView you wish to change using, for example,
TextView tv = (TextView) findViewById( id )
with id being the id of the TextView you wish to find.
You can then use
tv.setText(String s)
to set the text in this view.
In your OnClickListener, you could use
button.setBackgroundResource(YOUR_BUTTON_ID);
or
button.setImageResource(YOUR_BUTTON_ID);

Categories

Resources