How to update just a part of the activity - android

Well, the title says everything, but there's a little problem, I can't use a fragment or a loader, because I'm developing for android 2.3, so, how can I update just a part of the activity?

You can still use a fragment even in 2.3 by using the android-support-v4 jar

You can reference the views by ID. As an example to change a EditText you can:
EditText text = (EditText) findViewById(R.id.input);
test.setText("New Text");
Just change the cast to the proper type, so for a button:
Button btn = (Button) findViewById(R.id.button);
So for your code if you have a button with the xml element android:onclick="increment" you can have a function such as:
public void increment(View view) {
TextView number = (TextView) findViewById(R.id.number);
number.setText(Integer.toString(Integer.parseInt(number.getText()) + 1));
}

Related

Button to hide a TextView

I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});

Android app: Android Studio - formula calculator

I want to make a simple Android app for min.-version 2 for local use. The problem is I have never made Android apps. I've installed Android Studio and SDK Tools now, but I get errors when creating a blank app project. Actually I am a VB.NET programmer, I also know C#, PHP, but I've never done anything for smartphones before.
What I need, is a simple calculator with two text-boxes (e.g. txt1 (a) and txt2 (b) which are inputs as doubles), a calculate button, and a result-field (as double).. see an example-picture below.
When focusing a text-box a numeric keyboard must appear (like for a calc), with a decimal point button. And when I click on result-button then output must be calculated and printed following the formula below:
Can anybody give me a tip how to make this? Or can anybody tell where to write the code to make this app work?
Thank you.
So first you need to learn how android Layout XML files work. Then add 4 items: 2 EditTexts at the beginning, then one Button with the text "Calculate" and finaly one more EditText. Set for all of them some IDs (i.e. text1, text2, button, output).
Also don't forget to set all the items to have "match_parent" at "layout_width" and "wrap_content" at "layout_height" (for displaying as you want).
Then in your main activity class, in your on onCreate(Bundle savedInstanceState):
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText output = (EditText) findViewById(R.id.output);
text1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
text2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
output.setEditable(false);
Button calculate = (Button) findViewById(R.id.button);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (text1.getText().isEmpty() || text2.getText().isEmpty()) return;
double text1Value = Double.valueOf(text1.getText().toString());
double text2Value = Double.valueOf(text2.getText().toString());
double a, b;
if (text1Value > text2Value) {
a = text1Value;
b = text2Value;
} else {
a = text2Value;
b = text1Value;
}
double result = (a*a - b*b) / 4;
result = Math.sqrt(result);
output.setText(String.valueOf(result));
}
});
Hope I helped, announce me if there are any errors!

using setOnClickListener in a right way

I'm developing an android app, but I don't have experience with android and I need some help.
I'm going to simplify my code, here is my problem:
I have declared: TextView test = (TextView) findViewById(R.id.test);
After, i have a setOnClickListener:
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here i declare: TextView test = (TextView) findViewById(R.id.test2);
When i click test for first time it corresponds to findViewById(R.id.test) like it should, but when I click for second time it still corresponds findViewById(R.id.test).
Any idea why?
Thanks in advance! :)
It's because inside the click listener you're defining a new TextView. It's not the same test variable.
When you do TextView test = (TextView) findViewById(R.id.test2); inside the click listener, a new TextView test variable is defined which is not the same as the one you had defined previously.
If you just want to reassign a value, you should use test = (TextView) findViewById(R.id.test2); inside the onClickListener();

Autolink action tracking in texview onClickListner

I have a listview ,each item is a textView with property autoLink="web|email".Link will work properly,but I want to start another activity when text other than web|email is clicked,that was not happening.So I used setOnClickListner for textView,that also worked smoothly.My problem is when I click the email or web link both actions will occur -browser and other activity will open.How to prevent this?
I got the solutions.
I used getSelectionStart() and getSelectionEnd() functions of the Textview class,
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(textView.getSelectionStart()==-1&&textView.getSelectionEnd()==-1){
//This condition will satisfy only when it is not an autolinked text
//onClick action
}
}
});
TRY this ::
in layout :: android:autoLink="web"
OR
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());

editing a global variable

Hi im new to android and I have a program that has a global variable define and it works, so I can set it and get it in every activity, BUT it dosnt like to be changed in an on click listener. I made it so on the screen there is an edittext and when someone presses a button I want the edittext text to be put into the global variable. here is my code:
Button SiteButton = (Button) findViewById(R.id.SiteButton);
SiteButton.setOnClickListener(new View.OnClickListener() {
TextView textviewS = (TextView) findViewById(R.id.SiteIdT);
EditText edittextS = (EditText) findViewById(R.id.SiteIdE);
TextView textviewB = (TextView) findViewById(R.id.BusIdT);
EditText edittextB = (EditText) findViewById(R.id.BusIdE);
public void onClick(View v) {
textviewS.setText(edittextS.getText());
((Global) this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) this.getApplication()).setgVehicleId(textviewB.getText().toString());
}
});
but the getApplication() part is showing an error. can anyone help?
You should refer to your activity this, since View.OnClickListener doesn't have such a method:
// Bad code! read below
((Global) MyActivityClassName.this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) MyActivityClassName.this.getApplication()).setgVehicleId(textviewB.getText().toString());
By the way, how do you cast the return from getApplication() to Global? You will get a class cast exception there.

Categories

Resources