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.
Related
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.
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~!
I'm supporting an android 2.2+ app. I'm using the code image.setOnClickListener(new View.On Click Listener()
and a dialog to show a larger image in another layout.
Current project has an error. When I debug the project, I see a NullPointerException on image.setOnClickListener(new View.OnClickListener() saying the image is null.
Main Activity
#SuppressLint("ClickableViewAccessibility")
public class MainActivity extends ListActivity {
Context c;
Intent intent;
ImageView image, image1;
#SuppressLint("SdCardPath")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) image.findViewById(R.id.image_my);
// *** mean here
image_my.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TO DO Auto-generated method stub
show(v);
}
});
}
public void show(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.show);
image1 = (ImageView) image1.findViewById(R.id.image_my1);
dialog.show();
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_my1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:src="#drawable/image_def" />
</LinearLayout>
I'm not sure where you got this code from, but you are repeatedly using variables before you have assigned them:
image = (ImageView) image.findViewById(R.id.image_my);
Here you are calling findViewById on your image instance, but image will be null here. You probably want:
image = (ImageView) this.findViewById(R.id.image_my);
Similarly, in your show() method:
image1 = (ImageView) image1.findViewById(R.id.image_my1);
should be
image1 = (ImageView) this.findViewById(R.id.image_my1);
You might want to try using more descriptive names for your variables too - it makes it a bit easier to spot problems like this.
TextView text = (TextView) vi.findViewById(R.id.text);
text.setText(text_value);
This string edits the android:text value.
But if I want to edit the andoid:onClick value, what should I do?
For example:
// from:
android:onClick("action(1)");
// to:
android:onClick("action(2)");
Thanks a lot :)
PS: sorry for any error, I'm Italian :(
you cannot edit the android:onClick programmatically in android try setOnClickListener
text.setOnClickListener(this);
#Override
public void onClick(View v) {
}
Call setOnClickListener(), providing an implementation of View.OnClickListener as argument.
Nitpick: Calling setText() doesn't "edit" the XML attribute value. It just changes the same TextView internal value that is influenced by the attribute at construction time. The same goes for onClick.
Create the method in the activity class.
res/layout/main.xml
<FrameLayout
....>
<TextView android:id="#+id/text1"
..../>
<Button android:id="#+id/btn1"
android:onClick="actionTwo"
.../>
</FrameLayout>
class MyActivity extends Activity {
public void onCreate(Bundle b) {
setContentView(R.layout.main);
}
public void actionOne(View view) {
// the action here
}
public void actionTwo(View view) {
// the action here
TextView textView = (TextView) findById(R.id.text1);
textView.setText("Uhuuu funcionou!!!");
}
}
When the phone is shaked it is supposed to display an animation. The animation works the first time but the next shake it wont do it. Everything else is working correctly because every time I shake it it display the new text each time. Its just the animation wont do it again after the first. I do have the animation set for oneshot but that shouldnt effect the animation from triggering again?
Here is the activity I'm working with this on. Followed by its xml layout.
public class Ask extends Activity{
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask);
mSensorListener = new ShakeEventListener();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
final ImageView v = (ImageView)findViewById(R.id.talk);
mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
public void onShake() {
v.setBackgroundResource(R.anim.budtalk);
AnimationDrawable talking = (AnimationDrawable)v.getBackground();
talking.start();
}
});
}
}
<RelativeLayout 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/page1"
>
<ImageView android:background="#drawable/page2ani1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/talk"></ImageView>
</RelativeLayout>
Does this work?
final Animation ani = AnimationUtils.loadAnimation(this,R.anim.budtalk);
//later
v.startAnimation(ani);
This is how I have applied animations repeatedly to TextView elements.
Failing that, because you have changed the ImageView , perhaps you might need a v.postInvalidate() afterwards?