LinearLayout Hyperlink - Java for Android App Development

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

Related

New Developer - Need Some Advise on RSS Code

Hello,
I'm new to developing software in Java, but especially with Android. I am going through a tutorial on how to create an RSS Feed Reader and so far I've got it mostly working, but I've got a few bugs I can't seem to figure out.
I wanted to post images, but it wouldn't let me, but anyway:
main activity - title, pubdate both work, as does each article's title.
after clicking on a title, the other activity is launched and the article is shown:
showdescription activity is launched - title, pubdate, link all work and display correctly.
bug: description does not always display everything or even display at all depending on the xml source.
I'm wondering if my problem is somewhere these code snippets, as this tutorial was written some time ago.
These are pretty basic, so I'm doubting the problem is in here.
RSSHandler said:
if (localName.equals("description"))
{
currentstate = RSS_DESCRIPTION;
return;
}
...
case RSS_DESCRIPTION:
_item.setDescription(theString);
currentstate = 0;
break;
Click to expand...
Click to collapse
RSSItem said:
void setDescription(String description)
{
_description = description;
}
...
String getDescription()
{
return _description;
}
Click to expand...
Click to collapse
This is where I believe the problem exists, in the onItemClick method or the ShowDescription class.
RSSReader said:
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
Intent itemintent = new Intent(this,ShowDescription.class);
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("link", feed.getItem(position).getLink());
b.putString("pubdate", feed.getItem(position).getPubDate());
itemintent.putExtra("android.intent.extra.INTENT", b);
//startSubActivity(itemintent, 0);
startActivity(itemintent);
}
Click to expand...
Click to collapse
ShowDescription said:
public class ShowDescription extends Activity
{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.showdescription);
String theStory = null;
Intent startingIntent = getIntent();
if (startingIntent != null)
{
Bundle b = startingIntent.getBundleExtra("android.intent.extra.INTENT");
if (b == null)
{
theStory = "bad bundle?";
}
else
{
theStory = b.getString("title")
+ "\n\n" + b.getString("pubdate")
+ "\n\n" + b.getString("description")
+ "\n\nView Website:\n" + b.getString("link");
}
}
else
{
theStory = "Information Not Found.";
}
TextView db= (TextView) findViewById(R.id.storybox);
db.setText(theStory);
Button backbutton = (Button) findViewById(R.id.back);
backbutton.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
}
}
Click to expand...
Click to collapse
Originally the tutorial called for "startSubActivity" for starting the ShowDescription activity, but that doesn't seem to exist anymore?
The XML source I'm using can't be displayed because I can't link in outside pages, but I can PM it to you if you'd like.
I have tried using a .replaceAll("\\<.*?>","") but it didn't seem to change much about whether the <description> tag's content is displayed or not.
Anyway, if anyone takes the time to look at this, it'd be greatly appreciated, and thank you!
If you need me to post anymore code let me know.
So I think I've figured it out, well somewhat.
While stepping through the code, the <description> tag's content consists of a single < character. Then the parser goes through two more tags that contain, p and > before actually hitting the content I want.
So it looks something like this:
<description>
<
<?>
p
<?>
>
<?>
"wanted content"
</?>
</?>
</?>
</description>
Click to expand...
Click to collapse

WebView - Handle JavaScript Pop Up ?

Hi,
I am new to Android Development. I am making an application with a WebView object. This loads a URL with some elements. However, I cannot get JavaScript PopUp to show up when the button is pressed.
My WebView has JavaScript enabled, but all other properties are disabled.
What is the property to enable WebView popups to show??
I had to do this. Override the onJSAlert() method in the WebChromeClient class:
Code:
public class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result)
{
final JsResult finalRes = result;
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finalRes.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
}
Then add it to your webview:
Code:
MyWebChromeClient myWebChromeClient = new MyWebChromeClient();
webView.setWebChromeClient(myWebChromeClient);
Thanks. That did work!
One small thing, how do you set a Title for the pop-ups. Currently it shows the url in the Title.
I found a better example that sets the title:
http://lexandera.com/2009/01/adding-alert-support-to-a-webview/
footboydog said:
I found a better example that sets the title:
http://lexandera.com/2009/01/adding-alert-support-to-a-webview/
Click to expand...
Click to collapse
I tried that, but the Title is only displayed when the alert has one button/option. When there are multiple options, the Title shows the URL.
Sorry not sure how to fix that
footboydog said:
Sorry not sure how to fix that
Click to expand...
Click to collapse
Ok, I think I'll have to live with "file:///" showing as the Title for now...
you can convert to toast

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!

How To Layout This Kind of Activity?

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.

[Tutorial] Learn to create a Matrix Effect

Hello,
I create that thread to present you a tutorial learning you how to create a Matrix Effect, also known as Digital Rain Effect, on Android. The tutorial is also available as a Youtube video :
Create a Matrix Effect on Android
Also know as Digital Rain Effect, the Matrix Effect is a classic effect featured in the Matrix series films. A green code is falling and represents the activity of the virtual reality environment of the Matrix on screen. On Android, Matrix Effect has been implemented in various applications often as a Live Wallpaper. In that tutorial, you are going to learn how to create a simple Matrix Effect.
1. Create a Matrix Effect custom View
First, you need to create a custom view named MatrixEffect :
Code:
package com.ssaurel.digitalraineffect;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import java.util.Random;
/**
* Created by ssaurel on 22/09/2016.
*/
public class MatrixEffect extends View {
private static final Random RANDOM = new Random();
private int width, height;
private Canvas canvas;
private Bitmap canvasBmp;
private int fontSize = 40;
private int columnSize;
private char[] cars = "+-*/!^'([])#@&?,=$€°|%".toCharArray();
private int[] txtPosByColumn;
private Paint paintTxt, paintBg, paintBgBmp, paintInitBg;
public MatrixEffect(Context context, AttributeSet attrs) {
super(context, attrs);
paintTxt = new Paint();
paintTxt.setStyle(Paint.Style.FILL);
paintTxt.setColor(Color.GREEN);
paintTxt.setTextSize(fontSize);
paintBg = new Paint();
paintBg.setColor(Color.BLACK);
paintBg.setAlpha(5);
paintBg.setStyle(Paint.Style.FILL);
paintBgBmp = new Paint();
paintBgBmp.setColor(Color.BLACK);
paintInitBg = new Paint();
paintInitBg.setColor(Color.BLACK);
paintInitBg.setAlpha(255);
paintInitBg.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
canvasBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(canvasBmp);
canvas.drawRect(0, 0, width, height, paintInitBg);
columnSize = width / fontSize;
txtPosByColumn = new int[columnSize + 1];
for (int x = 0; x < columnSize; x++) {
txtPosByColumn[x] = RANDOM.nextInt(height / 2) + 1;
}
}
private void drawText() {
for (int i = 0; i < txtPosByColumn.length; i++) {
canvas.drawText("" + cars[RANDOM.nextInt(cars.length)], i * fontSize, txtPosByColumn[i] * fontSize, paintTxt);
if (txtPosByColumn[i] * fontSize > height && Math.random() > 0.975) {
txtPosByColumn[i] = 0;
}
txtPosByColumn[i]++;
}
}
private void drawCanvas() {
canvas.drawRect(0, 0, width, height, paintBg);
drawText();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(canvasBmp, 0, 0, paintBgBmp);
drawCanvas();
invalidate();
}
}
First, we define the variables for the Matrix Effect with the size of the code, the size of a column, the position of the bottom text for each column and then the caracters that will be used for the code. Note that you can put the caracters you want here or why not a custom font.
In the constructor, we initialize the paint objects. To get view width and height, we override the onSizeChanged method of the View. We initialize the position of the first caracter for each column. We use a random position between the top of the screen and the middle of the screen.
Then, we define a draw text method used to draw a random caracter for each column at the position indicated by txtPosByColumn variable.
The drawCanvas method is used to draw the background of our view and then the code.
Finally, we override the onDraw method of our custom view to call the drawCanvas method and invalidate the view to force a redraw. With that call, the Matrix Effect could progress from top to bottom in infinite mode. Note that it is not the most optimized way to manage a Matrix Effect, but for a tutorial it's sufficient.
2. Define the MatrixEffect View on the Layout
Now, we can define the MatrixEffect View on the layout of the Main Activity.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ssaurel.digitalraineffect.MainActivity">
<com.ssaurel.digitalraineffect.MatrixEffect
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
3. Set the layout on the Main Activity
Last step is to set the layout as content view for the Main Activity.
Code:
package com.ssaurel.digitalraineffect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
4. Demo
To enjoy the Matrix Effect, you have just to launch the application on an Android device and you sould see the following effect :
Don't hesitate to give it a try and give me your feedbacks.
Thanks.
Sylvain
Nice work mate, will give it a try.
Sent from my SM-G935F using XDA-Developers mobile app
silverrum said:
Nice work mate, will give it a try.
Sent from my SM-G935F using XDA-Developers mobile app
Click to expand...
Click to collapse
Thanks
I really like this animated wallpaper app.
Thanks for the detailed tutorial, it's always nice to see actually what the code does. :highfive:
In theory, would it be possible to take smali from the app you create and use it to create this effect on the notification screen (for example)?
Seeing the code I’m sure this app drain a lot of battery but it's very nice.
Ticklefish said:
Thanks for the detailed tutorial, it's always nice to see actually what the code does. :highfive:
In theory, would it be possible to take smali from the app you create and use it to create this effect on the notification screen (for example)?
Click to expand...
Click to collapse
Thanks.
Did you mean put the effect on the lock screen ? I thought it was not possible now to put live wallpaper on lock screen. If it's possible it could be great.
slnit said:
Seeing the code I’m sure this app drain a lot of battery but it's very nice.
Click to expand...
Click to collapse
For sure . But life is a choice, like in Matrix films, cool effect on your screen or save your battery lol
sylsau said:
Thanks.
Did you mean put the effect on the lock screen ? I thought it was not possible now to put live wallpaper on lock screen. If it's possible it could be great.
Click to expand...
Click to collapse
Should be possible to pull the AOSP lockscreen source (or the source from OEM variants that are actually available...) and replace the wallpaper code, right?
Interesting , definitely going to try it on Samsung TW later , great idea mate ..
thereassaad said:
Interesting , definitely going to try it on Samsung TW later , great idea mate ..
Click to expand...
Click to collapse
Thanks
Thanks for this tutorial With this I've started learning to work with canvas Nice job!
thereassaad said:
Interesting , definitely going to try it on Samsung TW later , great idea mate ..
Click to expand...
Click to collapse
Me too. hope it works
Excellent work.
rejm0901 said:
Me too. hope it works
Click to expand...
Click to collapse
It works mate ?
Hello! nice tutorial, everything works fine. But I would like to add buttons to change the colors of chars but i'm not able to call the method from the main activity.
I've tried this but giving errors
Code:
MatrixEffect matrixEffect = new MatrixEffect(); // this gives an error : MatrixEffect(Context, AttributSet) in MatrixEffect cannot be applied to ()
final Paint paintTxt = effetMatrix.peindreTxt;
paintTxt.setColor(Color.BLUE);
Is there a way that I can call paintTxt.setColor() to change color?
PS: Im just a noob on programming
esQmo said:
Hello! nice tutorial, everything works fine. But I would like to add buttons to change the colors of chars but i'm not able to call the method from the main activity.
I've tried this but giving errors
Code:
MatrixEffect matrixEffect = new MatrixEffect(); // this gives an error : MatrixEffect(Context, AttributSet) in MatrixEffect cannot be applied to ()
final Paint paintTxt = effetMatrix.peindreTxt;
paintTxt.setColor(Color.BLUE);
Is there a way that I can call paintTxt.setColor() to change color?
PS: Im just a noob on programming
Click to expand...
Click to collapse
If you want to add buttons to change the color of the Matrix Effect, you should work on the layout and add two buttons at the bottom of the screen under the MatrixView. The, you have to change the colors of the Matrix Effect when a user click on one of these buttons.
esQmo said:
Hello! nice tutorial, everything works fine. But I would like to add buttons to change the colors of chars but i'm not able to call the method from the main activity.
I've tried this but giving errors
Code:
MatrixEffect matrixEffect = new MatrixEffect(); // this gives an error : MatrixEffect(Context, AttributSet) in MatrixEffect cannot be applied to ()
final Paint paintTxt = effetMatrix.peindreTxt;
paintTxt.setColor(Color.BLUE);
Is there a way that I can call paintTxt.setColor() to change color?
PS: Im just a noob on programming
Click to expand...
Click to collapse
SOLVED!
---------- Post added at 11:13 PM ---------- Previous post was at 11:05 PM ----------
sylsau said:
If you want to add buttons to change the color of the Matrix Effect, you should work on the layout and add two buttons at the bottom of the screen under the MatrixView. The, you have to change the colors of the Matrix Effect when a user click on one of these buttons.
Click to expand...
Click to collapse
Already solved. I added 4 buttons for 4 colors and i used switch statement
1st, created a custom text color inside MAtrixEffect
Code:
public void setCustomTextColor(int color){
peindreTxt.setColor(color);
invalidate();
then, in mainactivity :
Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MatrixEffect matrixEffect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
matrixEffect = (MatrixEffect) findViewById(R.id.arrPlan);
final Button boutton_blanc = (Button) findViewById(R.id.b_bl);
final Button boutton_bleu = (Button) findViewById(R.id.b_b);
final Button boutton_rouge = (Button) findViewById(R.id.b_r);
final Button boutton_rose = (Button) findViewById(R.id.b_ro);
if (boutton_bleu != null) {
boutton_bleu.setOnClickListener(this);
}
if (boutton_blanc != null) {
boutton_blanc.setOnClickListener(this);
}
if (boutton_rouge != null) {
boutton_rouge.setOnClickListener(this);
}
if (boutton_rose != null) {
boutton_rose.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.b_bl:
matrixEffect.setCustomTextColor(Color.WHITE);
break;
case R.id.b_b:
matrixEffect.setCustomTextColor(Color.BLUE);
break;
case R.id.b_ro:
matrixEffectsetCustomTextColor(Color.MAGENTA);
break;
case R.id.b_r:
matrixEffect.setCustomTextColor(Color.RED);
break;
case R.id.b_def:
matrixEffect.setCustomTextColor(Color.GREEN);
break;
}
}
}
Now it works fine and i can change color while a button is pressed
esQmo said:
SOLVED!
---------- Post added at 11:13 PM ---------- Previous post was at 11:05 PM ----------
Already solved. I added 4 buttons for 4 colors and i used switch statement
1st, created a custom text color inside MAtrixEffect
Code:
public void setCustomTextColor(int color){
peindreTxt.setColor(color);
invalidate();
then, in mainactivity :
Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MatrixEffect matrixEffect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
matrixEffect = (MatrixEffect) findViewById(R.id.arrPlan);
final Button boutton_blanc = (Button) findViewById(R.id.b_bl);
final Button boutton_bleu = (Button) findViewById(R.id.b_b);
final Button boutton_rouge = (Button) findViewById(R.id.b_r);
final Button boutton_rose = (Button) findViewById(R.id.b_ro);
if (boutton_bleu != null) {
boutton_bleu.setOnClickListener(this);
}
if (boutton_blanc != null) {
boutton_blanc.setOnClickListener(this);
}
if (boutton_rouge != null) {
boutton_rouge.setOnClickListener(this);
}
if (boutton_rose != null) {
boutton_rose.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.b_bl:
matrixEffect.setCustomTextColor(Color.WHITE);
break;
case R.id.b_b:
matrixEffect.setCustomTextColor(Color.BLUE);
break;
case R.id.b_ro:
matrixEffectsetCustomTextColor(Color.MAGENTA);
break;
case R.id.b_r:
matrixEffect.setCustomTextColor(Color.RED);
break;
case R.id.b_def:
matrixEffect.setCustomTextColor(Color.GREEN);
break;
}
}
}
Now it works fine and i can change color while a button is pressed
Click to expand...
Click to collapse
Great
This is just the kind of guide that I have been waiting for! I am gonna implement this as a hidden feature in my Rom Control apk. Good lookin on the guide brother

Categories

Resources