setContentView gives an exception - android

In my android app I set
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN );
Then my touch screen event doesnt’ work any more.
Further Explaining,
I have a button and onClick it changes the contentView by setContentView(R.layout.choose_player);.
It works fine. But if you take the focus to the button by the trackball(making it yellow) and tap on it, it gives the exception.
java.lang.IllegalArgumentException: parameter must be a descendant of this view
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main1);
}
public void onClick(View v) {
setContentView(R.layout.main2);
}

i cant understand properly first. I think you may be using setContentView method multiple times. one is to set the layout on the view. another one inside the click event. right? that not works. use that method once to set the layout. on click event you have to do your tasks on the views inside the layout. hope it helps. please post some code snippets for the perfect answers.

you use setContentView only once in your 'onCreate' method, in general for the main window (one Activity = 1 view)
in your case you should probably change your Background..

Related

Android XML Graphical Layout Not Updating

When I first ran the sample HelloWorld app, it displays the hello world text on the emulator. I decided then to delete that and make a button. What I wanted is that when I click the button, it will show a text "This is the second activity". I made another XML file and another class to handle the second activity to display the text. But when I ran again, I cannot see the changes on the UI for the emulator. The text "This is the second activity" does not show after I clicked the button. I saved everything. How would I automatically update the UI of the emulator after some of the changes made on the design? I am new to android development. Please help me. Btw I cannot post images so it requires 10 reputation that's why I used online image viewing. Sorry for that.
Here is my Graphical layout on eclipse: activity_main.xml
http://s16.postimg.org/wusm4qrp1/image.png
second.xml
http://s29.postimg.org/qft17p5on/image.png
Running the emulator:
http://s28.postimg.org/f9dn32ku5/image.png
After clicking the button (in which case the text I edit does not show):
http://s16.postimg.org/75r62dodx/image.png
Because you didn't post your code, I'll try to explain it from the beginning.
I don't know if you can do it in an other way, but the following answer assumes we aim the good programming practice.
Both of the activities have their distinct layouts set in the following way, right?
public class MyFirstActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
...
public class MySecondActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
...
Then, in your first activity, define onClickListener of your button
...
setContentView(R.layout.activity_first);
Button myButton = (Button)findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyFirstActivity.this, MySecondActivity.class);
startActivity(intent);
}
});
That being said, I don't recommend you to have two activities for this functionality. You should have different activities when you need different layouts. Putting the button and the textview in the same layout and updating textview inside the button's onclicklistener is a better solution.

SetText() from another Activity

I have an EditText in my MainActivity. When the user clicks the NewFile activity the layout for this activity is transparent so the EditText can still be seen. I want to be able to update the EditText in the background from the NewFile activity. This is what I've tried, which results in a NullPointerException. I understand why this didn't work but what can I do instead to get the results I'm looking for.
NewFile.java
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newfile);
newet = (EditTextLineNumbers) findViewById(R.id.ide);
newet.setText("Testing");
}
Your background EditText is not from the layout currently set for the user, thus you are getting the exception.
Regarding updating the UI, 2 things you might need.
Pass the handler of the background activity to the new one
Make the new one as a dialog rather than a transparent activity.
Use the handler to send notifications and update the UI accordingly
How did you think it will work..? How do you access the view which is not in your activity layout..? To say it is not possible...
You can get the EditText of your previous layout by using the layout inflatter service to your layout and from that your view
In the NewFile activity, have an edit text which overlays your transparent editTextView(may be have a transparent BG for this textview which might give illusion that its in background), now update this editTextView
and
once you go back, take data from this edittext(using setActivityForResult and those mechanisms) and fill in the previous activity.

A button which goes onto an Activity

I have been trying to figure this thing out with no luck in eclipse. I have created 3 screens. One being the main menu which a button leading to another button which then leads to an Activity. I can get the button from the main menu to lead onto the button onto the second screen but i can't get the second button to lead onto the third screen.
Can anybody help me?
appproject
You should really post some code so we can help you...but it seems like you need to look at your onClick() method for the second button.
Providing you have three separate Activities for each of these three screens (I'll call them ActivityA, ActivityB, and ActivityC), you'll also probably need an XML layout for each. There are ways of doing it without an XML layout, but for now just stick with that.
The next thing is you want to make sure your Button is initialized properly. ActivityB should look like:
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Button b = new (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
#Override
protected void onClick() {
startActivity(new Intent(this, ActivityC.class);
}
});
}
The first thing you should do is check to make sure your button functions in a way similar to how I've described here. If that doesn't change anything, make sure you are initializing your Button in accordance to how it is defined in the layout XML. You must use an id for a Button that is in the same XML layout as the one you set in setContentView(). If not, it will do nothing, no matter what you put in the onClick() method.
I hope that helps!!

how to change the view of a button onClick in android

In my app I am trying to calculate an operation using timer. For controlling those operations I am using four buttons as Start, Stop, Pause and resume.
But I want to show only 2 buttons. At the beginning I have only two buttons Start and Pause.
When the start button is clicked timer gets started and immediately in Start button's place I want to show the Stop button.
I want to do the same for the other stop and pause buttons. How to do this please help me......
Using ToggleButton is a good solution for you. Do something like:
ToggleButton first = new ToggleButton(getContext());
ToggleButton second = new ToggleButton(getContext());
first.setTextOff("start");
first.setTextOn("stop");
second.setTextOff("pause");
second.setTextOn("resume");
and use setOnCheckedChangeListener() to implement your actions.
In your onClick(View v), v is the button that gets clicked. You can cast it like:
Button b = (Button) v;
so you can change its text with setText(), and set another listener. You can declare the alternate listeners once as members of the activity, and set them without re-declaring them each time.
Your application needs to maintain states, such as "Idle/Stopped", "In Progress", "Paused", etc. If you want to hide buttons, you can use View.setVisibility, and dynamically show and hide the buttons when your state changes (when other buttons are pressed). You would need to set your layout appropriately so that the buttons display nicely as they are shown/hidden dynamically
Or, you can change the text of the buttons, and their associated click listeners dynamically. This method is not very ideal becuase you may run in to cases where you want different amount of buttons for all your different states, and also, you're associating variable behavior with a single control. Also, you must manage your click listeners, adding and removing them dynamically.
here is a simple implementation
public class Demo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button.setText("stop");
}
});
}
}
In the main.xml have a Button widget like this,
<Button android:id="#+id/button"
android:text="start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

When has the Activity finished drawing itself?

This maybe somewhat of a strange question but I just can`t figure it out.
Basically I have a class that extends View and overrides the onDraw method so it draws a bitmap what it is called. What I want to do is when by choosing a picture from a gallery, send it through and intent and draw that on the View.
The problem is that I can`t get the Activity to draw the bitmap when it is started. I try setting in in onCreate, onStart... nothing works.
It works if I set the bitmap on a button press and calling invalitate(), so I suspect that I try to draw before the Activity hasn`t finished drawing itself.
Any suggestions?
Well, if you instantiate the class that extends the View in your onCreate, that should work.
Take a look at this example:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
customView = new CustomView(this);
setContentView(customView);
customView.requestFocus();
}
Override method onWindowFocusChanged() in Activity.
This is where you can be sure that views have been drawn. You can start obtaining the dimension/ position of the views in this method.
For eg. customView.getLeft(), etc.

Categories

Resources