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.
Related
I have implemented a custom view like this:
public class HeaderView extends LinearLayout {
#Bind(R.id.bill_summary_layout)
LinearLayout summaryLayout;
#Bind(R.id.bill_balance)
TextView balance;
#Bind(R.id.bill_closedate)
TextView closedDate;
#Bind(R.id.bill_status)
TextView status;
#Bind(R.id.bill_partial_overdue)
ViewStub partialOverdueView;
#Bind(R.id.bill_partial_closed)
ViewStub partialClosedView;
#Bind(R.id.bill_partial_open)
ViewStub partialOpenView;
Bill bill;
public HeaderView(Context context) {
super(context);
initUI(context);
}
public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
initUI(context);
}
public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initUI(context);
}
private void initUI(Context context) {
LayoutInflater.from(context).inflate(R.layout.header_view, this);
}
public Bill getBill() {
return bill;
}
public void setBill(Bill bill) {
this.bill = bill;
notifyMustRebind();
}
public void notifyMustRebind() {
setupUI();
}
private void setupUI() {
if(this.bill == null)
throw new RuntimeException("O objeto bill tem que estar setado e nao ser nulo. Talvez esqueceu de chamar o setBill?");
switch (bill.getState()) {
case OVERDUE:
setOverdueState();
break;
case OPEN:
setOpenState();
break;
case CLOSED:
setClosedState();
break;
case FUTURE:
setFutureState();
break;
}
}
private void setOverdueState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.overdue));
LayoutInflater.from(getContext()).inflate(R.layout.partial_overdue_view, this, true);
partialOverdueView.inflate();
}
private void setOpenState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.open));
partialOpenView.inflate();
}
private void setClosedState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.closed));
partialClosedView.inflate();
}
private void setFutureState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.future));
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
ButterKnife.unbind(this);
}
}
The header_view.xml:
<?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">
<LinearLayout
android:id="#+id/bill_summary_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_content_arrow"
android:backgroundTint="#color/colorAccent"
android:orientation="vertical"
android:paddingBottom="#dimen/header_margin"
android:paddingTop="#dimen/header_margin"
>
<TextView
android:id="#+id/bill_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="#dimen/header_text_margin"
android:text="R$ 2.300,10"
android:textColor="#android:color/white"
android:textSize="22sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/bill_closedate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="#dimen/header_text_margin"
android:text="Vencimento 15 MAI"
android:textColor="#android:color/white"
android:textSize="18sp"
/>
<TextView
android:id="#+id/bill_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="#dimen/header_text_margin"
android:text="Fechamento em 5 de Julho"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="12sp"
/>
</LinearLayout>
<ViewStub
android:id="#+id/bill_partial_overdue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="#layout/partial_overdue_view"
/>
<ViewStub
android:id="#+id/bill_partial_closed"
layout="#layout/partial_closed_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ViewStub
android:id="#+id/bill_partial_open"
layout="#layout/partial_open_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorAccent"/>
</LinearLayout>
The view above is inside another view called bill_view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<br.com.leonardo2204.nubanktest.ui.custom.HeaderView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/list_header_padding">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingLeft="#dimen/list_header_padding"
android:paddingStart="#dimen/list_header_padding"
android:text="De 5 Mar até 5 abr"
android:textAllCaps="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingEnd="#dimen/list_header_padding"
android:paddingRight="#dimen/list_header_padding"
android:text="Valores em R$"
android:textAllCaps="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
The view above is inflated inside a PagerAdapter, as this:
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = LayoutInflater.from(container.getContext()).inflate(R.layout.bill_view, container, false);
HeaderView header = (HeaderView) v.findViewById(R.id.header_view);
header.setBill(billList.get(position));
container.addView(v);
return v;
}
The problem is, I'm getting the stack trace below :
Process: br.com.leonardo2204.nubanktest, PID: 10815
java.lang.IllegalArgumentException: ViewStub must have a valid layoutResource
at android.view.ViewStub.inflate(ViewStub.java:289)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setClosedState(HeaderView.java:112)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setupUI(HeaderView.java:91)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.notifyMustRebind(HeaderView.java:76)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setBill(HeaderView.java:72)
at br.com.leonardo2204.nubanktest.ui.adapter.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:30)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:17547)
What I am doing wrong ? Is there a better approach to inflate the header view other than inside instantiateView ?
Thanks !
USE
android:layout="#layout/xyz"
instead of
layout="#layout/xyz"
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);
}
}
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 want to add a button into prefrencescreen, i got success into add a button into the prefrence but i could not get onClick event. i have attached my code a pic of prefence screen
setting.xml
<PreferenceCategory android:title="Application Details">
<Preference android:key="type"
android:title="Type"
android:summary="" />
</PreferenceCategory>
<PreferenceCategory android:title="Notification Settings">
<ListPreference android:key="sendNotificationType"
android:title="Status Notification For"
android:dialogTitle="Status Notification For" />
</PreferenceCategory>
settingdetail.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/textView2"
android:layout_marginLeft="15dp"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="#+id/setFromTimeBtn"
android:text="Summary"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/buyItNowBtn"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:background="#drawable/button"
android:text="#string/buyItNowBtnTxt"
android:textColor="#color/white" />
and the onCreate Method of prefenceActivity Class
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.setting);
setContentView(R.layout.settingview);
Preference typePref = (Preference) findPreference("type");
typePref.setLayoutResource(R.layout.settingdetail);
typePref.setSelectable(true);
Button btn = (Button) findViewById(R.id.buyItNowBtn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e(TAG,"TEST");
Toast.makeText(Setting.this, "TEST", Toast.LENGTH_SHORT).show();
}
});
}
screenshot
This thread is stale, but since there is no solution yet, here is how I succeeded:
1. Set the button click listener
There are two ways to set the click listener for a button, either programmatically or via XML.
Programmatically: In your custom preference, override onBindView and attach the listener there:
#Override
protected void onBindView(View view) {
View button = view.findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "button click listener works!");
}
});
super.onBindView(view);
}
Via XML: Add android:onClick="yourMethodName" to the layout definition of the Button element (which is part of the layout for your preference, see below) and implement the method in your PreferenceActivityas described here: Responding to Click Events
2. Fix OnPreferenceClickListener
Now the button click listener should work, but the Preference's OnPreferenceClickListener will not work anymore. In order to fix this, add
android:descendantFocusability="blocksDescendants"
to the top element in the layout file for your preference:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/scrollbarSize"
android:background="?android:attr/selectableItemBackground"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dip"
android:layout_marginEnd="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />
<Button android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="#android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
(No android:onClick="yourMethodName" here as I chose to do it programmatically.)
3. Attach the Layout
Finally you need to set this layout to your preference XML file:
Add android:layout="#layout/myLayoutName" or alternatively set it via setLayoutResource as you construct the preference in your PreferenceFragment.
Implement OnPreferenceClickListener in preferenceActivity
private Preference mBuyPreference = null;
mLogoutPreference = findPreference(yourkey);
mBuyPreference.setOnPreferenceClickListener(this);
#Override
public boolean onPreferenceClick(Preference preference) {
if (preference == mBuyPreference) {
try {
//Do Something
}
return false;
}
Instead of using the onClick() handler, override the onPreferenceTreeClick method in your PreferenceActivity. There you will get the clicked preference object and you can check its key.
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference.getKey().equals("key")) {
// do something
return true;
}
return false;
}
I've solved half of your problem by creating a custom preference like this -
In order to get to this, I created a custom preference like this -
public class CustomPreference extends Preference {
private LinearLayout mWidgetContainer;
private View mRowView;
public CustomPreference(Context context) {
super(context);
}
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected View onCreateView(ViewGroup parent) {
LayoutInflater viewInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRowView = viewInflater.inflate(R.layout.preferences_row_view, parent, false);
mWidgetContainer = (LinearLayout) mRowView.findViewById(android.R.id.widget_frame);
Button button = new Button(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.listview_row_bg);
button.setTextSize(14);
button.setPadding(5, 5, 5, 5);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), BuyScreenActivity.class);
getContext().startActivity(intent);
}
});
button.setTypeface(null, Typeface.BOLD);
button.setText("Buy now");
mWidgetContainer.addView(button);
return mRowView;
}
}
Then in your settings xml file you can create a setting like
<package.of.your.java.file.CustomPreference
android:key="type"
android:title="Type"
android:summary="" />
And my preferences_row_view.xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="50dip"
android:paddingLeft="5dip"
android:paddingRight="?android:attr/scrollbarSize" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_weight="1" >
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:lineSpacingMultiplier="1.3"
android:textSize="14sp" />
<TextView
android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#android:id/title"
android:layout_below="#android:id/title"
android:maxLines="4"
android:textSize="13sp" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
That will do.
But there is one more issue I'm facing which I've posted here on SO.
Add a button in Preference Row
Instead of
Button btn = (Button) findViewById(R.id.buyItNowBtn);
do this
Button btn = (Button)typePref.getView(view, viewGroup );
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!