Invoke new Screen

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Invoke new Screen

Postby zahidalinaqvi » Wed Jul 29, 2009 6:58 pm

How i can invoke a new screen with a normal java class.

its not accepting startActivity();

I have a normal java class which is not extending any class.

now i want to invoke another screen.


I am trying like attached file.
Attachments
Utils.java
(3.22 KiB) Downloaded 57 times
Utils.java
(3.22 KiB) Downloaded 55 times
User avatar
zahidalinaqvi
Developer
Developer
 
Posts: 35
Joined: Mon Aug 11, 2008 8:54 am
Location: Jaipur India

Top

Postby Sowmya » Thu Jul 30, 2009 11:26 am

Hi,
The class that has your new view should extend Activity class, if you want to use startActivity() method
or
If you want to set a new view in the same Activity,use LayoutInflater to inflate the view and use the settcontentview(..) method.

Hope this helps.
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Invoke new Screen

Postby zahidalinaqvi » Thu Jul 30, 2009 4:27 pm

Thanks for reply...

i tried but its not working.

this is my code.

and commented code i tried but not get success.

Code: Select all
package com.brain_scape;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Utils extends Activity

{
   String clicked = "Hello";
   Context context;
   //TextView t = new TextView(context);


   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //setContentView(R.layout.spanish);
      context = this;

   }


   /**
    * this is a quick helper method to create a <tt>TextView</tt>
    * for output to the screen
    * it creates a new <tt>TextView</tt> from the <tt>toString()</tt>
    * method of <tt>label</tt>
    * @param context the context where this TextView will be displayed
    * @param label create a label from this object
    * @return an instantiated TextView with text from <tt>label</tt>
    */

   public TextView label(final Context context, Object label)
   {
      final TextView t = new TextView(context);
      t.setFocusable(true);
      t.setText(label.toString());
      t.setOnClickListener(new OnClickListener(){
         public void onClick(View v) {
            //Main.temp =1;
            
            [b]View inflatedView = View.inflate(context, R.layout.spanish, null);
            setContentView(inflatedView);[/b]
            

[b]                                 // THIS ALERT BOX IS WORKING FINE IF IT IS ALONE HERE IN THIS EVENT[/b]

            //new AlertDialog.Builder(context).setTitle("Error")
            //.setMessage("hi")
            //.setPositiveButton("OK", null).show();
            
            //clicked = t.getText().toString();
            //v.setVisibility(View.GONE);
            /*new AlertDialog.Builder(context).setTitle("Error")
            .setMessage(t.getText().toString())
            .setPositiveButton("OK", null).show();*/
            
            //Main.temp = 2;
            //new Main().show();
            /*Intent i = new Intent();
            i.setClass(context, BrainActivity.class);
            startActivity(i)*/;
            
            /*new Thread(){
               public void run() {
                  try{
                  Intent i = new Intent();
                  i.setClass(context, SpanishActivity.class);
                  startActivity(i);
                  setContentView(R.layout.spanish);
                  } catch(Exception e) {
                     new AlertDialog.Builder(context).setTitle("Error")
                     .setMessage(e.getMessage())
                     .setPositiveButton("OK", null).show();
                     //Log.e("Utils",e.getMessage());
                  }
               }
            };*/
            
            //setContentView(R.layout.spanish);
            
            /*try{
               Intent i = new Intent();
               i.setClass(context, SpanishActivity.class);
               startActivity(i);
               //setContentView(R.layout.spanish);
               } catch(Exception e) {
                  new AlertDialog.Builder(context).setTitle("Error")
                  .setMessage(e.getMessage())
                  .setPositiveButton("OK", null).show();
                  //Log.e("Utils",e.getMessage());
               }*/
            
         }
      });
      
      return t;
   }
   
    /**
     * loads the contents of an application 'asset' into a string
     * @param context the execution context
     * @param name the name of the asset
     * @return the contents of the asset
     * @throws IOException if something happens while reading the file
     */
    public static String readAsset(Context context, String name) throws IOException
    {
       System.out.println("reading asset");
       System.out.println("context: " + context);
       AssetManager assets = context.getAssets();
       System.out.println("assets: " + assets);
        InputStream is =  assets.open(name, AssetManager.ACCESS_BUFFER);
        System.out.println("got input stream: " + is);

        String content = readAll(is);
        is.close();
        return content;
    }
   
    /**
     * read the entire contentsof an input stream into a string
     * @param is
     * @return a String containing the entire contents of the input stream
     * @throws IOException
     */
    public static String readAll(InputStream is) throws IOException
    {
        StringBuilder s = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line = reader.readLine();
        while (line != null)
        {
            s.append(line).append("\n");
            line = reader.readLine();
        }
        return s.toString();
    }


}
User avatar
zahidalinaqvi
Developer
Developer
 
Posts: 35
Joined: Mon Aug 11, 2008 8:54 am
Location: Jaipur India

Postby Sowmya » Fri Jul 31, 2009 5:22 am

What did you try?Can you you explain it in detail
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests