ads

Affichage des articles dont le libellé est activity. Afficher tous les articles
Affichage des articles dont le libellé est activity. Afficher tous les articles

vendredi 18 septembre 2015

Unable to Send data from One Activity to Another Activity



**FIRSTACTIVITY**

Code:


public class FirstMarks extends Activity {

    TextView tv_Subjects1, tv_Internal1, tv_External1;
    EditText et_Int_Eng, et_Int_M1, et_Int_MM, et_Int_Phy, et_Int_Chem, et_Int_Cp, et_Int_ElcsLab, et_Int_EpLab, et_Int_ItLab, et_Int_Draw;
    EditText et_Ext_Eng, et_Ext_M1, et_Ext_MM, et_Ext_Phy, et_Ext_Chem, et_Ext_Cp, et_Ext_ElcsLab, et_Ext_EpLab, et_Ext_ItLab, et_Ext_Draw;
    Button btn_Cal1;
    int IEng, IM1, IMM, IPhy, IChem, ICp, IElcsLab, IItLab, IEpLab, IDraw;
    int EEng, EM1, EMM, EPhy, EChem, ECp, EElcsLab, EItLab, EEpLab, EDraw;
    int ITotal1 =0, ETotal1 =0, Total1 =0;
    float Percentage1=0;
    String STotal1=" ", SPercentage1=" ";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marks_1);

        //SETTING FONT.
        Typeface mytypeface = Typeface.createFromAsset(getAssets(), "font.ttf");

        //INITIALISING VIEWS.
        //INITIALISING TEXT VIEWS.
        tv_Subjects1 = (TextView) findViewById(R.id.tv_Subject1);
        tv_Internal1 = (TextView) findViewById(R.id.tv_Internal1);
        tv_External1 = (TextView) findViewById(R.id.tv_External1);
        //INITIALISING EDIT TEXT.
        //INTERNAL
        et_Int_Eng = (EditText) findViewById(R.id.et_Int_Eng);
        et_Int_M1 = (EditText) findViewById(R.id.et_Int_M1);
        et_Int_MM = (EditText) findViewById(R.id.et_Int_MM);
        et_Int_Phy = (EditText) findViewById(R.id.et_Int_Phy);
        et_Int_Chem = (EditText) findViewById(R.id.et_Int_Chem);
        et_Int_Cp = (EditText) findViewById(R.id.et_Int_Cp);
        et_Int_ElcsLab = (EditText) findViewById(R.id.et_Int_ElcsLab);
        et_Int_EpLab = (EditText) findViewById(R.id.et_Int_EpLab);
        et_Int_ItLab = (EditText) findViewById(R.id.et_Int_ItLab);
        et_Int_Draw = (EditText) findViewById(R.id.et_Ext_Draw);
        //EXTERNAL
        et_Ext_Eng = (EditText) findViewById(R.id.et_Ext_Eng);
        et_Ext_M1 = (EditText) findViewById(R.id.et_Ext_M1);
        et_Ext_MM = (EditText) findViewById(R.id.et_Ext_MM);
        et_Ext_Phy = (EditText) findViewById(R.id.et_Ext_Phy);
        et_Ext_Chem = (EditText) findViewById(R.id.et_Ext_Chem);
        et_Ext_Cp = (EditText) findViewById(R.id.et_Ext_Cp);
        et_Ext_ElcsLab = (EditText) findViewById(R.id.et_Ext_ElcsLab);
        et_Ext_EpLab = (EditText) findViewById(R.id.et_Ext_EpLab);
        et_Ext_ItLab = (EditText) findViewById(R.id.et_Ext_ItLab);
        et_Ext_Draw = (EditText) findViewById(R.id.et_Ext_Draw);
        //INITIALISING BUTTON
        btn_Cal1 = (Button) findViewById(R.id.btn_Cal1);

        //INITIALISING FONT.
        tv_Subjects1.setTypeface(mytypeface);
        tv_Internal1.setTypeface(mytypeface);
        tv_External1.setTypeface(mytypeface);
        btn_Cal1.setTypeface(mytypeface);

        //GETTING VALUES FROM EDIT TEXT.
        //ENGLISH.
        try {
            IEng = getInt(et_Int_Eng);
            EEng = getInt(et_Ext_Eng);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a ENGLISH Number");
        }

        //M1.
        try {
            IM1 = getInt(et_Int_M1);
            EM1 = getInt(et_Ext_M1);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a M1 Number");
        }
        //MM.
        try {
            IMM = getInt(et_Int_MM);
            EMM = getInt(et_Ext_MM);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a MM Number");
        }

        //PHYSICS.
        try {
            IPhy = getInt(et_Int_Phy);
            EPhy = getInt(et_Ext_Phy);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a PHYSICS Number");
        }

        //CHEMISTRY
        try {
            IChem = getInt(et_Int_Chem);
            EChem = getInt(et_Ext_Chem);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a CHEMISTRY Number");
        }

        //CP.
        try {
            ICp = getInt(et_Int_Cp);
            ECp = getInt(et_Ext_Cp);

        } catch (NumberFormatException e) {
            Log.i("Log", "Not a CP Number");
        }

        //DRAWING.
        try {
            IDraw = getInt(et_Int_Draw);
            EDraw = getInt(et_Ext_Draw);
        } catch (NumberFormatException e) {
            Log.i("Log", "Not a DRAW Number");
        }

        //ELCS LAB.
        try{
            IElcsLab = getInt(et_Int_ElcsLab);
            EElcsLab = getInt(et_Ext_ElcsLab);

        }catch (NumberFormatException e){
            Log.i("Log", "Not a ELCS Number");
        }

        //EP/EC LAB.
        try{
            IEpLab = getInt(et_Int_EpLab);
            EEpLab= getInt(et_Ext_EpLab);

        }catch (NumberFormatException e){
            Log.i("Log", "Not a EP LAB Number");
        }

        //IT LAB.
        try{
            IItLab = getInt(et_Int_ItLab);
            EItLab = getInt(et_Ext_ItLab);

        }catch (NumberFormatException e){
            Log.i("Log", "Not a ITLAB Number");
        }



        //TOTALING ALL SUBJECTS
        //TOTALING INTERNAL MARKS.
        ITotal1 = (IEng + IM1 + IMM + IPhy + IChem + ICp + IElcsLab + IEpLab + IItLab + IDraw);

        //TOTALING EXTERNAL MARKS.
        ETotal1 = (EEng + EM1 + EMM + EPhy + EChem + ECp + EElcsLab + EEpLab + EItLab + EDraw);

        //TOTALING INTERNAL & EXTERNAL MARKS.
        Total1 = (ITotal1 + ETotal1);

        //CONVERTING INT TOTAL1 INTO STRING.
        //STotal1 = String.valueOf(Total1);

        //PERCENTAGE CALCULATION.
        Percentage1 = (float) Total1 / 10;
        //SPercentage1 = String.valueOf(Percentage1);

        //WHEN BUTTON CALCULATE IS CLICKED.
        btn_Cal1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(FirstMarks.this, FirstResult.class);
                Bundle bundle = new Bundle();
                bundle.putInt("res1", Total1);
                bundle.putFloat("per1", Percentage1);
                i.putExtras(bundle);
                startActivity(i);
                finish();

            }
        });


    }

    //FUNCTION TO GET TEXT FROM EDIT TEXT AND CONVERT INTO INTEGER.
    private int getInt(EditText et) throws NumberFormatException {
        return Integer.valueOf(et.getText().toString());
    }
}


**SECOND ACTIVITY**

Code:


public class FirstResult extends Activity {

    EditText et_Total1,et_Percentage1;
    Button btn_Exit1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result_first);

        //INITIALISING VIEWS.
        et_Total1 = (EditText) findViewById(R.id.et_Total1);
        et_Percentage1 = (EditText) findViewById(R.id.et_Percentage1);
        btn_Exit1 = (Button) findViewById(R.id.btn_Exit1);

        //GETTING DATA FROM PREVIOUS ACTIVITY.
        Bundle b = getIntent().getExtras();
        int res1 = b.getInt("res1", 0);
        float per1 = b.getFloat("per1", 0);

      //CONVERTING TO STRING.
        String sres1 = String.valueOf(res1);
        String sper1 = String.valueOf(per1);

        //SETTING RESULTS.
        et_Total1.setText(sres1);
        et_Percentage1.setText(sper1);

        //WHEN EXIT IS CLICKED.
        btn_Exit1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
          }
        });


    }
}





mardi 8 septembre 2015

The Wrong Activity Shows Up Android Studio



Can anybody help me. I have developed a quiz app. At the end of the quiz the result activity must show up but the scores activity shows up. The scores activity only shows up when the scores button is selected on the MainActivity.

Quiz.java


Code:


package app.mobiledevicesecurity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Quiz extends Activity
{
    List<Question> questionList;
    int score = 0;
    int qid = 0;
    Question currentQuest;
    TextView txtQuestion, scored;
    Button button1, button2, button3;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        QuizHelper db = new QuizHelper(this);
        questionList = db.getAllQuestions();
        currentQuest = questionList.get(qid);
        txtQuestion = (TextView) findViewById(R.id.txtQuestion);

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        scored = (TextView) findViewById(R.id.score);



        setQuestionView();

        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
              getAnswer(button1.getText().toString());
            }
        });

        button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                getAnswer(button2.getText().toString());
            }
        });

        button3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                getAnswer(button3.getText().toString());
            }
        });
    }
    public void getAnswer(String AnswerString)
    {
        if (currentQuest.getAnswer().equals(AnswerString))
        {
            score++;
            scored.setText("Score : " + score);
        }
        else
        {

            Intent intent = new Intent(Quiz.this,
                    Result.class);
            Bundle b = new Bundle();
            b.putInt("score", score);
            intent.putExtras(b);
            startActivity(intent);
            finish();


        }
        if (qid < questionList.size()) {
            currentQuest = questionList.get(qid);
            setQuestionView();
        }
        else
        {

            Intent intent = new Intent(Quiz.this,
                    Result.class);
            Bundle b = new Bundle();
            b.putInt("score", score);
            intent.putExtras(b);
            startActivity(intent);
            finish();


        }
    }

    private void setQuestionView()
    {
        txtQuestion.setText(currentQuest.getQuest());
        button1.setText(currentQuest.getOption1());
        button2.setText(currentQuest.getOption2());
        button3.setText(currentQuest.getOption3());
        qid++;
    }
}


Result.java

Code:


package app.mobiledevicesecurity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Result extends Activity {

    private static Button playbtn;
    private static Button menubutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        OnClickPlayButtonListener();
        OnClickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        int score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");

        Intent intent2 = new Intent(Result.this,
              Scores.class);
        Bundle bun = new Bundle();
        bun.putInt("score", score);
        intent2.putExtras(bun);
        startActivity(intent2);
        finish();
    }

    public void OnClickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void OnClickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }
}


Scores.java

Code:


package app.mobiledevicesecurity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);

        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        Bundle bun = getIntent().getExtras();
        int score = bun.getInt("score");
        txtScore1.setText("score:" + " " + score + " for the quiz.");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scores, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}





vendredi 4 septembre 2015

Pass data from one activity to another



Can you help me. I have developed a app with a quiz. When the quiz is finished a new activity (result) appears showing the score. I want to pass the score to a new activity namely score. Here is my code. What is wrong. The score must be showed in a textview.

Result.java

Code:


package app.mobiledevicesecurity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Result extends Activity
{

    private static Button playbtn;
    private static Button menubutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        OnClickPlayButtonListener();
        OnClickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        int score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");
        Intent i = new Intent(getApplicationContext(), Result.class);
        i.putExtra("somevariable",score);
        startActivity(i);
    }

    public void OnClickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void OnClickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                      }
                }
        );
    }


Scores.java

Code:


package app.mobiledevicesecurity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;

public class Scores extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
        Intent intent = getIntent();
        String value = intent.getStringExtra("somevariable");
        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        txtScore1.setText("You scored" + " " + value + " for the quiz.");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scores, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}





jeudi 3 septembre 2015

New Activity after G+ Login



Hi. I have build a google+ login. after the login, i want that a new layout/activity will be shown. when i create a new layout with sample text, it works find. but when i create a new navigation drawer activity, i get many error when i setvontentview to the new layout_activity. but only with the new main activity. the navigation drawer build three new layout files, like allways, and when i setcontent.... to one of the other files, i get only a blank screen, but no errors.

can someone help?