Hello guys am trying to make user login and if he is not an admin I want some bottom navigation bar
hidden I want to hide item before activity showUp for user
I am sending a certain value from activation of the user login and I want to verify before the screen appears to the user if the value is equal to the user answers hide the icon
I tried to get to the icon id but I did not know the correct way to do that please help and thank you
#Override
protected void onStart() {
super.onStart();
BottomNavigationViewEx bottomNavigationViewEx = new BottomNavigationViewEx().findViewById(R.id.addproudactbtnbar).setVisibility(false);
}
my XML
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottm_navigation_menu"
android:background="#color/colorPrimary"
app:menu="#menu/bottm_navigation_menu"
>
</com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>
public void setupbottmnavigationview(){
BottomNavigationViewEx bottomnavigationviewex = findViewById(R.id.bottm_navigation_menu);
BottomNvigationViewHelper.botomnavigationview(bottomnavigationviewex);
BottomNvigationViewHelper.enebleactivty(Home.this , bottomnavigationviewex);
Menu menu = bottomnavigationviewex.getMenu();
MenuItem menuItem = menu.getItem(ATIVYTY_NUM);
menuItem.setChecked(true);
}
my navigation helper
public class BottomNvigationViewHelper {
public static void botomnavigationview (BottomNavigationViewEx bottomNavigationViewEx){
bottomNavigationViewEx.enableAnimation(false);
bottomNavigationViewEx.enableItemShiftingMode(false);
bottomNavigationViewEx.enableShiftingMode(false);
bottomNavigationViewEx.setTextVisibility(false);
}
public static void enebleactivty(final Context context , BottomNavigationViewEx viewEx){
viewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.homebtnbar:
Intent intent1 = new Intent(context, HomeScreenActivity.class);
context.startActivity(intent1);
break;
case R.id.profilebtnbar:
Intent intent2 = new Intent(context, ProfileScreenActivity.class);
context.startActivity(intent2);
break;
case R.id.searchusersbtnbar:
Intent intent3 = new Intent(context, SearchScreenActivity.class);
context.startActivity(intent3);
break;
case R.id.deleverybtnbar:
Intent intent4 = new Intent(context, DeleviryScreenActivity.class);
context.startActivity(intent4);
break;
case R.id.addproudactbtnbar:
Intent intent5 = new Intent(context, Addproudacts.class);
context.startActivity(intent5);
break;
}
return false;
}
});
}
}
You can inflate the menu programmatically based on the type of the user with
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.new_navigation_drawer_items);
From this answer
Related
For a bit of context, I'm doing a learning app, when the user reach the end of the lesson a dialog shows but when I click the "go back" option to return to the previous fragment (the app is designed with a Navigation object and different fragments) I get the error on the title.
#Override
public void onClick(View view) {
Bundle bundle = new Bundle();
switch (view.getId()) {
case R.id.btnL:
if (pagina == 1) {
Toast toast = Toast.makeText(getContext(), "Estás al principio", Toast.LENGTH_SHORT);
toast.show();
} else {
llenarDatos(--pagina, datos);
}
break;
case R.id.btnR:
if (pagina < limite)
llenarDatos(++pagina, datos);
else
showDialog(view);
break;
case R.id.btn_no:
bundle.putBoolean("modo", true);
Navigation.findNavController(view).navigate(R.id.fragment_menu, bundle);
break;
case R.id.btn_yes:
break;
}
}
The case R.id.btn_no is the option with the error.
Dialog code:
private void showDialog(View v) {
dialog = new Dialog(getContext());
dialog.setContentView(R.layout.custom_dialog);
Button btnVolver = dialog.findViewById(R.id.btn_no);
Button btnExamen = dialog.findViewById(R.id.btn_yes);
btnVolver.setOnClickListener(this);
dialog.show();
}
I've read in another post that this happens because the dialog dismiss when pressing the button, so it doesn't exist anymore. How can I solve it?
I created an application that have 4 activities: login, sport, technology and sporttechnology.
the login activity that contains two check boxes in a listview (named sport and technology) and a button, when press the button the program check the text of the checked check boxes and opens the right activity based on it, if only the sport checkbox have been checked it should opens the sport activity, if only the technology checkbox have been chosen then it will open the technology activity, if both checkboxes have been checked then it will open an activity named "sporttecknology".
when run the app, when I choose only the sport checkbox and then press the button, it opens the sport activity. but when I choose only the technology checkbox and then press the button the app stop working, and also that happens when I choose both checkboxes then press the button,
so is there any error in my code?
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
//responseText.append("The following were selected...\n");
ArrayList<Interestclass> interestList = dataAdapter.interestList;
for (int i = 0; i < interestList.size(); i++) {
Interestclass interest = interestList.get(i);
if (interest.isSelected()) {
responseText.append(interest.getName());//"\n" +
}
}
Toast.makeText(getApplicationContext(), responseText, Toast.LENGTH_LONG).show();
String start = responseText.toString();
if (start.equals("Sport")) {
Intent intentSport = new Intent(MainActivity.this, Sport.class);
startActivity(intentSport);
}
else if (start.equals("Technology")) {
Intent intentTechnology = new Intent(MainActivity.this, Technology.class);
startActivity(intentTechnology);
}
else if (start.equals("SportTechnology")) {
Intent intentSportTech = new Intent(MainActivity.this, SportTechnology.class);
startActivity(intentSportTech);
}
else {
Intent intentTechnology = new Intent(MainActivity.this, Technology.class);
startActivity(intentTechnology);
}
/*
switch (start) {
case "Sport":
Intent intentSport = new Intent(MainActivity.this, Sport.class);
startActivity(intentSport);
break;
case "Technology":
Intent intentTechnology = new Intent(MainActivity.this, Technology.class);
startActivity(intentTechnology);
break;
}*/
}
});
}
How do you open a new activity when you click on an item in the menu list?
For example I have a menu item named "Teams" so after I click on that, the "TeamsActivity" should open. I've searched on the internet but that didn't help me.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
???????????????????
case R.id.Teams:
???????????????????
default:
return super.onOptionsItemSelected(item);
}
}
Basically you create an Intent and start it.
You can have a look in the tutorial of the developer-site of android.
In your case it's something like:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = null;
switch (item.getItemId()) {
case R.id.home:
intent = new Intent(this, HomeActivity.class);
break;
case R.id.Teams:
intent = new Intent(this, TeamsActivity.class);
break;
default:
return super.onOptionsItemSelected(item);
}
startActivity(intent);
return true;
}
I've been using ABS 4.0 with two MenuItems in one of my apps, but have discovered a little error: When pressing the second MenuItem, it does exactly the same as the first one...
I've tried just about everything I can think of, but it isn't working. I've altered onOptionItemSelected, as I thought that was the method I need to edit.
EDIT:
I've been looking at #Ollie's suggestions, but neither LogCat nor Debug is showing weird things. Maybe it's in some other part of the code, or a declaration for ABS? Here's the entire code, if you could look through it, that would be great!
The code for the whole Activity, as it's maybe in some other place?
package bas.sie.Antonius;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class TeacherInfo extends SherlockActivity {
String URLhome;
String Info;
String TeacherAb;
TextView mTxtvInfo;
Button mBtnTeacherStSchedule;
Button mBtnTeacherDaySchedule;
private static String mainUrl = "http://www.carmelcollegegouda.nl/site_ant/";
private static String endUrl = ".htm";
private static String[] myUrls = { "roosters/dagroosters/Doc_V1_",
"roosters/standaardroosters/Doc1_" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactinfo);
setTitle("Over deze leraar");
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
mTxtvInfo = (TextView) findViewById(R.id.TxtvTeacher);
Intent startingIntent = getIntent();
Info = startingIntent.getStringExtra("contact");
mTxtvInfo.setText(Info);
Intent startingIntent1 = getIntent();
TeacherAb = startingIntent1.getStringExtra("abbrev");
mBtnTeacherDaySchedule = (Button) findViewById(R.id.btnTeacherDaySchedule);
mBtnTeacherStSchedule = (Button) findViewById(R.id.btnTeacherStSchedule);
mBtnTeacherDaySchedule.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
URLhome = makeUrl(0);
Intent i = new Intent(TeacherInfo.this, MyWebView.class);
i.putExtra("home", URLhome);
startActivityForResult(i, 0);
}
});
mBtnTeacherStSchedule.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
URLhome = makeUrl(1);
Intent i = new Intent(TeacherInfo.this, MyWebView.class);
i.putExtra("home", URLhome);
startActivityForResult(i, 0);
}
});
}
private String makeUrl(int index) {
String s = mainUrl + myUrls[index] + TeacherAb + endUrl;
return s;
}// makeurl
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Instellingen")
.setIcon(R.drawable.ic_settings)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add("Over de app")
.setIcon(R.drawable.ic_about)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, AntoniusActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.settings:
Intent i = new Intent(this, About.class);
startActivity(i);
return true;
case R.id.about:
Intent about = new Intent(this, About.class);
startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
I'm thinking that the problem is in the declaration of the menu items, but I don't see any problem there...
Could you take a look at my menu.xml? Posted here:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/settings"
android:icon="#drawable/ic_settings"
android:title="Instellingen"></item>
<item android:id="#+id/about"
android:icon="#drawable/ic_about"
android:title="Over de app"></item>
</menu>
Create the menu like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
Then use a switch statement to handle selections:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Do stuff
return true;
case R.id.menu_item_2:
// Do stuff
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT: Finally, you should do different things for each item, if you change the Intent target Activity to another, it'll do what you expect:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// ... Stuff ...
case R.id.settings: // Settings item
Intent i = new Intent(this, About.class); // Start About.java Activity, but item says "settings"
// TODO: Change About to Settings?
i = new Intent(this, Settings.class);
startActivity(i);
return true;
case R.id.about: // About item
Intent about = new Intent(this, About.class); // Start About.java Activty
startActivity(about);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What I find odd is the way you create your menu.
You have defined menu layout it in a menu.xml, yet you do not reference this layout in a onCreateOptionMenu() method.
It should be something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
Pay attention to the getSupportMenuInflater() method which is used instead of getMenuInflater(). Why this must be so is somewhere in docemntation about android support library which in term is used by ActionBarSherlock library.
What you do is create menu in code programmatically by using a method menu.add() with a signature add(CharSequence). Nowhere it is in there that you give ItemId. I guess (and this is only a guess) android in that case assigns the same id to all items, something like zero or some other arbitrary number. You should use a method with a signature add(int, int, int,CharSequence) or add(int, int, int, int) as only those allow you to specify ItemId. So, both of your menu items have the same id. And this is (I guess again) the cause that they behave the same.
One more thing. Be careful that you use the correct substitute classes and methods from support library and ActionBarSherlock library.
Please let us know if this solved the problem as I am only running this in my head.
I had a very similar problem before where i wanted to show random xml layouts. I've done that - with lots of help by Ben Williams - with a class named DisplayRandomPage.class and I had a main.xml layout with four buttons.
That's the code of the Main.class:
switch (view.getId()) {
case R.id.first_button:
startActivity(new Intent(this, FirstPage.class));
break;
case R.id.second_button:
startActivity(new Intent(this, SecondPage.class));
break;
case R.id.third_button:
startActivity(new Intent(this, ThirdPage.class));
break;
case R.id.random_button:
Intent intent = new Intent(this, DisplayRandomPage.class);
startActivity(intent);
and this is in the DisplayRandomPage.class:
public class DisplayRandomPage extends Activity {
private Integer [] mLinearLayoutIds = {
R.layout.one
R.layout.two,
R.layout.three
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random random = new java.util.Random();
int rand = random.nextInt(3);
setContentView(mLinearLayoutIds[rand]);
}
}
What i'd like to do is creating a DisplaySpecificPage.class. Above I've shown my main.class with the switch-case-clause. So when i click on the first button, it will start the FirstPage.class, clicking the second, it will start SecondPage.class, and so on. So for each xml-file i have to create a new java-class although the different java-classes do all the same. Only the xml-files are different. So i'd like to put something like this:
pseudo-code:
case R.id.first_button:
startActivity(new Intent(this, DisplaySpecificPage.class)) with R.layout.first_page;
break;
how do i pass the ID from the layout (R.layout.first_page) on?
You should change your switch statement to
final Intent intent = new Intent(this, DisplaySpecificPage.class);
switch (view.getId()) {
case R.id.first_button:
intent.putExtra("mylayout", R.layout.one);
break;
case R.id.second_button:
intent.putExtra("mylayout", R.layout.two);
break;
case R.id.third_button:
intent.putExtra("mylayout", R.layout.three);
break;
case R.id.random_button:
intent.putExtra("mylayout", randomNumber);
startActivity(intent);
}
startActivity(intent);
This way you'd start the same activity no matter which button would be pressed, and inside the DisplaySpecificPage activity's onCreate method you should set the content to this passed layout:
final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
setContentView(layout);
The code above passes an extra parameter to the intent when starting the DisplaySpecificPage activity, with the name: "mylayout".
Inside the DisplaySpecificPage class' onCreate method you just retrieve this extra parameter using the getIntExtra method of the passed intent (getIntent() will return it for you), and you set the content view of this DisplaySpecificPage activity to the passed layout by setContentView(layout).
You should make sure though, to always pass a valid layout identifier to that intent, otherwise you'll get exception when trying to inflate it (so randomNumber should be selected properly).
Update
With adding extras to the Intent you can parametrize your activities.
So using intent.putExtra("paramName", paramValue) you'll pass the paramValue value on the name of paramName to the activity you start by this intent.
You want to start the DisplaySpecificPage activity, but want it to have different layout based on which button you click.
So you create an intent:
final Intent intent = new Intent(this, DisplaySpecificPage.class);
Before starting the activity by calling startActivity(intent), you have to put this extra information inside the intent, so the DisplaySpecificPage activity to know which layout it should set as its content view:
So if you pressed the second button, you want the layout of your new activity to be the one defined by the two.xml inside your res/layout folder. It is referenced by as R.layout.two (which is a static int value).
intent.putExtra("mylayout", R.layout.two);
This line puts the layout's value as an extra into the intent object, and now you can start the activity, the layout reference will be passed to it.
The "mylayout" is a name you choose for your parameter. It can be anything (valid), it will be used inside the DisplaySpecificPage activity to retrieve the layout's reference. It is retrieved by this name:
final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
The getIntExtra method gets the integer value from the intent which has the name "mylayout", and if it's not found, then it will return R.layout.one.
If you want to handle this case (when no parameter with name mylayout is set), you can write
final int layout = getIntent().getIntExtra("mylayout", -1);
if (layout < 0)
{
//TODO: no layout reference was passed
}
Here is my final code:
Main.class:
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void change(final View view) {
Intent intent2 = null;
final Intent intent = new Intent(this, DisplaySpecificPage.class);
switch (view.getId()) {
case R.id.first_button:
intent.putExtra("mylayout", R.layout.layout1);
break;
case R.id.second_button:
intent.putExtra("mylayout", R.layout.layout2);
break;
case R.id.third_button:
intent.putExtra("mylayout", R.layout.layout3);
break;
case R.id.random_button:
intent2 = new Intent(this, DisplayRandomPage.class);
startActivity(intent2);
}
// if the Random button was not clicked, if-condition is true.
if (intent2 == null)
startActivity(intent);
}
}
DisplaySpecificPage.class:
public class DisplaySpecificPage extends Activity{
public void onCreate(Bundle savedInstanceState) {
final int layout = getIntent().getIntExtra("mylayout", R.layout.one);
super.onCreate(savedInstanceState);
setContentView(layout);
}
}
DisplayRandomPage.class:
public class DisplayRandomPage extends Activity {
private Integer [] mLinearLayoutIds = {
R.layout.layout1,R.layout.layout2,R.layout.layout3
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random random = new java.util.Random();
int rand = random.nextInt(mLinearLayoutIds.length);
setContentView(mLinearLayoutIds[rand]);
}
}
Big thanks to rekaszeru!