I am trying to get my first button to update a display number in my view when clicked. This view will have several buttons and "outputs" displayed. After reading examples and Q's here, I finally put something together that runs, but my first button is still not working;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ship_layout);
mSwitcher = (TextSwitcher) findViewById(R.id.eng_val);
}
private TextSwitcher mSwitcher;
// Will be connected with the buttons via XML
void onClick(View v){
switch (v.getId()) {
case R.id.engplus:
engcounter++;
updateCounter();
break;
case R.id.engneg:
engcounter--;
updateCounter();
break;
}
}
private void updateCounter() {
mSwitcher.setText(String.valueOf(engcounter));
}
The .xml for this button is;
<TextSwitcher
android:id="@+id/eng_val"
android:visibility="visible"
android:paddingTop="9px"
android:paddingLeft="50px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/build"
android:layout_toRightOf="@+id/engeq"
android:textColor="#DD00ff00"
android:textSize="24sp"/>
This is within a Relative Layout that appears otherwise OK. When I had set the view to have a TextView with the number set as a string , the number displayed, but I could not figure out how to update the text with a numerical field. That may be my real problem.
I have gone through many examples generally referenced from the dev. site (UI, Common Tasks, various samples), and I am still not seeing the connection here...
Again, this is simply a try at getting variables to respond to buttons and update on the view.
So, a few Q's for anyone that can help;
1) Is there any easier way of doing this (ie. send numerical value to View) ? 2) Why isn't my TextSwitcher displaying the number? 3) Should I be using a TextSwitcher here? 4) Any examples of this you can point me to?
UPDATE!!!: Buttons with feedback are now working. Here is my fix, based on feedback, research and luck;
public class Myview extends Myapp implements ViewSwitcher.ViewFactory, View.OnClickListener {
public int engcounter = 1;
private TextSwitcher mSwitcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
mSwitcher = (TextSwitcher) findViewById(R.id.eng_val);
mSwitcher.setFactory(this);
Button engp = (Button) findViewById(R.id.engplus);
engp.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
engcounter++;
updateCounter();
}
});
Button engn = (Button) findViewById(R.id.engneg);
engn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
engcounter--;
updateCounter();
}
});
}
private void updateCounter() {
mSwitcher.setText(String.valueOf(engcounter));
}
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
return t;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
Some of this appears unnecessary at the moment (ie. Gravity on makeView), but it works! And should work for the 10-20 buttons I want to do misc things with.
The key was getting listeners right, and the whole set-up for TextSwitcher was abit awkward...still learning.
Any other final comments welcomed!
UPDATE UPDATE!! I found this also;