I am currently working on a line entry calculator application for android. When the Button "button" is clicked it should add two textviews into the linearlayout "holder_main_layout" which is within a scrollview. One of the textviews is the text value of an edit text to the left, the other is the evaluated answer to the right. All of this works, but when I reorient the phone (from vertical to landscape or landscape to a different landscape) the linear layout is blank again. I know its not smart to leave all of those textviews without a way to access them but this is proof of concept at the moment.
Why is the scrollview empty when I rotate the phone. And how can i prevent this from happening.
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LinearLayout holder_main_layout=(LinearLayout)findViewById(R.id.linearLayout2);
final CalculatorBase calc=new CalculatorBase();
final Button button = (Button) findViewById(R.id.button1);
final ScrollView scroller=(ScrollView)findViewById(R.id.scrollView1);
final main self=this;
final EditText input_text=(EditText)findViewById(R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView input_view=new TextView(self);
input_view.setText(input_text.getText());
holder_main_layout.addView(input_view);
TextView output_view=new TextView(self);
output_view.setText(String.valueOf(calc.eval_expression(input_text.getText().toString())));
output_view.setGravity(5);
holder_main_layout.addView(output_view);
input_text.setText("");
scroller.fullScroll(ScrollView.FOCUS_UP);
}
});
}
}
By default Android destroys and re-create Activity when orientation changes. You can do two things:
save state as described here: https://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
handle screen orientation change yourself: https://developer.android.com/guide/topics/resources/runtime-changes.html
Try the second method first. Modify your manifest to include android:configChanges line in Activity definition:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
Then place following method in Activity code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Related
I am very new to Android Development and this is my first Android App.
Here is how my MainActivity layout looks like:
And this is the Component Tree of the MainActivity
The gridView is a custom view and I have a resetGrid function in it which looks something like this
public class GridView extends View {
resetGird(){
// stuff that resets grid;
}
}
Here is the problem I am facing:
I can access the setOnClickListener of the reset button in the MainActivity file but not in the GridView file where the resetGrid function is located.
resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(PathFind.this, "reset", Toast.LENGTH_SHORT).show();
}
});
I want to access this setOnClickListener of the reset button in the GridView file also and call the resetGrid function whenever the button is pressed.
Not only that, my plan is to use all the controls listed in the image (start button, diagonal checkbox, speed slider) to control the GridView. But I can't seem to find a way to do that.
All the controls are available in the main_activity but I can't use them in the GridView.
You are in the right way.
The "trick" is to save GridView's instance in a MainActivity variable and then use its reference to call/execute GridView's methods (like "resetGrid()").
ResetButton lives OUTSIDE GridView so it's a bad pratice to reference "resetButton" from INSIDE GridView extended class.
Pseudo code:
class GridView extends View {
public void resetGrid() {
...do reset stuff here...
}
}
class MainActivity extends Activity {
private GridView mGridView;
private Button mResetButton;
public onCreateView() {
...
mGridView = findViewById(R.id.gridview);
mResetButton = findViewById(R.id.resetButton);
resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mGridView.resetGrid();
}
});
}
}
I have two activities main and settings. I want to change the background color of the main using a button that's on settings. I tried using RelativeLayout and get the layout of the main using the id and changing the color when touching the button but it doesn't work. I also tried doing with Intent but I don't know how to do it. Got the code for the settings activity below
public class Settings extends AppCompatActivity {
private Button changeColorButton;
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
changeColorButton = findViewById(R.id.changeColorButton);
layout = findViewById(R.id.mainLayout);
/** Called when the user taps the Settings button */
changeColorButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View V){
Intent intent = new Intent(Settings.this, Main.class);
startActivity(intent);
layout.setBackgroundColor(Color.BLUE);
}
});
}
}
Any help please?
You cannot change views of another activity like that.
You can send an 'extra' parameter with your Intent and read it on Settings activity. You can change the color based on the value you receive via the intent.
Or if its a more permanent setting, you can save it on SharedPreferences and read it on Main.
I setup a program where there is an image and a select button. When you press select the button disappears and a new back button appears. I want the app to be locked on portrait mode, but when you press select I want the screen rotation to unlock, and when turned sideways i want the image to fill be the center of the screen, and the back button to disappear. When turned sideways again I want the back button to reappear. When the back button is clicked it disappears and the select button returns again. I want it to lock the screen to portrait again.
If this is possible I would really appreciate any help! Thanks!
public class MainActivity extends AppCompatActivity {
private Button select;
private Button right;
private Button left;
private Button back;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
select = findViewById(R.id.select);
left = findViewById(R.id.button2);
right = findViewById(R.id.button3);
back = findViewById(R.id.back);
back.setVisibility(View.INVISIBLE);
select.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
right.setVisibility(View.INVISIBLE);
left.setVisibility(View.INVISIBLE);
back.setVisibility(View.VISIBLE);
select.setVisibility(View.INVISIBLE);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
right.setVisibility(View.VISIBLE);
left.setVisibility(View.VISIBLE);
back.setVisibility(View.INVISIBLE);
select.setVisibility(View.VISIBLE);
}
});
}
}
You can lock and unlock the orientation whenever you need want to by calling methods like these
/** Locks the device window in landscape mode. */
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
/** Locks the device window in portrait mode. */
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
/** Allows user to freely use portrait or landscape mode. */
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
It is up to you when you want to call them
I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.
I have a button I am trying to to listen to but the button is not on the screen yet. There are many different Fragments that I am using and the button that I want to listen to doesn't appear on the screen at first, so when I start the app it crashes right away. I am thinking that because it isn't on the screen yet and it is trying to listen to something that isn't there it begins to crash. How do I use it so that the button can begin listening once the fragment that the button is in appears on the screen?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This button is not in the main fragment
Button akbutton = (Button) findViewById(R.id.akbutton);
akbutton.setOnClickListener(new View.OnClickListener() {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = null;
public void onClick(View v) {
fragment = new ak47();
fragmentManager.beginTransaction()
.replace(R.id.container, ak47.newInstance(0))
.commit();
}
});
Ok What the MAIN problem is that your finding the View of your Button not inside where your setting it on Listener.So Please Declare it where it should be used with your Listener.Second solution is to add a single line to where your finding the view of button mainly this word final or static or both final static in general your code must look like this.final Button akbutton = (Button) findViewById(R.id.akbutton); where you can change final to my above written varialiables.