toolbar is not hiding in android activity - java

i have an activity which contains a Video View from YouTube and we want that when someone rotate phone , toolbar should hide . actually we have rotation lock in our app , so no activity can get rotate except this video containing activity . and i am rotating screen using onConfigurationChange .
i have implmented custom toolbar in that activity and when configuration change and code changes position of screen it should get hide or show , but it is not working .
i am using this code to hide toolbar
getSupportActionbar.hide()
well , i have some doubts about this that why does it is not working :
1 . first when configuration changes , activity creates from the scratch so command for hide toolbar gets overwrite and shoes
2 . i saw somewhere that getSupportActionbar.hide() should come before , setContentView()
here is the code snippet of onConfigurationChange
#Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
int accelometerState = android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
if (accelometerState == 1) {
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// toolbar.setVisibility(View.GONE);
// getSupportActionBar().hide();
isRotate = true;
}
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// toolbar.setVisibility(View.VISIBLE);
// getSupportActionBar().show();
isRotate = false;
}
} else {
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
} catch (Exception ex) {
Log.e("home", "something went wrong in the VideoViewFullScreen.java");
}
}
i am trying to figure out what is the problem here ,
if anyone has some solution or suggestion i appreciate them .
Thank You in advance

Well in this case you can define a separate layout for the landscape mode using orientation qualifiers in the layout resource files. Learn more.

Related

How to keep updating recyclerview in the next activity by the data flow sent by BLE device in current activity?

I am completely new to Android and just learned Object-oriented programming. My project requires me to build something on open-source code. I am really struggling with this special case. Two fragments are under activity_main, one of them is TerminalFragment. If TerminalFragment is active under activity_main, data from a BLE device will keep flowing into activity_main. Is it possible to keep passing the data to the next activity without clicking a button (in this case, menuItem(R.id.plot))? I added a recyclerview on activity_main2 and want it display the data which is flowing in activity_main.
In my testing with my current method, the recycler view won't update itself, it just captures the data in the recyclerview of activity_main with TerminalFragment once the user clicked the menuItem (id,plot). What kind of thing I need to add in my method? Should I create another Fragment instead of activity_main2? As I looked this up in the internet, it seems not possible to work that way between a fragment and next activity. Much Appreciated.
onOptionsItemSelected of TerminalFragment
if (id == R.id.plot){
Intent intent = new Intent(getActivity(), MainActivity2.class);
intent.putExtra("output",output); //output
startActivity(intent);
return true;
}
receive() of TerminalFragment
private void receive(byte[] data) {
if(hexEnabled) {
receiveText.append("Hello" + TextUtil.toHexString(data) + '\n');
} else {
String msg = new String(data);
if(newline.equals(TextUtil.newline_crlf) && msg.length() > 0) {
// don't show CR as ^M if directly before LF
msg = msg.replace(TextUtil.newline_crlf, TextUtil.newline_lf);
// special handling if CR and LF come in separate fragments
if (pendingNewline && msg.charAt(0) == '\n') {
Editable edt = receiveText.getEditableText();
if (edt != null && edt.length() > 1)
edt.replace(edt.length() - 2, edt.length(), "");
}
pendingNewline = msg.charAt(msg.length() - 1) == '\r';
}
receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0)); //print out data
output = receiveText.getText().toString(); // CharSequence to String
}
}
OnCreate of mainActivity2, *receiveData() is to add data to the recyclerview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
recyclerView = findViewById(R.id.dataflow);
dataList = new ArrayList<>();
String data;
Bundle extras = getIntent().getExtras();
if (extras != null)
{
data = extras.getString("output");
receiveData(data);
}
setAdapter();
}
i think your problems is unknown how to get activity_main data onto activity_main2 dynamically , You can user " EventBus " to solve data updates dynamically , whatever you use another fragment or activity_main2 .

Android execute after a view is complete

I am new to this, so go easy on me.
I am trying to execute a piece of code that needs to know the new width and height of an ImageView after the device is rotated.
The following code catches the configuration change, but the ImageView hasn't actually been changed yet, so I cannot get an accurate height and width.
// This fires whenever the configuration of the device changes (Portrait to Landscape of vice versa)
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
// This procedure moves the items based on the new size of the ImageView.
this.ShowMeasurements();
}
I have searched and looked at a number of ways that should work, but I am coming up short.
Any advice would be appreciated.
To be sure, that view has an accurate sizes you can try this code in onCreate():
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int viewWidth = imageView.getWidth();
int viewHeight = imageView.getHeight();
if (viewWidth > 0 && viewHeight > 0) {
//do some stuff here
}
//we remove this listener to not being notified every time the view
//changes it's size.
//But if you need this info every time, just remove the last line
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
where, imageView is a reference to your ImageView

Android - Finding ID of view from inflated layout

I know there are lots of other answers on stackoverflow on the same thing but I can't seem to get it to work.
What I'm trying to do is find the ID of a view from an inflated layout. I want WV1 to load google.com when the button is clicked, you can see I'm using onClick from XML to do this.
public void ButtonClicked(View view)
{
View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, null);
WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
WV1.setWebViewClient(new InsideWebViewClient());
if (WV1.isShown()) {
WV1.requestFocus();
}
else{
}
if (WV1.isFocused()) {
WV1.loadUrl("http://www.google.com");
}
}
This is in the MainActivity, the webview (WV1) is in the other, inflated class.
Problem is, nothing happens at all...
I've been stuck on this for quite some time now, I appreciate all help given to me.. If there's any other information you require then just ask, thanks in advance!
--Edit--
In the MainActivity, theres tabhost and a button that creates new tabs. When new tabs are created the MainActivity inflates the second class file containing the webview, I can't get the mainactivity to find the webview from the inflated class. I dont know if this helps any more or not...
Check out this link:
Android - Add textview to layout when button is pressed
Where one of the answers does this:
mLayout.addView(createNewTextView(mEditText.getText().toString()));
where mLayout is a linear layout in the activity.
You'd have to add a view to one of your current layouts or start up an activity that opens up with a web view in it.
Alternative way to create webview :
Webview WV1 = new WebView(view.getContext);
WV1.setWebViewClient(new InsideWebViewClient());
if (WV1.isShown()) {
WV1.requestFocus();
}
else{
}
if (WV1.isFocused()) {
WV1.loadUrl("http://www.google.com");
}
}
Now add this to a view group
viewgroup.addchild(WV1);
You're not attaching it to anything. You need to either supply the parent when you inflate it, or call addView on the ViewGroup that should contain it.
public void ButtonClicked(View view){
View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, (ViewGroup) findViewById(android.R.id.content));
WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
WV1.setWebViewClient(new InsideWebViewClient());
if (WV1.isShown()) {
WV1.requestFocus();
}
if (WV1.isFocused()) {
WV1.loadUrl("http://www.google.com");
}
}
Or:
((ViewGroup)findViewById(android.R.id.content)).addChild(WV1);
Both of these will add your view at the end. You may need to add some layout attributes to get it to look like you want.

Listener for Orientation: Portrait and Landscape?

I've an app which calculate the subnettable.
But if I change the orientation the TextViews are empty.
I search for an listener for this. Alternatives are welcome to :D
In the listener I'd recalc the table.
Use the following code in you activity:
#Override
public void onConfigurationChanged(Configuration myConfig) {
super.onConfigurationChanged(myConfig);
int orient = getResources().getConfiguration().orientation;
switch(orient) {
case Configuration.ORIENTATION_LANDSCAPE:
// handle landscape here
break;
case Configuration.ORIENTATION_PORTRAIT:
// handle portrait here
break;
default:
}
}
Use OrientationEventListener:
orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
public void onOrientationChanged(int orientation)
{
//do something
}
};
In your manifest > activity tag add:
<activity
android:name="com.activity.name"
android:configChanges="orientation|screenSize"
This will prevent your activity from getting recreated when you rotate or change your orientation and hence you wont get blank TextView (will retain your previous value)

Launch Different View Layout based on JSON Key Values

In my android application, I want to display different App Layout based on JSON Config files from server, For example if config Layout key is Set to ListView than show ListView when App Launched, Same way if Config Layout key is set to GridView than show GridView in app.
For this purpose I have :
public void sendView(View view) throws JSONException, IOException{
String[] viewType = pm.getScreetypeConfigFromJsonElement();
if ( viewType[0] == "LV"){
Intent intent = new Intent(this, AUListView.class);
startActivity(intent);
}else if(){....} //For any other view
}
And now Im calling this function inside MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
sendView(/*Cant solve how to pass view */);
} catch (JSONException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
.
.
.
}
How can I fix this issue of having to Launch different AppLayout Based on JSON Config Values in Android.
There are many ways to do this. One simple solution is to make different layouts and call setContentView() depending on the one you want. For example, make 2 layouts for your activity. One of them has a ListView and another one has a GridView say. You can inflate based on that:
String[] viewType = pm.getScreetypeConfigFromJsonElement();
if ( viewType[0] == "LV"){
setContentView(R.layout.main_layout_with_list_view);
else if ( viewType[0] == "GV"){
setContentView(R.layout.main_layout_with_grid_view);
You can put this code in your onCreate() method to dynamically inflate the layout based on your JSON values.
after super.onCreate(savedInstanceState); call the method where you are checking the json keys in that method use setContentView(R.layout.activity_main); in your conditions
if(viewType[0]== type1)
setContentView(R.layout.yourlayout1);
else if(viewType[0]== type2)
setContentView(R.layout.yourlayout2);
you dont have to have different activities have multilpe layouts and put all the code in this activity and it will work as per your requirement

Categories

Resources