[Q] Android full screen menus - General Questions and Answers

My question is relatively simple; is the code below an accepted method of changing full screen menus in Android 2.1? I thought I should ask, because when I switch back using the back key without doing the findViewById again, the menu doesn't work. So I have to reconstruct the menu each time (createMainMenu() method). To implement the back key I use a state, which is dependant upon which menu the system is in.
Also, I thought using multiple activities was not necessary in this small application.
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createMainMenu();
}
private void createMainMenu(){
setContentView(R.layout.main);
state=STATE_MAIN;
createKeyButton=(Button)findViewById(R.id.createKeyButton);
encryptTextButton=(Button)findViewById(R.id.encryptTextButton);
decryptTextButton=(Button)findViewById(R.id.decryptTextButton);
createKeyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
createKeyCreationMenu();
}
});
}
private void createKeyCreationMenu(){
setContentView(R.layout.create_key);
state=STATE_MENU;
keyLengthField=(EditText)findViewById(R.id.keySizeEditText);
}
public void onBackPressed(){
if(state==STATE_MENU){
createMainMenu();
} else if(state==STATE_MAIN){
super.onBackPressed();
}
return;
}

SOLVED
Although this method works, you get problems when the user turns the screen. I have resolved to launching new activities for new full screen menu's.
Can a moderator please move this thread to the android software forum?

Related

[Q] developping in android issue

hello,
I am learning to develop on android and I have an issue. I hope this is the right place to find help. I tried the development forum but apparently I can't post there.
So here is my issue:
I'm trying to create a header for my application. In this header, I have a button, and since I don't want to duplicate my button click handler in every activity, I created a MotherActivity (that inherits Activity) from which my activities will inherit from.
In my first activity, I wrote:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myActivity1); (myActivity1 <includes> layout.header)
}
and in my MotherActivity, I used:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout include_header = (LinearLayout)(findViewById(R.layout.header));
((ImageView)( include_header.findViewById(R.id.ImageButton1)))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
}
My problem is that findViewById(R.layout.header) returns null.
If I try to invert onCreate() and setContentView(), I receive this error:
ERROR/AndroidRuntime(872): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
So I don't know what to do, in both cases I receive an error...
Can you help me please ?
Thanks.
I managed to fix the error.
But there's something I don't understand. The whole point of this architecture with an inherited activity was to create a button in the header, and whose event handler wouldn't need to be rewritten in all the activities.
But the button doesn't do anything when clicked on.
How is that possible ?

[Q] Bluetooth toggle from within app

I want toggle bluetooth states from within the app i'm making between off, connectable & discoverable and connectable.....
i've tried
Code:
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
Phone.OS.Bluetooth.Turn(Phone.OS.Bluetooth.BTH_RADIO_MODE.BTH_POWER_OFF);
}
private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
Phone.OS.Bluetooth.Turn(Phone.OS.Bluetooth.BTH_RADIO_MODE.BTH_CONNECTABLE);
}
private void radioButton3_Checked(object sender, RoutedEventArgs e)
{
Phone.OS.Bluetooth.Turn(Phone.OS.Bluetooth.BTH_RADIO_MODE.BTH_DISCOVERABLE);
}
how do I get this done?

[Q] Sending "string" serially (byte code given)

Hi all,
I am developing an android app for my project which sends data serially, through RS232 + OTG cable, to a circuit developed by me (and the PC code for which is already done and developed in C#). I'm new to android and tried to get help from example apps. i found an app which establishes the serial communication successfully but the problem is that it sends data in the form of byte, but i want data to be sent as a string and also received as a string. portion of the activity of interest is being added which sends binary 10101010... but I want data in string format. like "abcdefg". it must also be noted that this code is ignoring the incoming data but i want the incoming data to be stored in a variable (which i think is not very difficult but it could be handy if someone can also guide about that)
if someone can help, it'll be highly appreciated
Code:
package android_serialport_api.sample;
import java.io.IOException;
import java.util.Arrays;
import android.os.Bundle;
import java.io.*;
import java.util.*;
//import javax.comm.*; // for SUN's serial/parallel port libraries
public class Sending01010101Activity extends SerialPortActivity {
SendingThread mSendingThread;
byte[] mBuffer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sending01010101);
mBuffer = new byte[1024];
Arrays.fill(mBuffer, (byte) 0x55);
if (mSerialPort != null) {
mSendingThread = new SendingThread();
mSendingThread.start();
}
}
@Override
protected void onDataReceived(byte[] buffer, int size) {
// ignore incoming data
}
private class SendingThread extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
try {
if (mOutputStream != null) {
mOutputStream.write(mBuffer);
} else {
return;
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
}
not enough info is availabe on the internet on this topic and I only expect help from this forum. its very important for me

Popup box Help

So i have created a pop up window with the source code below. I also have designed the popup really good. and it displays right. The only problem i have is the damn button to close it. lol I had it so it closed fine before. Then i started adding stuff in to make the app work the way i want it to.
So What i have done is this, I have the popup created i want to be able to have the Text box checked for a string if this string exists then I need to go to thread allApps which then launches my 2nd Activity which that works fine as well. But the problem is the damn pop uup is still up so i need to close this. Any way of doing this with using the code I have or do i need to redo something. Please Any help thanks
PHP:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final Button btnOpenPopup = (Button)findViewById(R.id.apps_button);
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(btnOpenPopup, Gravity.CENTER, 0, 0);
}});
}
public void allApps(View arg)
{
Intent intent = new Intent(arg.getContext(), AppList.class);
startActivity(intent);
}

Question regarding Android Dev Studio (java) WebView

Hi all
I have programmed my first app, a calculator using webview, i have one main question
When rotating the webview it did clear itself, i have solved this using the following code
protected void onSaveInstanceState(Bundle outState) {
Screen.saveState(outState);
outState.putString("editTextData1", edt1.getText().toString());
super.onSaveInstanceState(outState);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}
Click to expand...
Click to collapse
However the cursor for my calculator moves back to the start of the webview upon rotation and hence when i type it deletes the existing sum from before the rotation, please help

Categories

Resources