Hello,
I'm trying to write code of a widget sms for android. But I have a problem of cursor, after lot of test on compiling I dircoverd that
Code:
Cursor c = context.getContentResolver().query(Uri.parse("content://sms/"), null, null ,null,null);
make an error and I don't no why. If somebody knows how use a cursor or have a better idea to view sms without cursor, I woold like share it with him!
thank's
try something like this
Code:
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null);
Thank's to you Draffodx, I such begin my widget, now it can put on screen the sms I want... but I can't change of SMS with th button I've created. I don't understand how make a button with the widget because it need to be an Activity for a button and I've made an AppWidget...
I trying to do like this:
Code:
public class MySMSwidget extends AppWidgetProvider implements View.OnClickListener {
private Button Bnext;
private int sms_id=0;
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.widget_layout);
final Button button = (Button) findViewById(R.id.next);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v==Bnext){sms_id=sms_id+1;}
}
});
}
}.... and the rest of the code
But when I click the button, nothing happend.
hey, my idea seems to be a bad idea so I try this way:
Code:
public class MySMSwidget extends AppWidgetProvider {
private int sms_id=0;
public void onReceive (Context context, Intent intent){
if (Intent.ACTION_ATTACH_DATA.equals(intent.getAction()))
{
Bundle extra = intent.getExtras();
sms_id = extra.getInt("Data");
}
}
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
Cursor c = context.getContentResolver().query(Uri.parse("content://
sms/inbox"), null, null ,null,null);
String body = null;
String number = null;
String date = null;
c.moveToPosition(sms_id);
body = c.getString(c.getColumnIndexOrThrow("body")).toString();
number =
c.getString(c.getColumnIndexOrThrow("address")).toString();
date = c.getString(c.getColumnIndexOrThrow("date")).toString();
c.close();
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
updateViews.setTextColor(R.id.text, 0xFF000000);
updateViews.setTextViewText(R.id.text,date+'\n'+number+'\n'+body);
ComponentName thisWidget = new ComponentName(context,
MySMSwidget.class);
appWidgetManager.updateAppWidget(thisWidget, updateViews);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_ATTACH_DATA);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.next, changeData(context));
}
private PendingIntent changeData(Context context) {
Intent Next = new Intent();
Next.putExtra("Data", sms_id+1);
Next.setAction(Intent.ACTION_ATTACH_DATA);
return(PendingIntent.getBroadcast(context,
0, Next, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
my code isn't terminated.
I hope there will be someone to help to correct it.
Just want to display next SMS.
Please help.
I have an expandable list view with 2 parents and 3 children. I want to open a dialog based on each click. I can't find any examples showing you how to call something based on positions. At least not with the ExpandableListView tutorial I followed.
Code:
public class MainActivity extends Activity implements OnClickListener {
private LinkedHashMap<String, HeaderInfo> myDepartments = new LinkedHashMap<String, HeaderInfo>();
private ArrayList<HeaderInfo> deptList = new ArrayList<HeaderInfo>();
private MyListAdapter listAdapter;
private ExpandableListView myList;
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Just add some data to start with
loadData();
// get reference to the ExpandableListView
myList = (ExpandableListView) findViewById(R.id.myList);
// create the adapter by passing your ArrayList data
listAdapter = new MyListAdapter(MainActivity.this, deptList);
// attach the adapter to the list
myList.setAdapter(listAdapter);
// listener for child row click
myList.setOnChildClickListener(myListItemClicked);
// listener for group heading click
myList.setOnGroupClickListener(myListGroupClicked);
}
// load some initial data into out list
private void loadData() {
addProduct("Parent One", "Child One");
addProduct("Parent One", "Child Two");
addProduct("Parent One", "Child Three");
addProduct("Parent Two", "Child One");
addProduct("Parent Two", "Child Two");
addProduct("Parent Two", "Child Three");
}
// our child listener
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
[user=439709]@override[/user]
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// Create a switch that switches on the specific child position.
// get the group header
HeaderInfo headerInfo = deptList.get(groupPosition);
// get the child info
DetailInfo detailInfo = headerInfo.getProductList().get(
childPosition);
// display it or do something with it
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.cdialog);
// dialog.setTitle(R.id.titlebar);
dialog.setTitle(R.string.titlebar);
dialog.show();
return false;
}
};
// our group listener
private OnGroupClickListener myListGroupClicked = new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// get the group header HeaderInfo headerInfo =
deptList.get(groupPosition);
// display it or do something with it
return false;
}
};
I can get a custom dialog open if I click a child, but it's not set to any specific parent and child.
Any ideas?
EDIT ADD: Got it. Tried a switch/case like this and it worked. Finally! After two days of trying to understand it.:fingers-crossed:
Code:
switch(groupPosition) {
case 1:
switch (childPosition) {
case 0:
Intent protheanIntent = new Intent(Codex.this, CodexProthean.class);
Codex.this.startActivity(protheanIntent);
break;
case 1:
Intent rachniIntent = new Intent(Codex.this, CodexRachni.class);
Codex.this.startActivity(rachniIntent);
break;
}
case 2:
switch (childPosition) {
case 2:
Intent asariIntent = new Intent(Codex.this, CodexAsari.class);
Codex.this.startActivity(asariIntent);
break;
}
}
I have created a questionnaire in the beginning of the app in order to base a user model off the answers chosen. The values of the chosen options are stored as Shared Preferences as shown below. With the data retrieved I am then trying to tag a Google map with certain tags based on the answers chosen. The issue I am encountering is that of reading the stored Shared Preference values across the different activities, from the Question2 Activity, to that of the Map Activity (code below). Any pointers on how to solve the *context* conflict would be greatly appreciated.
Question2 Activity:
Code:
public class Question2 extends Activity{
RadioButton q2a1,q2a2,q2a3;
Button btn2;
public static final String MY_PREF = "MyPreferences";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.activity_question2, null);
return v;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
q2a1 = (RadioButton) findViewById(R.id.q2a1);
q2a2 = (RadioButton) findViewById(R.id.q2a2);
q2a3 = (RadioButton) findViewById(R.id.q2a3);
btn2 = (Button) findViewById(R.id.q2_button);
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences prefernces = getSharedPreferences(MY_PREF, 0);
SharedPreferences.Editor editor = prefernces.edit();
if (q2a1.isChecked()){
editor.putInt("answer_value2", 1);
editor.commit();
}
if (q2a2.isChecked()){
editor.putInt("answer_value2", 2);
editor.commit();
}
if (q2a3.isChecked()){
editor.putInt("answer_value2", 3);
editor.commit();
}else {
editor.putInt("answer_value2", 0);
editor.commit();
}
editor.commit();
Intent intent = new Intent(getApplicationContext(), Question3.class);
startActivity(intent);
}
});
}
}
Map Activity:
Code:
public class MapActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapLayout)).getMap();
map.setMyLocationEnabled(true);
SharedPreferences preferences = getSharedPreferences(Question2.MY_PREF,MODE_PRIVATE);
int q1answer = preferences.getInt("answer_value", 0);
int q2answer = preferences.getInt("answer_value2", 0);
int q3answer = preferences.getInt("answer_value3", 0);
if(q1answer == 1){
//method
}
if(q1answer == 2){
//method
}
if(q1answer == 3){
//method
}
if(q2answer == 1){
map.addMarker(new MarkerOptions().position(FOOD_FOOD).title("Food & Food").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(HEALTHSHOP).title("Health Shop").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(GRASSYHOPPER).title("Grasy Hopper").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(M_S).title("M&S").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(MINT).title("Mint").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}if(q2answer == 2){
Toast.makeText(getApplicationContext(), "Your result is " + q2answer, Toast.LENGTH_SHORT).show();
map.addMarker(new MarkerOptions().position(FOOD_FOOD).title("Food & Food").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(SUBWAY).title("Subway").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(NEWYORKSBEST).title("New York's Best").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(MCD).title("Mc Donald's").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(M_S).title("M&S").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(JUBILEE).title("Jubilee").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(GRASSYHOPPER).title("Grassy Hopper").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}if(q2answer == 3){
map.addMarker(new MarkerOptions().position(NEWYORKSBEST).title("New York's Best").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(SUBWAY).title("Subway").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(PASTIZZERIA).title("Pastizerria").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(MCD).title("Mc Donald's").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.addMarker(new MarkerOptions().position(BURGERKING).title("Burger King").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
if(q3answer == 1){
//implementation
}if(q3answer == 2){
//implementation
}if(q3answer == 3){
//implementation
}if(q3answer == 4){
//implementation
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
Use this
create this class in your package
Code:
public class SharedPrefence{
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "MyPreferences";
// Answers (make variable public to access from outside)
public static final String KEY_ANSWER_TWO = "answer_value2";
//similarly other answers tag can be added(this is the key of preference)
// Constructor
public SharedPrefence(Context context) {
_context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create Answer.
* */
public void createAnswer(int ans) {
// Storing name in pref
editor.putInt(KEY_ANSWER_TWO, ans);
// commit changes
editor.commit();
}
public int getAnswer(String tag,int default) {
return pref.getString(tag, default);
}
}
now create an activity and initialise this class in the starting with the activity context.
next call the create answer method of this class and you are done.
also no need to create sharedprefence in each activity.
and it's even better if you create only one activity and use ViewPager for all questions.you can google for it.
And if this was useful click on thanks below.
:victory::victory::victory::victory:
I have a large data to load from JSON.
I have implemented a custom list view by following a tutorial, now since the data is huge I want it load as the user scrolls.
This is my LoadRestaurant class code which is inside the main activity.
Code:
class LoadRestaurants extends AsyncTask<String, String, String> {
//Show Progress Dialog
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SearchAll.this);
pDialog.setMessage("Loading All Restaurants...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... arg) {
//building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//Getting JSON from URL
String json = jsonParser.makeHttpRequest(URL_RESTAURANT_LIST, "GET", params);
//Log Cat Response Check
Log.d("Areas JSON: ", "> " + json);
try {
restaurants = new JSONArray(json);
if (restaurants != null) {
//loop through all restaurants
for (int i = 0; i < restaurants.length(); i++) {
JSONObject c = restaurants.getJSONObject(i);
//Storing each json object in the variable.
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String location = c.getString(TAG_LOCATION);
String rating = c.getString(TAG_RATING);
//Creating New Hashmap
HashMap<String, String> map = new HashMap<String, String>();
//adding each child node to Hashmap key
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_LOCATION, location);
map.put(TAG_RATING, rating);
//adding HashList to ArrayList
restaurant_list.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
//dismiss the dialog
pDialog.dismiss();
//Updating UI from the Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
ListAdapter adapter = new SimpleAdapter(
SearchAll.this, restaurant_list,
R.layout.listview_restaurants, new String[]{
TAG_ID, TAG_NAME, TAG_LOCATION, TAG_RATING}, new int[]{
R.id.login_id, R.id.restaurant_name, R.id.address, R.id.rating});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Bundle bundle = new Bundle();
Intent intent = new Intent(SearchAll.this, RestaurantProfile.class);
String loginId = ((TextView) view.findViewById(R.id.login_id)).getText().toString();
intent.putExtra("login_id", loginId);
startActivity(intent);
}
});
}
});
}
}
}
I want to load around 20 restaurants and then it auto loads another 20 as soon as user reaches the end of first 20.
There are lots of tutorials online but its confusing to implement.
Please help me out!
The custom ListView, support for automatic loading you can try https://github.com/chrisbanes/Android-PullToRefresh
In my app there is an activity say Activity A which has an image view with some text views. code is listed below
Code:
public class EidCardFinal extends Activity {
private ImageView imageView;
private TextView receiver, sender, messagebody;
private Intent intent;
private Bundle bundle;
private static final int FONT_SELECT = 1;
// public String filepath = "MyFileStorage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eid_card_final);
intent = getIntent();
String message1 = intent.getStringExtra("RECEIVER");
String message2 = intent.getStringExtra("SENDER");
String message3 = intent.getStringExtra("MESSAGEBODY");
String check_click = intent.getStringExtra("bttnclick");
imageView = (ImageView) findViewById(R.id.imageView1);
receiver = (TextView) findViewById(R.id.textView1);
sender = (TextView) findViewById(R.id.textView2);
messagebody = (TextView) findViewById(R.id.textView3);
receiver.setText(message1);
sender.setText(message2);
messagebody.setText(message3);
// Selected image id
if ("BUTTONCLICK".equals(check_click)) {
String path = intent.getStringExtra("image");
Uri myUri = Uri.parse(path);
imageView.setImageURI(myUri);
} else {
int position = intent.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
// ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageAdapter.thumbIds[position]);
case R.id.change_fonts:
Intent fontintent = new Intent();
bundle = getIntent().getExtras();
fontintent.putExtras(bundle);
fontintent.setClass(getApplicationContext(), FontSelection.class);
this.startActivityForResult(fontintent, FONT_SELECT);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == FONT_SELECT) {
Bundle pathadd= data.getExtras();
String fontadd = pathadd.getString("FONTPATH");
//intent = getIntent();
/*String message1 = data.getStringExtra("RECEIVER");
String message2 = data.getStringExtra("SENDER");
String message3 = data.getStringExtra("MESSAGEBODY");
//String check_click = intent.getStringExtra("bttnclick");
imageView = (ImageView) findViewById(R.id.imageView1);
receiver = (TextView) findViewById(R.id.textView1);
sender = (TextView) findViewById(R.id.textView2);
messagebody = (TextView) findViewById(R.id.textView3);
receiver.setText(message1);
sender.setText(message2);
messagebody.setText(message3);*/
//bundle = getIntent().getExtras();
Typeface tyfa = Typeface.createFromAsset(getAssets(), fontadd);
receiver.setTypeface(tyfa);
sender.setTypeface(tyfa);
messagebody.setTypeface(tyfa);
From menu, user is taken to another activity say Activity B from where a custom font can be selected for Activity A. Code is listed below
Code:
public class FontSelection extends Activity {
String[] fontpath = { "fonts/android_7.ttf", "fonts/doridrobot.ttf",
"fonts/droidsansmono.ttf", "fonts/droidserif-bold.ttf",
"fonts/green-avocado.ttf", "fonts/lokicola.ttf",
"fonts/outwrite.ttf", "fonts/painting-the-light.ttf",
"fonts/roboto-black.ttf", "fonts/roboto-boldcondensed.ttf",
"fonts/roboto-medium.ttf", "fonts/roboto-regular.ttf" };
String[] fontname = { "android_7", "doridrobot", "droidsansmono",
"droidserif-bold", "green-avocado", "lokicola", "outwrite",
"painting-the-light", "roboto-black", "roboto-boldcondensed",
"roboto-medium", "roboto-regular" };
private Intent fontpathintent = new Intent();
private Bundle bundle1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_font_selection);
RadioButton radio1 = (RadioButton) findViewById(R.id.radioButton1);
Typeface tf1 = Typeface.createFromAsset(getAssets(), fontpath[0]);
radio1.setTypeface(tf1);
radio1.setText(fontname[0]);
RadioButton radio2 = (RadioButton) findViewById(R.id.radioButton2);
Typeface tf2 = Typeface.createFromAsset(getAssets(), fontpath[1]);
radio2.setTypeface(tf2);
radio2.setText(fontname[1]);
RadioButton radio3 = (RadioButton) findViewById(R.id.radioButton3);
Typeface tf3 = Typeface.createFromAsset(getAssets(), fontpath[2]);
radio3.setTypeface(tf3);
radio3.setText(fontname[2]);
RadioButton radio4 = (RadioButton) findViewById(R.id.radioButton4);
Typeface tf4 = Typeface.createFromAsset(getAssets(), fontpath[3]);
radio4.setTypeface(tf4);
radio4.setText(fontname[3]);
RadioButton radio5 = (RadioButton) findViewById(R.id.radioButton5);
Typeface tf5 = Typeface.createFromAsset(getAssets(), fontpath[4]);
radio5.setTypeface(tf5);
radio5.setText(fontname[4]);
RadioButton radio6 = (RadioButton) findViewById(R.id.radioButton6);
Typeface tf6 = Typeface.createFromAsset(getAssets(), fontpath[5]);
radio6.setTypeface(tf6);
radio6.setText(fontname[5]);
RadioButton radio7 = (RadioButton) findViewById(R.id.radioButton7);
Typeface tf7 = Typeface.createFromAsset(getAssets(), fontpath[6]);
radio7.setTypeface(tf7);
radio7.setText(fontname[6]);
RadioButton radio8 = (RadioButton) findViewById(R.id.radioButton8);
Typeface tf8 = Typeface.createFromAsset(getAssets(), fontpath[7]);
radio8.setTypeface(tf8);
radio8.setText(fontname[7]);
RadioButton radio9 = (RadioButton) findViewById(R.id.radioButton9);
Typeface tf9 = Typeface.createFromAsset(getAssets(), fontpath[8]);
radio9.setTypeface(tf9);
radio9.setText(fontname[8]);
RadioButton radio10 = (RadioButton) findViewById(R.id.radioButton10);
Typeface tf10 = Typeface.createFromAsset(getAssets(), fontpath[9]);
radio10.setTypeface(tf10);
radio10.setText(fontname[9]);
RadioButton radio11 = (RadioButton) findViewById(R.id.radioButton11);
Typeface tf11 = Typeface.createFromAsset(getAssets(), fontpath[10]);
radio11.setTypeface(tf11);
radio11.setText(fontname[10]);
RadioButton radio12 = (RadioButton) findViewById(R.id.radioButton12);
Typeface tf12 = Typeface.createFromAsset(getAssets(), fontpath[11]);
radio12.setTypeface(tf12);
radio12.setText(fontname[11]);
}
public void onRadioButtonClick(View view) {
bundle1 = getIntent().getExtras();
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.radioButton1:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[0]);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
fontpathintent.putExtras(bundle1);
startActivity(fontpathintent);
break;
case R.id.radioButton2:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[1]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton3:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[2]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton4:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[3]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton5:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[4]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton6:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[5]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton7:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[6]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton8:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[7]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton9:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[8]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton10:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[9]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
break;
case R.id.radioButton11:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[10]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton12:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[11]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
}
}
my problem is, upon selection of font, there is no change in activity A, i mean font in activity A remains unchanged.
ariez4u said:
In my app there is an activity say Activity A which has an image view with some text views. code is listed below
Code:
public class EidCardFinal extends Activity {
private ImageView imageView;
private TextView receiver, sender, messagebody;
private Intent intent;
private Bundle bundle;
private static final int FONT_SELECT = 1;
// public String filepath = "MyFileStorage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eid_card_final);
intent = getIntent();
String message1 = intent.getStringExtra("RECEIVER");
String message2 = intent.getStringExtra("SENDER");
String message3 = intent.getStringExtra("MESSAGEBODY");
String check_click = intent.getStringExtra("bttnclick");
imageView = (ImageView) findViewById(R.id.imageView1);
receiver = (TextView) findViewById(R.id.textView1);
sender = (TextView) findViewById(R.id.textView2);
messagebody = (TextView) findViewById(R.id.textView3);
receiver.setText(message1);
sender.setText(message2);
messagebody.setText(message3);
// Selected image id
if ("BUTTONCLICK".equals(check_click)) {
String path = intent.getStringExtra("image");
Uri myUri = Uri.parse(path);
imageView.setImageURI(myUri);
} else {
int position = intent.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
// ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(imageAdapter.thumbIds[position]);
case R.id.change_fonts:
Intent fontintent = new Intent();
bundle = getIntent().getExtras();
fontintent.putExtras(bundle);
fontintent.setClass(getApplicationContext(), FontSelection.class);
this.startActivityForResult(fontintent, FONT_SELECT);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == FONT_SELECT) {
Bundle pathadd= data.getExtras();
String fontadd = pathadd.getString("FONTPATH");
//intent = getIntent();
/*String message1 = data.getStringExtra("RECEIVER");
String message2 = data.getStringExtra("SENDER");
String message3 = data.getStringExtra("MESSAGEBODY");
//String check_click = intent.getStringExtra("bttnclick");
imageView = (ImageView) findViewById(R.id.imageView1);
receiver = (TextView) findViewById(R.id.textView1);
sender = (TextView) findViewById(R.id.textView2);
messagebody = (TextView) findViewById(R.id.textView3);
receiver.setText(message1);
sender.setText(message2);
messagebody.setText(message3);*/
//bundle = getIntent().getExtras();
Typeface tyfa = Typeface.createFromAsset(getAssets(), fontadd);
receiver.setTypeface(tyfa);
sender.setTypeface(tyfa);
messagebody.setTypeface(tyfa);
From menu, user is taken to another activity say Activity B from where a custom font can be selected for Activity A. Code is listed below
Code:
public class FontSelection extends Activity {
String[] fontpath = { "fonts/android_7.ttf", "fonts/doridrobot.ttf",
"fonts/droidsansmono.ttf", "fonts/droidserif-bold.ttf",
"fonts/green-avocado.ttf", "fonts/lokicola.ttf",
"fonts/outwrite.ttf", "fonts/painting-the-light.ttf",
"fonts/roboto-black.ttf", "fonts/roboto-boldcondensed.ttf",
"fonts/roboto-medium.ttf", "fonts/roboto-regular.ttf" };
String[] fontname = { "android_7", "doridrobot", "droidsansmono",
"droidserif-bold", "green-avocado", "lokicola", "outwrite",
"painting-the-light", "roboto-black", "roboto-boldcondensed",
"roboto-medium", "roboto-regular" };
private Intent fontpathintent = new Intent();
private Bundle bundle1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_font_selection);
RadioButton radio1 = (RadioButton) findViewById(R.id.radioButton1);
Typeface tf1 = Typeface.createFromAsset(getAssets(), fontpath[0]);
radio1.setTypeface(tf1);
radio1.setText(fontname[0]);
RadioButton radio2 = (RadioButton) findViewById(R.id.radioButton2);
Typeface tf2 = Typeface.createFromAsset(getAssets(), fontpath[1]);
radio2.setTypeface(tf2);
radio2.setText(fontname[1]);
RadioButton radio3 = (RadioButton) findViewById(R.id.radioButton3);
Typeface tf3 = Typeface.createFromAsset(getAssets(), fontpath[2]);
radio3.setTypeface(tf3);
radio3.setText(fontname[2]);
RadioButton radio4 = (RadioButton) findViewById(R.id.radioButton4);
Typeface tf4 = Typeface.createFromAsset(getAssets(), fontpath[3]);
radio4.setTypeface(tf4);
radio4.setText(fontname[3]);
RadioButton radio5 = (RadioButton) findViewById(R.id.radioButton5);
Typeface tf5 = Typeface.createFromAsset(getAssets(), fontpath[4]);
radio5.setTypeface(tf5);
radio5.setText(fontname[4]);
RadioButton radio6 = (RadioButton) findViewById(R.id.radioButton6);
Typeface tf6 = Typeface.createFromAsset(getAssets(), fontpath[5]);
radio6.setTypeface(tf6);
radio6.setText(fontname[5]);
RadioButton radio7 = (RadioButton) findViewById(R.id.radioButton7);
Typeface tf7 = Typeface.createFromAsset(getAssets(), fontpath[6]);
radio7.setTypeface(tf7);
radio7.setText(fontname[6]);
RadioButton radio8 = (RadioButton) findViewById(R.id.radioButton8);
Typeface tf8 = Typeface.createFromAsset(getAssets(), fontpath[7]);
radio8.setTypeface(tf8);
radio8.setText(fontname[7]);
RadioButton radio9 = (RadioButton) findViewById(R.id.radioButton9);
Typeface tf9 = Typeface.createFromAsset(getAssets(), fontpath[8]);
radio9.setTypeface(tf9);
radio9.setText(fontname[8]);
RadioButton radio10 = (RadioButton) findViewById(R.id.radioButton10);
Typeface tf10 = Typeface.createFromAsset(getAssets(), fontpath[9]);
radio10.setTypeface(tf10);
radio10.setText(fontname[9]);
RadioButton radio11 = (RadioButton) findViewById(R.id.radioButton11);
Typeface tf11 = Typeface.createFromAsset(getAssets(), fontpath[10]);
radio11.setTypeface(tf11);
radio11.setText(fontname[10]);
RadioButton radio12 = (RadioButton) findViewById(R.id.radioButton12);
Typeface tf12 = Typeface.createFromAsset(getAssets(), fontpath[11]);
radio12.setTypeface(tf12);
radio12.setText(fontname[11]);
}
public void onRadioButtonClick(View view) {
bundle1 = getIntent().getExtras();
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.radioButton1:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[0]);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
fontpathintent.putExtras(bundle1);
startActivity(fontpathintent);
break;
case R.id.radioButton2:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[1]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton3:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[2]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton4:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[3]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton5:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[4]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton6:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[5]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton7:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[6]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton8:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[7]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton9:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[8]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton10:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[9]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
break;
case R.id.radioButton11:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[10]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
case R.id.radioButton12:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[11]);
fontpathintent.putExtras(bundle1);
fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
startActivity(fontpathintent);
break;
}
}
my problem is, upon selection of font, there is no change in activity A, i mean font in activity A remains unchanged.
Click to expand...
Click to collapse
Your problem is that you restart your first acitivity in your font selection activity. Instead you'd want to call setResult() in there and then finish that activity. Only that way onActivityResult() is called in your first activity!
SimplicityApks said:
Your problem is that you restart your first acitivity in your font selection activity. Instead you'd want to call setResult() in there and then finish that activity. Only that way onActivityResult() is called in your first activity!
Click to expand...
Click to collapse
Well, thanks for the reply. I have made changes as per your instructions but unfortunately result is same. Changes in the code
Code:
case R.id.radioButton1:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[0]);
// fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
// fontpathintent.putExtras(bundle1);
// startActivity(fontpathintent);
setResult(1, fontpathintent);
finish();
break;
case R.id.radioButton2:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[1]);
// fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
// fontpathintent.putExtras(bundle1);
// startActivity(fontpathintent);
setResult(1, fontpathintent);
finish();
break;
Code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// Bundle pathadd= data.getExtras();
String customfont = data.getStringExtra("FONTPATH");
// String fontadd = pathadd.getString("FONTPATH");
// intent = getIntent();
/*
* String message1 = data.getStringExtra("RECEIVER"); String
* message2 = data.getStringExtra("SENDER"); String message3 =
* data.getStringExtra("MESSAGEBODY"); //String check_click =
* intent.getStringExtra("bttnclick"); imageView = (ImageView)
* findViewById(R.id.imageView1); receiver = (TextView)
* findViewById(R.id.textView1); sender = (TextView)
* findViewById(R.id.textView2); messagebody = (TextView)
* findViewById(R.id.textView3); receiver.setText(message1);
* sender.setText(message2); messagebody.setText(message3);
*/
// bundle = getIntent().getExtras();
Typeface tyfa = Typeface.createFromAsset(getAssets(),
customfont);
receiver.setTypeface(tyfa);
sender.setTypeface(tyfa);
messagebody.setTypeface(tyfa);
}
}
}
ariez4u said:
Well, thanks for the reply. I have made changes as per your instructions but unfortunately result is same. Changes in the code
Code:
case R.id.radioButton1:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[0]);
// fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
// fontpathintent.putExtras(bundle1);
// startActivity(fontpathintent);
setResult(1, fontpathintent);
finish();
break;
case R.id.radioButton2:
if (checked)
fontpathintent.putExtra("FONTPATH", fontpath[1]);
// fontpathintent.setClass(getApplicationContext(),EidCardFinal.class);
// fontpathintent.putExtras(bundle1);
// startActivity(fontpathintent);
setResult(1, fontpathintent);
finish();
break;
Code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// Bundle pathadd= data.getExtras();
String customfont = data.getStringExtra("FONTPATH");
// String fontadd = pathadd.getString("FONTPATH");
// intent = getIntent();
/*
* String message1 = data.getStringExtra("RECEIVER"); String
* message2 = data.getStringExtra("SENDER"); String message3 =
* data.getStringExtra("MESSAGEBODY"); //String check_click =
* intent.getStringExtra("bttnclick"); imageView = (ImageView)
* findViewById(R.id.imageView1); receiver = (TextView)
* findViewById(R.id.textView1); sender = (TextView)
* findViewById(R.id.textView2); messagebody = (TextView)
* findViewById(R.id.textView3); receiver.setText(message1);
* sender.setText(message2); messagebody.setText(message3);
*/
// bundle = getIntent().getExtras();
Typeface tyfa = Typeface.createFromAsset(getAssets(),
customfont);
receiver.setTypeface(tyfa);
sender.setTypeface(tyfa);
messagebody.setTypeface(tyfa);
}
}
}
Click to expand...
Click to collapse
Mmmh strange... I assume your onActivityResult() gets called? I'm not familiar with the typefaces but maybe you need to invalidate() the view to see the change. Well you might as well check if tyfa is a valid typeface in that method, because to me the code looks functional.
SimplicityApks said:
Your problem is that you restart your first acitivity in your font selection activity. Instead you'd want to call setResult() in there and then finish that activity. Only that way onActivityResult() is called in your first activity!
Click to expand...
Click to collapse
i have acted upon your instructions but unable to find the solution, i think (as you have suggested too) that onActivityResult() is not called, beccause i have tried below but no result as well.
Code:
tyfa = Typeface.createFromAsset(getAssets(),
[B][COLOR="Red"]"fonts/outwrite.ttf"[/COLOR][/B]);
receiver.setTypeface(tyfa);
sender.setTypeface(tyfa);
messagebody.setTypeface(tyfa);
SimplicityApks said:
Mmmh strange... I assume your onActivityResult() gets called? I'm not familiar with the typefaces but maybe you need to invalidate() the view to see the change. Well you might as well check if tyfa is a valid typeface in that method, because to me the code looks functional.
Click to expand...
Click to collapse
i have acted upon your instructions but unable to find the solution, i think (as you have suggested too) that onActivityResult() is not called, beccause i have tried below but no result as well.
Code:
tyfa = Typeface.createFromAsset(getAssets(),
[B][COLOR="Red"]"fonts/outwrite.ttf"[/COLOR][/B]);
receiver.setTypeface(tyfa);
sender.setTypeface(tyfa);
messagebody.setTypeface(tyfa);
ariez4u said:
i have acted upon your instructions but unable to find the solution, i think (as you have suggested too) that onActivityResult() is not called, beccause i have tried below but no result as well.
Code:
tyfa = Typeface.createFromAsset(getAssets(),
[B][COLOR="Red"]"fonts/outwrite.ttf"[/COLOR][/B]);
receiver.setTypeface(tyfa);
sender.setTypeface(tyfa);
messagebody.setTypeface(tyfa);
Click to expand...
Click to collapse
I think I found what the problem is here, you call setResult() with your requestCode, but instead it should be called with a resultCode as first parameter, so it should be setResult(RESULT_OK, fontpathintent);! Let me know if that works, see here for a sample.
SimplicityApks said:
I think I found what the problem is here, you call setResult() with your requestCode, but instead it should be called with a resultCode as first parameter, so it should be setResult(RESULT_OK, fontpathintent);! Let me know if that works, see here for a sample.
Click to expand...
Click to collapse
really obliged. thank you very much. my problem is solved