I'm trying to make an ImageView open another activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gtacheats);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gtacheats, menu);
return true;
ImageView Image1 = (ImageView) findViewById(R.id.Image1);
Image1.setClickable(true);
Image1.setOnClickListener(new View.OnClickListener() {
public void onClick(View V) {
Intent intent = new Intent(V.getContext(), Activity2.class);
startActivityForResult(intent, 0);
}
});
}
}
Where i'm doing wrong? It say "Unreachable code"
move return true as last statement of onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.gtacheats, menu);
ImageView Image1 = (ImageView) findViewById(R.id.Image1);
Image1.setClickable(true);
Image1.setOnClickListener(new View.OnClickListener() {
public void onClick(View V) {
Intent intent = new Intent(V.getContext(), Activity2.class);
startActivityForResult(intent, 0);
}
});
return true;
}
Related
Please help,
I want the menu to be hidden when I enter the Activity at the begining.
And after progressbar is set to gone, the menu will not be hidden.
But when I ran my app, the menu was always hidden.
I don't know how to show the menu on screen again.
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
private ActivityHomeBinding binding;
private ActionBar actionBar;
private Menu menu;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityHomeBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
actionBar = getSupportActionBar();
progressBar = binding.progressBar;
progressBar.setVisibility(View.VISIBLE);
// it's not work
onPrepareOptionsMenu(menu);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.function_menu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
menu.clear();
if (progressBar.getVisibility() == View.VISIBLE) {
onCreateOptionsMenu(menu);
return false;
} else {
onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.function_menu, menu);
return true;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.menu_home:
intent = new Intent(HomeActivity.this, HomeActivity.class);
homeActivityResultLauncher.launch(intent);
return true;
case R.id.menu_summary:
intent = new Intent(HomeActivity.this, SummaryActivity.class);
summaryActivityResultLauncher.launch(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Is there a way how you can add a string to an arraylist, which then fills a listview when a button is clicked in another activity? To be more clear: In activity A, I have a Listview and a menu item where I can go to Activty B. In Activtiy B I have got an EditText and a button. I want to achieve that everytime I click on the Button in activty B, the new String gets listed in the Listview. As a "clicklistener" I used a boolean. Is there a better way? Because with the code below I can only make one list entry which gets changed by every next button click.
Java Code Activity A:`package com.example.schoolapp;
public class MainActivity extends AppCompatActivity {
TextView tvSubjects;
ListView listView;
static ArrayList<String> arrayList = new ArrayList<>();
static ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvSubjects = findViewById(R.id.tv_subjects);
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
arrayList);
arrayAdapter.notifyDataSetChanged();
listView.setAdapter(arrayAdapter);
if (NewNoteActivity.buttonClicked){
Intent intent = getIntent();
String string = intent.getStringExtra("Subject");
arrayList.add(string);
arrayAdapter.notifyDataSetChanged();
}
}
//Code for the menu, works fine
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.item_newSubject:
Intent intent = new Intent(this, NewNoteActivity.class);
startActivity(intent);
return true;
default:
return false;
}
}
}`
Java Code for Activity B:
public class NewNoteActivity extends AppCompatActivity {
EditText et_subject;
Button btn_createSubject;
String subject;
static boolean buttonClicked = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
et_subject = findViewById(R.id.et_subject);
btn_createSubject = findViewById(R.id.btn_createSubject);
btn_createSubject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonClicked = true;
subject = et_subject.getText().toString();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Subject", subject);
startActivity(intent);
}
});
buttonClicked = false;
}
}
This is my First Question & I'm a new android learner,
Suddenly in my ellipse 2nd layout button is not working;
after pressing button of my first layout it working & going to second layout.
But in 2nd layout my Button is not working, I tested it very simple code. Please check
public class MainActivity extends Activity {
Button b1,b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
//setContentView(R.layout.activity_main_activity2); Need to remove
startActivity(new Intent(MainActivity.this,MainActivity2.class));
}
});
}
#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;
}
}
This is my 2nd activity code
public class MainActivity2 extends Activity {
EditText ed;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ed = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "Working", 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.main_activity2, menu);
return true;
}
}
*You are simply changing the layout on first activity using setContentView(). You are not navigating from one activity to another.
Either you can register the button of layout 2 by onClicklistener after setting it on activity.
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main_activity2);
button = (Button) findViewById(R.id.button21);
button.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"Working",Toast.LENGTH_SHORT).show();
}
});
}
});
or you can goto your other activity
b1.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// setContentView(R.layout.activity_main_activity2); remove
startActivity(new Intent(MainActivity.this,MainActivity2.class));
}
});
I'm trying to make a property animation on ImageButton, a button will start the animation when clicked on, but in the private static class ImageButtonAnimatorHelper method, it's showing an error.
public class MainActivity extends Activity {
// ImageButton jackfruit = (ImageButton) findViewById(R.id.btnjackfruit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.btn);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ball();
}
public void ball() {
ImageButton ball = (ImageButton) findViewById(R.id.btnball);
ball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// ImageButton b = (ImageButton) findViewById(R.id.btnball);
ImageView banan = (ImageView) findViewById(R.id.banana);
banan.setVisibility(View.VISIBLE);
} // onClick
}); // setOnClickListener
ObjectAnimator horizontalAnimator = ObjectAnimator.ofInt(
new ImageButtonAnimatorHelper(ball), "marginLeft", 0, 600);
horizontalAnimator.setDuration(2000);
horizontalAnimator.setRepeatCount(ValueAnimator.INFINITE);
horizontalAnimator.setRepeatMode(ValueAnimator.REVERSE);
horizontalAnimator.setInterpolator(new LinearInterpolator());
horizontalAnimator.start();
} // onCreate
private static class ImageButtonAnimatorHelper {
ImageButton ballButton;
public ImageButtonAnimatorHelper(ImageButton imagebutton) {
ballButton = imagebutton;
}
public void setMarginLeft(int margin) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) ballButton
.getLayoutParams();
params.leftMargin = margin;
ballButton.setLayoutParams(params);
} // setMarginLeft
} // ImageButtonAnimatorHelper
});
}
#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;
}
}
I'm confused with when to use private/public/abstract class..
These are two activities which are linked between each other but those are not working
and i have provided method name in xml file as onClick="menu" for both the buttons and the method over here
public class Welcome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
/**Intent i = new Intent(this,Menup.class);
finish();
startActivity(i);*/
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
public void menu(View v)
{
finish();
Intent i = new Intent(this,Menup.class);
startActivity(i);
}
}
it will be moved to the next activity name and code below
public class Menup extends Activity {
Button route,map,ticket;
TextView bal;
String time,src,des,clas,journey,noa,noc,amount;
int itime,old=50,amt,camt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menup);
bal=(TextView)findViewById(R.id.textView1);
//getting the values
Intent i=getIntent();
time=i.getExtras().getString("time");
itime=Integer.valueOf(time);
src=i.getExtras().getString("src");
des=i.getExtras().getString("des");
clas=i.getExtras().getString("class");
journey=i.getExtras().getString("journey");
noa=i.getExtras().getString("noa");
noc=i.getExtras().getString("noc");
amount=i.getExtras().getString("amount");
camt=Integer.valueOf(amount);
route=(Button)findViewById(R.id.imageButton1);
map=(Button)findViewById(R.id.imageButton2);
ticket=(Button)findViewById(R.id.imageButton3);
route.getBackground().setAlpha(0);
map.getBackground().setAlpha(0);
ticket.getBackground().setAlpha(0);
amt=old-camt;
bal.setText("Current Balance "+amt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menup, menu);
return true;
}
public void toroute(View v)
{
Intent r = new Intent(Menup.this,Route.class);
startActivity(r);
}
public void tomap(View v)
{
Intent m = new Intent(Menup.this,Map.class);
startActivity(m);
}
public void toticket(View v)
{
Intent d=new Intent(Menup.this,Tick.class);
d.putExtra("noa",noa);
d.putExtra("noc",noc);
d.putExtra("src",src);
d.putExtra("des",des);
d.putExtra("class", "Class I");
d.putExtra("journey", "Single");
d.putExtra("amount", amount);
d.putExtra("time", itime);
startActivity(d);
}
#Override
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exit");
builder.setMessage("Are you sure , you want to exit Ticketwala?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
finish();
System.exit(0);
dialog.dismiss();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
}).show();
}
}
You don't have to "finish()" your activity Welcome before starting the next activity. But if you must, then put it after startActivity();