I would like to add a toggle switch to my action bar. I have seen several guide on this site on how to do so however non seem to make the view appear(No toggle switch appears in the action bar. It is ass if I did not add it). What am I doing wrong.
menu xml
<menu xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4" tools:context=".MainActivity">
<item
android:id="@+id/switchId"
android:title=""
app:showAsAction="always"
android:actionLayout="@layout/unit_toggle"
/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
SwitchLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v7.widget.SwitchCompat
android:id="@+id/switchAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Main Activity
package com.json.example.jsonstart;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends ActionBarActivity {
static String weatherUrl = "http://ift.tt/1DhX3gv";
static String temp = "";
Button refresh, save, show;
TextView sqlDisplay;
WeatherDBAdapter weatherDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weatherDBHelper = new WeatherDBAdapter(this);
sqlDisplay = (TextView) findViewById(R.id.sqlDisplay);
new MyAsynkTask().execute();
refresh = (Button) findViewById(R.id.bRefresh);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyAsynkTask().execute();
}
});
save = (Button) findViewById(R.id.bSave);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int tempToSave = Data.tempInt;
addTemp(Data.tempInt);
}
});
show = (Button) findViewById(R.id.bDisplay);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sqlData = weatherDBHelper.getTemps();
sqlDisplay.setText(sqlData + "Hi");
}
});
}
public void addTemp(int t) {
weatherDBHelper.insertTemp(t);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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);
}
private class MyAsynkTask extends AsyncTask<String, String, String> {
public int convertTemp;
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost(weatherUrl);
httpPost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder theStringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
theStringBuilder.append(line + "/n");
}
result = theStringBuilder.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
JSONObject jsonObject;
try {
Log.v("JSON", result);
jsonObject = new JSONObject(result);
JSONObject queryJSONObject = jsonObject.getJSONObject("main");
// JSONObject resultsJSONObject = queryJSONObject.getJSONObject("temp");
temp = queryJSONObject.getString("temp");
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
TextView tempTV = (TextView) findViewById(R.id.line1);
Data convert = new Data();
convertTemp =convert.convert(temp);
tempTV.setText("The Temperature is " + convertTemp);
}
}
}
Sorry if this is a dumb question, but I have been searching for nearly 2 hours and yet to figure out what I am doing wrong. Thanks in advanced for any help it is greatly appreciated.
No comments:
Post a Comment