[Q]how to invoke search dialoge by button help needed - Java for Android App Development

I have created a free dictionary app before one year
there is option menu to invoke the search dialoge
now i want to invoke search dialog by pressing a button
i have created button in main.xml
now i want to code
but i dont know how to do
i m trying from long time a go but unfortunetly i dont knowledge of java so please help me out
i will be thankfull
thanks
here is my old app which i have shared in xda one year before
topic is here
http://forum.xda-developers.com/showthread.php?t=1786568
and its the screen shot
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
help me please

rafeeq02 said:
I have created a free dictionary app before one year
there is option menu to invoke the search dialoge
now i want to invoke search dialog by pressing a button
i have created button in main.xml
now i want to code
but i dont know how to do
i m trying from long time a go but unfortunetly i dont knowledge of java so please help me out
i will be thankfull
thanks
here is my old app which i have shared in xda one year before
topic is here
http://forum.xda-developers.com/showthread.php?t=1786568
and its the screen shot
here is the code
Code:
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
abstract public class SearchInterfaceBase extends ListActivity {
abstract ListAdapter makeMeAnAdapter(Intent intent);
private static final int LOCAL_SEARCH_ID = Menu.FIRST+1;
private static final int GLOBAL_SEARCH_ID = Menu.FIRST+2;
TextView selection;
ArrayList<String> items=new ArrayList<String>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
try {
XmlPullParser xpp=getResources().getXml(R.xml.words);
while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType()==XmlPullParser.START_TAG) {
if (xpp.getName().equals("word")) {
items.add(xpp.getAttributeValue(0));
}
}
xpp.next();
}
}
catch (Throwable t) {
Toast
.makeText(this, "Request failed: "+t.toString(), 40000)
.show();
}
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
onNewIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
ListAdapter adapter=makeMeAnAdapter(intent);
if (adapter==null) {
finish();
}
else {
setListAdapter(adapter);
}
}
public void onListItemClick(ListView parent, View v, int position,long id) {
selection.setText(parent.getAdapter().getItem(position).toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, LOCAL_SEARCH_ID, Menu.NONE, "Search Urdu Dictionary").setIcon(android.R.drawable.ic_search_category_default);
menu.add(0, 0, 0, R.string.about).setIcon(R.drawable.about);
menu.add(0, 1, 1, R.string.str_exit).setIcon(R.drawable.exit);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case LOCAL_SEARCH_ID:
onSearchRequested();
return(true);
case GLOBAL_SEARCH_ID:
startSearch(null, false, null, true);
return(true);
case 0:
openOptionsDialog();
break;
case 1:
exitOptionsDialog();
break;
}
return true;
}
private void exitOptionsDialog() {
// TODO Auto-generated method stub
{
new AlertDialog.Builder(this)
.setTitle(R.string.app_exit)
.setIcon(R.drawable.exit)
.setMessage(R.string.app_exit_message)
.setNegativeButton(R.string.str_no,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{}
})
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
finish();
}
})
.show();
}
}
private boolean openOptionsDialog() {
// TODO Auto-generated method stub
{
new AlertDialog.Builder(this)
.setTitle(R.string.app_about)
.setIcon(R.drawable.about)
.setIcon(R.drawable.rafeeq1)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
})
.show();
}
return false;
}
}
i want to invoke the search dialogue by pressing button
lnstead of this
Code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case LOCAL_SEARCH_ID:
onSearchRequested();
return(true);
help me please
Click to expand...
Click to collapse
You can easily achieve this by adding a androidnClick attribute for the button. Like this -
Code:
android:onClick="xyz"
Here, xyz is the name of a function which will be called when the button is clicked. Define xyz with a View object parameter like this -
Code:
public void xyz(View v)
{
//Call the onSearchRequested function here
}
Good luck
Sent from my LT26i using Tapatalk

abcdjdj said:
You can easily achieve this by adding a androidnClick attribute for the button. Like this -
Code:
android:onClick="xyz"
Here, xyz is the name of a function which will be called when the button is clicked. Define xyz with a View object parameter like this -
Code:
public void xyz(View v)
{
//Call the onSearchRequested function here
}
Good luck
Sent from my LT26i using Tapatalk
Click to expand...
Click to collapse
thanks sir ,, yes i have google for it and got the same answer
but unfortunetly i dont konw how to code for click and onSreachRequested
will u please
code it for me
i have created button and its ID is button
so please if u can i will thank full to u sir

rafeeq02 said:
thanks sir ,, yes i have google for it and got the same answer
but unfortunetly i dont konw how to code for click and onSreachRequested
will u please
code it for me
i have created button and its ID is button
so please if u can i will thank full to u sir
Click to expand...
Click to collapse
You don't need to know the id of the button if you are adding the onClick attribute in the XML file. You'll need the id only if you want to a reference to the Button from your Java code using the findViewById function. And calling the onSearchRequested function from the xyz function (on click function) should work just the same as it used to before from the menu. I'm sorry, I'm on my phone right now so I can't provide you with proper code except snippets.
Sent from my LT26i using Tapatalk

abcdjdj said:
You don't need to know the id of the button if you are adding the onClick attribute in the XML file. You'll need the id only if you want to a reference to the Button from your Java code using the findViewById function. And calling the onSearchRequested function from the xyz function (on click function) should work just the same as it used to before from the menu. I'm sorry, I'm on my phone right now so I can't provide you with proper code except snippets.
Sent from my LT26i using Tapatalk
Click to expand...
Click to collapse
THANKS SIR FOR UR REPLY
I HAVE CREATED THIS
xml
Code:
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="sInvoke" android:id="@+id/button"></Button>
</LinearLayout>
and code
Code:
public void sInvoke(View v) {
}
now how to invoke search dialog and how to use onSearchRequested()

rafeeq02 said:
THANKS SIR FOR UR REPLY
I HAVE CREATED THIS
xml
Code:
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="sInvoke" android:id="@+id/button"></Button>
</LinearLayout>
and code
Code:
public void sInvoke(View v) {
}
now how to invoke search dialog and how to use onSearchRequested()
Click to expand...
Click to collapse
i did this ,
Code:
public boolean sInvoke(View v) {
switch (((MenuItem) items).getItemId()) {
case LOCAL_SEARCH_ID:
onSearchRequested();
return(true);
}
return false;
}
but app is unfortunetly forse closed
sir ,how to code this properly ?

rafeeq02 said:
THANKS SIR FOR UR REPLY
I HAVE CREATED THIS
xml
Code:
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="sInvoke" android:id="@+id/button"></Button>
</LinearLayout>
and code
Code:
public void sInvoke(View v) {
}
now how to invoke search dialog and how to use onSearchRequested()
Click to expand...
Click to collapse
Well I have never tried using search bars but logically speaking, if you do whatever was being done in the switch case and use the same code in the sInvoke, it should work.
Sent from my LT26i using Tapatalk

abcdjdj said:
Well I have never tried using search bars but logically speaking, if you do whatever was being done in the switch case and use the same code in the sInvoke, it should work.
Sent from my LT26i using Tapatalk
Click to expand...
Click to collapse
thanks once again
Code:
public void sInvoke(View v) {
switch (((MenuItem) items).getItemId()) {
case LOCAL_SEARCH_ID:
onSearchRequested();
return;
}
}
now please let me know where is my mistake how solve this
still app is force closed

rafeeq02 said:
thanks once again
Code:
public void sInvoke(View v) {
switch (((MenuItem) items).getItemId()) {
case LOCAL_SEARCH_ID:
onSearchRequested();
return;
}
}
now please let me know where is my mistake how solve this
still app is force closed
Click to expand...
Click to collapse
Um why have you added a switch case here? When the user clicks the button, you only want the search bar to be invoked so all you have to do is call the onSearchRequested() function in sInvoke. Nothing else has to be done. If it is force closing, then please post the logcat too.
Sent from my LT26i using Tapatalk

abcdjdj said:
Um why have you added a switch case here? When the user clicks the button, you only want the search bar to be invoked so all you have to do is call the onSearchRequested() function in sInvoke. Nothing else has to be done. If it is force closing, then please post the logcat too.
Sent from my LT26i using Tapatalk
Click to expand...
Click to collapse
THANKS THANKS HUNDRED TIMES U HELPED ME
IT WORKED ,, IT WORKED
I LOVE YOU ABCDJDJ ,,I LOVE YOU XDA ,,
actualy sir I dont know java but i love do soming for my students so i tries like this things
if u dont mind may ask for this kind of helps?

rafeeq02 said:
THANKS THANKS HUNDRED TIMES U HELPED ME
IT WORKED ,, IT WORKED
I LOVE YOU ABCDJDJ ,,I LOVE YOU XDA ,,
actualy sir I dont know java but i love do soming for my students so i tries like this things
if u dont mind may ask for this kind of helps?
Click to expand...
Click to collapse
You're welcome
I understand that using technology to teach is an effective method of teaching. Nice idea:thumbup:
There is no problem in asking for help as long as you follow the rules of Xda and search before posting.
Also, learning Java will really help you a lot for developing android apps. In case you get some free time and feel like learning some Java, please do read a book called Head First Java. It is absolutely amazing and explains concepts somewhat like a comic book with lots of pictures etc. Oh and kindly do not call me sir. I'm still studying in school
Good luck with your development
Sent from my LT26i using Tapatalk

abcdjdj said:
You're welcome
I understand that using technology to teach is an effective method of teaching. Nice idea:thumbup:
There is no problem in asking for help as long as you follow the rules of Xda and search before posting.
Also, learning Java will really help you a lot for developing android apps. In case you get some free time and feel like learning some Java, please do read a book called Head First Java. It is absolutely amazing and explains concepts somewhat like a comic book with lots of pictures etc. Oh and kindly do not call me sir. I'm still studying in school
Good luck with your development
Sent from my LT26i using Tapatalk
Click to expand...
Click to collapse
U r so kind , u helped me here for development learning so u r my teacher ,
I saw ur contribution ,its quit great,, i m going to download the book , i love to learn c u soon,, one more question whats ur name ? take care

rafeeq02 said:
U r so kind , u helped me here for development learning so u r my teacher ,
I saw ur contribution ,its quit great,, i m going to download the book , i love to learn c u soon,, one more question whats ur name ? take care
Click to expand...
Click to collapse
You're welcome
Well revealing my real name would defeat the purpose of having a username but let me give you a hint - You can check the real names of developers by going on their Github page.
Regards,
abcdjdj
Sent from my LT26i using Tapatalk

Related

parsing links??

hey everyone
im having a lil bit of trouble here and im hoping someone will have the knowledge i need...im using eclipse trying to build an app. i have a real simple app inmind and it consists of 4 icons that will link to their own forum, but im stuck trying to figure out how to link these websites when clicked...that being said im fairly new to the java world and im just looking for a nudge in the right direction here on where/how to make these buttons work.
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
[/URL] Uploaded with ImageShack.us[/IMG]
Android uses a Uri scheme to access resources outside the scope of your program. Checkout the Uri class and the startActivity method in the Context class.
As far as what the uri needs to contain, it is very simple, just the website address you want to go to. Here is the uris needed to access google services
http://developer.android.com/guide/appendix/g-app-intents.html
From something awesome
thanks for your advice.i really wish i knew more about java; i take it all my changes will be in my java & manifest files?? i dont know what actions to take really and i need a lil bit more indepth answer on exactly what i need to add/change...
this example assumes you used an xml layout file called http_button_layout.xml and has a button inside called button1
Code:
/**
* Created by IntelliJ IDEA.
* User: Tyler
* Date: 5/10/11
* Time: 6:47 PM
*/
public class HTTPClickButton extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
//call the parents onCreate
super.onCreate(savedInstanceState);
//set the view to some xml layout file with a button in it
setContentView(R.layout.http_button_layout);
//get an instance of that button to be used in the code
Button launchWebPage = (Button)findViewById(R.id.button1);
//attach an onClickListener to the button and define its action
launchWebPage.setOnClickListener(new View.OnClickListener() {
//the action to partake upon pressage of said button
public void onClick(View view) {
//the Uri.parse() method takes a string and converts it to a uri that android knows what to do with.
//in this case it sees a link and launches the web browser
//changing Intent.ACTION_VIEW will change the behavior of the link
//Intent.ACTION_WEB_SEARCH is the other option
startActivity(new Intent( Intent.ACTION_VIEW , Uri.parse("http://www.fistfulofneurons.com/") ));
}
});
}
}
hope this helps. of course this is one button but it will work for more. just rinse and repeat
to answer that i haven't made an xml layout file yet called http_button_layout.xml but i take it i will have to make an xlm layout file and java class for each icon? sorry supernewb when it comes to this code stuff, almost like learning a new language,lol...
you dont have to. but you will have to perform the .setOnClickListener() on each button.
are you using the Button class or is it just an image or someother class? cause it doesnt matter if it is a Button or not. even if its a TextView you can still perform the .setOnClickListener() i believe it is a function defined in the View abstract class.
im using an imagebutton option, fyi....dont know how much that changes things here tho...
You are golden. Since you are using java as the layout you just need to perform the setOnClickListener() on a reference of your ImageButton and forget anything i said about xml. Although you should investigate xml for layouts as its alot easier and faster to make a layout
You mind posting what you have so far to generate that screen grab up top?
From something awesome
HTML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/xlapps"
>
<ImageButton
android:id="@+id/imageButton1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/xlnet"
></ImageButton>
<ImageButton
android:id="@+id/imageButton2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/xlnet"
></ImageButton>
<ImageButton
android:id="@+id/imageButton3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/xlnet"
></ImageButton>
<ImageButton
android:id="@+id/imageButton4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/xlnet"
></ImageButton>
</LinearLayout>
this is my main xml if this answers your question about the screen grab? rightnow i only have my main app icon, i need to get my 4 other icons inserted but other than that this parsing is my only hurdle, really..
lol now im confused, so does that mean i only need to do this part of your code in java, and does that mean i don't have to make a button_layout.xml? lol sorry java's not my first langauge, they totally should of taught this in school
Code:
//attach an onClickListener to the button and define its action
launchWebPage.setOnClickListener(new View.OnClickListener() {
//the action to partake upon pressage of said button
public void onClick(View view) {
//the Uri.parse() method takes a string and converts it to a uri that android knows what to do with.
//in this case it sees a link and launches the web browser
//changing Intent.ACTION_VIEW will change the behavior of the link
//Intent.ACTION_WEB_SEARCH is the other option
startActivity(new Intent( Intent.ACTION_VIEW , Uri.parse("http://www.fistfulofneurons.com/") ));
}
});
K so you are using an xml layout. Is that called main.xml? In my example you would replace my R.layout.http_button_layout with R.layout.main
And then when referencing the buttons you would change my R.id.button1 to the @+id of each button in your layout. So it would be R.id.imagebutton1.
The R class is a class generated at compile time to help reference ui elements defined in xml from your java code
And i don't think you can have ids defined in xml with capital letters. So you might want to change imageButton1 to image_button1
From something awesome
awesome yea im using main.xml for the layout.... so all i need to do is set up 1 java class for all the icons and do all your code there?...
well i started a new java class, i just called it buttons, im not sure if thats the right way to do all this but i changed what you said and im getting alot less errors so thats good.lol but heres a look at my buttons.java file. any idea what could be wrong? it showed my imagebutton1 of (R.id.imagebutton1) and image_button1 in the (find view by id) could not be resolved or is not a field...
Code:
package com.frostXLNetwork;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class buttons {
public class HTTPClickButton extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
//call the parents onCreate
super.onCreate(savedInstanceState);
setContentView(R.id.imagebutton1);
Button launchWebPage = (Button)findViewById(R.id.image_button1);
launchWebPage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent( Intent.ACTION_VIEW , Uri.parse("http://xlfx.30.forumer.com/index.php?") ));
}
you are making it harder than it is. when you see what has to be done you'll kick yourself... =)
i cleaned up some of the tags in your main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/xlapps">
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xlfx"
android:id="@+id/XLFx"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xlenlightenment"
android:id="@+id/XLEnlightenment"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xlfit"
android:id="@+id/XLFit"/>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stylelife"
android:id="@+id/XLife"/>
</LinearLayout>
and your main activity called XLNetwork.java
Code:
public class XLNetwork extends Activity {
//Define some String Constants that will point to the website of your choice
public static final String XLF_X = "http://xlfx.30.forumer.com/index.php?";
public static final String XL_ENLIGHTENMENT = "http://www.facebook.com/enlightenment2011?ref=ts";
public static final String XL_FIT = "http://xlfx.30.forumer.com/index.php?";
public static final String X_LIFE = "http://xlfx.30.forumer.com/index.php?";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton b1 = (ImageButton) findViewById(R.id.XLFx);
linkOnButtonPress(b1, XLF_X);
ImageButton b2 = (ImageButton) findViewById(R.id.XLEnlightenment);
linkOnButtonPress(b2, XL_ENLIGHTENMENT);
ImageButton b3 = (ImageButton) findViewById(R.id.XLFit);
linkOnButtonPress(b3, XL_FIT);
ImageButton b4 = (ImageButton) findViewById(R.id.XLife);
linkOnButtonPress(b4, X_LIFE);
}
/*
* linkOnButtonPress()
*
* i made this so i wouldnt have to type the same process over and over.
* what gets passed is the reference to the button you want, and the web address you
* want the button to link to.
*
*/
private void linkOnButtonPress(ImageButton button, final String webSite) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent( Intent.ACTION_VIEW , Uri.parse(webSite) ));
}
});
}
}
i cant thank you enough, you were a lifesaver!!

How to launch an Activity on ListView Item click android

hello
I am new to android application development and i am developing and application for learning purpose.
Can any body please help me in creating an activity that launch and activity on ListView Item Click.
I am using Following code for Populating Listview Items.
filename: activity_menu.java
package com.my.android.app;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class activity_menu extends Activity {
String[] countries = new String[] {
"Contacts",
"SMS",
"Files & Photos",
"Prefrences",
"Logout",
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.phonebook,
R.drawable.sms,
R.drawable.filesphotos,
R.drawable.settings,
R.drawable.key,
};
// Array of strings to store currencies
String[] currency = new String[]{
"Manage Contacts Backup",
"Manage SMS Backup",
"Manage files & Photos Backup",
"Set your prefrences",
"Logout of Application",
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<5;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", countries);
hm.put("cur", currency);
hm.put("flag", Integer.toString(flags) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
Please help on this. waiting for Responses.
Try this
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
// Do your action here
}
});
There are multiple ways. If using a ListFragment/ListActivity, onListItemClick can also be used. However, I think that that is used especially with a programmatically-defined ListView.
If you want to start a new Activity, do some research about Intents.
SagiLo said:
Try this
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
// Do your action here
}
});
Click to expand...
Click to collapse
This. Use the position parameter if you want it to only open when a particular item is clicked (It is zero-based, if I remember correctly)
Then, to open the Intent:
Code:
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
Opening on any listitem click:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
});
Opening on a specific item:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
how so i implement the code to open new activity on row cick?
cyr0s said:
This. Use the position parameter if you want it to only open when a particular item is clicked (It is zero-based, if I remember correctly)
Then, to open the Intent:
Code:
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
Opening on any listitem click:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
});
Opening on a specific item:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
Click to expand...
Click to collapse
How do i implement this code in my main activity's java file?
suppose, i have a main activity which contains the listview and i have another activity "Activity_game"
and i want to open Activity_game on clicking the first row,
so i put "0" at "ITEM_POSITION_HERE" and "game" at the place of "ActivityName"
but it gives my a bunch of errors and it doesn't work
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
nnnn1111 said:
How do i implement this code in my main activity's java file?
suppose, i have a main activity which contains the listview and i have another activity "Activity_game"
and i want to open Activity_game on clicking the first row,
so i put "0" at "ITEM_POSITION_HERE" and "game" at the place of "ActivityName"
but it gives my a bunch of errors and it doesn't work
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
Click to expand...
Click to collapse
We need to know which errors you get.
Read this: http://forum.xda-developers.com/showthread.php?t=2439748
one question
nikwen said:
We need to know which errors you get.
Read this: http://forum.xda-developers.com/showthread.php?t=2439748
Click to expand...
Click to collapse
my problem is , when i run the app, it doesn't show any rows , not even any listview , or string
(just blank white page)
i have two activities
first one is Activity_main.xml in which ive implemented listview
here is it's java file
package com.example.desi;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
public class MainActivity extends Activity {
String[] items = { "item 1", "item 2", "item 3", "item 4", "item 5" };
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(this, Munda.class);
startActivity(newActivity);
break;
}
}
}
and another activity is Activity_munda.xml
so, i want to run "Activity_munda" through on row click of listview
i also want to have many activities later in my app
my project is error free right now
do you think i have incomplete code?
nnnn1111 said:
my problem is , when i run the app, it doesn't show any rows , not even any listview , or string
(just blank white page)
i have two activities
first one is Activity_main.xml in which ive implemented listview
here is it's java file
package com.example.desi;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
public class MainActivity extends Activity {
String[] items = { "item 1", "item 2", "item 3", "item 4", "item 5" };
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(this, Munda.class);
startActivity(newActivity);
break;
}
}
}
and another activity is Activity_munda.xml
so, i want to run "Activity_munda" through on row click of listview
i also want to have many activities later in my app
my project is error free right now
do you think i have incomplete code?
Click to expand...
Click to collapse
Your code is incomplete. You haven't populated the layout "activity_main.xml" here. and haven't added the list view, So it is indeed correct the app displays a blank page with no errors.
vijai2011 said:
Your code is incomplete. You haven't populated the layout "activity_main.xml" here. and haven't added the list view, So it is indeed correct the app displays a blank page with no errors.
Click to expand...
Click to collapse
Right. Read this: http://www.vogella.com/articles/AndroidListView/article.html
i already tried that
vijai2011 said:
Your code is incomplete. You haven't populated the layout "activity_main.xml" here. and haven't added the list view, So it is indeed correct the app displays a blank page with no errors.
Click to expand...
Click to collapse
but with no luck
its very complicated
can you please give me a simple tutorial ?
please dont refer to any other tutorial as i have already tried all of them,
thanks
nnnn1111 said:
but with no luck
its very complicated
can you please give me a simple tutorial ?
please dont refer to any other tutorial as i have already tried all of them,
thanks
Click to expand...
Click to collapse
Come on, you haven't tried all of them. Please don't let everyone else do the work for you. And if you can manage to do it yourself, it will be a better learning experience.
I can offer that you try the tutorial I posted and if your code doesn't work, you can post it together with a logcat and we'll check it.
haha i know
nikwen said:
Come on, you haven't tried all of them. Please don't let everyone else do the work for you. And if you can manage to do it yourself, it will be a better learning experience.
I can offer that you try the tutorial I posted and if your code doesn't work, you can post it together with a logcat and we'll check it.
Click to expand...
Click to collapse
but ive been searching for like 15 days about this problem on stack overflow , saw like 30 posts but they arent clear , please do me a favor and
tell me in few steps you will also feel good helping me.
and it will be a VERY big favor to me , trust me
nnnn1111 said:
but ive been searching for like 15 days about this problem on stack overflow , saw like 30 posts but they arent clear , please do me a favor and
tell me in few steps you will also feel good helping me.
and it will be a VERY big favor to me , trust me
Click to expand...
Click to collapse
Read this about ListActivity. It should answer your questions: http://www.vogella.com/articles/AndroidListView/article.html#listactivity
And I won't post step by step guides on how to implement ListViews. There are many great tutorials out there. I can help you if you have problems/errors with your code, but @vijai2011 has already explained what you need to do.
You cannot expect anyone to spoon feed you. We are here to help people who try to do something but run into problems. Not people who doesnt even try to understand what they do. The guides by vogella are very simple and there is no problem with the guide. You cannot simply copy paste the codes from the tutorial and expect it to compile and work.
vijai2011 said:
You cannot expect anyone to spoon feed you. We are here to help people who try to do something but run into problems. Not people who doesnt even try to understand what they do. The guides by vogella are very simple and there is no problem with the guide. You cannot simply copy paste the codes from the tutorial and expect it to compile and work.
Click to expand...
Click to collapse
i didn't find my answer on that website
nnnn1111 said:
i didn't find my answer on that website
Click to expand...
Click to collapse
Then it may mean that you dont know what you are searching for! Because the tutorial is all about what you need - ListView!

LinearLayout Hyperlink

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

(FIXED)Dismiss RecentsDialog/Add Back button function in OnClick method.

Hello guys/girls.
i have been trying for a couple days to do something
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
as u can see i have three buttons in th bottom.
when i press on them it opens an application but behind the recents menu.
i would like to close the recents menu However i have tried to figure it out bit till now no solution found :/
I thought of either call/launch the close-system_dialog intent/broadcast i found in androids recentsapplicationdialog source but i couldnt find proper way or method to add.
then i wanted to try and add a Back button keycode/event but didnt really knew how to implement it.
Here is my code :
Code:
package com.example.recentsmenu;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SearchButton extends Button{
public SearchButton(final Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
try{
v.getContext().startActivity((new Intent("android.search.action.GLOBAL_SEARCH")).setFlags(0x10000000 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
catch(Exception ex){
Toast.makeText(context, "Not Installed/Working.",
Toast.LENGTH_LONG).show();
}
}
});
}
}
if someone could help please do
thank you
greetings spacecaker.
It is working sir ? But the only problem you are encountering sir is the it is at the back. As I can see, you need to make a new method pertaining that it will trigger to close the layout of recent dialog.
make a method that will close the layout, dont know how exactly
Code:
public class SearchButton extends Button{
public SearchButton(final Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
try{
v.getContext().startActivity((new Intent("android.search.action.GLOBAL_SEARCH")).setFlags(0x10000000 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
catch(Exception ex){
Toast.makeText(context, "Not Installed/Working.",
Toast.LENGTH_LONG).show();
}
}
});
}
collapselayout(); //i think this is
}
At the end of your onClick method in Java add
Code:
finish();
This is what I did in the redundant activity that opened in the Jelly Bean Easter Egg I ported
But it should be an activity where your method is, if its a Service, you must use
Code:
Activity act = new Activity();
act.finish();
Where, Activity is the Activity name (Activity.class)
If it's a Fragment (Though I know it isn't)
Code:
getActivity().finish();
PineappleOwl said:
It is working sir ? But the only problem you are encountering sir is the it is at the back. As I can see, you need to make a new method pertaining that it will trigger to close the layout of recent dialog.
make a method that will close the layout, dont know how exactly
Code:
public class SearchButton extends Button{
public SearchButton(final Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
try{
v.getContext().startActivity((new Intent("android.search.action.GLOBAL_SEARCH")).setFlags(0x10000000 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
}
catch(Exception ex){
Toast.makeText(context, "Not Installed/Working.",
Toast.LENGTH_LONG).show();
}
}
});
}
collapselayout(); //i think this is
}
Click to expand...
Click to collapse
Nope also isnt :/
it says this
The button works it opens the apps and such
but it wont close the recents dialog.
iamareebjamal said:
At the end of your onClick method in Java add
Code:
finish();
This is what I did in the redundant activity that opened in the Jelly Bean Easter Egg I ported
But it should be an activity where your method is, if its a Service, you must use
Code:
Activity act = new Activity();
act.finish();
Where, Activity is the Activity name (Activity.class)
If it's a Fragment (Though I know it isn't)
Code:
getActivity().finish();
Click to expand...
Click to collapse
But its neither a Activity or A Service.
its a Button
extends Button
SpaceCaker said:
Nope also isnt :/
it says this View attachment 2542371
The button works it opens the apps and such
but it wont close the recents dialog.
Click to expand...
Click to collapse
So uyou use custom button sir, hihi I check the android source linked at the 1st post and I saw this
Code:
public void onClick(View v) {
for (TextView b: mIcons) {
if (b == v) {
// prepare a launch intent and send it
Intent intent = (Intent)b.getTag();
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
try {
getContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w("Recent", "Unable to launch recent task", e);
}
}
break;
}
}
dismiss();
}
So adding
RecentApplicationDialog spacecakerisawesome = new RecentApplicationDialog;
Then
In your custom button
Add these
spacecakerisawesome.onClick(); or any other method name
So we can set it as
Code:
public void onClickSearch(View v) {
// intent thingy sir here
dismiss();
}
And you can use the method in your custom view sir
Hope it helps. Hehe
Sent from my Nexus 7 using Tapatalk
Why are you even extending a button? I don't see the any reason behind it.
Import recentdialog;
public class SearchButton extends Button{
Recentdialog x = new Recentdialog;
public SearchButton(final Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
x.onClickSearch();
}
});
}
}
Try is. Hihii
Sent from my Nexus 7 using Tapatalk
---------- Post added at 11:55 PM ---------- Previous post was at 11:54 PM ----------
EatHeat said:
Why are you even extending a button? I don't see the any reason behind it.
Click to expand...
Click to collapse
I think he need it sir for a new button. XD
Sent from my Nexus 7 using Tapatalk
EatHeat said:
Why are you even extending a button? I don't see the any reason behind it.
Click to expand...
Click to collapse
i need a Button
otherwise i would use ImageView.
for the recentsdialog. to add the 3 quickbuttons at bottom to launch apps or such
it works but it doesnt hide recentsdialog.
Sent from my S500 using xda app-developers app
SpaceCaker said:
i need a Button
otherwise i would use ImageView.
for the recentsdialog. to add the 3 quickbuttons at bottom to launch apps or such
it works but it doesnt hide recentsdialog.
Sent from my S500 using xda app-developers app
Click to expand...
Click to collapse
Something wrong with ImageButton? :silly:
EatHeat said:
Something wrong with ImageButton? :silly:
Click to expand...
Click to collapse
Never used ImageButton xD
i think its same as Button or ImageView
Sent from my S500 using xda app-developers app
SpaceCaker said:
Never used ImageButton xD
i think its same as Button or ImageView
Sent from my S500 using xda app-developers app
Click to expand...
Click to collapse
1. What you are trying to do can be done by using an ImageView/ImageButton. You don't need to extend extra classes.
2. That's an activity. Just call finish() before passing your search intent.
3. Profit.
EatHeat said:
1. What you are trying to do can be done by using an ImageView/ImageButton. You don't need to extend extra classes.
2. That's an activity. Just call finish() before passing your search intent.
3. Profit.
Click to expand...
Click to collapse
Tried the ImageView / ImageButton but same error on the finish(); tag
also i am working on GB 2.3.6 and not ICS or Jb :/
gonna try to implement this .
try{
Object service = context.getSystemService("ACTIVITY_SERVICE");
Class<?> ActivityManager = Class.forName("android.app.Dialog");
Method collapse = ActivityManager.getMethod("collapse");
collapse.invoke(service);
}
catch(Exception ex){
Toast.makeText(context, "Dialog not closed.",
Toast.LENGTH_LONG).show();
}
Click to expand...
Click to collapse
nope didnt worked damn :/
Sent from my S500 using xda app-developers app
For GB, the recents is a dialog. Close the dialog using the following intent. This should work if its a dialog.
Code:
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Will try that
Sent from my S500 using xda app-developers app
also didnt worked :/ damn
Sent from my S500 using xda app-developers app
SpaceCaker said:
also didnt worked :/ damn
Sent from my S500 using xda app-developers app
Click to expand...
Click to collapse
Did you send a broadcast with that intent? Send a broadcast and then try it.
could yo give me a sample of how / whwre i should put the broadcast?
wheni added code it displayed yello line under closedialog
Sent from my S500 using xda app-developers app
SpaceCaker said:
could yo give me a sample of how / whwre i should put the broadcast?
wheni added code it displayed yello line under closedialog
Sent from my S500 using xda app-developers app
Click to expand...
Click to collapse
I implemented these 3 on ics
They worked by using dismiss(); though i dont know how to do it on gb.
Though i have not seen the gb recentspanel view.
There is a suggestion
Search if there is a variable
Like dialog x or alertdialog xy etc.
If it is present.try to Dismiss it by 1of them
xy.dismiss();
Xy.close();
Xy.hide();
Xy.hide(true);
But if there isnt then i do not know anything about it
Edit-are u using eclipse.if yes plz tell me how u compile system apps on it
Sent from my GT-S5570 using XDA Premium 4 mobile app

[Guide] Listeners in Java development

You are new to java development and want to get buttons working?
Maybe you are a Pro but want a reminder?
whatever you are this Guide is to help you to make buttons/check boxes...etc working and functional
Some people are distracted between guides over internet and want the easiest way to get their project working, me too
Steps :
1-Define the button :
Code:
Button btn1;
Checkbox chkbox1;
RadioButton radio1;
2- Intialize it :
Code:
btn1 = (Button) findViewById(R.id.btn1);
chkbox1= (Checkbox ) findViewById(R.id.chkbox1);
radio1= (RadioButton) findViewById(R.id.radio1);
3-Add the listener :
Button:
Code:
btn1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
//Write awesome code here
}
});
CheckBox :
Code:
chkbox1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (start.isChecked()) {
//if the checkbox checked
} else {
//if not checked
}
}
});
}
radio button:
Code:
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio1:
if (checked){
}
else{
}
break;
}
}
or use it in a radio Group :
Code:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio1:
if (checked)
//Write code
break;
case R.id.radio2:
if (checked)
//Write code
break;
}
}
Also insted of this you can use a onCheckedChanged for a radio button (Thanks for GalaxyInABox)
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
____________________________________________________________________
____________________________________________________________________
Also you can implement a Onclicklistener for the whole class to save resources : (thanks for @Jonny )
after defining and initializing your objects add this :
Code:
OnClickListener click_listener = new OnClickListener() {
public void onClick(View view) {
int id = view.getId();
if (id == your_id) {
//do stuff for this object
} else if (id == your_id2) {
//do other stuff for diffrent object
} else if (id == your_id3) {
//and so on
}
}
};
To do list :
-add on touch listeners
-add on drag listeners
Note : you can add a click listener to almost any thing (Textview or imageView or even EditText) just using the same method of adding listener to button
also there is some other ways to add a listener but this is the fastest and less disturbing :good:
If this guide is useful, press thanks
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?
Sent from my GT-S5302 using Tapatalk 2
sak-venom1997 said:
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?
Click to expand...
Click to collapse
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.
And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
GalaxyInABox said:
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.
Click to expand...
Click to collapse
I meant that the op shuld edit this guide and use those instead of OnCickListeners
GalaxyInABox said:
And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
Click to expand...
Click to collapse
ya new with java8 it will be a nice usage scenario of lambadas
Sent from my GT-S5302 using Tapatalk 2
GalaxyInABox said:
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.
And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
Click to expand...
Click to collapse
sak-venom1997 said:
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
ok, i will add this
You can also add onClick property in XML and then handle it in a code.
Awesome tutorial! Thank you very much!
Please, you could share more related knowledge. It's really useful!
Also, an activity can be a listener. In this case:
MyActivity implements onClickListener {
btn1.setOnClickListener(this);
public void onClick (View v) {
//your code
}
}
For this kind of stuff, using some well known libraries from well known Android dev is a must.
https://github.com/JakeWharton/butterknife
Very powerfull, super easy to use, error prone and without any performance impact.
rafalniski said:
You can also add onClick property in XML and then handle it in a code.
Click to expand...
Click to collapse
SKAm69 said:
Also, an activity can be a listener. In this case:
MyActivity implements onClickListener {
btn1.setOnClickListener(this);
public void onClick (View v) {
//your code
}
}
Click to expand...
Click to collapse
will add them both, although I don't like this way
Mohamedrashad. Thanks a lot.
Sent from my P880 using Tapatalk
If you have multiple clickable objects then it's best to use just 1 onClickListener for all of them and use a switch on their ID's. This reduces resource usage as you only have 1 listener, not 5, 10 or however many you would have otherwise. It's not essential for this but it is a best practice if you want to streamline your code.
Mobile right now so I can't chuck up an example until tomorrow evening or so.
You dude had a great thread. Its helping me. Bravoo !!
Sent from my GT-I8190 using XDA Premium 4 mobile app
As @Jonny already pointed out: Use your class as a listener instead of creating a new (anonymous) inner class! Say you have a ListView, instead of doing this:
Code:
class MyFragment extends Fragment {
private void someMethod() {
((ListView) getView().findViewById(R.id.someListView)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Code...
}
});
}
}
you can do this:
Code:
class MyFragment extends ListFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
private void someMethod() {
((ListView) getView().findViewById(R.id.someListView)).setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Code...
}
}
This may look stupid, but when you have many listeners, you can un-clutter it. In my opinion this is the best way. You can also add "this" class as listener for as many ui elements as you want(because all of them extend view, you can use one OnClickListener), then you only need to have a switch statement to distinguish between the views. And voila, you prevented cluttering the code with boilerplate stuff.
Example using code in an app I'm making - app for my school.
Code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Some code here for view/layouts etc
websitebutton = (Button) view.findViewById(R.id.website_btn);
facebookbutton = (Button) view.findViewById(R.id.facebook_btn);
twitterbutton = (Button) view.findViewById(R.id.twitter_btn);
websitebutton.setOnClickListener(handler);
facebookbutton.setOnClickListener(handler);
twitterbutton.setOnClickListener(handler);
return view;
}
OnClickListener handler = new OnClickListener() {
public void onClick(View view) {
switch (view.getId()) {
case R.id.website_btn :
Uri website = Uri.parse("http://wirralgrammarboys.com/");
Intent websiteintent = new Intent(Intent.ACTION_VIEW, website);
startActivity(websiteintent);
break;
case R.id.facebook_btn :
Uri facebook = Uri.parse("https://www.facebook.com/WirralGSB");
Intent facebookintent = new Intent(Intent.ACTION_VIEW, facebook);
startActivity(facebookintent);
break;
case R.id.twitter_btn :
Uri twitter = Uri.parse("https://twitter.com/WGSB");
Intent twitterintent = new Intent(Intent.ACTION_VIEW, twitter);
startActivity(twitterintent);
break;
}
}
};
Jonny said:
Example using code in an app I'm making.
Code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Some code here for view/layouts etc
websitebutton = (Button) view.findViewById(R.id.website_btn);
facebookbutton = (Button) view.findViewById(R.id.facebook_btn);
twitterbutton = (Button) view.findViewById(R.id.twitter_btn);
websitebutton.setOnClickListener(handler);
facebookbutton.setOnClickListener(handler);
twitterbutton.setOnClickListener(handler);
return view;
}
OnClickListener handler = new OnClickListener() {
public void onClick(View view) {
int id = view.getId();
if (id == R.id.website_btn) {
Uri website = Uri.parse("http://wirralgrammarboys.com/");
Intent websiteintent = new Intent(Intent.ACTION_VIEW, website);
startActivity(websiteintent);
} else if (id == R.id.facebook_btn) {
Uri facebook = Uri.parse("https://www.facebook.com/WirralGSB");
Intent facebookintent = new Intent(Intent.ACTION_VIEW, facebook);
startActivity(facebookintent);
} else if (id == R.id.twitter_btn) {
Uri twitter = Uri.parse("https://twitter.com/WGSB");
Intent twitterintent = new Intent(Intent.ACTION_VIEW, twitter);
startActivity(twitterintent);
}
}
};
Click to expand...
Click to collapse
i'm adding this to OP if you don't mind jonny
mohamedrashad said:
i'm adding this to OP if you don't mind jonny
Click to expand...
Click to collapse
That's fine - if I didn't want people to use/adapt/learn from the code then I wouldn't put it up, use it as you want :good:
Sent from my HTC One using Tapatalk
Keep it up
Great tutorials, keep em coming!
Hey what about starting a new activity with onClickListiner
Sent from my M3S_D7 using XDA Free mobile app
---------- Post added at 03:57 PM ---------- Previous post was at 03:49 PM ----------
Hey and do u mind sending a source codes.zip file
Sent from my M3S_D7 using XDA Free mobile app
Rebound.co said:
Hey what about starting a new activity with onClickListiner
Sent from my M3S_D7 using XDA Free mobile app
---------- Post added at 03:57 PM ---------- Previous post was at 03:49 PM ----------
Hey and do u mind sending a source codes.zip file
Sent from my M3S_D7 using XDA Free mobile app
Click to expand...
Click to collapse
in the onClick method just have this code:
Code:
startActivity(new Intent(this, YourActivity.class));

Categories

Resources