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
Related
I have a ViewPager with 3 fragments in it, each fragment has a next and prev. button, and you can also swipe left and right to navigate through fragments. and although I'm able to get the input from each fragment when the next button is clicked, I sill can't figure out a way to get the input when the user scrolls between fragments.
this is the code in my first fragment:
public class signupfrag1 extends Fragment {
private TextInputEditText fName, lName;
public Spinner spncountry, spncity;
private Button next1;
private String vfName, vlName, vcountry, vcity;
private String method;
signupfrag1Listener activityCommander;
public interface signupfrag1Listener{
public void getDataFromFrag1(String fName, String lName, String countryName, String cityName);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try{
activityCommander = (signupfrag1Listener) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.signupfrag1, container, false);
fName = (TextInputEditText) view.findViewById(R.id.edtFName);
lName = (TextInputEditText) view.findViewById(R.id.edtLName);
spncountry = (Spinner) view.findViewById(R.id.cmbCountry);
spncity = (Spinner) view.findViewById(R.id.cmbCities);
next1 = (Button) view.findViewById(R.id.btnNext2);
next1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vfName = fName.getText().toString();
vlName = lName.getText().toString();
if(vfName.equals("") || vfName.length()==1){
fName.setError("please enter a correct first name!");
}
else if(vlName.equals("") || vlName.length()==1){
lName.setError("Please enter a correct first name!");
}
else
startFrag2(view);
}
});}
public void startFrag2(View view){
activityCommander.getDataFromFrag1(vfName, vlName, vcity, vcountry);
}
this is the code from my main activity:
public class Main2Activityforfragments extends AppCompatActivity implements signupfrag1.signupfrag1Listener, signupfrag2.signupfrag2Listener {
private ViewPager fragsViewPager;
private SectionsPagerAdapter1 sectionsPagerAdapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2_activityforfragments);
fragsViewPager = (ViewPager) findViewById(R.id.fragsViewPager);
sectionsPagerAdapter1 = new SectionsPagerAdapter1(getSupportFragmentManager());
fragsViewPager.setAdapter(sectionsPagerAdapter1);
}
//this get called when the next button is clicked
#Override
public void getDataFromFrag1(String fName, String lName, String cityName, String countryName) {
if(fragsViewPager.getCurrentItem() == 0){
fragsViewPager.setCurrentItem(1);}
}
if you are in the first fragment then create ref. of interface and call its method and pass the values on Click what you want and it is needed to implement Two interface in SecondFragment. call its method in the First Fragment. you can create interface Two in SecondFragment
Two Second=new SecondFragment(); // it will be called on click of firstFragment which pass the value.
Second.two("Value one","Value two");
interface Two{
void two(String value,String value); // implement this inteface in SecondFragment and call it in FirstFragment
}
When you reach to second you get the value from the first fragment and continue with the third fragment as same.
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]));
}
I have a problem. I do not know how to call a different class. the code is for a button (when you click the button a message appears in random) so i thought it would be better if i placed it in a different class as i have also used arrays.Now i do not know how to call the class.
This is my code.I want to call "Firstin" inside SecondActivity.
public class SecondActivity extends Activity {
private Firstin mFirst = new Firstin ();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//finds textview
final Text Ftext = (Text) findViewById(R.id.textView2);
#SuppressWarnings("unused")
final Text Stext = (Text) findViewById(R.id.textView3);
//finds button view
Button btnView = (Button) findViewById(R.id.button1);
#SuppressWarnings("unused")
Button btnView2 = (Button) findViewById(R.id.button2);
btnView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String Answer = mFirst.firstAnswer();
Ftext.setTextContent(Answer);
}
});
this is the class I'm trying to call:
package com.example.insultgenerator;
import java.util.Random;
public class Firstin {
public String firstAnswer(){
String [] mResults={
"its cool",
"we cool",
"im cool",
"he cool",
"she cool"
};
// the button was clicked so replace the answer label with answer
String Answer = "" ;
// the two double is a 'empty string
Random RandomGen = new Random();// telling the program to construct a random generator
int RandomNum= RandomGen.nextInt(mResults.length);
Answer = mResults[RandomNum];
return(Answer);
}
}
You should cast to TextView
final TextViewFtext = (TextView) findViewById(R.id.textView2);
#SuppressWarnings("unused")
final TextViewStext = (TextView) findViewById(R.id.textView3);
then use TextView.setText(...) method:
String Answer = mFirst.firstAnswer();
Ftext.setText(new Firstin().firstAnswer());
}
});
then last call the method from the other class with new Firstin().firstAnswer() which creates a instance of the class and executes the method.
I have this error nullpointerexception, when i do getText().toString() from EditTex:
public class SendMessActivity extends SherlockFragmentActivity {
private EditText tEmail;
private Button sendButton;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_mess_layout);
tEmail = (EditText)findViewById(R.id.editEmailTo);
sendButton = (Button)findViewById(R.id.btn_sendmess);
endButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//String textEmail = tEmail.getText().toString(); //nullpointerexception
Editable textEmail1Editable = tEmail.getText(); //nullpointerexception
String textEmail = textEmail1Editable.toString()
Log.d(DEBUGTAG, "SENDING START:::::::: " + textEmail);
}
});
}}
Please, tell me how to do it
UPDATE Q
David, thank you, for your surmise,the problem was really in the my tangled Layouts
I had all 4 levels of nesting LinearLayouts.
After I left the simplified scheme and only 2 levels I have, all began to work
You need to call setContentView(your_layout.xml) so it knows what layout to use. Without setting the layout, all of your calls to findViewById(...) that try to find the views in your layout will return null.
public class SendMessActivity extends SherlockFragmentActivity {
private EditText tEmail;
private Button sendButton;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(your_layout.xml); //set your activity layout here.
tEmail = (EditText)findViewById(R.id.editEmailTo);
sendButton = (Button)findViewById(R.id.btn_sendmess);
endButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String textEmail = tEmail.getText().toString(); //nullpointerexception
Log.d(DEBUGTAG, "SENDING START:::::::: " + textEmail);
}
});
}}
If you have no assigned text then I would assume that the EditText retrieval of the text would be a null entry and you are attempting to make a string of that null entry.
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