When click button in menu it opens xml file - eclipse

i am trying to make a menu in my app. when i click the menu button i made called aboutUs it is supposed to open an XML file that explains what this app is about. Except when i run the app and click on the menu button the app just force closes. heres my mainactivity.java
package com.JordanZimmittiDevelopers.BlazeCustomerService1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.JordanZimmittiDevelopers.BlazeCustomerService.R;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mail = (Button)findViewById(R.id.button1);
mail.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"jordanzimmitti#gmail.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "I Have A Question Or Probelm:");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "My question or problem is:");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Click Your Defult E-mail To Send Your Message:"));
finish();
break;
}
}
Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.aboutUs:
Intent i = new Intent("com.JordanZimmittiDevelopers.BlazeCustomerService.AboutThisApp");
startActivity(i);
}
return false;
}
}

Make an activity that explain the application then override onCreateOptionsMenu() like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("Help").setIcon(R.drawable.HelpButton).setIntent(new Intent(this,HelpActivity.class));
return super.onCreateOptionsMenu(menu);
}

Related

Android Studio says "Expression expected"

Android Studio tells there is an error in Java file, in string summonButton2(); Android Studio says "Expression expected".
I want the method summonButton2 to be launched automatically. I undesrtand that I'm doing it wrong. What exactly and is there any other way to start a method, except adding it to onCreate method? Thanks in advance.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int numberOfLinesLeft = 3;
Button secondaryActivityAddButton;
LinearLayout llForSecondaryButton;
LinearLayout llForSecondaryEditText;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
summonButton2();
}
public void summonButton2(View view){
llForSecondaryButton = findViewById(R.id.secondaryButton);
secondaryActivityAddButton = new Button(this);
secondaryActivityAddButton.setText("" + numberOfLinesLeft);
llForSecondaryButton.addView(secondaryActivityAddButton);
secondaryActivityAddButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
plusTextField();
if(numberOfLinesLeft == 0) {
view.setVisibility(View.GONE);
}
}
});
}
public void plusTextField() {
llForSecondaryEditText = findViewById(R.id.linearLayout1);
// add edittext
et = new EditText(this);
et.setText("text" + numberOfLinesLeft );
llForSecondaryEditText.addView(et);
numberOfLinesLeft--;
secondaryActivityAddButton.setText("" + numberOfLinesLeft);
}
}
summonButton2(new View(this));
Helped in my case.

How to save the current state of switches in Android Studio?

I know very little about programming. I downloaded Android Studio and started tinkering with it. I tried to make the app that they put on the tutorial and it worked. However I tried to add more functionality to it and I've failed so far. Excuse me if you see unnecessary junk on my code, I'm just kinda trying everything at first and I do feel a little misguided.
Anyways, onto the question. I have a Switch (id:toggle_text) with an OnClick action (change_font). When the switch is toggled it should change the font size of a different activity through intent1. Currently not only does it not send the font size variable (the variable keeps the default value you put on getIntExtra), but now that I tried to add the ability to save the current state it just shows errors. Here's the code:
package com.example.myfirstapp;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
import android.widget.TextView;
import static com.example.myfirstapp.R.id.toggle_text;
import static com.example.myfirstapp.R.string.change_font;
public class ShowAnOption extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_an_option);
SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyz", MODE_PRIVATE);
toggle.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
}
public void change_font(View v) {
int fssize;
if (toggle.isChecked())
{
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.commit();
fssize=20;
}
else
{
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", false);
editor.commit();
fssize=40;
}
Intent intent1 = new Intent (getBaseContext(), DisplayMessageActivity.class);
intent1.putExtra("Font_Size", fssize);
}
}
It says "cannot resolve symbol toggle" on toggle.setChecked() and the if statement. What can I do to fix this? Also, why does it not get sent to the other activity? Here's the code on the other activity:
package com.example.myfirstapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
Intent intent1 = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
int Font_Size = intent1.getIntExtra("Font_Size",50);
TextView textView = new TextView(this);
textView.setTextSize(Font_Size);
textView.setText(message);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
}
}
Thanks and sorry for the long read. If there's anything else that needs to be known let me know and I'll gladly show.
We did not try to change the font size but here is how to use the switch widget.
Our design is two activities MainActivity and SwitchActivity we changed a CheckBox from unchecked to checked The switch is on the MainActivity code below
setOnCheckedChangeListener();
private void setOnCheckedChangeListener() {
swAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "Switch On", Toast.LENGTH_SHORT).show();
Intent intentSP = new Intent(MainActivity.this, SwitchActivity.class );
Bundle extras = new Bundle();
extras.putString("FONT","true" );
intentSP.putExtras(extras);
startActivity( intentSP );
} else {
Toast.makeText(MainActivity.this, "Switch Off", Toast.LENGTH_SHORT).show();
}
}
});
}
Now in the SwitchAcvity we capture the value from the intent and fire the method
doWhat()
Intent intentSP = getIntent();
Bundle bundle = intentSP.getExtras();
tORf = bundle.getString("FONT");
doWhat(null);
And here is the doWhat method
public void doWhat(View view){
if(tORf.equals("true")){
chkBoxOne.setChecked(true);
}else {
Toast.makeText( SwitchActivity.this, "NOT TRUE", Toast.LENGTH_LONG ).show();
}
}

ActionBarActivity with fragment and Tab Bar how to implement it

As you see my Code, it dont have error when run debug, I put the tabhost into my fragment, but when app run. It dont show the tabhost
package com.example.phamxuanson.myapplication;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
mTitle = getTitle();
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
Please see my piuctures
i.imgur.com/AuPA4GO.png
i.imgur.com/Vlrxuhz.png
i.imgur.com/or8m6Mj.png
Why wasn’t tabhost shown?

Android Check for Wireless connection upon Returning to Activity from Settings

My Android application is mainly a Webview (labeles WebViewActivity) that navigates a mobile website. I have a connection detector that opens another activity on every URL load if there is no wireless connection. From this next activity there is a button that opens up the Wireless Settings. Upon pressing the back button I would like for the second activity (labeled MainActivity) to refresh itself and continue back to whichever page my Webview had loaded. What do I need to change in the MainActivity.java to execute that?
My MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
protected void onRestart(){
if(checkConnection()){
Intent intent= new Intent(this, WebViewActivity.class);
startActivity(intent);
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
super.onBackPressed();
}
public void openSettings(View view){
Intent intent= new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
public boolean checkConnection(){
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
//Get Internet Status
isInternetPresent = cd.isConnectingToInternet();
if(!isInternetPresent)
return false;
return true;
}
}
My WebviewActivity:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView mWebView;
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
if (checkConnection()) {
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
mWebView.loadUrl("http://my.fellowshipnwa.org/?publicapp");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new myWebClient());
} else {
openSplash();
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
// Let the system handle the back button
new AlertDialog.Builder(this)
.setTitle("Exit myFellowship App?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
WebViewActivity.super.onBackPressed();
}
}).create().show();
}
}
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(!checkConnection()){
openSplash();
return true;
}else{
if( url.startsWith("tel:")){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
else if( url.startsWith("mailto:")){
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
startActivity(intent);
return true;
}
}
return false;
}
}
public void openSplash(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public boolean checkConnection(){
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
//Get Internet Status
isInternetPresent = cd.isConnectingToInternet();
if(!isInternetPresent)
return false;
return true;
}
}
Any help is Greatly Appreciated! Even if it means telling me I goofed up on creating this question.
Thank you friends.
I played with it for a while and used a few other examples to piece together something that is working. One small hitch is if the user turns on a connection, but it is not quite connected when they return to the app it will stay on the ConnectorActivity. So I intercept the back button to check for a connection. If present, it will finish the current activity. Then, upon returning to the webview it will reload.
ConnectorActivity.java:
#Override
protected void onRestart(){
super.onRestart();
if(checkConnection()){
finish();
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(checkConnection()){
super.onBackPressed();
}
}
Piece from WebViewActivity.java:
#Override
protected void onRestart(){
super.onRestart();
mWebView.reload();
}

Why isn't the TextView working?

I'm coding an android program that arranges three words in alphabetical order using two simple activities.But in the second(output) activity,either the TextView or the Intents aren't working.The output isn't getting displayed.The java code for first(input) activity is as follows:
`package com.example.names;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Input extends Activity {
EditText t1,t2,t3;
final Context context=this;
String n[]=new String[100];
String t[]=new String[100];
String a1,a2,a3;
Button alpha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
n[0]=t1.getText().toString();
n[1]=t2.getText().toString();
n[2]=t3.getText().toString();
for(int j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(n[j].compareToIgnoreCase(n[k])>0)
{
String temp=n[j];
n[j]=n[k];
n[k]=temp;
}
}
}
a1=n[0];
a2=n[1];
a3=n[2];
alpha=(Button)findViewById(R.id.button1);
alpha.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(Input.this,Output.class);
i.putExtra("x", a1+a2+a3);
Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show();
startActivity(i);
}
});}}`
And the java code for second activity is as follows:
package com.example.names;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class Output extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView p1=new TextView(this);
Bundle extras=getIntent().getExtras();
if (extras!=null){
String ans=extras.getString("x");
p1.setText(ans);
p1.setTextSize(50);
Toast.makeText(Output.this, "All lines have been executed",Toast.LENGTH_LONG).show();
setContentView(p1);
}};}
Why isn't the output getting displayed then???
The application is not crashing also.
Change your code with this below code.
Get edittext value when button is clicked.
Your Input Activity looks like this.
public class Input extends Activity {
EditText t1,t2,t3;
final Context context=this;
String n[]=new String[100];
String t[]=new String[100];
String a1,a2,a3;
Button alpha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
alpha=(Button)findViewById(R.id.button1);
alpha.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
n[0]=t1.getText().toString();
n[1]=t2.getText().toString();
n[2]=t3.getText().toString();
for(int j=0;j<2;j++)
{
for(int k=j+1;k<3;k++)
{
if(n[j].compareToIgnoreCase(n[k])>0)
{
String temp=n[j];
n[j]=n[k];
n[k]=temp;
}
}
}
a1=n[0];
a2=n[1];
a3=n[2];
Intent i=new Intent(Input.this,Output.class);
i.putExtra("x", a1+a2+a3);
Toast.makeText(Input.this, "Method OnClick has been invoked",Toast.LENGTH_SHORT).show();
startActivity(i);
}
});}}
Your Output Activity is fine it is working no changes in that activity.