I have this activity,
package org.dewsworld.ui;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
TextView textView ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView1) ;
textView.setText("hello ") ;
}
}
And main.xml is
<?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:id="#+id/textView1">
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="wrap_content" android:text="TextView" />
</LinearLayout>
And the logcat is
And I can't find the error... :(
<?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" >
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="wrap_content" android:text="TextView" />
</LinearLayout>
try now
You have 2 views with the same ids Linearlayout and TextView. remove the id for the LinearLayout and try it again. It will work.
Related
Following this tutorial I moved my ActionBar (Toolbar) from my main layout to another layout and I include the toolbar to my main layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/my_toolbar">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
And my toolbar layout is this:
<?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.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/CustomActionBarTheme"/>
</LinearLayout>
But I got the following error
Why I cant make reference of my toolbar if it has the same id? Can I include my toolbar and make reference so I can put elements below the toolbar?
Edit 1 - Activity Code
package com.marcelo.notemuevas;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] itemname ={
"Carpinteria"
};
String[] descripcion={
"Reparación"
};
Integer[] imgid={
R.mipmap.icono1
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
myToolbar.setLogo(R.mipmap.ic_launcher);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid, descripcion);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, carpinteria.class);
myIntent.putExtra("key", 5); //Optional parameters
MainActivity.this.startActivity(myIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
Edit 2 - Solution
Now I make it work, I added a new Id to the include then I make reference to it like James Lockhart and Phan Văn Linh suggest (dont know why the first time didnt work) so my new question is, the IDs works like variables? only exist where it has been declared?
This is the working xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbarLayout"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbarLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Servicios"
android:id="#+id/serviciosTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/colorPrimaryDark"
android:textColor="#FFFFFF"
android:gravity="center"
android:paddingTop="18dp"
android:paddingBottom="18dp"
/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/serviciosTxt">
</ListView>
</RelativeLayout>
</RelativeLayout>
In your XML
<include
android:id=#"+id/toolBarLayout" // set the id for layout that contains toolbar
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolBarLayout"> // change my_toolbar to toolBarLayout
Then you access to your Toolbar by change
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
to
LinerLayout myLinear = (LinerLayout) findViewById(R.id.toolBarLayout);
Toolbar myToolbar = (Toolbar) myLinear.findViewById(R.id.my_toolbar);
Please try this:
In your toolbar layout file, in the LinearLayout tag, set the 'android:id' attribute to a unique layout id name, for example: android:id="#+id/toolbarLayout"
Once you have completed (1), in your main layout refer to your toolbar layout file as follows: android:layout_below="#id/toolbarLayout"
Hope this helps.
First post. I'm following a few courses on Android programming so fairly new but not a complete newbie. I have a background in programming from long ago..
For some odd reason, older projects have the OnClick running fine but every project I created today, with either Genymotion or AVD the OnClick never fires even in the following barebone example I created.
https://www.dropbox.com/s/js1qh263vldte0z/deleteme.zip?dl=0
Here's the original project I'm working on which I can't even click on the buttons (as if there's a transparency in front or something).
If someone can explain why when I open older projects based on 22 everything works but running now on 23 (with 16 as backward) OnClick doesn't work that'd be very nice of you. Thanks!
The 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"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="ca.shaarable.planetxerox.admin_add_products_hardware">
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_first"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2">
<TextView
android:id="#+id/admin_add_printer_tv_name"
android:text="#string/admin_add_printer_tv_name"
android:textSize="12sp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/admin_add_printer_et_name"
android:text="#string/admin_add_printer_et_name"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_second"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.65">
<TextView
android:id="#+id/admin_add_printer_tv_thumbnail"
android:text="#string/admin_add_printer_tv_thumbnail"
android:textStyle="italic"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/admin_add_printer_image"
android:src="#drawable/igen5"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="#+id/admin_add_printer_third"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.15">
<LinearLayout
android:id="#+id/admin_add_printer_sublayout"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/admin_add_printer_CANCEL"
android:text="#string/admin_add_printer_CANCEL"
android:background="#F99F1C"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/admin_add_printer_ADD"
android:text="#string/admin_add_printer_ADD"
android:background="#2ABDBA"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And here's the code for the class:
package ca.shaarableapps.presssupport;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class AdminAddPrinter extends AppCompatActivity implements View.OnClickListener
{
private EditText printerName;
private ImageView printerThumbnail;
private Button cancelBtn, addBtn;
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_add_printer);
printerName = (EditText)findViewById(R.id.admin_add_printer_et_name);
printerThumbnail = (ImageView)findViewById(R.id.admin_add_printer_image);
cancelBtn = (Button)findViewById(R.id.admin_add_printer_CANCEL);
addBtn = (Button)findViewById(R.id.admin_add_printer_ADD);
}
#Override
public void onClick(View v)
{
Log.v(TAG, "Nooooooooooooo");
switch (v.getId())
{
case R.id.admin_add_printer_ADD:
addBtn.setText("Good Job");
break;
}
}
}
This is the layout as seen by the user...
Try to add .setOnClickListener(this); inside onCreate(...) method.
protected void onCreate(Bundle savedInstanceState){
...
addBtn.setOnClickListener(this);
}
Hope this help
The problem is here in you code.
<Button
android:id="#+id/admin_add_printer_ADD"
android:text="#string/admin_add_printer_ADD"
android:background="#2ABDBA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick" // add this line
/>
i want a page with text on top(EditText) then image (in FragmentLayout) and then two tabs below that image.
my output is not showing any tab in output but i have written code to implement tabs.
this code is notshowing any error and in output i mam not getting any tab in output
i am not getting correct output and this is not even showing any error
i tried it without extending TabActivity also (only with extens Activity) but still output is same whose image is loaded above
Code of activity_main.xml is
<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=".MainActivity" >
<TextView
android:id="#+id/tbol"
android:layout_width="250dp"
android:layout_height="30dp"
android:text="#string/heading"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#F00"
android:textSize="25sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#drawable/leaf" >
</FrameLayout>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
code of MainActivity.java is
package com.example.tabhost;
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
TabHost th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
th=getTabHost();
//For desc tab
TabSpec descspec=th.newTabSpec("Description");
descspec.setIndicator("Description", null);
Intent firstintent=new Intent(this, DecsriptionTab.class);
descspec.setContent(firstintent);
//For Information Tab
TabSpec infospec=th.newTabSpec("Information");
infospec.setIndicator("Information", null);
Intent secondintent=new Intent(this, Info_Tab.class);
descspec.setContent(secondintent);
}
}
this is code of 2 .xml files
desc_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="This is desc tab"
android:padding="15dp"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
info_layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="This is info tab"
android:padding="15dp"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This is code of corresponding java files
DecsriptionTab.java
package com.example.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class DecsriptionTab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.desc_layout);
}
}
Info_Tab.java
package com.example.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class Info_Tab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info_layout);
}
}
Hi i have an activity class that has a layout, the layout itself has a custom view, this is the activity class:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Minesweeper extends Activity {
public static final String KEY_DIFFICULTY = "minesweeper.difficulty";
public static final int DIFFICULTY_EASY = 1;
public static final double DIFFICULTY_MEDIUM = 1.5;
public static final int DIFFICULTY_HARD = 2;
private int diff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
diff++;
Log.d("diff", String.valueOf(diff));
Mine.setlevel(diff);
setContentView(R.layout.minesweeper);
Mine.clearMines();
}
}
and here's the layout of the activity:
<?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="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background" >
<com.mine_sweeper.Table
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>`
everything's good on graphical view in eclipse on layout file but when i try to add a button to the layout file it only sticks to the left side of my custom view witch is a 9*9 grid, so my question is how to move it under my custom veiw?
i have tried changing custom view's layout_width and layout_height already.
Try this. User vertical or horizontal to change positions in nested LinearLayout. android:orientation="vertical" or android:orientation="horizontal"
`
<?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="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button" />
<com.mine_sweeper.Table
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
`
You can also user other layouts like GridLayout etc. this is just one approach using LinearLayout
The activity_main.xml is like this
<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" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>
The main activity class is like this
package com.example.testfragmentshoneycomb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
first_fragment.xml is like this
<?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="wrap_content"
android:orientation="vertical"
android:background="#color/grey" >"
<TextView
android:id="#+id/text_view_one_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View one" />
<TextView
android:id="#+id/text_view_two_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View two" />
<TextView
android:id="#+id/text_view_three_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View three" />
</LinearLayout>
FirstFragment class is like this
package fragments;
import com.example.testfragmentshoneycomb.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
return view;
}
}
It shows only first button and nothing else on the screen. If I remove the first button from activity_main.xml it shows the fragment but doesn't show the second button.
Min SDK version is 11 and build target is android 4.1
set android:orientation="vertical" in your activity layout.
Its because by default LinearLayout's orientation is horizontal. Therefore the whole screens width is captured by the First Button and Fragment.
Are you sure you want to see it like?
First_Button Fragment Second_Button
If Yes the use layout_weight. If No then give orientation=vertical to LinearLayout which will show your layout output as
First_Button
Fragment
Second_Button
set LinearLayout orientation to vertical, it's horizontal by default.
read docs carefully
Use the following layout :
<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" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>