sendMessage (View view) {
}
The second lowercase "view" never gets referred to in the code written around it, I am actually reading the tutorials from google and am very frustrated because I cant figure this out...
Sent from my SPH-L900 using xda app-developers app
JamesChilds said:
sendMessage (View view) {
}
The second lowercase "view" never gets referred to in the code written around it, I am actually reading the tutorials from google and am very frustrated because I cant figure this out...
Sent from my SPH-L900 using xda app-developers app
Click to expand...
Click to collapse
You did not specify the type of the returned value:
These should work:
Code:
void sendMessage (View view) {
}
Code:
boolean sendMessage (View view) {
}
It is void. I understand it works but I dont know why? And I would rather know why before I continue learning.
Sent from my SPH-L900 using Tapatalk 4 Beta
The view is needed if you use androidnClick in a certain XML layout file. Without View view, you won't be able to use the method with onClick. You can remove the View view part if you don't use it with onClick. If you don't use it, it isn't necessary.
Sent from my awesome fridge
What makes it necessary? What I'm asking for is an explanation of the prupose of the lowercase view.
Sent from my SPH-L900 using Tapatalk 4 Beta
Could you possibly share a link to where you found this tutorial/code?
I'm not sure of your programming experience, so forgive me if I 'simplify' things too much.
Code:
sendMessage (View view)
The method signature defines a method called sendMessage which requires a View to be passed in.
View is the name of the Android class. The 'lowercase view' is the object, or instance, of View being passed in. You could essentially rename it if it helps:
Code:
sendMessage (View myview)
The purpose of including the view object is to manipulate it (get/set its properties). It would be done as so:
Code:
sendMessage (View myview) {
int height = myview.getHeight();
int id = getId();
//and so on
}
If the view object is not being used inside the method body, you could remove it from the signature.
Code:
sendMessage () {
//Code that doesn't need view
}
Hope that helps!
That is what I thought it meant. Now it doesn't necesarily have to be referred to or manipulated does it? As the examples im learning from don't.
Sent from my SPH-L900 using Tapatalk 4 Beta
Alkonic said:
Could you possibly share a link to where you found this tutorial/code?]
http://developer.android.com/training/basics/firstapp/starting-activity.html
Sent from my SPH-L900 using Tapatalk 4 Beta
Click to expand...
Click to collapse
JamesChilds said:
Alkonic said:
Could you possibly share a link to where you found this tutorial/code?]
http://developer.android.com/training/basics/firstapp/starting-activity.html
Sent from my SPH-L900 using Tapatalk 4 Beta
Click to expand...
Click to collapse
This code snippet now makes much more sense. What the tutorial is trying to do is listen for a button click - Android gives you two ways to do this: through XML or Java.
In Java:
Code:
Button myButton = (Button) findViewById(R.id.button1);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Handle the click here.
}
});
In XML
Code:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
Code:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
Both these snippets do the same thing: register a method to receive click events.
When you do this in Java, you manually implement an OnClickListener and you're forced to override the onClick(View v) method.
When you specify the sendMessage in XML, Android assumes that you've created it with the correct signature: sendMessage(View myView)
Even if you don't use the view, you have to include it in your method signature: If you remove the view, and just use sendMessage(), your clicks won't work.
I prefer setting the click listeners in Java - it's simpler.
Click to expand...
Click to collapse
Thank you ;] all posts where very helpful.
Sorry I did not thank before
Sent from my SPH-L900 using Tapatalk 4 Beta
Related
Hey there! I was coding an app I added a preferences menu, and it works, but changes happen only when I restart the application,anyone knows how to make changes happen whithout exitting the app??? Thanks in advance
My code ( from main activity):
preferencias = preferenceManager.getDefaultSharedPreferences(TimeToSpeechActivity.this);
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
//nothing here, do I have to put anything?
}
};
preferencias.registerOnSharedPreferenceChangeListener(listener);
getPrefs();
changefont(fuente, letra);
if (boole == true) {fontcolors();}
private void getPrefs(){
fuente = Typeface.createFromAsset(getAssets() , preferencias.getString("elegirfuente", "fonts/Default.ttf"));
letra = Integer.parseInt(preferencias.getString("fontstyle", "0"));
bol = preferencias.getBoolean("randomcolors", true);
}
Click to expand...
Click to collapse
I have nothing put in preference activity, do i have to put anything?
Also, do I have to edit this?: (SharedPreferences prefs, String key) I ask this because i havent created prefs and key varibles
Thanks in advance!!!
It should update for you if you put getPrefs() inside the sharedpreferences listener
Sent from my Xoom using xda premium
thanks, but if I do that I get a nullpointer exception, I think its caused because the params I use to set "changefont(fuente, letra);" and "if (boole == true) {fontcolors();}" have null value....another idea ,please? I am breaking my head with this...
My updated code:
preferencias = preferenceManager.getDefaultSharedPreferences(Time ToSpeechActivity.this);
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener () {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
getPrefs();
}
};
preferencias.registerOnSharedPreferenceChangeListener(listener);
changefont(fuente, letra);
if (boole == true) {fontcolors();}
private void getPrefs(){
fuente = Typeface.createFromAsset(getAssets() , preferencias.getString("elegirfuente", "fonts/Default.ttf"));
letra = Integer.parseInt(preferencias.getString("fontstyle ", "0"));
bol = preferencias.getBoolean("randomcolors", true);
}
Click to expand...
Click to collapse
i tried to put changefont and fontcolors functions inside the sharedpreference listener... but if i do, that options are not set... so I think there is a problem with the listener...why???
I will add the code of my preference class:
public class PantallaOpciones extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.opciones);
}
}
Click to expand...
Click to collapse
One way ive always done preference updates was to use a dedicated method for updating (like you have with openPrefs()) then calling it after operations:
runOperation(){
updatePrefsMethod();
getPrefsMethod();
}
If i was near my pc i could give you a more solid example, but alas, im stuck at work lol
Also, use the stacktraces to check which line gives it null (itll say something like "at com.yourapp.identifier(offending Class - line number)
maybe if you a null check on the boolean for color that sets it false, you may not get the nullPointerException
Sent from my Xoom using xda premium
z3nful said:
One way ive always done preference updates was to use a dedicated method for updating (like you have with openPrefs()) then calling it after operations:
runOperation(){
updatePrefsMethod();
getPrefsMethod();
}
If i was near my pc i could give you a more solid example, but alas, im stuck at work lol
Also, maybe if you a null check on the boolean for color that sets it false, you may not get the nullPointerException
Sent from my Xoom using xda premium
Click to expand...
Click to collapse
OK, could you give me an example as soon as you can,please?
Hi
Currently I am busy with development for my app. This app is supposed to read a website and give me back information from website. Unfortunately the site gives me back with all html/php code and this is difficult to remove the code before I display content on the app. How do I display a website without displaying php data?
Sent from my MT27i using xda app-developers app
Mphidi said:
Hi
Currently I am busy with development for my app. This app is supposed to read a website and give me back information from website. Unfortunately the site gives me back with all html/php code and this is difficult to remove the code before I display content on the app. How do I display a website without displaying php data?
Sent from my MT27i using xda app-developers app
Click to expand...
Click to collapse
I'm still a beginner, Why not you use webview if you want to display it as a webpage itself?
Code:
WebView wv = new WebView(context);
wv.setBackgroundColor(1);
wv.setBackgroundResource(color.transparent);
wv.loadUrl(URL);
wv.setWebViewClient(new WebViewClient() {
[user=439709]@override[/user]
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
view.loadUrl(url);
return true;
}
//this method will clear cache everytime after loading the page.Set it to flase if you dont want it.
[user=439709]@override[/user]
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.clearCache(true);
}
});
and a variable
Code:
private static String URL = "http://www.example.com/example.html";
Thanks I will test it
Sent from my MT27i using xda app-developers app
Hello!
First at all, I'm a beginner in android coding, I'm more a graphist with Photoshop as main tool.
A friend has made an app for my themes and is in holidays until september.
Beeing logical and understanding fast, with my friend Google we found the functions/codes I needed. Except one:
I have a horizontal scroll layout showing the apps needed to install the theme. Each app is showed in a linearlayout.
What I would like is make each linearlayout of apps clickable and make them show the app on the playstore when you click on it.
Thanks for reading
I assume you're using Textviews to add line by line the dependencies.. There is a property called autolink or very much like it that makes a link whenever it finds a url in the text...
But instead of Textviews in a linearlayout, why not use ListView? Then you can handle the click in the item to create an intent to open the browser with the url needed... it's a bit of more work but has a better esthetic than a bunch of TextViews... For instance, it makes it easier to use when in a ldpi device...
Sent from my LG-P350 using xda app-developers app
Sorry for the late.
I'm beginner in java coding so I don't undersand well what you mean dude.
In fact, what I have now is:
In res\layout\main.xml:
Code:
<HorizontalScrollView
android:id="@+id/layout5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout3"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
>
<LinearLayout
android:id="@+id/layout4"
android:background="#00000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
In MainActivity.java:
Code:
boolean apexInstalled = appInstalledOrNot("com.anddoes.launcher");
RelativeLayout apexApp = (RelativeLayout) getLayoutInflater().inflate(R.layout.item, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (125 * scale + 0.5f),(int) (150 * scale + 0.5f));
ImageView apexI = (ImageView) apexApp.findViewById(R.id.appIcon);
apexI.setBackgroundResource(R.drawable.apexicon);
TextView apexT = (TextView) apexApp.findViewById(R.id.appText);
if(apexInstalled){
apexT.setText(R.string.installe);
apexT.setTextColor(Color.parseColor("#5a925d"));
}
else{
apexT.setText(R.string.nninstalle);
apexT.setTextColor(Color.parseColor("#de3747"));
}
apexT.setTypeface(font);
TextView apexTitle = (TextView) apexApp.findViewById(R.id.appTitle);
apexTitle.setText("Apex Launcher"); // \n == retour a la ligne
apexTitle.setTypeface(font);
apexApp.setBackgroundColor(Color.argb(190, 0, 0, 0));
listApp.addView(apexApp, params);
And I have many blocks like this one but with other apps, and I would like them to point on the playstore, what do I have to add?
Use a ListView with a custom Adapter: http://www.vogella.com/articles/AndroidListView/
Then add an OnItemClickedListener to the ListView.
In its onItemClick method you can use an Intent like this one:
Code:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkg); //Your package name here
if (getActivity().getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), "Google Play is not installed on the device.", Toast.LENGTH_LONG).show();
}
Thanks trying to help me, but I understand aproximatively the intent, but the listview etc, no..
I'm just trying to modify a bit an app somebody made for me using my logic to understand what I have to do. The problem is that I don't understand how to apply what you tell me :/
Lyechee said:
Thanks trying to help me, but I understand aproximatively the intent, but the listview etc, no..
I'm just trying to modify a bit an app somebody made for me using my logic to understand what I have to do. The problem is that I don't understand how to apply what you tell me :/
Click to expand...
Click to collapse
OK, then let's forget about the ListView if you just want to modify your existing app with as little effort as possible.
Try that in your Java code:
Code:
LinearLayout layout = findViewById(R.id.layout4);
layout.setClickable(true);
layout.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkg); //Your package name here
if (getActivity().getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), "Google Play is not installed on the device.", Toast.LENGTH_LONG).show();
}
}
});
(The word Override has to be written with a capital letter at the beginning. XDA does not allow that.)
But if I understand well that code, it will make a "link case" in the whole scroll layout no?
Or a link for each app?
If it's not possible to cut the horizontal scroll in little squares (links), is that possible to put a hyperlink on each imageview? So that click on the icon shows the store.
Lyechee said:
But if I understand well that code, it will make a "link case" in the whole scroll layout no?
Or a link for each app?
If it's not possible to cut the horizontal scroll in little squares (links), is that possible to put a hyperlink on each imageview? So that click on the icon shows the store.
Click to expand...
Click to collapse
Yes, it does. I thought that the Linear layout was loaded as the layout for each app.
Ok. I think that the RelativeLayout in the MainActivity.java is your row, right?
If it is, that should work:
Code:
apexApp.setClickable(true);
apexApp.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkg); //Your package name here
if (getActivity().getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), "Google Play is not installed on the device.", Toast.LENGTH_LONG).show();
}
}
});
Yeah, each RelativeLayout is an app square. I'm not at home so I can't test it now, will later in the evening, thanks for your help!
Lyechee said:
Yeah, each RelativeLayout is an app square. I'm not at home so I can't test it now, will later in the evening, thanks for your help!
Click to expand...
Click to collapse
Welcome.
Hello! I'm in front of a problem.
I wanted to face it alone, but I don't manage it..
I have had a problem with "uri", and after googleing, I've found that I had to import it.
But my problem is that I get that error:
Code:
The method getActivity() is undefined for the type new View.OnClickListener(){}
Any idea?
Lyechee said:
Hello! I'm in front of a problem.
I wanted to face it alone, but I don't manage it..
I have had a problem with "uri", and after googleing, I've found that I had to import it.
But my problem is that I get that error:
Code:
The method getActivity() is undefined for the type new View.OnClickListener(){}
Any idea?
Click to expand...
Click to collapse
Ah, you're right. I'm sorry.
I copied the code from one of my fragments. Just delete the getActivity().
So it is:
Code:
apexApp.setClickable(true);
apexApp.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkg); //Your package name here
if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
startActivity(intent);
} else {
Toast.makeText(MainActivtiy.this, "Google Play is not installed on the device.", Toast.LENGTH_LONG).show(); //replace the MainActivity by your Activity
}
}
});
Works like a charm, thank you a lot Exactly what I wanted
If you need a graphist, I'm your man
PS: Is there a tool in Eclipse to make blocks of code lines? I mean, put them in blocs and then hide what you don't need.
Lyechee said:
Works like a charm, thank you a lot Exactly what I wanted
If you need a graphist, I'm your man
PS: Is there a tool in Eclipse to make blocks of code lines? I mean, put them in blocs and then hide what you don't need.
Click to expand...
Click to collapse
Yeah, there should be a minus on the left side next to each method. Click on it to hide the method.
Want to be added? [INDEX] List of themers and designers
Suppose I added a digit '1' to the arraylist<Integer> which is at position 0.So when I call remove method like this.
array.remove(1);
It gives an exception.although the method remove(object) exists.so I how can I remove the object(integer) by not using the position
Sent from my GT-S5570 using XDA Premium 4 mobile app
arpitkh96 said:
Suppose I added a digit '1' to the arraylist<Integer> which is at position 0.So when I call remove method like this.
array.remove(1);
It gives an exception.although the method remove(object) exists.so I how can I remove the object(integer) by not using the position
Sent from my GT-S5570 using XDA Premium 4 mobile app
Click to expand...
Click to collapse
array.remove(new Integer(1));
SimplicityApks said:
array.remove(new Integer(1));
Click to expand...
Click to collapse
That didn't worked.I am using it like this
Code:
//CopyIds is the arraylist
private BroadcastReceiver Copy_Receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle b=arg1.getExtras();
if(b!=null){
int id=b.getInt("id");
Integer id1=new Integer(id);
if(CopyIds.contains(id)){
boolean completed=b.getBoolean("COPY_COMPLETED",false);
View process=rootView.findViewWithTag("copy"+id);
if(completed){ rootView.removeViewInLayout(process);CopyIds.remove(id1);}
else{
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
long total=b.getLong("total");
long done=b.getLong("done");
((TextView)process.findViewById(R.id.progressText)).setText("Copying \n"+name+"\n"+utils.readableFileSize(done)+"/"+utils.readableFileSize(total)+"\n"+p1+"%");
ProgressBar p=(ProgressBar)process.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);}
}else{
View root=getActivity().getLayoutInflater().inflate(R.layout.processrow, null);
root.setPaddingRelative(10,10,10,10);
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
root.setTag("copy"+id);
((TextView)root.findViewById(R.id.progressText)).setText("Copying \n"+name);
ProgressBar p=(ProgressBar)root.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);
CopyIds.add(id);
rootView.addView(root);
}
}
}};
Log says error receiving broadcast.arryindexoutofbounds exception ,size 1 index 1
Sent from my GT-S5570 using XDA Premium 4 mobile app
arpitkh96 said:
That didn't worked.I am using it like this
Code:
//CopyIds is the arraylist
private BroadcastReceiver Copy_Receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle b=arg1.getExtras();
if(b!=null){
int id=b.getInt("id");
Integer id1=new Integer(id);
if(CopyIds.contains(id)){
boolean completed=b.getBoolean("COPY_COMPLETED",false);
View process=rootView.findViewWithTag("copy"+id);
if(completed){ rootView.removeViewInLayout(process);CopyIds.remove(id1);}
else{
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
long total=b.getLong("total");
long done=b.getLong("done");
((TextView)process.findViewById(R.id.progressText)).setText("Copying \n"+name+"\n"+utils.readableFileSize(done)+"/"+utils.readableFileSize(total)+"\n"+p1+"%");
ProgressBar p=(ProgressBar)process.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);}
}else{
View root=getActivity().getLayoutInflater().inflate(R.layout.processrow, null);
root.setPaddingRelative(10,10,10,10);
String name=b.getString("name");
int p1=b.getInt("p1");
int p2=b.getInt("p2");
root.setTag("copy"+id);
((TextView)root.findViewById(R.id.progressText)).setText("Copying \n"+name);
ProgressBar p=(ProgressBar)root.findViewById(R.id.progressBar1);
p.setProgress(p1);
p.setSecondaryProgress(p2);
CopyIds.add(id);
rootView.addView(root);
}
}
}};
Log says error receiving broadcast.arryindexoutofbounds exception ,size 1 index 1
Click to expand...
Click to collapse
Strange since I don't get any compile time errors (sorry don't have time to test it right now) and that should be the way to do it...
Anyway, if it still doesn't work for you, just manually search for the right index using get().equals in a for loop and remove the element at the right index then... (that's what the remove(Object) method does anyway).
SimplicityApks said:
array.remove(new Integer(1));
Click to expand...
Click to collapse
That works. But why ArrayList is not generified. I would like to use it like:
Code:
arrayList.<Integer>remove(new Integer(1));
It would throw Compiler time Error in case if you pass object of wrong type, which would safe time people like OP.
SimplicityApks said:
Strange since I don't get any compile time errors (sorry don't have time to test it right now) and that should be the way to do it...
Anyway, if it still doesn't work for you, just manually search for the right index using get().equals in a for loop and remove the element at the right index then... (that's what the remove(Object) method does anyway).
Click to expand...
Click to collapse
I solved it
Sent from my GT-S5570 using XDA Premium 4 mobile app
I'm having a problem on layouting my app specially when I images are involve.
In the attached image below, how to create a navigation button like that? It also appears translucent, how to do that?
clonedaccnt said:
I'm having a problem on layouting my app specially when I images are involve.
In the attached image below, how to create a navigation button like that? It also appears translucent, how to do that?
Click to expand...
Click to collapse
just looks like a listView, well ... I lie that looks like a horrid webview/html oddity, but in android you would just use a listView for the main view
Or
If the menu items are static and not mutable then a simple frameLayout as parent then a linearLayout for the buttons views
Thanks for the answer. I'll try to use what you said.
deanwray said:
just looks like a listView, well ... I lie that looks like a horrid webview/html oddity, but in android you would just use a listView for the main view
Or
If the menu items are static and not mutable then a simple frameLayout as parent then a linearLayout for the buttons views
Click to expand...
Click to collapse
Can you give me some images or links that you think is a beautiful and clean home navigation?
clonedaccnt said:
Can you give me some images or links that you think is a beautiful and clean home navigation?
Click to expand...
Click to collapse
not sure I understand, but whatever you're after, if it's just inspiration, there will be many examples just searching google
using xml item or json for dynamic data
I m writed simple example for you :
If you have a limited and not dymanic data, you can use xml item for listview, like this :
Add this your string.xml
Code:
<string-array name="schoolsname">
<item>School 1</item>
<item>School 2</item>
<item>School 3</item>
</string-array>
Add listview to your layout file :
Code:
<ListView
android:id=”@+id/list”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:entries="@array/schoolsname"
/>
And in your Activity class you just defined your listview, like this :
Code:
public class MainActivity extends Activity implements OnItemClickListener{
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list= (ListView) findViewById(R.id.list);
list.setOnItemClickListener(this);
}
// [B]onItemClick When clicked your list item[/B]
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
}
Sorry my english. I m still learning.