How to Go to New Screen/Page in Android Studio? - android

I have a quick question regarding Android Studio.
So First Of all I am COMPLETELY new to this thing so don't get me wrong if i make a mistake.
So what I am doing is that I have created a title screen for my app called "Canada Quiz". And below the title I have a "Start" Button. And what I done so far is I have attached a code that would do something like this: So once a Start Button is clicked it would hide it. Now my Question.
How would I make it so That once Clicked it should hide the Start Button and show the Question and a TRUE and FALSE button. All I have is this. I just want the Questions the appear as soon as the person taps the Start Button. And in My Content_Main.xml i have a phone as a model witch displays the start button and the Canada quiz. So Please help me out as soon as POSSIBLE
Thank you!
package com.example.nilesh.myapplication;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button startButton;
private Button trueButton;
private Button falseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.startButton);
ArrayList <String> questions = new ArrayList<String>();
String firstQue = " Is Canada Located in North America? ";
String secondQue = " Is Ontario the Largest provice in Canada? ";
String thridQue = " Is Toronto the Largest City in Canada? ";
String fourQue = " Canada has 3 professional Basketball Teams. ";
String fiveQue = " Canada has 11 Provinces and 3 Territories. ";
String sixQue = " Justin Trudeau is not Canada's Prime Minister. ";
final String [] allQuestions = {firstQue,secondQue,thridQue,fourQue,fiveQue,sixQue};
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
startButton.setVisibility(View.INVISIBLE);
}
});
}
#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_main, 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);
}
public Button getTrueButton() {
return trueButton;
}
public void setTrueButton(Button trueButton) {
this.trueButton = trueButton;
}
public Button getFalseButton() {
return falseButton;
}
public void setFalseButton(Button falseButton) {
this.falseButton = falseButton;
}
}
Note I cannot see the TRUE BUTTON and FALSE BUTTON the content_main.xml file....

To go to next screen, use this code:
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);

Related

How to create button dynamically from one activity to another activity

I am creating an android application that consists of adding button dynamically.Here when I click the button in one activity then a button will dynamically created another activity. Is it possible to create a button when clicking a button in one activity then create a button in another activity.Please help me with this.
package com.example.dynamicbutton;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button Add_Button;
Activity_2 act_child;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Add_Button = (Button)findViewById(R.id.button_add);
Add_Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent invisible = new Intent(MainActivity.this,Activity_2.class);
startActivity(invisible);
act_child.visible.setVisibility(View.GONE);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The solution is simple.Do Following in First Activity.
If the button clicked use following.
Intent invisible = new Intent(MainActivity.this,Activity_2.class);
//here add this line
.putExtra("visibility", "1");
startActivity(invisible);
else
Intent invisible = new Intent(MainActivity.this,Activity_2.class);
//here add this line
.putExtra("visibility", "0");
startActivity(invisible);
In your Second Activity:
String visibility = getIntent().getStringExtra("visibility");
if (visibility.equals("1")){
act_child.visible.setVisibility(View.VISIBLE);
}else{
act_child.visible.setVisibility(View.GONE);
}
Don't make variables public.Change your design to
1 ] Pass some variable to Second Activity . (In Button click of First Activity).
2 ] Get the variable in Second Activty & change the visibilty of Button (in Second Activity according to that variable)
Intent i = new Intent(CurrentActivity.this,SecondActivity.class);
i.putExtra("flag","show");
startActivity(i);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("flag");
if(value.equals("show")){
button1.setVisible(View.Visible);
}
else{
button1.setVisible(View.Gone);
}

Displaying a button under a string message

Hello I am very new to android programming. I would like to get some help regarding displaying a button under a string message under an activity which has been newly activated. Hope someone can help me. Here is the code which I have for the class MyActivity which activates when the button app tutorial is pressed. The xml for my main activity is also there. Also the displaymessage activity class and xml is also there.
package com.example.sadi_ace.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.sadi_ace.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/** Called when the user clicks the Send button */
public void TutorialText(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
String message = "This is the android tutorial, if you want to pass on the to the " +
"functions please press SKIP!";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
// #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_my, 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);
}
}
XML for main activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#800080"
android:weightSum="1">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="#string/button_tutorial"
android:background="#FFFFFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="TutorialText"
/>
</RelativeLayout>
DisplayMessageActivity class code
package com.example.sadi_ace.myfirstapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
// #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_display_message, 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);
}
}
XML for DisplayMessageActivity class
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#800080"
android:weightSum="1">
</RelativeLayout>
Add button in your xml file activity_my like
<Button
android:id="#+id/btnskip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
After that use this button id in "onCreate " method and assign it to some button
Button skip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
skip=(Button)findViewById(R.id.btnskip);
//add on click listener
skip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, DisplayMessageActivity.class);
String message = "This is the android tutorial, if you want to pass on the to the " +
"functions please press SKIP!";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
});
}

Create a button than send the user to the main page

I am new with Android Studio world. I made a two activity pages. The first button in the first page send the user to the second page which it works fine
public void change(View v){
setContentView(R.layout.activity_main_activity2);
but I made the same button in the second page to send the user the main page but no luck. Please see the code and let me know why I got this error.
public void HomePage(View v){
setContentView(R.layout.activity_main_activity);
This is the first activity page
package com.example.lenovo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends ActionBarActivity {
private Menu menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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);
}
public void change(View v){
setContentView(R.layout.activity_main_activity2);
}
}
and this is the second activity
package com.example.lenovo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import static com.chapter2.example.lenovo.chapter2.R.layout;
public class MainActivity2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main_activity2);
Button mButton = (Button) findViewById(R.id.button2);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent mIntent = new Intent(MainActivity2.this,
MainActivity.class);
startActivity(mIntent);
}
});
}
#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_main_activity2, 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);
}
public void HomePage (View v){
setContentView(R.layout.activity_main);
}
}
Currently you are switching layouts of Activity instead of changing Activity. this not right way to switch between Activities. use startActivity method with intent to start next Activity on Button click:
Start MainActivity2 from MainActivity:
public void change(View v){
Intent intent=new Intent(v.getContext(),MainActivity2.class);
startActivity(intent);
}
Start MainActivity2 from MainActivity:
public void HomePage(View v){
Intent intent=new Intent(v.getContext(),MainActivity.class);
startActivity(intent);
}
and make sure to have added both Activities in AndroidManifest.xml
If you want use inbuilt device back button then simply put
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
and if you want to go back by button click put this line
bbsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
}
In the first activity you are not launching the mainactivity2.class when you click the button, instead you are just changing the content view.
and the R.id.button2 is not registered in the first activity.
So it is not working
You have to send user first page to second page using Intent
You need to add onClick tag in that button in activity_main layout like this
android:onClick="change"
in activity_main_activity2 add onClick tag in that button like this
android:onClick="HomePage"
In MainActivity2 in setContentView change like this
setContentView(R.layout.activity_main_activity2);
In first activity do like this
public void change(View v){
Intent mIntent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(mIntent);
}
In second activity do like this
public void HomePage(View v){
Intent mIntent = new Intent(MainActivity2.this,MainActivity.class);
startActivity(mIntent);
}
Don't forget to declare those Activities in manifest

Why my aplication call the OnCreate's event every i call an activity?

i having a problem.
I try add a value in a listview since other activity, so i have 2 activities
the number 1 have a button (to call the activity 2) and have a listview
the other activity hace a 2 edittext and a button where call to activity 1 (here pass the 2 values ina putextras)
SO when i call the activity 2 and i back the event Oncreate is called againg, someone know why?
my code activity 1
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private ArrayList<User> arrayOfUsers;
private UsersAdapter adapter;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lvItems);
arrayOfUsers = new ArrayList<User>();
adapter = new UsersAdapter(this, arrayOfUsers);
listView.setAdapter(adapter);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}
#Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if(extras != null)
{
if( extras.getString("ename") != null && extras.getString("ehome") != null) {
User newUser = new User(extras.getString("ename"), extras.getString("ehome"));
adapter.add(newUser);
}
}
listView.setAdapter(adapter);
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
#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_main, 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);
}
public void clicbtn(View v){
Intent i = new Intent(MainActivity.this,MainActivity2.class);
startActivity(i);
}
}
my code activity 2
public class MainActivity2 extends ActionBarActivity {
private EditText ename;
private EditText ehome;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ename = (EditText) findViewById(R.id.editText);
ehome = (EditText) findViewById(R.id.editText2);
}
#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_main_activity2, 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);
}
public void accion(View v){
Intent i = new Intent(MainActivity2.this, MainActivity.class);
//i.putExtra("ename",ename.getText().toString());
//i.putExtra("ehome",ehome.getText().toString());
startActivity(i);
}
}
onCreate() is getting called because you are again starting it as a new activity from this line
Intent i = new Intent(MainActivity2.this, MainActivity.class);
//i.putExtra("ename",ename.getText().toString());
//i.putExtra("ehome",ehome.getText().toString());
startActivity(i);
If you want to send values to previous activity use startActivityForResult
This SO question can help you implementing this How to manage `startActivityForResult` on Android?

Android edittext with a static string

I am working on an app where the user inserts his/her name and i will be displayed in a textview.
package com.opgaveet.buttonlistener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText edit;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button1);
edit = (EditText) findViewById(R.id.editText1);
text = (TextView) findViewById(R.id.textView2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Your name has been submitted", Toast.LENGTH_LONG).show();
String name = edit.getText().toString();
text.append(name);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This code work fine, no issues there, but it only displays the name that has been inserted. Is there a method to also display "Welcome name inserted" instead of just the name, when the name has been submitted?
You could simply concatenate the "Welcome" part, something like this:
text.append("Welcome " + name);
What you'll want to do is use a string in your strings.xml file with a marker in that string which will be replaced.
Create a string in your strings.xml file:
<string name="welcome_text">Welcome %s inserted</string>
Now in your code do this:
text.append(String.format(getString(R.string.welcome_text), name);
You can also just do
getString(getString(R.string.welcome_text), name);
More info here:
http://developer.android.com/reference/java/util/Formatter.html
Use:
String name = "Welcome " + edit.getText().toString();`
or:
text.append("Welcome" + name);`
TO append data of same textview. Create a textview and string like this.
String: <string name="welcome">Welcome</string>
Textview
android:id="#+id/text"
android:text="#string/welcome"
In Activity set like this.
txt_welcome = (TextView)findViewById(R.id.text);
txt_welcome.setText(txt_welcome.getText().toString()+" "+USERNAME);

Categories

Resources