I am trying to make an app in which there will be a spinner with several values. A button will print that value in a text holder.
But, as you can see in the code bellow, the program cannot resolve the symbols "Teste1" and "Teste2".
Spinner merendas;
String[] morfes = {"Teste1","Teste2"};
ArrayAdapter <String> adapter;
merendas = (Spinner)findViewById(R.id.merendas);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,morfes);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
merendas.setAdapter(adapter);
merendas.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long morfes) {
switch (position){
case 0:
morfes = Teste1;
break;
case 1:
morfes = Teste2;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
Thanks for your time.
A button will print that value in a text holder.
If you want to print which spinner item is selected , You don't need setOnItemSelectedListener() for your spinner.
You can simply use
String selected = spinner.getSelectedItem().toString();
yourtextview.setText(selected);
Related
I am really new to android and I am really frustrated with it. I want to switch between fragments in drawer menu. However, the screen stays the same. In the drawer it says that I am on a particular fragment but I see no change. I set a different background to differentiate between them.
I believe that fragment manager is deprecated based on what people are saying but I don't know how to change it with. Also the thin is I don't know which tutorials are out of date because I have very little knowledge about android development.
Fragment code:
public class ToolsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tools, container,false);
}
}
Main activity code:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new CharacterFragment());
navigationView.setCheckedItem(R.id.nav_characters);
}
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
} else{
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_characters:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new CharacterFragment());
break;
case R.id.nav_episodes:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new EpisodesFragment());
break;
case R.id.nav_tools:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new EpisodesFragment());
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
You forgot to call commit()
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new CharacterFragment()).commit();
This is the code. Please help me. If I click any item on NavigationView then the drawer was closing.
The app is running well but this one function is not working. When I debug this app then I saw navView.setNavigationItemSelectedListener is not called. If I click any item menu then close the drawer. How can I fix this issue? Please help me
private void initView() {
mToolbar = findViewById(R.id.dash_toolbar);
drawer = findViewById(R.id.drawer);
navView = findViewById(R.id.navigation_view);
bottomNavigationView = findViewById(R.id.bottom_navigation);
}
private void actionBar() {
setSupportActionBar(mToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.app_name);
}
}
private void drawerMenu() {
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
AppBarConfiguration mAppBarConfiguration = new AppBarConfiguration.Builder
(R.id.nav_home, R.id.nav_share)
.setDrawerLayout(drawer)
.build();
navView.setNavigationItemSelectedListener
(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
int id = menuItem.getItemId();
if (id == R.id.nav_home) {
Toast.makeText(DashboardActivity.this, "Home ", Toast.LENGTH_SHORT).show();
}
if (id == R.id.nav_share) {
Toast.makeText(DashboardActivity.this, "Share message", Toast.LENGTH_SHORT).show();
}
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
I have a custom list item XML with 2 Buttons and I want to get when one is clicked (and a way of knowing which one). My ListView is made with data from a database. This is what I use for populating the list:
private DBManager manager;
private Cursor cursor;
private ListView lista;
SimpleCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.m_home_layout);
manager = new DBManager(this);
lista = (ListView) findViewById(R.id.listview);
String[] from = new String[]{manager.CN_NAME, manager.CN_DESCRIPTION, manager.CN_TIME, manager.CN_DAY};
int[] to = new int[] {R.id.list_title, R.id.list_description, R.id.list_time, R.id.list_day};
cursor = manager.cargarCursorData("data");
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to,0);
lista.setAdapter(adapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
I've seen people saying that you have to use bindView but I don't have anything like that and I don't know what it is. Sorry for my ignorance.
You can go through this tutorials for Creating Custom Adapter, in which you can get the which Button is clicked:
http://www.survivingwithandroid.com/2012/10/android-listview-custom-adapter-and.html
http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//identify object
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
**title = (TextView)findViewById(R.id.theTitle);
Intent intent = getIntent();
String Title = intent.getExtras().getString("name");
title.setText(Title);**
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
Intent intent = new Intent(this, addCounter.class);
startActivity(intent);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
When nav_gallery = true, it brings me to activity2 which has the following code:
public void addOk(){
String sendName = name.getText().toString();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("name", sendName);
startActivity(intent);
}
I want to send the information from activity2 back to the main activity. Is my getIntent() in the wrong place?
Your getIntent() method is inside the onCreate method.
But onCreate is called only once, at creation. And if the MainActivity is still in the stack, there is no reason why onCreate() would be called again.
You should try to insert your code inside onResume() or onStart().
So I'm trying to add some content to my navigation drawer dynamically. In principle, it works. I can add stuff in my app, and I can make it show up in the navigation drawer. Only problem is, I have to restart the app for the new stuff in the navigation drawer to popup (because I can only get the code working in the onCreate method), which is not the desired behaviour.
This is what I've got in the onCreate method at the moment:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
MenuItem it1 = menu.add(R.id.group1, 1, 2, "TEST DYNAMIC");
Very basic, but it does what I want. It adds a "test" header to the menu.
What I want, is to be able to access this bit of code at will, so for example when I press a button. I haven't been able to do this myself unfortunately. It doesn't matter if it's in a method in the MainActivity, or in one of my other fragments, but it shouldn't only be run when the MainActivity is created, that's not enough. Also, I probably need a Navigation Drawer re-draw after adding something to it (I'm guessing), but I don't know how to do that either, so any help on that would be great.
I tried the following code
Here button is the variable if type Button and i added onClickListener on it.
EDIT :: In Activity do like this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Menu menu = navigationView.getMenu();
SubMenu topChannelMenu = menu.addSubMenu("Category");
topChannelMenu.add("Menu Name");
MenuItem mi = menu.getItem(menu.size()-1);
mi.setTitle(mi.getTitle());
}
});
EDIT :: In Fragment do like this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Menu menu = navigationView.getMenu();
SubMenu topChannelMenu = menu.addSubMenu("Category");
topChannelMenu.add("Menu Name");
MenuItem mi = menu.getItem(menu.size()-1);
mi.setTitle(mi.getTitle());
}
});
Its working...
This adds menu to the end of previous menu.