defined xml layout in layout-land does not appear in android application - android

I am developing an application according to this example. I defined a landscape layout for header.xml in a layout-land folder, but when I change the orientation to landscape, defined layout does not appear in the screen.
Do know Why ?
Thanks
Updated :
Activity Code :
public class ACENewsFeedActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Array list for list view
ArrayList<HashMap<String, String>> rssItemList = new ArrayList<HashMap<String,String>>();
RSSParser rssParser = new RSSParser();
List<RSSItem> rssItems = new ArrayList<RSSItem>();
RssFeed rssFeed;
private static String TAG_TITLE = "title";
private static String TAG_LINK = "link";
private static String TAG_DESRIPTION = "description";
private static String TAG_PUB_DATE = "pubDate";
//private static String TAG_GUID = "guid"; // not used
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rss_item_list);
/**
* Calling a backgroung thread will loads recent articles of a website
* #param rss url of website
* */
new loadRSSFeedItems().execute();
}
....
}
XMl Layout in landscape mode :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:background="#layout/header_gradient"
android:orientation="horizontal">
<!-- Logo -->
<!-- Refresh -->
<!-- Plus Button -->
<ImageButton
android:id="#+id/btnAddSite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:background="#null"
android:src="#drawable/plus"
android:layout_centerVertical="true" />
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/logo" />
<ImageView
android:id="#+id/refreshList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/refresh" />
</RelativeLayout>

Android allows you to provide different versions of resource files to support specific device configurations including screen size/resolution and (as you are trying to do) device orientation. When android is loading a layout file it will look first in the res/layout-port folder (if it is in portrait orientation) or in the res/layout-land folder (if it is in landscape orientation). If it doesn't find the file it will then look in the regular res/layout folder.
Additionally, as noted here, when certain device configurations change (like device orientation) during runtime android will restart whatever process is currently running by saving the state, destroying it, and then starting it with the saved state info. This allows it to load the layout files again, and it will look it the folder for the new orientation when it tries to load them.
So, if you start your application in portrait it will load the file in res/layout-port or res/layout. If you then rotate the device to landscape it will destroy your process and restart. However, this time it will be in landscape so it will check res/layout-land instead for the layout files.
If you have your files set up this way but it is not operating as you think it should, I'd first verify that it is definitely not using the correct files by putting two different header.xml files in the layout-land and layout-port folders, maybe one with a red background and one with a green background. Make sure to double-check the file references and maybe use Toast to post some debugging info on-screen to ensure that it is inflating layouts properly.
The default behavior is for android to handle the orientation change (which involves destroying your activity and creating a new instance of it which will reload all layout files). This default behavior will always occur unless your activity tag in your manifest file contains the property android:configChanges="orientation". (This tag can take arguments other than orientation - android will handle the config changes for all events except the ones you pass as arguments to this tag.)
If you include the android:configChanges="orientation" tag you are telling android NOT to destroy your activity and NOT to reload layout files when the orientation of the device changes. Instead of its default behavior it will call a method (which you define) to allow you to make any changes you wish to make to handle the orientation change yourself rather than letting android handle it automatically. It's intended so that if destroying your activity would be a major inconvenience it doesn't have to be automatically destroyed.
EDIT: added some things from the comment discussion

you should the define android:configChanges="orientation" in the manifest file for that activity and override the onConfigChanged() method in that setContentView()
Like so:
#Override
public void onConfigurationChanged(Configuration newConfig) {
setContentView(R.layout.your_xml);
super.onConfigurationChanged(newConfig);
}

Related

In Android: How to Force refresh (update) of a TextView that is INSIDE a Dialog Window?

This is probably a basic procedure, and in fact I've been extensively searching for a suitable answer, but I haven't found anything usable or that actually works. Now, the case:
A Dialog window is placed inside a method:
public void method_with_Dialog_code() {
Dialog simpleDialog = new Dialog(this, R.style.FilterDialogTheme);
simpleDialog.setContentView(R.layout.dialog_xml_layout);
simpleDialog.setCancelable(true);
TextView insideTextView = (TextView) simpleDialog.findViewById( R.id.insidetextview );
insideTextView.setText("This text should change when the WiFi is offline");
simpleDialog.show();
}
The respective dialog_xml_layout.xml file is simply:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/insidetextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
If the device is for example disconnected from the Internet, it generates a message that should be shown in the insideTextView. But notice that simpleDialog and insideTextView are inside the method, so they are local objects, so the first issue is how to execute:
insideTextView.setText("This device is now offline");
from another part of the code, that is, outside of the method?
If I decide to make simpleDialog and insideTextView as Global variables, I can with no problem, from another part of the program, set the line:
insideTextView.setText("This device is now offline");
But the instruction doesn't work. The TextView is never updated with the new message.
So, any ideas? Maybe with TextView.addTextChangedListener, so insideTextView could be updated when the TextView.setText is executed externally?
Gracias.
make insideTextView a member variable of the class and use it later.

Add an image to an android splash screen in intellij android studio? [duplicate]

This question already has answers here:
How do I make a splash screen? [closed]
(31 answers)
Closed 8 years ago.
Okay so what I'm wanting to do is add an image to my splash screen, so it displays the image before the app starts. I think(?) I found the right code to actually do the splash screen but I can't get the image in it. From what I've read it needs to be in a png file, which it is but how do I move it from a file on my computer to the code, and then where do I go from there?
Assuming that you have exactly the code given in How do I make a splash screen? then you simply need to save your picture as splash.png in the app/main/src/res/drawable folder. Be sure to clean and rebuild your project before running it. Note that you can give the PNG any name you want. Just change splash in android:src="#drawable/splash" to match the name you use. Also, I strongly encourage you to learn about the directory structure in an Android Studio project.
My post here answers this question.
To "move" the image from a file to your code, you need to place it into your drawable folder, then refrence it somewhere by using
#drawable/image
To better understand how to change the splash screen image please read below.
Add Splash Screen Image
First you need a splash screen image. Because Android devices come in
various resolutions, you may want to ship several splash screens as
described in Google's Best Practices for Supporting Multiple Screens.
For simplicity, we'll just ship one here that is 480x800. It should
support most phone sizes pretty well, and Android will scale it as
best it can.
Add the the image/gif you want into your Resources\Drawable
You need to define the splash screen in your layout.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView id="#+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/splash"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent" <!-- Not needed->-->
android:layout_height="wrap_content"
android:text="Hello World, splash"/> <!--Not Needed -->
</LinearLayout>
And your activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 1000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}

Does Android Have MasterPage Concept like .NET or Tiles concept in Struts to add Header on all pages?

I am developing an Android Application. In this application, Logo bar is shown on all pages(Activities) or we can say it has header on all pages.
This Logo Bar have few icons like Home, Login, Notification, etc. and on Clicking on these icons corresponding navigation will perform.
for example if user is any where in application and click on home icon, he will navigate to the home page of application.
I am able to inflate logobar.XML into my All Activity by coding. but problem is i have to call onClickListener on all pages for all icons in Logo Bar.
This is not a good programming way.
How can i implement Logo Bar Activity in all other activity without repeating of code?
Is android have any Master Page concept as in .Net or tiles concept as in Struts?
Please guide me.
Edit: ok i got it. may be this answer will help you.
Try using Tab widget with tabactivity check this link for using fragment and tab http://developer.android.com/reference/android/app/TabActivity.html for android. i think for lower versions also we can use this. this si what the link says - "you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT."
you have to create your masterLayout in xml and that you have to include it in your other
layouts in which you have to have it.
The solution was pretty easy.
You need to extends "Activity" Class,in onCreate function SetContentView to your base xml layout and also need to override setContentView in base Activity Class
For Example:
1.Create "base_layout.xml" with the below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000"
android:padding="15dp" >
<LinearLayout android:orientation="horizontal" android:background="#000000"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="50dp" android:paddingLeft="10dp">
<ImageView android:layout_width="wrap_content" android:id="#+id/ImageView01"
android:adjustViewBounds="true" android:layout_height="wrap_content"
android:scaleType="fitCenter" android:maxHeight="50dp" />
</LinearLayout>
<LinearLayout android:id="#+id/linBase"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</LinearLayout>
2.Create "BaseActivity.java"
public class BaseActivity extends Activity {
ImageView image;
LinearLayout linBase;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_layout);
image = (ImageView)findViewById(R.id.ImageView01);
image.setImageResource(R.drawable.header);
linBase = (LinearLayout)findViewById(R.id.linBase);
}
#Override
public void setContentView(int id) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(id, linBase);
}
}
and
public class SomeActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.some_layout);
//rest of code
}
}
The only thing I noticed so far was that when requesting a progress bar (requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)) this needs to be done before calling super.onCreate. I think this is because nothing can be drawn yet before calling this function.
This worked great for me and hopefully you will find this useful in your own coding.
There is something like that, but only available on api 11+ (3.2 and Android 4.0.x Ice Cream Sandwich). Its called actionbar ( http://developer.android.com/reference/android/app/ActionBar.html).
I have done this using XML file.
I am just creating runtime view from XML file , and add it to the Activity layout.
I have created method for that
public static void setLoginview(Context ctx, RelativeLayout layout) {
LayoutInflater linflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = linflater.inflate(R.layout.loginheader, null);
layout.addView(myView);
try {
layout.getChildAt(0).setPadding(0, 50, 0, 0);
} catch (Exception e) {
}
}
ctx is the application contetx and layout is the layout in which i want to add that view.

How to overlay a button programmatically?

What I would like to accomplish is to, at runtime, place a button in the middle of the screen, as the very top layer, overlaying anything below it. (It's not big, so it will not completely cover the screen, just whatever happens to be below it.)
I looked at creating a custom dialog, however that blocks all other user input. I want all of the views below this new button to act normally and respond to the user, but I just want to add (and later remove) the button above everything.
Hopefully that makes sense. I'm just wondering what might be the best approach to look into?
Use a FrameLayout, with the button as it's 2nd child. Set it to GONE when you don't want it visible.
I had to overlay a simple layout programmatically on top of any visible activity. Normal activity layout xmls don't know anything about the overlay. Layout had one textview component but could have any structure you see fit. This is my overlay layout.
res/layout/identity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/identitylayout"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<TextView
android:id="#+id/identityview"
android:padding="5dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#FFFFFF" android:background="#FF6600"
android:textSize="30dp"
/>
</RelativeLayout>
Overlay is shown on top of the existing content, after timeout is deleted from the screen. Application calls this function to display overlay.
private void showIdentity(String tag, long duration) {
// default text with ${xx} placeholder variables
String desc = getString(R.string.identity);
desc = desc.replace("${id}", reqId!=null ? reqId : "RequestId not found" );
desc = desc.replace("${tag}", tag!=null ? tag : "" );
desc = desc.trim();
// get parent and overlay layouts, use inflator to parse
// layout.xml to view component. Reuse existing instance if one is found.
ViewGroup parent = (ViewGroup)findViewById(R.id.mainlayout);
View identity = findViewById(R.id.identitylayout);
if (identity==null) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
identity = inflater.inflate(R.layout.identity, parent, false);
parent.addView(identity);
}
TextView text = (TextView)identity.findViewById(R.id.identityview);
text.setText(desc);
identity.bringToFront();
// use timer to hide after timeout, make sure there's only
// one instance in a message queue.
Runnable identityTask = new Runnable(){
#Override public void run() {
View identity = findViewById(R.id.identitylayout);
if (identity!=null)
((ViewGroup)identity.getParent()).removeView(identity);
}
};
messageHandler.removeCallbacksAndMessages("identitytask");
messageHandler.postAtTime(identityTask, "identitytask", SystemClock.uptimeMillis()+duration);
}
Timer messageHandler is member of main Activity instance (private Handler messageHandler) where I put all scheduled tasks. I am using Android 4.1 device lower than that I don't know what happens.

Android activity loads wrong layout file

I am developing a simple app where you can see a list of musicians, and when you click on a musician, you get some details about it (genre, top hits and biography).
The functionality that I want to achieve is this: in case you're viewing it on a wide screen, let the list of musicians be on the left and the details should appear on the right (once a musician is clicked); if it's a narrow screen, the details should appear separately on a new screen. The functionality should be accomplished with fragments and Framelayouts.
So I have the activity_main.xml, and I went to Add new resource file -> layout file and I added activity_main.xml (w600dp) which I expect to automatically load on landscape-oriented smartphones or tablets.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/muzicari_lista"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
w600dp/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/lista_muzicara"></FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/detalji_muzicar"></FrameLayout>
</LinearLayout>
XML's of fragments that contain the list and the details are fairly simple with a few textblocks.
Here's the onCreate method of the mainActivity's class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unosi = new ArrayList<Muzicar>(); //let's presume there's something in this list
wideL = false;
FragmentManager fm = getFragmentManager();
//fetching FragmentManager
FrameLayout ldetalji = (FrameLayout)findViewById(R.id.detalji_muzicar);
if(ldetalji!=null){
//layout for wide screens
wideL=true;
FragmentDetalji fd;
fd = (FragmentDetalji)fm.findFragmentById(R.id.detalji_muzicar);
//checking if there is FragmentDetalji already created:
if(fd==null) {
//if not, we're creating it now
fd = new FragmentDetalji();
fm.beginTransaction().replace(R.id.detalji_muzicar, fd).commit();
}
}
FragmentLista fl = (FragmentLista)fm.findFragmentByTag("Lista");
//we're checking if there's already fl created
if(fl==null){
//if it hasn't been created:
fl = new FragmentLista();
Bundle argumenti = new Bundle();
argumenti.putParcelableArrayList("Alista",unosi);
fl.setArguments(argumenti);
fm.beginTransaction().replace(R.id.muzicari_lista, fl).commit();
}else{
//case when we change from portrait to landscape, and there was FragmentDetalji already open
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
Classes FragmentDetalji and FragmentLista don't do much special besides utilizing onCreateView (though I can add them if they might be the source of the trouble).
The problem is that no matter what I use for loading this app, activity_main gets loaded in its default form, not the w600dp version! What am I doing wrong?
To provide alternative layout resources, you need to create a sub-directory inside your res folder named layout-sw600dp and place your altered layout in there, with the EXACT name as the other small screen layout. Then your Activity should automatically look in this folder. So if your xml file is titled activity_main.xml it should have the exact same name in the layout-sw600dp folder as well.
You can also supply alternate layouts for landscape by creating a sub-directory called layout-land or layout-sw600dp-land for large screen.
Also note that there are differences between using sw600dp and w600dp -- See this question for more info: Difference between sw600dp and w600dp?

Categories

Resources