I'm trying to have one TextView switch from one sentence to another alongside my ImageSwitcher. Here is a sample code for my activity
private Integer images[]={R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4....};
private int currImage=0;
private Integer text[]={R.string.text0,R.string.text1,R.string.text2,R.string.text3,R.string.text4....};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeImageSwitcher();
setInitialImage();
setImageRotateListener();
setInitialText();
}
private void initializeImageSwitcher() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
return imageView;
}
});
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
}
private void setImageRotateListener() {
final ImageButton rightarrow = (ImageButton) findViewById(R.id.next);
rightarrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
currImage++;
if (currImage == 29) {
currImage = 0;
}
setCurrentImage();
setCurrentText();
}
});
}
private void setInitialImage() {
setCurrentImage();
}
private void setInitialText(){
setCurrentText();
}
private void setCurrentImage() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setImageResource(images[currImage]);
}
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(Integer.toString(text[currImage]));
}
The it compiles and run just fine. Issue lies in the text output. Instead of the sentences defined in the string, the TextView displays a series of numbers.
For example:
<string name="text1">Sentence one</string>
<string name="text2">Sentence two</string>
<string name="text3">Sentence three</string>
<string name="text4">Sentence four</string>
The output for "text1" would be 8513856, "text2" would be 841363, "text3" would be 18413587, and so on.
If anyone one has an idea of how to solve this issue, your help would be greatly appreciated.
You are displaying the resource id's of the strings, not the strings themselves.
Change to:
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(getResources().getString(text[currImage]));
}
Related
Hello as the title state I'm trying to setup a next and previous buttons but I'm still new at coding so this has me a little confused.
I tried to use if statements with an enum within a single button but it defaults to last if statement when the event is handled here's the code-
private enum EVENT{
pe1, pe2, pe3, pe4;
}
EVENT currentEvent = EVENT.pe1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_liners);
nextBtn = (Button) findViewById(R.id.nextBtn);
olText = (TextView) findViewById(R.id.olText);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (currentEvent==EVENT.pe1) {
olText.setText("PE1");
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_left));
currentEvent=EVENT.pe2;
}
if (currentEvent==EVENT.pe2){
olText.setText("PE2");
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_left));
currentEvent=EVENT.pe3;
}
}
});
}
I tried to use the enumerator to assign a number to each if statement so when the user hit previous it would subtract and when they hit next it would add, each number would have some text or image within its if statement but as I said it defaults to the last if statement- Any help is much appreciated.
How about this?
int eventNum = 0;
int maxEvents = XXX;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_liners);
prevBtn = (Button) findViewById(R.id.prevBtn);
nextBtn = (Button) findViewById(R.id.nextBtn);
olText = (TextView) findViewById(R.id.olText);
setEventData(true);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.equals(prevBtn) && eventNum > 0) {
eventNum--;
setEventData(false);
return;
}
if(v.equals(nextBtn) && eventNum < maxEvents - 1) {
eventNum++;
setEventData(true);
return;
}
}
}
nextBtn.setOnClickListener(listener);
prevBtn.setOnClickListener(listener);
}
private void setEventData(boolean animLeft) {
olText.setText("PE" + (eventNum + 1));
if(animLeft) {
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_left));
} else {
olText.startAnimation(AnimationUtils.loadAnimation(olText.this, android.R.anim.slide_in_right));
}
}
You'll want to create a class variable that keeps track of which text your TextView is showing. So in the following example, I create a list of Strings that I just store in a String array. Then I create an iterator variable which stores which String from the list I'm currently viewing in the TextView. Every time you click the previous or next button, you simply store your current state in the iterator variable so you can recall it the next time a click event comes in.
String[] labels = {"one", "two", "three", "four"};
int currentView = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onPreviousButtonClicked(View view) {
TextView textView = (TextView) findViewById(R.id.clickableLink);
currentView--; //decrement our iterator
if(currentView < 0) currentView = 0; //check to make sure we didn't go below zero
textView.setText(labels[currentView]);
}
public void onNextButtonClicked(View view) {
TextView textView = (TextView) findViewById(R.id.clickableLink);
currentView++; //increment our iterator
if(currentView > labels.length-1) currentView = labels.length-1; //check to make sure we didn't go outside the array
textView.setText(labels[currentView]);
}
I am trying to write an app, which allows you to calculate a circle. I made a main layout and a second layout. Main layout: activity_main
second layout: kreislay
Don't wonder about those strange words, I'm a german.
Here is my code:
public class MainActivity extends ActionBarActivity {
public Button kreisd;
public Button kugeld;
public Button kreiscalc;
public Context con;
public EditText radius_eingabe = null;
public TextView kreis_ergebnis_vol;
public TextView kreis_ergebnis_G;
public String str1;
public double st1;
public double G_kreis;
public static final double PI = 3.141592653589793d;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LayoutInflater factory = getLayoutInflater();
final LayoutInflater factory_main = getLayoutInflater();
final View view2 = factory.inflate(R.layout.kreislay, null);
final View main_view = factory_main.inflate(R.layout.activity_main, null);
kreisd = (Button)findViewById(R.id.kreis);
kugeld = (Button)main_view.findViewById(R.id.kugel);
kreiscalc = (Button)view2.findViewById(R.id.calc_kreis);
kreisd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
con = MainActivity.this;
setContentView(R.layout.kreislay);
}
});
kreiscalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
con = MainActivity.this;
radius_eingabe = (EditText) view2.findViewById(R.id.editText);
str1 = radius_eingabe.getText().toString();
st1 = Double.parseDouble(str1);
G_kreis = PI*st1*st1;
kreis_ergebnis_G = (TextView)view2.findViewById(R.id.G_kreis);
kreis_ergebnis_G.setText(String.valueOf(G_kreis));
}
});
}
My problem is, that it is not working. When i hit the button to calculate the circle, just nothing happens, although I entered a radius. There is NO error code in LogCat. I Need help. I mean, the Area of a circle is = Pi*r^2, isn't it?
Thanks for your attention.
maybe this?
public static final double PI = 3.141592653589793d;
The "d" at the end is wrong I think.
Hope it helps
I have created a custom dialog class (extends Dialog) , inside that dialog xml layout
i have expand/ collapse textview with animation, this is working fine,
but when i expand or collapse the view (of the textview) the size of the dialog also change (with out animation), how can make it also re-size with animation?
public class ErrorDialog extends Dialog {
private String err;
private TextView txt_help_gest;
public ErrorDialogOnClickListener errorDialogListener;
private String shortDesc;
private Animation slide_up_Animation;
private Animation slide_down_Animation;
private Animation rotate_arrow_down;
private Animation rotate_arrow_up;
public ErrorDialog(Context context,String shortDesc, String err, ErrorDialogOnClickListener errorDialogListener) {
super(context);
this.err=err;
this.errorDialogListener = errorDialogListener;
this.shortDesc=shortDesc;
slide_up_Animation= AnimationUtils.loadAnimation(context, R.anim.slide_up);
slide_down_Animation= AnimationUtils.loadAnimation(context, R.anim.slide_down);
rotate_arrow_down= AnimationUtils.loadAnimation(context, R.anim.rotate_arrow_down);
rotate_arrow_up= AnimationUtils.loadAnimation(context, R.anim.rotate_arrow_up);
getWindow().getAttributes().windowAnimations = R.style.Animations_SmileWindow;
}
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_error);
setTitle("שגיאה");
setCancelable(false);
// set values for custom dialog components - text, image and button
Button btnClose=(Button)findViewById(R.id.btnClose);
TextView text = (TextView) findViewById(R.id.textDialog);
txt_help_gest = (TextView) findViewById(R.id.txt_help_gest);
//help_title_gest = (TextView) findViewById(R.id.help_title_gest);
final ImageView imgArrow = (ImageView) findViewById(R.id.imgArrow);
LinearLayout ll_help_title = (LinearLayout) findViewById(R.id.ll_help_title);
// ll = (View) findViewById(R.id.ll);
text.setText(shortDesc+"\n התוכנית תסגר.");
txt_help_gest.setText(err);
txt_help_gest.setVisibility(View.GONE);
ll_help_title.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View arg0) {
//txt_help_gest.clearAnimation();
if(txt_help_gest.isShown()){
//collapse();
//slide_up_Animation.reset();
txt_help_gest.startAnimation(slide_up_Animation);
txt_help_gest.setVisibility(View.GONE);
imgArrow.startAnimation(rotate_arrow_up);
}
else{
// expand();
// slide_down_Animation.reset();
txt_help_gest.setVisibility(View.VISIBLE);
txt_help_gest.startAnimation(slide_down_Animation);
imgArrow.startAnimation(rotate_arrow_down);
}
}
});
btnClose.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(View arg0) {
errorDialogListener.onButtonClick();
dismiss();
}
});
}
public interface ErrorDialogOnClickListener {
void onButtonClick();
}
}
Hi I'm kind of beginner to android OS programming, and I got stuck with a problem, I cant figure out how to do a dynamic background, based on timers (say each 10 seconds a background changes to a different one) I have some code but it comes up with error, here's a sample:
private static final long GET_DATA_INTERVAL = 10000;
int images[] = {R.drawable.smothie1,R.drawable.omletherb1};
int index = 0;
ImageView img;
Handler hand = new Handler();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
LinearLayout layout= (LinearLayout)findViewById(R.id.LinearView1);
hand.postDelayed(run, GET_DATA_INTERVAL);
}
Runnable run = new Runnable() {
public void run() {
layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
if (index == images.length)
index = 0;
hand.postDelayed(run, GET_DATA_INTERVAL);
Any help would be greatly apprieciated :D thanks
EDIT: The errors I get are on this line:
layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
It says that:
-layout cannot be resolved
-the method getDrawable(int) is undefined for the type Object
This error:
layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
It says that:
-layout cannot be resolved
-the method getDrawable(int) is undefined for the type Object
Please help :)
I have finally worked it out, after removing a few errors I have came up with this (and its working) :
public class CookBookActivity extends Activity {
/** Called when the activity is first created. */
private static final long GET_DATA_INTERVAL = 1000;
int images[] = {R.drawable.omletherb1,R.drawable.smothie1};
int index = 0;
LinearLayout img;
Handler hand = new Handler();
private LinearLayout layout;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
layout = (LinearLayout)findViewById(R.layout.main);
hand.postDelayed(run, GET_DATA_INTERVAL);
Typeface tf2 = Typeface.createFromAsset(getAssets(),
"fonts/BPreplay.otf");
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setTypeface(tf2);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/BPreplay.otf");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setTypeface(tf);
Button mainNext = (Button) findViewById(R.id.nextScreen1);
mainNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
startActivity(i);
}
});
}
Runnable run = new Runnable() {
public void run() {
layout.setBackgroundDrawable(getDrawable(index++));
if (index == images.length)
index = 0;
hand.postDelayed(run, GET_DATA_INTERVAL);
}
};
protected Drawable getDrawable(int i) {
// TODO Auto-generated method stub
return getResources().getDrawable(images[i%2]);
}
}
There are a set of buttons, I want to get the result:
When I click one of them, first I divide them into two parts: the clicked one and the others. I'm trying to set different color or alpha value to different them.
Now I use setAlpha, but when I change the value from 0 to 255, it works, but when I change the value from 255 to 0 , it doesnot work. I don't know why.
Maybe after I invoke the methodButton.setAlpha(), I need invoke another method?
my code:
public class MainActivity extends Activity {
// button alpha value: minimize value
public static int BUTTON_ALPHA_MIN = 0;
// button alpha value: maximize value
public static int BUTTON_ALPHA_MAX = 255;
private LinearLayout centerRegion;
private LinearLayout bottomRegion;
private Button btnCheckIn;
private Button btnReview;
private Button btnMyCircles;
private Button btnSettings;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get all the widgets
getAllWidgets();
// set buttons click response function
btnCheckIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.RED);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
}
});
btnReview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.BLUE);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
}
});
btnMyCircles.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.YELLOW);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MAX);
v.getBackground().setAlpha(BUTTON_ALPHA_MIN);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
centerRegion.setBackgroundColor(android.graphics.Color.MAGENTA);
btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX);
btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MAX);
v.getBackground().setAlpha(BUTTON_ALPHA_MIN);
}
});
}
/**
* get all the widgets
*/
public void getAllWidgets() {
this.centerRegion = (LinearLayout) this.findViewById(R.id.center_region);
this.bottomRegion = (LinearLayout) this.findViewById(R.id.bottom_region);
this.btnCheckIn = (Button) this.findViewById(R.id.button_check_in);
this.btnReview = (Button) this.findViewById(R.id.button_review);
this.btnMyCircles = (Button) this.findViewById(R.id.button_my_circles);
this.btnSettings = (Button) this.findViewById(R.id.button_setting);
}
}
Using AlphaAnimation should work; verified on my device.
public class Test extends Activity implements OnClickListener {
private AlphaAnimation alphaDown;
private AlphaAnimation alphaUp;
private Button b1;
private Button b2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.linear_layout);
b1 = new Button(this);
b1.setText("Button 1");
b1.setOnClickListener(this);
ll.addView(b1);
b2 = new Button(this);
b2.setText("Button 2");
b2.setOnClickListener(this);
ll.addView(b2);
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(1000);
alphaUp.setDuration(1000);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
}
public void onClick(View v) {
if (v == b1) {
b1.startAnimation(alphaUp);
b2.startAnimation(alphaDown);
} else {
b1.startAnimation(alphaDown);
b2.startAnimation(alphaUp);
}
}
}
The key is calling setFillAfter(true) so that the alpha change persists.
Thanks for the question and the answer. This really helped me out.
For my solution, I needed to set the alpha of a button without seeing any animation effect, but the button.setAlpha(x) was failing sporadically. Using animations instead did the trick, but I had to set the duration to zero to get the automatic effect.
alphaDown = new AlphaAnimation(1.0f, 0.3f);
alphaUp = new AlphaAnimation(0.3f, 1.0f);
alphaDown.setDuration(0);
alphaUp.setDuration(0);
alphaDown.setFillAfter(true);
alphaUp.setFillAfter(true);
I use this for player controls in a media application, so I had something like this:
boolean bInitPrevEnabled = m_btPrev.isEnabled();
boolean bInitNextEnabled = m_btNext.isEnabled();
boolean bInitPlayEnabled = m_btPlay.isEnabled();
m_btPrev.setEnabled(true);
m_btNext.setEnabled(true);
m_btPlay.setEnabled(true);
// Process enabling of the specific buttons depending on the state
if (bInitPrevEnabled != m_btPrev.isEnabled())
m_btPrev.startAnimation((m_btPrev.isEnabled()) ? alphaUp : alphaDown);
if (bInitNextEnabled != m_btNext.isEnabled())
m_btNext.startAnimation((m_btNext.isEnabled()) ? alphaUp : alphaDown);
if (bInitPlayEnabled != m_btPlay.isEnabled())
m_btPlay.startAnimation((m_btPlay.isEnabled()) ? alphaUp : alphaDown);
Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.main_btn);
Drawable d = getResources().getDrawable(R.drawable.imagen);
d.setAlpha(60);
btn.setBackgroundDrawable(d);
}
This works for me :)
public void setAlpha (int alpha) - deprecated
public void setAlpha (float alpha) (0f < alpha < 1f)
Added in API level 11