How can I refer to onListItemClick within onDialogPositiveClick? So the page does not open before/while the dialog box opens..Can I literally put onListItemClick in onDialogPositiveClick, or do I have to do something completely different? Here is my code...thanks for all/any help!
public class MainActivity extends ListActivity implements TheDialog.NoticeDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
String[] sites = {"Google", "Amazon", "Ebay" , "Reddit", "SmashingMag", "CCC"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylist_item, R.id.textView1, sites);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
DialogFragment newFragment = new TheDialog();
newFragment.show(getFragmentManager(), "Confirm");
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}
}
#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 void onDialogPositiveClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
}
What you can do is not fire intent on list item click and move the switch code to
onDialogPositiveClick(DialogFragment dialog){
Intent i = null;
switch(position){
case 0:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(i); break;
case 1:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
startActivity(i); break;
case 2:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
startActivity(i); break;
case 3:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
startActivity(i); break;
case 4:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
startActivity(i); break;
case 5:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
startActivity(i); break;
}}
This way user will only go to the next page when positive button is clicked, hope you want this kind of behavior.
Related
Have problem after search in searcview last or middle activity in listview.
public class MainActivity extends AppCompatActivity {
ListView listView;
SearchView searchView;
ArrayList<String> items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview);
items = new ArrayList<>();
items.add("st17");
items.add("st18");
items.add("st23");
items.add("st29");
items.add("st33");
items.add("st34");
items.add("st35");
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ArrayAdapter<String> adapter = new ArryAdapter<>(this,android.R.layout.simple_list_item_1,
android.R.id.text1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener((adapterView, view, position, l) -> {
Toast.makeText(MainActivity.this, "click -" + adapterView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
switch (position) {
case 0:
Intent newActivity = new Intent(MainActivity.this, Activity_st17.class);
startActivity(newActivity);
break;
case 1:
Intent newActivity1 = new Intent(MainActivity.this, Activity_st18.class);
startActivity(newActivity1);
break;
case 2:
Intent newActivity2 = new Intent(MainActivity.this, Activity_st23.class);
startActivity(newActivity2);
break;
case 3:
Intent newActivity3 = new Intent(MainActivity.this, Activity_st29.class);
startActivity(newActivity3);
break;
case 4:
Intent newActivity4 = new Intent(MainActivity.this, Activity_st31.class);
startActivity(newActivity4);
break;
case 5:
Intent newActivity5 = new Intent(MainActivity.this, Activity_st32.class);
startActivity(newActivity5);
break;
case 6:
Intent newActivity6 = new Intent(MainActivity.this, Activity_st33.class);
startActivity(newActivity6);
break;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
ArrayList<String> templist = new ArrayList<>();
for (String temp : items) {
if (temp.toLowerCase().contains(newText.toLowerCase())) {
templist.add(temp);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, templist);
listView.setAdapter(adapter);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
});
}
}
I have list:
st17
st18
st23
st29
st33
st34
st35
For example, I'm looking for st33 in the search, then it shows st33 it's good, but when I click it, opens st17.
But this shows that I click on st33 but still, after pressing, it still opens st17
Toast.makeText(MainActivity.this, "click -" + adapterView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
Help me please.
I'm new in Android programming. I'm working on an application that have multiple activities. I've created a custom menu with ListView. I would like to put this menu in a base activity to be available in all activities. How should I do this?
Till now, I have something like this:
This is for the button to toggle the menu
menuToggelIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Hide layouts if VISIBLE
if(menuLayout.getVisibility() == View.VISIBLE)
{
menuLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
menuLayout.setVisibility(View.VISIBLE);
}
}
});
And this is for the menu
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = menuArray[position];
Context context = getApplicationContext();
switch (name) {
case "CASE1":
Intent case1Intent = new Intent(context, Activity1.class);
startActivity(case1Intent);
break;
case "CASE2":
Intent case2Intent = new Intent(context, Activity2.class);
startActivity(case2Intent);
break;
case "CASE3":
Intent case3Intent = new Intent(context, Activity3.class);
startActivity(case3Intent);
break;
case "CASE4":
Intent case4Intent = new Intent(context, Activity4.class);
startActivity(case4Intent);
break;
case "CASE5":
Intent case5Intent = new Intent(context, Activity5.class);
startActivity(case5Intent);
break;
case "CASE6":
Intent case6Intent = new Intent(context, Activity6.class);
startActivity(case6Intent);
break;
case "CASE7":
Intent case7Intent = new Intent(context, Activity7.class);
startActivity(case7Intent);
break;
default:
break;
}
}
});
Android custom menu
make one BaseActivity class and all activity extends by BasyActivity class.
BaseActivity class define your main things that show all the screen like menu and other thing. for example
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manu_file_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
Toast.makeText(getApplicationContext(), "Hello World", 0).show();
}
return super.onOptionsItemSelected(item);
}
}
and this activity extends all other activity.
I have encountered a problem. When I'm at this page (the main menu) and I try to click the calculate button it will go back to the login page. However, the calculate and records page are all also opened, and can be accessed by clicking the back button.
Button Calculate;
Button Records;
Button Logout;
Calculate = (Button) findViewById(R.id.buttonCalculate);
Records =(Button) findViewById(R.id.buttonRecords);
Logout = (Button) findViewById(R.id.buttonLogout);
Calculate.setOnClickListener(this);
Records.setOnClickListener(this);
Logout.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
public void onClick(View view)
{
Intent i = new Intent(this,Calculate.class);
startActivity(i);
Intent f = new Intent(this,Records.class);
startActivity(f);
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
}
}
Try this way:
public void onClick(View v)
{
switch(v.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
default:
break;
}
}
and make sure you have to added all the Activities in your manifest.xml file
try this..
public void onClick(View view)
{
Switch(view.getId()){
case R.id.buttonCalculate;
Intent i = new Intent(this,Calculate.class);
startActivity(i);
break;
case R.id.buttonRecords;
Intent f = new Intent(this,Records.class);
startActivity(f);
break;
case R.id.buttonLogout;
Intent g = new Intent(this,MainActivity.class);
startActivity(g);
break;
}
}
So, I'm using jfeinstein10 library for my SlidingMenu. It works fine but I'm having a problem to toggle the menu when the user taps in one of the menu options.
I have the MainActivty where the slidingmenu is and a fragment called SampleListFragment where I set the menu options.
What I'm trying to do is call a function from the MainActivity when I click the option. This function should only toggle the menu, but instead I get a NullPointException error.
My MainActivity
public class MainActivity extends BaseActivity implements SlidingActivityBase {
private SlidingMenu menu;
private ImageButton btn_pesquisa;
private ImageButton btn_toggle;
private MakeMateria makeMat = new MakeMateria();
private static final int SCREEN_ORIENTATION_PORTRAIT = 1;
String id_test;
SampleListFragment listFragment = new SampleListFragment();
public MainActivity() {
super(R.string.title_bar_content);
}
public void mainToggle() {
Log.d("1", "" + this);
toggle();
Log.d("2", "" + this);
}
public static Intent newInstance(Activity activity, int pos) {
Intent intent = new Intent(activity, MainActivity.class);
intent.putExtra("pos", pos);
return intent;
}
public void testeEvent(){
Log.d("Funciona","works");
toggle();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
R.drawable.titlebar);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setBackgroundDrawable(bg);
BitmapDrawable bgSplit = (BitmapDrawable) getResources()
.getDrawable(R.drawable.titlebar);
bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
}
int pos = 0;
if (getIntent().getExtras() != null) {
pos = getIntent().getExtras().getInt("pos");
}
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
menu = new SlidingMenu(this);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindScrollScale((float) 1.0);
menu.setMenu(R.layout.menu_frame);
// set the Above View
setContentView(R.layout.content_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, new MainFragment()).commit();
setSlidingActionBarEnabled(true);
btn_pesquisa = (ImageButton) findViewById(R.id.btnPesquisa);
btn_toggle = (ImageButton) findViewById(R.id.btn_menu);
btn_toggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
toggle();
}
});
btn_pesquisa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
SearchActivity.class);
//startActivity(intent);
overridePendingTransition(R.anim.view_transition_in_left,
R.anim.view_transition_out_left);
}
});
}
public void getMenu(){
menu.toggle();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
This part is from my fragment:
#Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
switch (position) {
case 0:
Log.d("1", "1");
getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
mainActivity.getMenu();
break;
case 1:
Log.d("2", "2");
toggle();
break;
case 2:
Log.d("3", "3");
toggle();
break;
case 3:
Log.d("4", "4");
toggle();
break;
case 4:
Log.d("5", "5");
toggle();
break;
case 5:
Log.d("6", "6");
toggle();
break;
case 6:
Log.d("7", "7");
toggle();
break;
case 7:
Log.d("8", "8");
toggle();
break;
case 8:
Log.d("9", "9");
toggle();
break;
}
if (newContent != null)
switchFragment(newContent);
}
MainActivity mainActivity is global, and the insance of it I did in the onCreateView.
The NPE points to the lines where I call the function and where I call the toggle inside the function.
Many thanks.
I did this -> Android : Accessing container activity object from fragment using putExtra?
The problem was that I passing a null object, but when I did like this ->
((MainActivity) this.getActivity()).getMenu()
I was able to get the correct value from the object.
#Wenger thanks for the help.
I've got spinner navigation on my action bar and have a navigation listener for this.
When the activity is created the listener (below) picks up on the default spinner item which means case 0 is run on creation, opening another activity.
How do I stop it registering a navigation change when the activity is created?
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
Intent i = new Intent(Main.this, Example.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
Update:
Think I solved it with this, I declared a boolean flag, changed it to false oncreate.
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
if(flag == true) {
if(Main.class == Main.class) {
} else {
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
}
if(flag == false) {
flag = true;
}
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
There may be a simpler way but you could put a boolean flag in onCreate() to false and check for that in your listener then set it to true after the first time when it sets up so it won't run the Intent code when you first run it. Depending on what you need, you may want to put it in onResume() so it doesn't run if you back into this Activity