I create a custom view for actionbar.When i use <inclued layout=#layout/XXXXX />is ok.However ,when i use LayoutInflater to inflate the view ,it cannot inflate it .Here is my code
public class TitleLayout extends LinearLayout {
// private Button btn_back;
// private Button btn_edit;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.view_title,this);
}
}
TitleLayout is my custom view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_back"
android:layout_gravity="center"
android:text="Back"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/text_title"
android:text="This is a title"
android:layout_gravity="center"
android:textSize="30sp"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/btn_edit"
android:text="Edit"/>
</LinearLayout>
It is a simple view; When i use <inclued /> to inflate is ok. But use LayoutInflate will show nothing.
<!--<include-->
<!--layout="#layout/view_title"/>-->
<com.example.administrator.test.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.administrator.test.TitleLayout>
really appreciate your help!!!
try this
public class TitleLayout extends LinearLayout {
public TitleLayout (Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.your_layout, this);
}
}
Related
I am trying to create a custom button layout. I created an xml file "custom_button.xml" which I then inflate in the "ButtonLanguageSelection" class. In this class I added the onClickListener. I added this class to the "main_activity.xml". It behaves like it should except it won't receive any touch events. Then I copied the code from "custom_button.xml" and added it directly without inflating it, I just added the android:onClick parameter and in this way it worked. I can't find out what would be the problem, the layouts are the same.
Has some of you have similar problems? What could be the problem? I attached the code if it helps someone to find the problem.
Thank you for any help!
custom_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button_language_selection_states"
android:weightSum="1"
android:clickable="true"
>
<TextView
android:id="#+id/textview_select_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingTop="30px"
android:layout_weight="0.4"
android:textSize="21sp"
android:textColor="#333333"
/>
<View
android:layout_width="match_parent"
android:background="#drawable/gradient"
android:layout_height="1dp"
android:layout_margin="10px">
</View>
<TextView
android:id="#+id/textview_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:paddingTop="40px"
android:layout_weight="0.6"
android:textSize="28sp"
android:textColor="#ffffff"
/>
</LinearLayout>
ButtonLanguageSelection
public class ButtonLanguageSelection extends LinearLayout
{
private TextView introductoryTextView;
private TextView language;
private final String TAG = "ButtonLanguageSelection";
public ButtonLanguageSelection(Context context) {
super(context);
}
public ButtonLanguageSelection(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.button_language_selection, this);
introductoryTextView = (TextView)findViewById(R.id.textview_select_language);
language = (TextView)findViewById(R.id.textview_language);
this.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onclick");
}
});
}
public ButtonLanguageSelection(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setIntroductoryTextView(String s)
{
introductoryTextView.setText(s);
}
public void setLanguage(String s)
{
language.setText(s);
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/screen_welcome_bg">
<defaultpackage.ButtonLanguageSelection
android:id="#+id/ButtonLanguageSelection_Slovene"
android:layout_width="341px"
android:layout_height="260px"
android:layout_marginRight="2px"/>
<defaultpackage.ButtonLanguageSelection
android:id="#+id/ButtonLanguageSelection_Italian"
android:layout_width="341px"
android:layout_height="260px"
android:layout_marginRight="2px"/>
<!--<defaultpackage.ButtonLanguageSelection-->
<!--android:id="#+id/ButtonLanguageSelection_English"-->
<!--android:layout_width="341px"-->
<!--android:layout_height="260px" />-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="341px"
android:layout_height="260px"
android:background="#drawable/button_language_selection_states"
android:weightSum="1"
android:clickable="true"
android:onClick="sendMessage"
>
<TextView
android:id="#+id/textview_select_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingTop="30px"
android:layout_weight="0.4"
android:textSize="21sp"
android:textColor="#333333"
/>
<View
android:layout_width="match_parent"
android:background="#drawable/gradient"
android:layout_height="1dp"
android:layout_margin="10px">
</View>
<TextView
android:id="#+id/textview_language"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:paddingTop="40px"
android:layout_weight="0.6"
android:textSize="28sp"
android:textColor="#ffffff"
/>
</LinearLayout>
I think the problem is writing all the init code in only one constructor. Consider making an init function and call it from all the constructors. Also consider setting the onClick events listeners in your activity rather than than the layout constructor itself.
Hi I am trying to position my custom view class in my main relative layout.
The relativelayout layout_torightof is not working in xml layout and causing teh other views to draw on my custom view class. Here is my code.
public class LeftNavBarUI extends RelativeLayout implements OnClickListener {
TextView facebook, twitter;
private static Context mContext;
public LeftNavBarUI(Context context, AttributeSet attrs) {
super(context);
mContext = context;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.leftbar, this);
facebook = (TextView) findViewById(R.id.facebook);
twitter = (TextView) findViewById(R.id.twitter);
facebook.setOnClickListener(this);
twitter.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(mContext, AlbumActivity.class);
switch(v.getId()){
case R.id.facebook:
{
intent.putExtra(AppConstants.PROVIDER_NAME, "facebook");
mContext.startActivity(intent);
break;
}
case R.id.twitter: /** AlerDialog when click on Exit */
{
intent.putExtra(AppConstants.PROVIDER_NAME, "twitter");
mContext.startActivity(intent);
break;
}
}
}
}
<org.labs.lifeviewmobile.ui.LeftNavBarUI
android:id="#+id/leftbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/title"
style="#style/ActionBarView_TabText"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:textStyle="bold"
android:textSize="13sp"
android:background="#drawable/bar_gradient"
android:layout_toRightOf="#+id/leftbar"
android:paddingLeft="7dip"
android:paddingBottom="5dip"
android:paddingTop="5dip" />
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:animateLayoutChanges="true"
android:clipChildren="true"
android:gravity="top"
android:layout_toRightOf="#+id/leftbar"
android:listSelector="#drawable/image_background"
android:numColumns="4"
android:padding="10dip"
android:paddingTop="10dp"
android:scrollbarFadeDuration="0"
android:scrollbarStyle="outsideOverlay"
android:visibility="gone"
android:scrollbars="vertical" />
<TextView
android:id="#+id/blank_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/leftbar"
android:layout_centerVertical="true"
android:textStyle="bold"
android:textSize="13sp"
android:textColor="#ffffff"
android:paddingLeft="125dp"
/>
<ProgressBar
android:id="#+id/pbar"
style="#android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true" />
The issue is with this line
android:layout_toRightOf="#+id/leftbar"
It should be
android:layout_toRightOf="#id/leftbar"
You forgot to pass the AttributeSet to the parent, change your constructor to:
public LeftNavBarUI(Context context, AttributeSet attrs) {
super(context, attrs);
// rest of your constructor code
}
The attributes are what you set in xml, you need to pass those to your parent or they won't apply.
My Application freezes and LogCat grows heap (frag case) to 14mb if add more then 3 own "components" to current activity/layout. It takes very long until the fourth "component" is added and Android asks to close my application.
I've written a Activity which has some components (EditTexts) and a own "component". I want to do something similar to the contacts app (Number adding) but with ingredients. So I've created a class IngredientsLayout which extends LinearLayout and a Layout xml file with a TextView and a "Add"-Button which I've placed on the Activity-Layout. Every time I press the Add Button, a single IngredientLayout with 3 EditTexts and a ImageButton will be added to the current IngredientsLayout which should "hold" all added ingredients.
What I'm doing wrong?
IngredientsLayout class:
public class IngredientsLayout extends LinearLayout {
ImageButton imageButtonAddIngredient;
ArrayList<IngredientLayout> arrayList;
public IngredientsLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ingredients_list, this);
arrayList = new ArrayList<IngredientLayout>();
imageButtonAddIngredient = (ImageButton) findViewById(R.id.imageButtonAddIngredient);
imageButtonAddIngredient.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayoutIngredientsList);
IngredientLayout ingredientLayout = new IngredientLayout(getContext(), null);
ingredientLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
arrayList.add(ingredientLayout);
layout.addView(ingredientLayout);
}
});
}
}
ingredients_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutIngredientsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:text="#string/ingredients_list_name" />
<ImageButton
android:id="#+id/imageButtonAddIngredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/ic_menu_add_ingredient" />
</LinearLayout>
</LinearLayout>
</merge>
IngredientLayout class:
public class IngredientLayout extends LinearLayout {
public IngredientLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.ingredients_list_row, this);
}
}
ingredients_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutIngredientRow"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editTextAmount"
android:layout_width="45dp"
android:layout_height="fill_parent"
android:layout_weight="0.11"
android:ems="10"
android:hint="#string/ingredients_list_listrow_amount"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextUnit"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="0.06"
android:ems="10"
android:hint="#string/ingredients_list_listrow_unit" /><EditText
android:id="#+id/editTextName"
android:layout_width="127dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="0.005"
android:ems="10"
android:hint="#string/ingredients_list_listrow_name" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/ic_menu_cancel" />
</LinearLayout>
</merge>
LinearLayouts are not meant for this kind of dynamic accessing.
What you need to do is a ListView and a adapter.
And every time you add a new item into the adapter you call it's NottifyDataChanged to update the list view with the new item.
I have a custom "view" widget (CustomController) on my application's main screen.
In the widget's constructor, I can access an image in the widget's xml file with the following code:
(Note, img_cursor is an ImageView in "custom_controller.xml")
public final class CustomController extends LinearLayout{
public CustomController(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view_controller = layoutInflater.inflate(R.layout.custom_controller,this);
img_cursor = (ImageView) view_dpad.findViewById(R.id.img_cursor);
...
However, I want to access a TextView on the main activity's screen.
The custom controller widget is displayed on the main screen through main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linlayRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.test.projectname.CustomController
android:id="#+id/dpad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="15px"
android:layout_marginLeft="15px" />
<Button
android:text="Connect"
android:id="#+id/btn_connection"
android:layout_height="wrap_content"
android:layout_width="100dp">
</Button>
<Button
android:text="Settings"
android:id="#+id/btn_settings"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</Button>
<Button
android:text="Debug x,y"
android:layout_width="wrap_content"
android:id="#+id/btn_debug_x_y"
android:layout_height="wrap_content">
</Button>
<TextView
android:id="#+id/txt_view_x"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X = 0">
</TextView>
<TextView
android:id="#+id/txt_view_y"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y = 0">
</TextView>
</LinearLayout>
How do I access the TextViews in main.xml from the CustomController class?
I've tried stuff like:
public final class CustomController extends LinearLayout{
public CustomController(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main_controller = layoutInflater.inflate(R.layout.main,this);
textview = (TextView) view_dpad.findViewById(R.id.txt_view_x);
...
I can't seem to access the main screen's view. It doesn't like:
View main_controller = layoutInflater.inflate(R.layout.main,this);
Thanks for any help!
I'm trying to create custom layout for android. It normally draws on screen, but without inner views. Draws only my group_box.xml. How i can get access from my custom layout to inner views (TextView with id test) or how to draw they?
main.xml
<my.example.GroupBox
android:layout_width="match_parent"
android:layout_height="40sp">
<TextView android:text="TEST"
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</my.example.GroupBox>
group_box.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/groupbox">
<LinearLayout style="#style/groupboxContent"
android:id="#+id/content"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView style="#style/groupboxLabel"
android:id="#+id/caption"
android:text="#string/visit"/>
</RelativeLayout>
GroupBox.java
public class GroupBox extends LinearLayout {
public GroupBox(Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.group_box, this);
}
public GroupBox(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.group_box, this);
}
}
you can access to the elements via setters and getters.
put this in your GroupBox.java
caption = (TextView) findViewById(R.id.caption);
public void setLabel(CharSequence text) {
caption.setText(text);
}
add an id to your control in xml:
<my.example.GroupBox
android:layout_width="match_parent"
android:layout_height="40sp"
android:id="mycontrol">
<TextView android:text="TEST"
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</my.example.GroupBox>
then find your control in the main activity and do this:
yourcontrol = (GroupBox) findViewById(R.id.mycontrol)
yourcontrol.setLabel("test");