So i have a problem with my local variable it says Unreachable statement.
This is the line of code i get the error (" View textViewOptions; ") i cant even run the app
private View findViewById(int position) {
View view = findViewById(R.id.textViewOptions);
return view;
View textViewOptions;
textViewOptions.setOnClickListener(new View.OnClickListener() {
ViewHolder holder;
public void onClick(View view) {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.textViewOptions);
//inflating menu from xml resource
popup.inflate(R.menu.chatmenu);
//adding click listener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.BlockUser:
//handle menu1 click
return true;
case R.id.MuteNotificationsCH:
//handle menu2 click
return true;
default:
return false;
}
}
});
popup.show();
}
});
}
As mentioned in commnets, return keyword is used to exit the method. From there onwards execution will stop. So the code below return statement will not be executed and hence will become unreachable.
So put return view; as the last statement of the method
Related
First of all, I have two navigation view for both sides in android as shown in this Image. Left side for user who signup as parents meanwhile the right side for the user who signup as a tuition provider. For example, if User A signs up as a parent, so he/she can only open the left side navigation ONLY. I have made some studies, mostly using radio button/radio group. Unfortunately, I have to use the spinner, what coding should I write to ensure the navigation view can be opened for certain users. For your information, I have 3 activity.
1) User.Java wherein coding, I have coding initialize, constructor and getter.
2) RegistrationActivity.Java where I put all the spinner coding.
//USER TYPE SPINNER
List<String> categories = new ArrayList<>();
categories.add(0, "Choose Category");
categories.add("Parents");
categories.add("Tuition Provider");
ArrayAdapter<String> dataAdapter;
dataAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
RegisterUserType.setAdapter(dataAdapter);
RegisterUserType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(position==0)
{
//Toast.makeText(getApplicationContext(),"No Item Selected",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position) +" Selected",Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
3) Home.Java where I put all my navigation view coding.
menuLeft = (ImageButton) findViewById(R.id.menu_left);
menuRight = (ImageButton) findViewById(R.id.menu_right);
parentsNavigation = findViewById(R.id.nav_view);
tuitionProviderNavigation = findViewById(R.id.nav_view2);
menuLeft.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (drawerLayout.isDrawerOpen(GravityCompat.START))
{
drawerLayout.closeDrawer(GravityCompat.START);
}
else
{
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
menuRight.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (drawerLayout.isDrawerOpen(GravityCompat.END))
{
drawerLayout.closeDrawer(GravityCompat.END);
}
else
{
drawerLayout.openDrawer(GravityCompat.END);
}
}
});
parentsNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item)
{
UserMenuSelector(item);
return false;
}
});
tuitionProviderNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item)
{
UserMenuSelector(item);
return false;
}
});
I can not paste all my coding here because it is too long but I can email you to better understand my XML layout.
All you have to do is lock the drawer that you want to Disable and unlock again if you want to Enable it
use drawerLayout.setDrawerLockMode();
in your case below code will close your one drawer that have gravity start
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.START);
and when you want to unlock again then simply
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START);
Remember one more thing
drawerLayout.setDrawerLockMode() allows the application to restrict the user's ability to open or close the given drawer (cannot open via sliding) but DrawerLayout will still respond to calls to openDrawer(), closeDrawer()
so in your case you also consider disabling the ImageView click because they have these methods
In Your Case
when user will click on a spinner item you check if(position==1)
if this condition true you know it's a parent
so do this in your Home.Java
do this
if(parent==true){
//this will disable right drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);
//and this will enable left drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START);
//and also do to disable right ImageView
menuRight.setEnabled(false);
//and also do to enable left ImageView
menuLeft.setEnabled(true);}
now for tuition provider do same but this time enable right and disable left,
also same for menu button.
So, I have this class project : Class Project
and I want to set all text and radio button selection to be empty after I click the button "Tambah" (it mean add).
I can do it with the text field and text area using this method :
private void kosongkanText(){
txtIdPelanggan.setText("");
txtNamaPelanggan.setText("");
Talamat.setText("");
txtNotlp.setText("");
but how to do it to radio button?. already try this, and not working :
groupJenisKelamin.setSelected(null, rootPaneCheckingEnabled);
I dont really get what is your question, but if you want to uncheck the radio button that already checked you can use setChecked(false)
for example :
private boolean flagpria = false;
private boolean flagwanita = false;
rdbtnMale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (rdbtnMale.isChecked()) {
if (!flagmale) {
rdbtnPria.setChecked(true);
rdbtnWanita.setChecked(false);
flagpria = true;
flagwanita = false;
} else {
flagpria = false;
rdbtnPria.setChecked(false);
rdbtnWanita.setChecked(false);
}
}
}
});
rdbtnFemale.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (rdbtnFemale.isChecked()) {
if (!flagfemale) {
rdbtnWanita.setChecked(true);
rdbtnPria.setChecked(false);
flagwanita = true;
flagpria = false;
} else {
flagwanita = false;
rdbtnWanita.setChecked(false);
rdbtnPria.setChecked(false);
}
}
}
});
those code above mean you have 2 radio button if you check "pria" in "wanita" this code will uncheck it
How to change text button when click in adapter
I try it's not work
public void setQuestData(final ViewHoder viewHoder, final int position) {
viewHoder.btn_select_qq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!profileFeedListModelTwos.get(position).getStatus_select().equalsIgnoreCase("SELECTED")){
viewHoder.btn_select_qq.setText("Accepted");
notifyDataSetChanged();
}
notifyDataSetChanged();
}
});
How to fix it ? and where is my problem?
Do not handle the click event inside the adapter class , instead handle it in the fragment using BaseRecyclerViewAdapter.OnRecyclerViewInteractionListener() or adapter listener of the adapter that you are using.
Set position as a tag inside your adapter then use it get the item in your fragment.
Make your adapter extend BaseRecyclerViewAdapter or adapter that you are using
Basic Idea
adapter = new Adapter(enter code here for setting click listener)
Inside YourAdapter just set the clickListener to your view
viewHoder.btn_select_qq.setOnClickListener(this)
Inside your fragment handle the action on click
YourAdapter adapter = new YourAdapter(getActivity(),new BaseRecyclerViewAdapter.OnRecyclerViewInteractionListener() {
#Override
public void onClick(View view) {
int position = (int) view.getTag();
ItemObject item =adapter .getItem(position);
switch (view.getId()) {
case R.id.view1:
//TODO write logic here
break;
case R.id.view2:
//TODO write logic here
break;
case R.id.view3:
//TODO write logic here
break;
}
}
});
I've just started out in Java programming and am having a bit of trouble implementing an OnClickListener switch case for my clickable TextViews. I've managed to make a switch case for menu items, but i'm obviously not understanding it enough to make a more general case.
Here's the bits of my code that are important to it
public class MyActivity extends Activity implements SensorEventListener {
TextView tv, tv1, tv2, tv3;
#Override
public void onCreate(Bundle savedInstanceState) {
//get textviews
tv = (TextView) findViewById(R.id.xval);
tv1 = (TextView) findViewById(R.id.yval);
tv2 = (TextView) findViewById(R.id.zval);
tv3 = (TextView) findViewById(R.id.scalar);
And then I setup individual on click listeners for each TextView, e.g.
tv1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do things
}
}
});
But i'm trying to set it up so i have a a combined OnClickListener, like:
#Override
public boolean onClickListener (View v) {
switch (tv.findViewById()) {
case tv:
//Do things
return true;
case tv1:
//Do things
return true;
case tv2:
//Do things
return true;
case tv3:
//Do things
return true;
}}
I'm aware that code is very wrong, but i can't seem to wrap my head around it. I've already assigned my findViewById so i'm not sure what else to put into the switch!
Thankyou!
I'll provide an alternative answer. First you have to create an OnClickListener, which will receive your OnClick events:
OnClickListener listener = new OnClickListener()
{
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.xval:
//code
break;
case R.id.yval:
//code
break;
case R.id.zval:
//code
break;
case R.id.scalar:
//code
break;
default:
break;
}
}
};
Then, you have to associate that listener to every TextView you have:
tv.setOnClickListener(listener);
tv1.setOnClickListener(listener);
tv2.setOnClickListener(listener);
tv3.setOnClickListener(listener);
Once you click one of the TextViews, your OnclickListener onClick() callback will be called and it will check the TextView id you have clicked and run the code accordingly, dependeing on the case.
tv.setOnClickListener(this);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
#Override
public boolean onClick (View v) {
switch (v.getId()) {
case R.id.xval:
//Do things
return true;
case R.id.yval:
//Do things
return true;
case R.id.zval:
//Do things
return true;
case R.id.scalar:
//Do things
return true;
}}
Create one listener, add it to all TextView. Switch on the view id, which is a simple int
View.OnClickListener listener = new View.OnClickListener()
{
public void onClick(View v)
{
switch (v.getId()) {
case R.id.xval:
//Do things
return true;
case R.id.yval:
//Do things
return true;
case R.id.zval:
//Do things
return true;
case R.id.scalar:
//Do things
return true;
}
}
};
tv.setOnClickListener(listener);
tv1.setOnClickListener(listener);
tv2.setOnClickListener(listener);
tv3.setOnClickListener(listener);
Below is the code for my submenu buttons and I'm trying to make it delete the note and return to the main list view. The delete option is called "Red" for now.
I copied my delete code from my main activity thinking it would work, but it does not. I'm very new to android coding, so help would be appreciated.
This is how I delete in my Main Activity.java
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
currentNoteId = (int)info.id;
menu.add(0, MENU_DELETE_ID, 0, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == MENU_DELETE_ID) {
Noteitem note = notesList.get(currentNoteId);
datasource.remove(note);
refreshDisplay();
}
return super.onContextItemSelected(item);
}
Here is my code for my NoteEditorActivity.java
Again I'm trying to delete, but I can't seem to figure out how to delete the note from the submenu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_exit:
EditText et = (EditText)findViewById(R.id.noteText);
if (et.length() > 0) {
saveAndFinish();
}
else
{
finish();
}
case R.id.menu_red:
currentNoteId = (int) MENU_DELETE_ID;
datasource.remove(note);
return true;
default:
return super.onOptionsItemSelected(item);
}
Put break statements in your switch case
switch (item.getItemId())
{
case R.id.action_exit:
EditText et = (EditText)findViewById(R.id.noteText);
if (et.length() > 0){
saveAndFinish();
}else{
finish();
}
//you are missing this!!!
break;
case R.id.menu_red:
datasource.remove(note);
finish();
break;
default:
return super.onOptionsItemSelected(item);
break;
}
Try reading this here also: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
its just weird..you aren't calling notesList.add() method anywhere in your code, so I just think it's empty at all..
you are certainly missing the break statement there, but I guess it is not the issue why your note is not deleted after clicking in the menu item. You are saving your note in the "previous" (in terms of backstack) activity? if so, you might try to just alter the return code for the setActivityResult() call (or add some extras to the intent) and then check for it in your onActivityResult() callback..
because right now everytime you close the activity via back key, the notes is saved (the saveAndFinish() method gets called);
please describe better where you actually do save notes (to DB or so) and where you wanna delete them..I could then provide you with some code snippet probably.