20.9.11

Android Recursively change fonts ?

I think it's time to start blogging about android :)

I just got my Samsung Galaxy S2 a week ago and it's been so fun playing with !

So today i'm gonna show how to use custom fonts, There is a nice artical about Styling TextViews in here: Customize Android Fonts

You can see there that they are accessing a TextView using it's ID and then changing the font of the spacific TextView.
TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font);  
Now that can be a pain in the a** if you have alot of TextViews in your page...
So iv'e created a Recursive that changes all TextView's & Button's fonts on the same page :)
void UpdateFonts(ViewGroup parent, Typeface font) {
  for (int i = 0; i < parent.getChildCount(); i++) {
   View child = parent.getChildAt(i);
   if (child instanceof ViewGroup) {
    UpdateFonts((ViewGroup) child, font);
   } else if (child != null) {
    if (child.getClass() == TextView.class) {
     ((TextView) child).setTypeface(font);
    } else if (child.getClass() == Button.class) {
     ((Button) child).setTypeface(font);
    }
   }
  }
 }
This is an eay recursive that loops through all children of a parent element and changes all of his children's fonts.
You can call it like that if you want all page:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/choco.ttf");
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content).getRootView();
UpdateFonts(rootView, font);
Hope you enjoyed my first Android article :)
See you next time ;)

2.9.11

Fliping lots of text lines... ?


Environment:

I'm using Windows 7 64bit with Notepad++ 5.9.3, Visual Studio 2010 Ultimate, C# .NET 2.0.


Problem:

So i face a really annoying problem today,
I had an array with 400 lines written in C#.
The problem is that, the list was reversed from the order i wanted.


Example:

The array i have:










The array i need:









Like i said it's a 400 lines array...

Research:


I really needed this reversed, i tried Notepad++ with several plugins like TextFX without any success, i actually found out that it's possible to do it by recording a Macro in the Notepad++, but it's like not my thing.

So i used my best friend! C# :)

I'll make a quick explanation of what i'm doing and then just paste the code.



Solution:

So i made a Console App that gets a file path from the application arguments.
It reads all the lines and pushs them into a Stack then writes it to a new file using Pop :)

Heres the code: (Just compile and it works)

class Program
{
 static void Main(string[] args)
 {
  if (args == null)
  {
   Console.WriteLine("args is null"); // Check for null array
  }
  else
  {
   if (args.Length == 0)
   {
    Console.WriteLine("Please enter a file path to reverse in the console arguments.");
   }
   else
   {
    string p = args[0];
    string o = Path.GetDirectoryName(p) + Path.PathSeparator + Path.GetFileNameWithoutExtension(p) + "Reversed" + Path.GetExtension(p);
    Console.WriteLine(o);
    try
    {
     String line;
     Stack< string > lines = new Stack< string >();
     // Create an instance of StreamReader to read from a file.
     // The using statement also closes the StreamReader.
     using (StreamReader sr = new StreamReader(p))
     {
      // Read and display lines from the file until the end of
      // the file is reached.
      while ((line = sr.ReadLine()) != null)
       lines.Push(line);
     }
     // create a writer and open the file
     // The using statement also closes the StreamWriter.
     using (TextWriter tw = new StreamWriter(o))
     {
      // write a line of text to the file
      while (lines.Count > 0)
       tw.WriteLine(lines.Pop());
     }
    }
    catch (Exception e)
    {
     // Let the user know what went wrong.
     Console.WriteLine("The file could not be read/written:");
     Console.WriteLine(e.Message);
    }
    Console.WriteLine("Reversed.");
   }
  }
  Console.ReadLine();
 }
}