Problems with intent in second activity - java

I am new in the android dev and java
My idea is to make BMI that will have history.
So I am at the point where I struggle with the intent for the second activity.
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivity2();
}
});
}
private void openActivity2() {
Intent intent = new intent(this, Activity2.class);
startActivity(intent);
}
I am getting:
can not resolve symbol intent

'i' should capital when you create new Intent() object The openActivity2() should look like this
private void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}

Related

How to make a second transition through a fragment

I made the first transition from the fragment to the activity, everything works fine, but I can't make the second transition. I'm a beginner, and I understand that this is a stupid mistake.
View v = inflater.inflate(R.layout.activity_physics, null);
Button phys35 = (Button) v.findViewById(R.id.phys35);
phys35.setOnClickListener(new View.OnClickListener() {
Public void onClick(View v) {
Intent intent = new Intent(getContext(), Phys35.class);
StartActivity(intent);
}
});
return v;
Button phys36 = (Button) v.findViewById(R.id.phys36);
phys36.setOnClickListener(new View.OnClickListener() {
Public void onClick(View v) {
Intent intent = new Intent(getContext(), Phys36.class);
StartActivity(intent);
}
});
return v;
Please help, thank you in advance for your help.

How to stop looping in activites and at the same handle the default on screen navigation back?

ImageView Back = findViewById(R.id.imageView11);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Nav.class);
startActivity(intent);
finish();
}
});
Button cash = findViewById(R.id.button11);
cash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Cash.class);
startActivity(intent);
finish();
}
});
TextView Card = findViewById(R.id.textView26);
Card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Card.class);
startActivity(intent);
finish();
}
});
Button AddCard = findViewById(R.id.button12);
AddCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, AddedCard.class);
startActivity(intent);
finish();
}
});
TextView promo = findViewById(R.id.textView28);
promo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, promocode.class);
startActivity(intent);
finish();
}
});
Button Voucher = findViewById(R.id.button13);
Voucher.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Vouchers.class);
startActivity(intent);
finish();
}
});
}
}
If i remove finish(); this create loops when I press default navigation back button.
like if I visited promocode class,then voucher class.from vouchar class I want to go back in Main Class by pressing default navigation back button.it will reverse like vouchar to promode then main class.
But I want by default back button will bring me vouchar class to directly main class.
But after adding finish(); whenever i press navigation back button the app quits.
I want a solution that...default navigation back button will act like my every customized back button for every each individuals frames !!
The key is to provide proper navigation.
Here's the Official Documentation to understand the tasks and back stack.
The other way to provide back navigation to the custom activity is to override onBackPressed() method of your activity like :
#Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(YourCurrentActivity.this, MainActivity.class));
finish();
}

Distribute an edit text to many TextViews

i'm new to Android programming, i want to creating an android apps that need user input in MainActivity then display the username in another activity. i planned create 5 more activity to display the username.
this is the MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.start);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);
getMenuInflater().toString();
sendName();
startActivity(intent);
}});
}
public void sendName(){
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);
EditText inputnama = (EditText) findViewById(R.id.inputnama);
intent.putExtra("username", message);
}
and this is new1Activity
Intent username = getIntent();
String username1 = getIntent().getStringExtra("username1");
i used the same code to another new Activity but it don't display anything.
can anyone explain to me what's wrong?
EditText inputName = (EditText) findViewById(R.id.inputnama);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(inputName != null){
String message = inputName.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("prefName", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", message);
editor.apply();
editor.commit();
startActivity(new Intent(MainActivity.this, Quiz1Activity.class);
}
}});
In QuizActivity and other activities
SharedPreferences sharedPreferences = getSharedPreferences("prefName", 0);
String userName = sharedPreferences.getString("username", "");
store values in Intent;
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);
EditText inputnama = (EditText) findViewById(R.id.inputnama);
intent.putExtra("username", inputnama.getText().toString() );
get values using Intent;
String username1 = getIntent().getStringExtra("username");
If i understood the question correctly you need this when starting your activity:
Intent intent = new Intent();
intent.putExtra("userName", userName);
startActivity(intent);
and to get the name in the new activity:
getIntent().getExtras().getString("userName");
In your onClick method, you declare your :
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);.
Then in sendName(), you have :
Intent intent = new Intent(MainActivity.this, Quiz1Activity.class);.
You should understand that these two intents are not the same, they are two differents objects. The modifications on one won't affect the other.
So your intent in onClick wasn't modified. What you want to do is to return the modified intent from sendName, something like startIntent(sendName());.
Additionally, you could just pass intent as parameter in sendName.
Use this clicklistener.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new
Intent(MainActivity.this,Quiz1Activity.class);
getMenuInflater().toString();
EditText inputnama = (EditText) findViewById(R.id.inputnama);
intent.putExtra("username", inputnama.getText().toString());
startActivity(intent);
}});

how can i pass a string array through a button from one activity to another?

String[] charc={"FLash","ARROW","SUPERGIRL","BATMAN","SUPERMAN","ATOM","AQUAMAN","CYBORG","JOKER","BANE","GREENLANTERN","ZOOM","REVERSEFLASH","FIRESTORM"};
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"onCreate was Called",Toast.LENGTH_SHORT).show();
lv = (ListView) findViewById(R.id.idList);
b = (Button) findViewById(R.id.Btn);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,charc);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
s=charc[i];
b.setText(s);
Toast.makeText(getApplicationContext(),"Item Clicked: "+i,Toast.LENGTH_SHORT).show();
}
});
}
public void btnclick(View v){
startActivity(new Intent(MainActivity.this, approutes.list.Display.class)
.putExtra("Flash",s));
}
Intent accept String array
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("string", array);
startActivity(intent);
In your receiving intent you need to do:
Intent i = getIntent();
string_array = i.getStringArrayListExtra("string");
Try this.
From Your first Activity
Intent intent = new Intent(context, FirstActivity.class);
intent.putExtra("string-array", charc);
context.startActivity(intent);
Then In Next Activity Get it Like this in on Create()
Intent intent = getIntent();
String [] stringArray = intent.getStringArrayExtra("string-array");
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("list_as_string", charc);
startActivity(intent);
along with these you should add your SecondActivity in Manifest as Activity and make sure you have added btnclick() method to button in xml layout or you can set button click with following
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call intent here
}
});
To pass data between activities there is Intent object. You should do:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("charc", charc);
context.startActivity(intent);
And to get charc in NextActivity do:
Intent intent = getIntent();
char[] charc = intent.getStringExtra("charc");

Android OnClickListener, intent and context

I am new to java and android. Here I am trying to set up my onclicklistener so when clicked, it will show another activity, i.e. ActivityB.class. The problem is with Intent i = new Intent(context, ActivityB.class); I am not sure what to put there for context. I tried to use this and context, and both are wrong.
Could you please kindly explain when and why I should use this and when to use other terms for the context?
public class MainActivity extends Activity {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, ActivityB.class);
startActivity(i);
}
});
}
Change the code to.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
}
});
As you need to pass context while using intent.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ActivityB.class));
}
});
Fastest method!
Hope it helps! :D
1) Replace context with getApplicationContext()
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ActivityB.class);
startActivity(intent);
}
});
2) Replace context with MainActivity.this
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ActivityB.class);
startActivity(intent);
}
});
Hope it will help you!
Try this..
If you declare ActivityB.class in manifest this should work.
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
Intent i = new Intent(this, ActivityB.class);
startActivity(i);

Categories

Resources