At the moment i use this code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
, but now the Button shouldnt be only a back-function. I like to start an other activity:
Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
startActivity(intent);
I know i should use the setOnClickListener, but I don't know where i call the Listener.
Although I agree with Tanis.7x comment, you shouldn't be using that button if it's not to go back by calling finish() or popBackStack(),
the action for the home button is called with the menu options
http://developer.android.com/guide/topics/ui/actionbar.html#Home
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Related
I would like to add a share button that opens something like this.
I am using this source from the official Android site
I want to send standard text so I thought I should use this part:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
But I have no idea where to place the code.
How would I do it if I wanted to place it in 1 of these 3 spots?
I would prefer to have it in the Menu or up top and just remove the button below but I am open to any solution.
To add a click event listener to the FloatingActionButton:
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
share();
}
});
private void share(){
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
}
To handle a menu item click:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.yourItemId:
share();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
From your comment : This worked for me. Is there anyway to do this on top of the screen next to the settings button?
You have to create menu directory in resource/res. Then, create a menu. Add following source code to menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/share"
android:title="Shar"/>
</menu>
Add following source code to MainActivity
#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) {
int id = item.getItemId();
switch (id){
case R.id.share:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If you want to trigger this code from your navigation drawer, then you need to override the onNavigationItemSelected method in your calling activity like this
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.nav_share: {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
break;
}
}
//close navigation drawer
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
Identify the ID of the menu item, then place the code accordingly.
By default the button(setDisplayHomeAsUpEnabled), returns the parent activity, how would I change it? For example: I need to back to a Intent
toolbar = (Toolbar) findViewById(R.id.tb_dados);
toolbar.setTitle("Adicione uma descrição");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
You can try this....
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
// your intent code here.
break;
}
return super.onOptionsItemSelected(item);
}
you can use onOptionsItemSelected method and perform any other function if you want
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Toast.makeText(this, "back Button Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Since you are using custom toolbar so you can easily use custom toolbar like this
toolbar.setNavigationIcon(R.drawable.back_arrow);// use your back arrow image
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this,MainActivity.class));//call which activity you want in back press
finish();
}
});
I am trying to manually implement the actions that must take place when the up button on the actionbar is pressed but for some reason nothing happens when I press it.
here is my code:
public class ActivityOne extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_one);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar_actionbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Button button = (Button)findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivityTwo();
}
});
Button button2 = (Button)findViewById(R.id.btn2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openActivityThree();
}
});
}
void openActivityTwo(){
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
}
void openActivityThree(){
Intent intent = new Intent(this, ActivityThree.class);
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_activity_one, 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;
}
else if(id == R.id.homeAsUp){
Log.i("","Up is pressed");
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
I understand that I have to explicitly assign a parent activity for the activity I want to implement up navigation on the manifest file, but problem is that this activity has multiple parents so I thought calling the finish() method when the up button is pressed on this activity will be the better approach.
I have already tried both id == R.id.home and id == R.id.homeAsUp and they both do not work. I do not know if it is because I am using AppCompactActivity or what Please help
Here is how you can implement this, try this code
use android.R.id.home instead of R.id.home or R.id.homeAsUp
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//use onBackPressed() OR finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The correct way to do it is to override public boolean onNavigateUp() just like overriding onBackPressed().
Using android studio 3.6.2 I've found that the Up button is not displayed by default in my apps, but it can be easily added..
Go to AndroidManifest.xml and update each activity that needs an Up button as follows:
<activity
android:name=".yourClassName"
android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourPackageName" />
</activity>
The target destination for the Up button is set by changing parentActivityName. In this example I have set it as '.MainActivity' but you can change that to whatever activity you want the user to navigate to.
Hi im trying to make a notification appear at random times throughout the day. right now all it does is make the notification appear when i press the a button. I would like the button to start making them appear every hour or so. Here is my MainActivity class.
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NotificationCompat.Builder notification = new NotificationCompat.Builder(MainActivity.this);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setTicker("Hey!!!");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("You're Awesome");
Uri sound = RingtoneManager.getDefaultUri(Notification.DEFAULT_SOUND);
notification.setContentText("keep being awesome!!!:)");
notification.setSound(sound);
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
notification.setLargeIcon(picture);
PendingIntent mypendingintent;
Intent myintent = new Intent();
Context mycontext = getApplicationContext();
myintent.setClass(mycontext, Activity2.class);
myintent.putExtra("ID", 1);
mypendingintent = PendingIntent.getActivity(mycontext, 0, myintent, 0);
notification.setContentIntent(mypendingintent);
Notification n = notification.build();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(1,n);
}
});
}
#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);
}
}
Look over here: https://developer.android.com/training/scheduling/alarms.html
What you need to do is make an alarm, for example which will activate your app code every hour like you want. In this alarm you will make a new notification.
If you need a code example look over here: Alarm Manager Example
I am trying to open another activity when a button is clicked from Context Menu Action Bar. But the application is force closing when i click the button.Can't we use intents in contextual action mode?
This is my code:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.item_id:
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
break;
}
}