Class not found in xml created in Android Studio - android

I am trying CardsUI, I have following code: Including NowActivity Code
NowActivity.java where I define the behaviour
package com.example.rushdesktop.testingweather;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
public class NowActivity extends LinearLayout implements OnGlobalLayoutListener {
public NowActivity(Context context, AttributeSet attrs)
{ super(context, attrs); initLayoutObserver(); }
public NowActivity(Context context)
{ super(context); initLayoutObserver(); }
private void initLayoutObserver()
{ // force vertical orientation and add observer
setOrientation(LinearLayout.VERTICAL);
getViewTreeObserver().addOnGlobalLayoutListener(this); }
#Override public void onGlobalLayout()
{
getViewTreeObserver().removeGlobalOnLayoutListener(this);
final int heightPx =getContext().getResources().getDisplayMetrics().heightPixels;
boolean inversed = false;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++)
{
View child = getChildAt(i);
int[] location = new int[2];
child.getLocationOnScreen(location);
if (location[1] > heightPx) { break; }
if (!inversed) {
child.startAnimation( AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_left));
}
else {
child.startAnimation( AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_right));
}
inversed = !inversed;
}
}
I use it in my main activity xml:
<ScrollView 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:fillViewport="true">
<com.example.rushdesktop.testingweather.NowActivity android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e3e3e3" android:orientation="vertical" >
<TextView android:id="#+id/txtView" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView1" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView2" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView3" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
<TextView android:id="#+id/txtView4" style="#style/nowCardStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="#string/hello_world" tools:context=".MyActivity" />
</com.example.rushdesktop.testingweather.NowActivity>
</ScrollView>
As in the code I use the Java class in my main activity xml. But when I build my code I get error showing the class was not found in the XML. Please help me with the issue.
Here is the link to my full android studio code:
https://drive.google.com/file/d/0B6P-8t-14MK-dEZ0bHJqRzBvUmVaSW5ySTR3My1mN0U1OThV/edit?usp=sharing
Thanks in advance.

Be sure that the only child of the scroll layout (com.example.rushdesktop.testingweather.NowActivity) inherits some sort of layout (i.e. RelativeLayout, FrameLayout...) does it?

in your xml layout you need to use the View, not the activity.. the activity will set the layout it uses via setContentView(R.layout.[your layout])
in your xml layout, if you switch to graphical editor view you should be able to see the view you want listen in the custom & library views tab (in eclipse), providing you have included the library in the project build

Related

Android error inflating class <unknown> while loading highchart

I am trying to load highcharts via Dialog. Below is my code
Gradle
implementation 'com.highsoft.highcharts:highcharts:9.0.1'
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical"
android:padding="10dp">
<com.highsoft.highcharts.core.HIChartView
android:id="#+id/goly_gauge"
android:layout_width="match_parent"
android:layout_height = "350dp"
/>
<com.highsoft.highcharts.core.HIChartView
android:id="#+id/golm_gauge"
android:layout_width="match_parent"
android:layout_height="350dp"
/>
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="OK" />
</LinearLayout>
</ScrollView>
Fragment
suggestion_list.setOnItemClickListener((parent, view, position, id) -> {
String selectedFromList = (String) (suggestion_list.getItemAtPosition(position));
showPreFilledData(selectedFromList);
Log.e(TAG, arraylist.get(position));
});
private void showPreFilledData(String string) {
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.product_sales_layout);// error comes at this point
dialog.setTitle("Product Sales");
Button ok;
ok = (Button) dialog.findViewById(R.id.ok);
switch (string) {
case "A2A 100 MG TABS":
GolYgauge(55.1);
GolMgauge(32.9);
break;
case "AQUA-VIT SACHET":
GolYgauge(45.8);
GolMgauge(22.7);
break;
case "BRONCOPHYLINE SYRUP":
GolYgauge(65.7);
GolMgauge(42.4);
break;
}
ok.setOnClickListener(v -> dialog.dismiss());
Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
dialog.show();
}
When I click on an item in my list view I want to open a layout and want to display data inside a gauge. But I am getting error
android.view.InflateException: Binary XML file line #15 in com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout: Binary XML file line #15 in com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout: Error inflating class
The line number 15 is <com.highsoft.highcharts.core.HIChartView. I am stuck to it. How can get rid from this issue?
Any help would be highly appreciated.
Seems like a combination of Dialog class wrapping the Activity context and the library's HIChartView not able to process this wrapped context. I force-unwrapped the context and then your code started working. How to:
Extend the HIChartView with a custom implementation handling the wrapped context:
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.AttributeSet;
import com.highsoft.highcharts.core.HIChartView;
public class CustomHIChartView extends HIChartView {
public CustomHIChartView(Context context) {
super(unwrap(context));
}
public CustomHIChartView(Context context, AttributeSet attributeSet) {
super(unwrap(context), attributeSet);
}
// unwrap the context until I get the original passed-in Activity context
private static Activity unwrap(Context context) {
while (!(context instanceof Activity) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
return (Activity) context;
}
}
The unwrapping function is copied from https://stackoverflow.com/a/51640906/12321475, there you can also read on why the base context are sometimes being wrapped.
Modify your layout to use the custom implementation:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<your.package.name.CustomHIChartView
android:id="#+id/goly_gauge"
android:layout_width="match_parent"
android:layout_height = "350dp"
/>
<your.package.name.CustomHIChartView
android:id="#+id/golm_gauge"
android:layout_width="match_parent"
android:layout_height="350dp"
/>
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="OK" />
</LinearLayout>
</ScrollView>

how to make an expandable list of cardviews?

I searched a lot to find a direct and clear solution for I think a popular problem but unfortunately I couldn't find it.
We want to have a list of cardviews so each card bind to a specific data and each card has a list inside that shows meta or detail data about its parent.
So we have a nested list inside a cardview.
With a simple search, we know that we should use expanded list view which parents items are cardviews and we must have another layout for its child.
So when you click on cards a list of items appears below of your cards on the root.
But we want to show child list inside of cards?
And there is no access to the something like child list id or any other things to refer to a transition animation and change of layout.
So the clear question is how to add a listview inside a cardview?
i follow my work by using tablelayout and table row. and prefer not to use external libraries for stability and version problems.
this is what i mean.
Image that describe my question
If you don't want to use an external library, you can make your CardView expand like this:
Layout file for an expandable CardView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
>
<TextView
android:id="#+id/textView_name"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<!--My dropdown Button -->
<RelativeLayout
android:id="#+id/button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="end"
android:layout_alignParentRight="true"
android:gravity="center"
>
<View
android:layout_width="12dp"
android:layout_height="12dp"
android:background="#drawable/triangle"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false" />
</RelativeLayout>
</RelativeLayout>
<!--The layout below is my ExpandableLayout -->
<LinearLayout
android:id="#+id/expandableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imageView_Owner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Owner"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView_OwnerUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
My ExpandableRecyclerAdapter Class
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ExpandableRecyclerAdapter extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
private List<Repo> repos;
private SparseBooleanArray expandState = new SparseBooleanArray();
private Context context;
public ExpandableRecyclerAdapter(List<Repo> repos) {
this.repos = repos;
//set initial expanded state to false
for (int i = 0; i < repos.size(); i++) {
expandState.append(i, false);
}
}
#Override
public ExpandableRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
this.context = viewGroup.getContext();
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.expandable_card_row, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ExpandableRecyclerAdapter.ViewHolder viewHolder, final int i) {
viewHolder.setIsRecyclable(false);
viewHolder.tvName.setText(repos.get(i).getName());
viewHolder.tvOwnerLogin.setText("Owner: " +repos.get(i).getOwner().getLogin());
viewHolder.tvOwnerUrl.setText(repos.get(i).getOwner().getUrl());
Picasso.with(context)
.load(repos.get(i).getOwner().getImageUrl())
.resize(500, 500)
.centerCrop()
.into(viewHolder.ivOwner);
//check if view is expanded
final boolean isExpanded = expandState.get(i);
viewHolder.expandableLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.buttonLayout.setRotation(expandState.get(i) ? 180f : 0f);
viewHolder.buttonLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayout, i);
}
});
}
#Override
public int getItemCount() {
return repos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvName,tvOwnerLogin, tvOwnerUrl;
private ImageView ivOwner;
public RelativeLayout buttonLayout;
public LinearLayout expandableLayout;
public ViewHolder(View view) {
super(view);
tvName = (TextView)view.findViewById(R.id.textView_name);
tvId = (TextView)view.findViewById(R.id.textView_id);
tvUrl = (TextView)view.findViewById(R.id.textView_url);
tvOwnerLogin = (TextView)view.findViewById(R.id.textView_Owner);
tvOwnerUrl = (TextView)view.findViewById(R.id.textView_OwnerUrl);
ivOwner = (ImageView) view.findViewById(R.id.imageView_Owner);
buttonLayout = (RelativeLayout) view.findViewById(R.id.button);
expandableLayout = (LinearLayout) view.findViewById(R.id.expandableLayout);
}
}
private void onClickButton(final LinearLayout expandableLayout, final RelativeLayout buttonLayout, final int i) {
//Simply set View to Gone if not expanded
//Not necessary but I put simple rotation on button layout
if (expandableLayout.getVisibility() == View.VISIBLE){
createRotateAnimator(buttonLayout, 180f, 0f).start();
expandableLayout.setVisibility(View.GONE);
expandState.put(i, false);
}else{
createRotateAnimator(buttonLayout, 0f, 180f).start();
expandableLayout.setVisibility(View.VISIBLE);
expandState.put(i, true);
}
}
//Code to rotate button
private ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(new LinearInterpolator());
return animator;
}
}
In your Activity/Fragment, set up RecyclerView like this
private RecyclerView recyclerView;
private List<Repo> data;
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//fetch data and on ExpandableRecyclerAdapter
recyclerView.setAdapter(new ExpandableRecyclerAdapter(data));
So, its quite simple to create an expandable CardView without external Library. The code is almost exactly the same as using a normal CardView with RecyclerView, the main change is setting the View.GONE/View.VISIBLE on button click.
You can use ExpandLayout,and then add it in cardview.
<com.kyo.expandablelayout.ExpandableLayout
android:id="#+id/expandlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp" >
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/child"
app:canExpand="true" />
</com.kyo.expandable.ExpandableLayout>

I want to write a class which dynamically add the views like button or TextView or

I want to write a class which dynamically add the views like button or TextView or ...
I did it but there is some problem
1- I want to use an option that means I want to use scroll or I want to show all of the views in the screen.
1-1 I want to use scroll (Vertical or Horizontal) to show all of the views.
1-2 I want to use android:layout_weight to fit the views in the same size which are beautiful on screen on any size of screen but I don't know how.
m=number of rows
n=number of columns
2- I don't know is it a good method for describing a class for considering the Object Oriented Concepts
In XML file there is only a RelativeLayout.
the Class:
import android.app.ActionBar;
import android.content.Context;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import java.util.ArrayList;
public class MakeTwoDimensionArrayView {
private ScrollView scrollView;
private LinearLayout mainLinearLayout;
private LinearLayout [] subLinearLayout;
MakeTwoDimensionArrayView(Context c, int m, int n, ArrayList<?> V){
scrollView=new ScrollView(c);
mainLinearLayout=new LinearLayout(c);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
mainLinearLayout.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
subLinearLayout=new LinearLayout[m];
scrollView.addView(mainLinearLayout);
for(int i=0; i<m; i++){
subLinearLayout[i]=new LinearLayout(c);
subLinearLayout[i].setOrientation(LinearLayout.HORIZONTAL);
subLinearLayout[i].setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for(int j=0; j<n; j++){
int t=i*n+j;
View v= (View) V.get(t);
v.setTag(t+"");
subLinearLayout[i].addView(v);
}
mainLinearLayout.addView(subLinearLayout[i]);
}
}
public ScrollView getLayout(){
return scrollView;
}
}
The main
public class MainActivity extends AppCompatActivity {
MakeTwoDimensionArrayView makeButton;
ArrayList <Button> buttons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int m=3, n=2;
buttons=new ArrayList<Button>();
for (int i=0; i<m*n; i++){
Button b=new Button(this);
buttons.add(b);
}
makeButton=new MakeTwoDimensionArrayView(this, m, n, buttons);
setContentView(makeButton.getLayout());
}
}
You should implement horizontalScrollView within scrollView. and in that horizontalScrollView add buttons. then it will be both vertically and horizontally scrollable. But for that you should know about ScrollView and HorizontalScrollView. because they have only one direct child layout. This is an XML file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:textSize="17dip" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/itemContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item1" />
<Button
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item2" />
<Button
android:id="#+id/item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item3" />
<Button
android:id="#+id/item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item3" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
This is only demo for you to understand how scrollView and horizontalScrollView works in an xml if you want to implement dynamically then change in your MakeTwoDimensionArrayView class and in onCreate() of activity class. if still you have question then let me know.

Could not click on the buttons in SlidingDrawer

I have a problem with SlidingDrawer. All buttons in it could not be click.
Here is my xml file for SlidingDrawer:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="110dp"
android:content="#+id/content"
android:handle="#+id/handle">
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="#string/menu" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
>
<Button
android:id="#+id/btn_video"
style="#style/button_menu_bottom"
android:drawableTop="#drawable/mnu_video"
android:text="#string/video" />
...
...
</LinearLayout>
</SlidingDrawer>
Here is my java file extended from SlidingDrawer
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import com.example.R;
public class BottomMenuBar extends SlidingDrawer {
private SlidingDrawer mSlidingDrawer;
private Button mButtonSlidingDrawer;
private LinearLayout mLayoutContent;
private Button mButtonVideo;
private Button mButtonPhoto;
private Button mButtonChat;
private Button mButtonSetting;
public BottomMenuBar2(Context context, AttributeSet attrs) {
super(context, attrs);
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.bar_bottom_menu, this, true);
mSlidingDrawer=(SlidingDrawer)findViewById(R.id.sld_bottom_menu);
mButtonSlidingDrawer=(Button)findViewById(R.id.handle);
mLayoutContent=(LinearLayout)findViewById(R.id.content);
mButtonVideo=(Button)findViewById(R.id.btn_video);
mButtonSetting=(Button)findViewById(R.id.btn_setting);
}
public Button getmButtonSlidingDrawer() {
return mButtonSlidingDrawer;
}
public void setmButtonSlidingDrawer(Button mButtonSlidingDrawer) {
this.mButtonSlidingDrawer = mButtonSlidingDrawer;
}
...
Setter & Getter methods
...
}
Here is my main.xml file:
<?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:background="#drawable/bg_main"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
...
Any content
...
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.BottomMenuBar
android:id="#+id/sld_bottom_menu"
android:layout_width="wrap_content"
android:layout_height="110dp"
android:handle="#+id/handle"
android:content="#+id/content"
/>
</LinearLayout>
</LinearLayout>
For my buttons in sliding drawers, I tell each button what method to use in the xml with android:onClick.
<Button
android:id="#+id/sortbutton"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:onClick="SortClickHandler"
android:text="#string/sd_sortmethod_button"
android:textSize="12dp" />
And then define the method in whatever activity is appropriate:
public void SortClickHandler(View v) {
// Do stuff here
}
EDIT
I think I found your issue. From the developer docs here it says, and I quote:
SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer
should only be used inside of a FrameLayout or a RelativeLayout for instance.
So, I think if you change your layout to a RelativeLayout you will find that your buttons will work. I just double-checked all of my uses of a sliding drawer and in all cases the root element of the view containing it is a RelativeLayout.
I don't understand why you're extending SlidingDrawer.
You should look up the documentation here.
<SlidingDrawer
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="#+id/handle"
android:content="#+id/content">
<ImageView
android:id="#id/handle"
android:layout_width="88dip"
android:layout_height="44dip" />
<FrameView
android:id="#id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
[INSERT VIEWS HERE]
<FrameView>
</SlidingDrawer>
In your Activity you don't even technically need to gain access to the SlidingDrawer object (The sliding functionality is already in place as standard, it simply contains the the view's in content, you don't need to interfere).
From your Activity gain reference to the view's you add in [INSERT VIEWS HERE]. You can then do setOnClickListener() on each of these buttons.

Unable to access R.id from inside custom view

In my foo_layout.xml file I have a subclassed RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.android.myapp.FooView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pegboard_table"
android:orientation="vertical"
android:scaleType="fitXY"
>
<ImageView
android:id="#+id/triangular"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/pegboard_board"
android:scaleType="fitXY"
android:gravity="center"
android:visibility="invisible"
/>
<Chronometer
android:id="#+id/timer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/timer_display"
android:textSize="40sp"
android:textColor="#000"
android:layout_alignParentTop="true"
android:gravity="center"
android:visibility="invisible"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/board_table"
android:visibility="invisible"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#drawable/tab_bar">
<!-- android:layout_alignLeft="#id/options_tab"-->
<ImageView
android:id="#+id/game_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/game_select" android:paddingLeft="15sp"/>
<ImageView
android:id="#+id/undo_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/game_select"
android:layout_weight="1"
android:src="#drawable/undo"
/>
<ImageView
android:id="#+id/settings_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/undo_select"
android:layout_weight="1"
android:src="#drawable/settings"
/>
<ImageView
android:id="#+id/info_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/settings_select"
android:layout_weight="1"
android:src="#drawable/info"
/>
</LinearLayout>
</com.android.myapp.FooView>
</FrameLayout>
Then I have a java file FooView.java with the class extending
RelativeLayout.
package com.android.myapp;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
public class FooView extends RelativeLayout {
public FooView(Context context) {
super(context);
fillTable();
// TODO Auto-generated constructor stub
}
public FooView(Context context, AttributeSet attrs) {
super(context, attrs);
fillTable();
// TODO Auto-generated constructor stub
}
public FooView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
fillTable();
// TODO Auto-generated constructor stub
}
public void fillTable() {
int board = this.getChildCount();
View boardView = findViewById(R.id.board_table);
if (board > 0)
;
}
}
board is always 0.
boardView is always null.
This doesn't seem like the right behavior. I've tried it with other views inside the FooView hierarchy and findViewById() always returns null.
For the life of me I can't figure out what I'm doing wrong.
First, your widget is attempting to reference widgets outside of itself. Calling findViewById() will get you children of your widget, not widgets elsewhere in the layout.
Second, you are trying to do this from your constructor. That is not safe. Please wait until onFinishInflate().
Third, you may not want to hardwire in R.id values for those other widgets, but allow those to be configurable, either through view attributes or setters. To access those widgets, given the configured IDs, you would call findViewById() not on yourself, but on your activity, obtained from getContext().
The image view and chronometer view are not part of the custom view until you add them using view.setlayout in the constructor.

Categories

Resources