int b = 1;
/* while loop execution */
while(b<=25)
{
if(b==5 || b==10 || b==21)
{
int a = 1;
while( a <= 10 )
{
if(a==2 || a==5)
{
a++;
}
else{
NSLog(@"value of a: %d*%d=%d\n", b,a,(b*a));
a++;
}
}
b++;
}else
{
b++;
}
}
/* while loop execution */
while(b<=25)
{
if(b==5 || b==10 || b==21)
{
int a = 1;
while( a <= 10 )
{
if(a==2 || a==5)
{
a++;
}
else{
NSLog(@"value of a: %d*%d=%d\n", b,a,(b*a));
a++;
}
}
b++;
}else
{
b++;
}
}
Sign up here with your email
15 comments
Write commentsBy using Nested #do while loop tables printing
Reply#import
int main (int argc, const char * argv[])
{
int i=1;
do{
if(i==2 || i==3 ||i==5 ||i==8 ||i==20 || i==15 ||i==11 ||i==19)
{
int j=1;
do{
if(j==2 || j==5 || j==8|| j==10|| j==19)
{
j++;
}
else
{
NSLog(@"%i*%i=%i",i,j,i*j);
j++;
}
}while(j<=20);
i++;
}
else
{
i++;
}
}while(i<=20);
return 0;
}
print data in Log
Replyimport android.util.Log;
String name = "Name";
Log.e("name",""+name);
Toast message
Replyimport android.widget.Toast;
Toast.makeText(this, Integer.toString(sum3), Toast.LENGTH_LONG).show();
//sum3 is a Toast message
Log Tag message
Replyimport android.util.Log;
public class Third extends AppCompatActivity {
private static final String TAG = "Message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
@Override
protected void onStart(){
super.onStart();
Log.i(TAG,"onStart");
}
@Override
protected void onPause(){
super.onPause();
Log.i(TAG,"onPause");
}
@Override
protected void onStop(){
super.onStop();
Log.i(TAG,"onStop");
}
@Override
protected void onResume(){
super.onResume();
Log.i(TAG,"onResume");
}
}
by clicking button change the text in TextView
Replypublic void onCickFourth(View v){
TextView e41=(TextView)findViewById(R.id.text41);
e41.setText("Hi");
}
onClickListner and onLongClickListner
Replypublic void onCickFourth(View v){
Button b41=(Button) findViewById(R.id.button41);
b41.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
TextView clickText=(TextView)findViewById(R.id.text41);
clickText.setText("On Click");
}
}
);
b41.setOnLongClickListener(
new Button.OnLongClickListener(){
public boolean onLongClick(View v){
TextView clickText=(TextView)findViewById(R.id.text41);
clickText.setText("Long Click");
return true;
}
}
);
}
Gestures in android
Reply//textView in Screen
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
public class Fourth extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {
private TextView text41;
private GestureDetectorCompat gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
text41 = (TextView)findViewById(R.id.text41);
this.gestureDetector = new GestureDetectorCompat(this,this);
gestureDetector.setOnDoubleTapListener(this);
}
//start gestures
@Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
text41.setText("onSingleTapConfirmed");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent motionEvent) {
text41.setText("onDoubleTap");
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
text41.setText("onDoubleTapEvent");
return true;
}
@Override
public boolean onDown(MotionEvent motionEvent) {
text41.setText("onDown");
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
text41.setText("onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
text41.setText("onSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
text41.setText("onScroll");
return true;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
text41.setText("onLongPress");
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
text41.setText("onFling");
return true;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
text41.setText("onPointerCaptureChanged");
}
//End of gestures
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
Checkbox in Android
Replyimport android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;
public class Checkbox extends AppCompatActivity {
private CheckBox c1;
private CheckBox c2;
private CheckBox c3;
private Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
buttonListener();
addListenerToCheckBox();
}
public void buttonListener(){
c1 = (CheckBox)findViewById(R.id.check1);
c2 = (CheckBox)findViewById(R.id.check2);
c3 = (CheckBox)findViewById(R.id.check3);
b1 = (Button)findViewById(R.id.buttonCheck1);
b1.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v){
StringBuffer result=new StringBuffer();
result.append("Tiger : ").append(c1.isChecked());
result.append("\nLion : ").append(c2.isChecked());
result.append("\nCat : ").append(c3.isChecked());
Toast.makeText(Checkbox.this, result.toString(),Toast.LENGTH_LONG).show();
}
}
);
}
public void addListenerToCheckBox(){
c1=(CheckBox)findViewById(R.id.check1);
c1.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v){
if(((CheckBox)v).isChecked()){
Toast.makeText(Checkbox.this, "Tiger is Selected",Toast.LENGTH_LONG).show();
}
}
}
);
}
}
Radio Button in Android
Replyit contains 1 radio group inside 3 radio buttons and 1 Button
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Radio extends AppCompatActivity {
private static RadioGroup group;
private static RadioButton radioB;
// private static RadioButton r1;
// private static RadioButton r2;
// private static RadioButton r3;
private static Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
buttonListener();
}
public void buttonListener(){
group = (RadioGroup)findViewById(R.id.radioGroup);
// radioB = (RadioButton)findViewById(R.id.radioButton1);
b = (Button) findViewById(R.id.radioButton);
b.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View view) {
int selected_id = group.getCheckedRadioButtonId();
radioB = (RadioButton)findViewById(selected_id);
Toast.makeText(Radio.this, radioB.getText().toString(), Toast.LENGTH_LONG ).show();
}
}
);
}
}
Rating bar in android
Replyimport android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
public class Rating extends AppCompatActivity {
private static Button b;
private static RatingBar rate;
private static TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
listenerForRatingBar();
}
public void listenerForRatingBar(){
rate = (RatingBar)findViewById(R.id.rating);
t=(TextView)findViewById(R.id.rateT);
rate.setOnRatingBarChangeListener(
new RatingBar.OnRatingBarChangeListener(){
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
t.setText(String.valueOf(v));
}
}
);
}
}
Rating Bar using Button in Android
Replyimport android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
public class Rating extends AppCompatActivity {
private static Button b;
private static RatingBar rate;
private static TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
listenerForRatingBar();
onButtonClickedListener();
}
public void listenerForRatingBar(){
rate = (RatingBar)findViewById(R.id.rating);
t=(TextView)findViewById(R.id.rateT);
rate.setOnRatingBarChangeListener(
new RatingBar.OnRatingBarChangeListener(){
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
t.setText(String.valueOf(v));
}
}
);
}
public void onButtonClickedListener(){
rate = (RatingBar)findViewById(R.id.rating);
b = (Button)findViewById(R.id.ratingB);
b.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View view) {
Toast.makeText(Rating.this, String.valueOf(rate.getRating()), Toast.LENGTH_SHORT).show();
}
}
);
}
}
Show Alert message
Replypublic class Alert extends AppCompatActivity {
private static Button button_alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert);
onButtonClickListener();
}
public void onButtonClickListener(){
button_alert = (Button)findViewById(R.id.alertButton);
button_alert.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alert_builder = new AlertDialog.Builder(Alert.this);
alert_builder.setMessage("Do you want to close this app")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert= alert_builder.create();
alert.setTitle("Alert !!!");
alert.show();
}
}
);
}
}
go one activity to other activity
ReplyIn Manifest
.java
public void onClickNewActivity(){
clickButton = (Button)findViewById(R.id.startNewActivity);
clickButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.naresh.basicapp.Alert");
startActivity(intent);
}
}
);
}
Analog and digital clock
Replypublic class AlanogAndDigitalClock extends AppCompatActivity {
public static Button clock_Button;
public static DigitalClock digital_Clock;
public static TextClock text_Clock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alanog_and_digital_clock);
onClickButtonListener();
}
public void onClickButtonListener(){
clock_Button = (Button)findViewById(R.id.clockButton);
digital_Clock = (DigitalClock)findViewById(R.id.digitalClock);
text_Clock = (TextClock)findViewById(R.id.analogClock);
clock_Button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (digital_Clock.getVisibility() == DigitalClock.GONE){
digital_Clock.setVisibility(DigitalClock.VISIBLE);
text_Clock.setVisibility(TextClock.GONE);
}else {
digital_Clock.setVisibility(DigitalClock.GONE);
text_Clock.setVisibility(TextClock.VISIBLE);
}
}
}
);
}
}
ConversionConversion EmoticonEmoticon