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
Related
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.
How can I make a component on my MainUI invisible by pressing a button? I want to dismiss a warning box on my MainActivity with a button that says "Close" or with a cross image.
view.setVisibility(View.GONE);
where view is the component you want to hide
Button crossButton = (Button) findViewById(R.id.crossImage)
Button button1 = (Button) findViewById(R.id.buttonOne)
//and so on
crossButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
button1.setVisibility(GONE);
//and so on
}
});
I am trying to make a Button change its background color and launch a new activity on click. Below is the code I wrote, but I am getting error message saying to declare 'Button btn1' as 'final Button btn1'. If I do that, the button change its color on click and launch another activity as I wished, but if returning to Main activity from the new activity, the once changed background color stays permanently. How can I improve my code? Thank you.
public class Home extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
btn1.setBackgroundColor(Color.GRAY);
Intent intent = new Intent(v.getContext(), activity_street.class);
startActivityForResult(intent, 0);
}
});
No need to make btn1 final just use v parameter of onClick method to change color of clicked Button background :
v.setBackgroundColor(Color.GRAY);
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.
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);
}