I am having a bunch of trouble adding a TextView to my LinearLayout. Here is what I have:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some button text" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout);
TextView textView = new TextView(this);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setText("Hello, world!");
mainLayout.addView(textView);
}
}
I don't get any errors in LogCat, it just doesn't work.
Any ideas?
There is no space for your TextView because Button is taking them all.
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="some button text" />
Change it to wrap_content
Make sure the spacing between your views is correct
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some button text" />
</LinearLayout>
You can also try mainLayout.addView(newView, 0); to add it before the button for verification
Brian is right. There is also an typo in the first line. I guess it should be android.com
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
and not android/com.
Related
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>
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
I am trying to make dynamic layout in which there is a scroll view.
In the scroll view , I am adding vertical linear layout which references main.xml.
The code stops as soon , I try to add the layout referencing main.xml.
ScrollView scroll;
EditText search;
Button checkin;
LinearLayout vt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
scroll=new ScrollView(getApplicationContext());
vt=(LinearLayout) findViewById(R.id.llv);///referencing linear layout located in main.xml.This is where problem the is occurring.
//vt.setOrientation(LinearLayout.VERTICAL);
scroll.addView(vt);
this.setContentView(scroll);
}
main.xml ( where R.id.llv is located )
<?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" >
<LinearLayout
android:id="#+id/llv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
try to add
setContentView(R.layout.main);
before:
vt=(LinearLayout) findViewById(R.id.llv);
try this :
view row = LayoutInflater.from(this).inflate(R.layout.main, null);
LinearLayout layout=row.findViewById(R.id.YourLayout);
Check this to create Linear Layout Dynamically.
http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/
I need to create a single activity tabhost, ideally create tabs and views, and dynamically populate the views with widgets. Having a 'control' button at the bottom of the screen is important.
I am able to create three tabs, using three layouts. Getting the EditTexts to a) appear, and b) line up below the tabs [using the separator] was a challenge, assisted by existing answers on StackOverflow.
What I now cannot work out is why the EditTexts on the second layout/tab don't appear, whereas those on the first tab do?
The layout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<View
android:id="#+id/separator"
android:layout_below="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="2dip" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_below="#+id/separator"
android:layout_above="#+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tabview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/ll1text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="ll1text" />
</LinearLayout>
<LinearLayout
android:id="#+id/tabview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/ll2text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="ll2text" />
</LinearLayout>
<RelativeLayout
android:id="#+id/tabview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/rl3text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:text="ll3text" />
</RelativeLayout>
</FrameLayout>
<Button
android:layout_alignParentBottom="true"
android:text="Start Travelling"
android:id="#+id/btnSend"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</RelativeLayout>
</TabHost>
The activity code
setContentView(R.layout.template);
TabHost th = getTabHost();
th.addTab(th.newTabSpec("tab1").setIndicator("Customer").setContent(R.id.tabview1));
LinearLayout ll = (LinearLayout) findViewById(R.id.tabview1);
EditText et1 = new EditText (this);
et1.setText("ET1");
ll.addView(et1);
TextView tv2 = new TextView(this);
tv2.setText("et2:");
ll.addView(tv2);
EditText et2 = new EditText (this);
et2.setText("ET2");
ll.addView(et2);
th.addTab(th.newTabSpec("tab2").setIndicator("Job").setContent(R.id.tabview2));
ll = (LinearLayout) findViewById(R.id.tabview2);
EditText et21 = new EditText (this);
et21.setText("et21");
ll.addView(et21);
EditText et22 = new EditText (this);
et22.setText("et22");
ll.addView(et22);
EditText et23 = new EditText (this);
et23.setText("et23");
ll.addView(et23);
th.addTab(th.newTabSpec("tab3").setIndicator("Tab 3").setContent(R.id.tabview3));
RelativeLayout rl = (RelativeLayout) findViewById(R.id.tabview3);
EditText et3 = new EditText (this);
et3.setText("ET3");
rl.addView(et3);
th.setCurrentTab(0);
How come the three EditTexts on the second tab don't show?
I think the problem is that you are trying to hold all your tabs under one activity. You should separate them as follows:
Use a class that deals with the TabActivity
Create different Activities for each of your tabs
Add a tabhost layout
Check out this tutorial for help.
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.