Visibility of ScrollView - android

I am using two ScrollViews and want to set visibility of one as GONE or INVISIBLE but when I do it by following java code the application terminates. If I do the same thing using Properties pan, in the eclipse, it just works. What is wrong?
Java code is:
public class Test extends Activity {
List<ScrollView> sv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
setContentView(R.layout.main);
}
}
main.xml
<?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"
android:gravity="fill_vertical">
<ScrollView android:layout_height="wrap_content" android:id="#+id/scrollView1" android:layout_width="match_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/linearLayout1" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ScrollView>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scrollView2">
<TextView android:text="TextView" android:layout_width="match_parent" android:id="#+id/textView2" android:layout_height="match_parent"></TextView>
</ScrollView>
</LinearLayout>

you haven't setContentview(yourLayout.xml)
it wont be able to findViewById.
do the findViewById AFTER the setContentView

First of you have to set layout file.
setContentView(R.layout.new_view);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
try this if you are getting any error then please post logcat.

Related

TextView with wrap_content height obscured by ListView below it

I am not seeing my "Hello World" textview.
The listview, which I populate with elements, is all that shows.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
tools:context="lister.quantumproductions.com.lister.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_above="#+id/recipe_list_view"></TextView>
<ListView
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadListView();
}
private void loadListView() {
mListView = findViewById(R.id.recipe_list_view);
final ArrayList<Recipe> recipes = Recipe.getRecipesFromFile("recipes.json", this);
RecipeAdapter a = new RecipeAdapter(this, recipes);
mListView.setAdapter(a);
}
}
Your ListView fills the entire screen, and then you say that the TextView should be above the ListView, which makes it appear outside the screen, so it's not visible.
You need to invert the dependency.
So instead of the TextView being above the ListView, the ListView should be below the TextView.
RelativeLayout causes the last item covers the previous one if it has match_parent width and height. Change the order or use LinearLayout instead of.
you can use LinearLayout as the parent with Vertical Orientation it will work perfectly:-
I have corrected your xml code , use this code it works
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="lister.quantumproductions.com.lister.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_above="#+id/recipe_list_view"></TextView>
<ListView
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
it will work and both textview and listview will be shown .
incase you want to use relative layout only just make listview also wrap_content
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
tools:context="lister.quantumproductions.com.lister.MainActivity">
<TextView
android:id="#+id/recipe_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"></TextView>
<ListView
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/recipe_text_view"></ListView>
</RelativeLayout>
try this
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"></TextView>
<ListView
android:layout_below="#id/text_view"
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>

Changing layouts error?

Trying to change a layout from an ImageButton but when i run it on my phone it does not switch instead it says program has stopped.
Java;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton ImageButton = (ImageButton) findViewById(R.id.image);
ImageButton.setBackgroundColor(1);
ImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
setContentView(R.id.aaa);
}
});
}
}
XML for first layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bassammetwally.anew.MainActivity">
<RelativeLayout
android:layout_width="420dp"
android:layout_height="50dp"
android:background="#3955a1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Writer's Block"
android:textColor="#ffffff"
android:textSize="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:id="#+id/Asd"/>
<ImageButton
android:layout_width="20dp"
android:layout_height="40dp"
android:src="#drawable/pen"
android:layout_marginLeft="380dp"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/image"/>
</RelativeLayout>
</LinearLayout>
Second layout to change to;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/aaa">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ans"/>
</LinearLayout>
I think it might have something to do with my syntax.
You need to use a layout reference instead of an id
setContentView(R.layout.view);
Where view.xml is the layout you want.
You may want to rethink your approach, though. Using Fragments is preferred over completely replacing the layout because your ImageButton is no longer usable and you'll need to recall findViewById for any new views that are displayed.
If you just want to load a layout file and add it to the current layout, see Inflating one layout

Android edittext not updating

My edittext's methods work properly as I have checked it on debbug, but in the graphical layout there are no changes at all.
When changing the text programatically, in the graphical layout doesn't appear, but if I try myedittext.getText() on the debbug expressions/watches it returns the correct string, so the settext() method is working OK but the changes are not shown.
Here is the code:
public class easmpartne extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easmpartne);
FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this,
getSupportFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("General"),
Tab1.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Categ"),
Tab2.class, null);
LayoutInflater inflater = this.getLayoutInflater();
viewGeneral= inflater.inflate(R.layout.tab1, null);
viewCateg= inflater.inflate(R.layout.tab2, null);
txtRef=(EditText)viewGeneral.findViewById(R.id.txtRef);
txtRef.setText("example");
txtRef.setEnabled(false);
}
}
Neither settext nor setenabled seems no have any effect on the graphical layout when executing.
easmpartne.xml:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</android.support.v4.app.FragmentTabHost>
tab1.xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:layout_marginLeft="5dp"
android:fillViewport="true"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linlayouteasmpartne"
android:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tablelayout1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/ref"
android:id="#+id/textView" />
<EditText
android:layout_width="170dp"
android:layout_height="wrap_content"
android:id="#+id/txtRef" />
</TableRow>
....
(I don't know if this could be a problem with de tabhost as it is the first time I use it)
Is that the correct way to get the edittext in this case?
What are the possible problems/solutions?
Thanks.
the point is, you see, that the layout is not kind of a shared variable so when you or the runtime reads it, it instances all views referenced inside and you have to stick to it. Then you follow the lifecycle of the components so to set the values you want you should do it in onViewStateRestored(Bundle) when the view is actually done or something like that, or onStart or so

Multiple layout within one activity android

In my activity IndividualActivity i have set the contextview of individual.xml now i also want to add another sub_individual.xml in the activity without removing, replacing or hiding the individual.xml so that they remain together.
public class IndividualActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.individual);
...
}
}
individual.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IndividualActivity"
android:background="#drawable/default_wall" >
<ListView
android:id="#+id/listx"
android:layout_width="fill_parent"
android:layout_height="380dp"
android:dividerHeight="1dp"/>
...
sub_individual.xml
<?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="40dip"
android:layout_marginLeft="0dp"
android:background="#323331"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:text="custom title bar" >
<ImageView
android:id="#+id/bk_btn"
android:layout_width="35dip"
android:layout_height="35dip"
android:background="#DDD" />
...
This functionality is supported by using Fragments. See http://developer.android.com/guide/components/fragments.html
You can use this from xml
Use "include" tag in your first xml and add second xml in this tag.

Android findViewById Working Sometimes

I am having a very weird glitch that I cannot get my head around. Any help will be appreciated as this works on a phone running Android 2.3, tablet running 3.1 but not on a phone running Android 2.1.
I am trying to get the LinearLayout defined in the following XML with the ID of "overview_linear_layout".
overview_view.xml
<?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">
<include layout="#layout/listing_view" android:layout_width="fill_parent" android:id="#+id/header_view" android:layout_height="wrap_content"></include>
<ScrollView android:id="#+id/scrollView1" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent" android:id="#+id/overview_linear_layout">
<TextView android:scrollbars="vertical" android:longClickable="false" android:layout_weight="1" android:clickable="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/main_description"></TextView>
</LinearLayout>
</ScrollView>
<include layout="#layout/listing_view" android:layout_width="fill_parent" android:id="#+id/footer_view" android:layout_height="wrap_content" android:layout_weight="0.0"></include>
</LinearLayout>
The code I am using to get the view is as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview_view);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.overview_linear_layout);
if(linearLayout!=null) {
System.out.println("Found linear layout");
}
else {
System.out.println("Did not find linear layout");
}
}
I have tried Project->Clean in Eclipse but that does not help either.
if eclipse don't show overview_linear_layout when you type click on the red cross and click create constant overview_linear_layout in R .
change this
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.overview_linear_layout);
to
LinearLayout linearLayout = (LinearLayout)findViewById(R.layout.overview_linear_layout);`

Categories

Resources