NullPointerException when try change text in textView from other class - java

Can u help me understand why when I want set text in TextView, I got NullPointerException? I know, because my TextView is null but how I can get TextView again?
What is my logic:
Start Application
Click button
Go to nullPoint and get data from firebase, when the proccessing is finished back to MainActivity and update text in TextView.
This is my example code:
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
}
nullPoint
public class nullPoint {
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
MainActivity ma = new MainActivity();
ma.setTextView(5);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:layout_gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"/>
</LinearLayout>

I guess the nullPoint class is not inside the MainActivity. As a result, inside your nullPoint class there is no reference for the TextView in which you are trying to set a data.
In your case I would like to suggest you to implement a listener like the following.
public interface FirebaseResponseListener {
void onFirebaseResponseReceived(int count);
}
Now from your MainActivity, you need to implement the listener as follows.
public class MainActivity extends AppCompatActivity implements FirebaseResponseListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.listener = this; // Initialize the listener here
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
#Override
void onFirebaseResponseReceived(int count) {
setTextView(count);
}
}
And you have to add a listener in your nullPoint class as well.
public class nullPoint {
public FirebaseResponseListener listener;
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
listener.onFirebaseResponseReceived(5);
}
}
Hope that solves your problem.

Related

what is the new method for changing textview color in android programaticallly [duplicate]

This question already has answers here:
How to set the text color of TextView in code?
(38 answers)
How to set text color of a TextView programmatically? [duplicate]
(4 answers)
Closed 4 years ago.
XML
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
app:titleTextColor="#ffffff"
>
<TextView
android:id="#+id/name"
android:layout_width="275dp"
android:layout_height="match_parent" />
</android.support.v7.widget.Toolbar>
Activity
public class Chat extends AppCompatActivity {
private TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
name = (TextView) findViewById(R.id.name);
TextView textView = new TextView(R.layout.activity_chat);
name.setText((getIntent().getStringExtra("Recievers_Name")));
name.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
I tried
name.setTextColor(getResources().getColor(R.color.white));
name.setTextColor(color.WHITE);
name.setTextColor(Color.parseColor("#FFFFFF"));
But not working... please help.................................................................
textView.setTextColor(view.getResources().getColor(R.color.red_1));
This should work for most Api versions
getResources().getColor() is deprecated. Use this
TextView name = (TextView) findViewById(R.id.textview_name_id);
name.setTextColor(ContextCompat.getColor(context, R.color.black));
I think you are missing TextView declaration and initialization in your activity.
Declare it as a variable of your activity, and initialize it after calling setContentView().
public class MainActivity extends Activity {
private TextView mNameTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_launch);
mNameTextView = (TextView)findViewById(R.id.name);
// remaining codes
mNameTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
}
}
Try this method it's work fine on all Api levels.
public int _getColor(int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 23) {
return ContextCompat.getColor(this, id);
} else {
return getResources().getColor(id);
}
}
Try this it is working..
public class LayoutActivity extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
textView=findViewById(R.id.textView);
textView.setTextColor(Color.RED);
}
}

Intent is not calling when button is pressed

I have the following piece of codes.I am calling a second activity from main activity.Whenever the send button is pressed i want a toast to show button is pressed and start the activity.But due to some context problems only toast is appearing.Please correct the context for intent and give some clear explaination about these contexts.
MainActivity.java
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("My app","onCreate is called");
Toast1("onCreate");
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
}
});
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the string
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView =(TextView) findViewById(R.id.textView);
Log.i("intent","displaymessage");
textView.setText(message);
}
}
activity_main.xml
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="#string/edit_message"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="#+id/button"
app:layout_constraintHorizontal_chainStyle="spread" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:onClick="sendMessage"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintLeft_toRightOf="#+id/editText"
app:layout_constraintRight_toRightOf="parent" />
activity_display_message.xml
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
When i remove the onClickListener() method then the intent is working.
Since you are setting your own View.OnClickListener, you are removing the one from the XML definition. Button only support one View.OnClickListener. First, the XML will create one from the android:onclick attribute like :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendMessage(v);
}
});
Then you are setting yours with the Toast. The button will only keep the last one, so the Intent is never send.
Solutions :
call the sendMessage method in your listener
Like:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
sendMessage(v); //Or anywhere in that method, your call.
}
});
remove the listener to keep the one create by the android:onclick.
FYI:
Usually, a set### methods means this is not supporting multiple values, add### methods do.
Also, you can check at android- multi onClick listener in one button to implement your own multi listener button if you like. But I didn't check if there were some more up to date...
Just remove android:onClick="sendMessage" for button and try. Either you have to set click listener in xml or in class file. Both it won't work
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintLeft_toRightOf="#+id/editText"
app:layout_constraintRight_toRightOf="parent" />
Change this in your Activity :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("My app","Button is pressed");
Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
sendMessage();
}
});
private void sendMessage() {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
Just copy paste the following MainActivity code and it will do the work. The xmls are loaded first and then on runtime you are setting the onclick listener to the button again. so the xmls onclick has been replaced by your onclick listener in java code. Hence you get toast but the sendMessage() is never called
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("My app","onCreate is called");
Toast1("onCreate");
Button btn=(Button)findViewById(R.id.button);
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
}

Buttons interfering with each other on Android Studio

I am trying to make a button on my homepage of an app that will lead to a search page, that will have a handful more buttons leading to other pages. However, I used the same code from my activity main for the button in my second page (seachpage) and now when I run the code, my first button on my main page, when clicked it just shuts the app down. I don't even know how to approach this properly because I copied the same code, just changed the "findViewById" and the "startActivity" accordingly with their new labels. Any recommendation or help would be massively appreciated!
Activity main Java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button yourButton = (Button) findViewById(R.id.TranslateButton);
if (yourButton == null) throw new AssertionError();
yourButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SearchPage.class));
}
});
}
}
Activity main xml for the button:
Secondary page (searchpage) Java code:
public class SearchPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_page);
Button accommodationButton = (Button) findViewById(R.id.accommodationButton);
if (accommodationButton == null) throw new AssertionError();
accommodationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(SearchPage.this, Accommodation.class));
}
});
}
}
Secondary page xml for the button:
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="#string/accommodation"
android:id="#+id/accommodationButton"
android:layout_below="#+id/search_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_weight="1"/>
Thank you again for taking the time and consideration to read and/or respond to my question~!

How to create an interface to get info from a Fragment to an Android Activity?

Over the past days I've desperately been trying to build an android app with a simple fragment (which I use twice). I want to pass the contents of the fragments' EditText-boxes to a new activity. I just can't figure out how to get those contents from the fragments. What I have so far is this:
I've got my edit_text_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="my hint" />
</LinearLayout>
and the corresponding MyEditTextFragment.java:
public class MyEditTextFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.edit_text_fragment, container, false);
return view;
}
}
I then use this fragment twice in my main.xml like this:
<LinearLayout 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"
android:orientation="vertical">
<fragment
android:id="#+id/detailfragment_placeholder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.example.fragmenttester5.MyEditTextFragment" />
<fragment
android:id="#+id/detailfragment_placeholder2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.example.fragmenttester5.MyEditTextFragment" />
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit all of it" />
</LinearLayout>
and in my MainActivity I hooked up the button to a new activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button submitButton = (Button) findViewById(R.id.submit_button);
submitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("result1", "the_result_from_the_first_editText");
intent.putExtra("result2", "the_result_from_the_second_editText");
startActivity(intent);
}
});
}
}
I think I now need to define some kind of interface in the Fragment, but I can't find how. I read a couple examples and tutorials (like this one), but they make no sense to me at all. I don't understand the code given and I just don't understand how to adjust it for my use case.
So my question; can anybody help me to get the contents of the fragment from within the activity? Examples would be very very welcome since I'm just banging my head against the wall here..
You are right, that's kind of a standard way to pass data from a Fragment to an activity.
Basically you define a Listener interface which the Activity implements, and the Activity registers itself as a Listener with the Fragment.
Here's a simple example:
Fragment
class MyFragment extends Fragment {
interface Listener {
public void somethingHappenedInFragment(Object... anyDataYouWantToPassToActivity);
}
private Listener mListener;
public void setListener(Listener listener) {
mListener = listener;
}
// ... your code ...
// Now here you pass the data to the activity
mListener.somethingHappenedInFragment(some, data);
// ... more of your code
}
Activity
public MyActivity extends Activity implements MyFragment.Listener {
// ... your code ...
// creating the Fragment
MyFragment f = new MyFragment();
// register activity as listener
f.setListener(this);
// ... more of your code
// implementation of MyFragment.Listener interface
#Override
public void somethingHappenedInFragment(Object... anyDataYouWantToPassToActivity) {
// here you have the data passed from the fragment.
for (Object o : anyDataYouWantToPassToActivity {
System.out.println(o.toString();
}
}
}
On a high level, there are two tasks that you commonly need to solve with Fragments. The first is communicating data from an Activity to a Fragment. The second is communicating data from a Fragment to an Activity.
An Activity knows which Fragments it contains since it creates them, so it's easy to communicate that way - just call methods on the Fragment itself. But the inverse is not true; Fragments might be attached to any number of random Activities, so it doesn't know anything about it's parent.
The solution is to implement an interface that the Activity implements and the Fragment knows how to communicate with. That way, your Fragment has something it knows how to talk with. There are specific code examples for how to do it here: http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
(In particular, check out the "Creating event callbacks to the activity" code examples).
So you'd create an Interface to talk with the Activity if the event happened in the Fragment. For situations like this, you can simply make an accessible method in the Fragment that the Activity can call. So
public class MyEditTextFragment extends Fragment {
private EditText mEditText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.edit_text_fragment, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mEditText = (EditText) getView().findViewById(R.id.my_edit_text);
}
public Editable getText() {
return mEditText.getText();
}
}
Then
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MyEditTextFragment fragment1 = (MyEditTextFragment)
getFragmentManager().findFragmentById(R.id.detailfragment_placeholder);
final MyEditTextFragment fragment2 = (MyEditTextFragment)
getFragmentManager().findFragmentById(R.id.detailfragment_placeholder2);
Button submitButton = (Button) findViewById(R.id.submit_button);
submitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String firstResult = fragment1.getText().toString();
String secondResult = fragment2.getText().toString();
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("result1", firstResult);
intent.putExtra("result2", secondResult);
startActivity(intent);
}
});
}
}
This assumes that you assigned the Fragment tags in your FragmentTransaction. Be sure to check for null Fragments (omitted for brevity)
Activity will be received data from updateDetail() method in Fragment
//// Activity
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Annv - Fragment", "onClick here");
}
});
}
// if the wizard generated an onCreateOptionsMenu you can delete
// it, not needed for this tutorial
#Override
public void onRssItemSelected(String link) {
// DetailFragment fragment = (DetailFragment) getFragmentManager()
// .findFragmentById(R.id.detailFragment);
// if (fragment != null && fragment.isInLayout()) {
// fragment.setText(link);
// }
// Intent start = new Intent(this, RssfeedSecondActivity.class);
// startActivity(start);
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
}
}
}
/// Fragment
public class MyListFragment extends Fragment {
private OnItemSelectedListener listener;
private OnItemStartActivityListener listenerStartAct;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview,
container, false);
Button button = (Button) view.findViewById(R.id.button1);
Log.d("Annv - Fragment", "run on " + getActivity().toString());
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateDetail();
}
});
return view;
}
public interface OnItemSelectedListener {
public void onRssItemSelected(String link);
}
public interface OnItemStartActivityListener {
public void onRssStartActivity(String link);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
Log.d("Annv - Fragment", "activity " + activity.getLocalClassName());
listener = (OnItemSelectedListener) activity;
} else if (activity instanceof OnItemStartActivityListener) {
Log.d("Annv - Fragment", "activity " + activity.getLocalClassName());
listenerStartAct = (OnItemStartActivityListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet MyListFragment.OnItemSelectedListener");
}
}
// May also be triggered from the Activity
public void updateDetail() {
// create fake data
// String newTime = String.valueOf(System.currentTimeMillis());
// // Send data to Activity
// listenerStartAct.onRssItemSelected(newTime);
if (getActivity() instanceof OnItemSelectedListener) {
listener.onRssItemSelected("start start");
} else {
String newTime = String.valueOf(System.currentTimeMillis());
listenerStartAct.onRssStartActivity(newTime);
}
}
}

SwitchDisplayable a la Android

i'm still porting a J2ME app to Android and now my problem is with the GUI.
For what i've seen, Android's Activities are great, but my J2ME is filled with the classic:
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
Display display = getDisplay();
if (alert == null)
display.setCurrent(nextDisplayable);
else
display.setCurrent(alert, nextDisplayable);
}
I just can't make every Displayable to an Activity, so i thought about replacing them with View. I tried that but it just doesn't works, the app doesn't changes the screen.
Update:
Thanks for answering, but placed al vews inside FrameLayout and still nothing. This is the core of my test code, so you can check it out:
public class TestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t = (TextView)findViewById(R.id.text); // Shows "Hi"
showDialog(); // it just shows a dialog asking if the user wants to change screen
}
showDialog() {
// in OnClick()... i do the following, and here is where it fails, i tried so far:
TestView testv= new MarcoLoco(MFActivity.this);
setContentView(testv);
testv.invalidate();
testv.requestFocus();
testv.showMeSomething();
}
public class TestView extends View{
private Context context;
TextView tv;
public TestView(Context context) {
super(context);
this.context=context;
}
public void showMeSomething() {
tv = (TextView)findViewById(R.id.tessto); // it should show "Bye"
}
}
After the OnClick the "Hi" dissapears from the screen but nothing appears, no "Bye".
Why, oh, why!?
I don't have any expirience with Java ME, but this might help you
Put your Views inside a FrameLayout, then you can use
mViewA.setVisibility(View.VISIBLE);
mViewB.setVisibility(View.GONE);
mViewA.requestFocus();
to switch between diffrent views
EDIT:
Here is a short example program that toggles between 2 textviews:
public class test extends Activity {
boolean showTV1 = true;
OnClickListener ocl = new OnClickListener() {
#Override
public void onClick(View v) {
showTV1= !showTV1;
if (showTV1){
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.GONE);
tv1.requestFocus();
} else {
tv2.setVisibility(View.VISIBLE);
tv1.setVisibility(View.GONE);
tv2.requestFocus();
}
}
};
private TextView tv1, tv2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.TextView01);
tv2 = (TextView) findViewById(R.id.TextView02);
tv1.setOnClickListener(ocl);
tv2.setOnClickListener(ocl);
}
}
and main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/TextView01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#0f0"
android:text="Hi" />
<TextView android:id="#+id/TextView02" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#f00"
android:text="Bye" android:visibility="gone"/>
</FrameLayout>
I am not sure I fully understand your questions, but try looking at ViewAnimator and ViewFlipper - perhaps these can help
PS. just out of curiosity... have you tried any of automatic converters?
Why are you porting from J2ME to Android on your own, instead of using a conversion tool ?
Quicker, no need to learn and excel with Android, no need to debug and maintain 2 code bases... Give it a try at www.UpOnTek.com. Thanks.

Categories