Though this tends to be very basic question, i cannot solve this issue. I searched the similar kind of issue but none solves my issue.
I created my own class where i created some basic controls and i called this class in my xml as
<com.mypackagename.classname
..
..
/>
and some views goes inside this. and before this now i would like to add relativelayout as
activity_main1.xml:
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
<com.test.MainLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/drawer_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_drawer" />
</LinearLayout>
</com.test.MainLayout>
</RelativeLayout >
and i my main activity i declared as
MainActivity:
Myview view;
view = (Myview)this.getLayoutInflater().inflate(R.layout.activity_main1, null);
setContentView(view);
where MyView is the class where i have my own controls.
After adding relative layout i tried something like
MainActivity1.java:
public class MainActivity1 extends FragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
view = (Myview)this.getLayoutInflater().inflate(R.layout.activity_main1, null);
item.addView(view);
setContentView(item); // facing an error here
.......... rest of the code
}
and my layout activity is
public class MainLayout1 extends LinearLayout
{
....
}
while running it is throwing classcastexception and the error is
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.view.layout.MainLayout
view = (Myview)this.getLayoutInflater().inflate(R.layout.activity_main1, null);
At this point in your java class, you are getting MyView Object in view, but it is actually a layout file instance which returns its parent layout here its a RelativeLayout.
Since inflate(R.layout.activity_main1, null); returns the object of RelativeLayout. Instead of this you have to get instance of your MainLayout1 like(R.id.mainlayout) and then cast it into the MyView object like:
RelativeLayout item = (View)this.getLayoutInflater().inflate(R.layout.activity_main1,null);
view = (Myview) item.findViewById(R.id.mainlayout);
item.addView(view);
setContentView(item);
Related
I am trying to inflate a custom view object into my Layout XML, but I am getting an inflate exception, can someone help me?
Here is my Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ViewGroup
android:id="#+id/desenha_foto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/button_capture"/>
<Button
android:id="#+id/button_capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="snapIt"
android:text="#string/Capture"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Here is my code, where I try to inflate the ViewGroup:
public class Desenha_Foto extends ActionBarActivity {
private DesenhaFotoView draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bitmap bitmap = Global.getInstance().getBitmap();
draw = new DesenhaFotoView(this, bitmap);
setContentView(R.layout.activity_desenha_foto);
final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.desenha_foto);
viewGroup.addView(draw);
Global.getInstance().getProgressDialog().dismiss();
}
}
You can't use a ViewGroup tag in the xml layout because ViewGroup is an abstract class. You either use one of ViewGroup's children(LinearLayout, RelativeLayout etc) or you use a custom implementation(a class that extends ViewGroup). You have your own view, DesenhaFotoView use that instead of the ViewGroup.
I want to display a graph in a fragment, but I didn't found many information about how to use GraphView in general. Can someone please help me to display a simple graph?
I started with this: http://android-graphview.org/#doc_createsimplegraph but don't get it working in a fragment.
What I did was to create a fragment_graph.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1"
android:background="#102f65" />
</LinearLayout>
and add the relevant code in the Fragment class:
public class GraphFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph, container, false);
// add the data here if necessary
// ...
LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph1);
layout.addView(graphView);
return view;
}
// ...
}
Then you can use the fragment "as is".
Most credit actually goes to vogella.
define a empty LinearLayout with fixed width and height in your layout.xml file and then add the graphview view to it.
Layout
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1" />
Java
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
Just for the record:
You cannot use it in connection with ScrollViews. If a ScrollView is wrapping your LinearLayout the lib doesn't show anything.
I have an template view it looks like ;
template.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/view_background"
android:gravity="center_vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/btnMultiple"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#drawable/button_backgroundblue"
android:onClick="btnMultiple_clicked"
android:tag="4"
android:text="#string/mc_abcd"
android:textColor="#drawable/button_textcolor"
android:textSize="#dimen/h2" />
I'm creating this view programmatically and then i'm adding this View into ViewFlipper as like ;
activity_main.java
public void btnCreateView_clicked(View view) {
ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
View myView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.template, viewFlipper, false);
viewFlipper.addView(myView);
viewFlipper.showNext(); // Showing lastly created myView successfully. }
Also my activity has public function as like ;
activity_main.java
public void btnMultiple_clicked(View view) {
//Save the world !
}
When i press btnCreateView button from first view it's successfully creating template (myView) and then adding it into ViewFlipper. When i click btnMultiple i'm getting error and my application crashes ; "Could not find a method btnMultiple_clicked(View) in the activity class android.app.Application for onClick handler on view class android.widget.Button with id 'btnMultiple'" i'm sure there is existing method btnMultiple_clicked(View).
PS! If i add listener to btnMultiple programmatically it's gonna work but i wonder why "android:onClick="btnMultiple_clicked"" doesn't work ?
Android looks for the method defined in the onClick attribute in the Activity. The problem in your code is that you used for the LayoutInflater initialization the Application's Context and not the Context of the Activity so the method will not be found there(the Context is passed to the View). Use:
View myView = LayoutInflater.from(this).inflate(R.layout.template, viewFlipper, false);
or any other reference that points to the Activity where those buttons will be used.
This is a good example why you should use in most cases the Context of the Activity.
So I'm experimenting with implementing an MVC pattern in Android where my views are subclassed from RelativeLayout, LinearLayout, ScrollView, etc... It's working until I try to get a hold of a view within my view. I get an NPE. I've tried accessing the view in order to set the onClickListener in the constructor and also in onAttachedToWindow(), but I get the NPE in both places.
For example, here's a view class:
public class ViewAchievements extends LinearLayout
{
private RelativeLayout mRelativeLayoutAchievement1;
public ViewAchievements(Context context, AttributeSet attrs)
{
super(context, attrs);
mRelativeLayoutAchievement1 = (RelativeLayout) findViewById(R.id.relativeLayout_achievement1);
mRelativeLayoutAchievement1.setOnClickListener((OnClickListener) context); //NPE on this line
}
#Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
mRelativeLayoutAchievement1.setOnClickListener(mOnClickListener); //Also get NPE on this line
}
}
Can someone please tell me the proper way to get a hold of my subviews, in this case mRelativeLayoutAchievement1?
Here's an XML snippet:
<com.beachbody.p90x.achievements.ViewAchievements 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="#color/gray_very_dark"
android:orientation="vertical" >
<!-- kv Row 1 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:baselineAligned="false">
<RelativeLayout
android:id="#+id/relativeLayout_achievement1"
style="#style/linearLayout_achievement"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_margin="#dimen/margin_sm"
android:layout_weight="1" >
<TextView
android:id="#+id/textView_achievement1"
style="#style/text_small_bold_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/margin_large"
android:text="1/20" />
</RelativeLayout>
...
And here's how I'm creating the view from my Activity:
public class ActivityAchievements extends ActivitySlidingMenu
{
private ViewAchievements mViewAchievements;
#Override
public void onCreate(Bundle savedInstanceState)
{
mViewAchievements = (ViewAchievements) View.inflate(this, R.layout.view_achievements, null);
setContentView(mViewAchievements);
...
You're trying to get the child views during the view's constructor. Since they are child views, they haven't been inflated yet. Can you move this code out of the constructor, possibly into View.onAttachedToWindow()?
http://developer.android.com/reference/android/view/View.html#onAttachedToWindow()
My problem is communication between custom GameView (extends SurfaceView) and TextView: I want to set TextView's text from inside of the GameView.
In main activity i'm using this layout file, it should explain structure of my app:
<?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"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
>
<TextView
android:id="#+id/scoreTV"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="score: 0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:layout_alignParentRight="true" />
</RelativeLayout>
<org.gk.grApp.GameView
android:id="#+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I can't change TextView's text in my GameView object, because it's impossible to touch UI thread from another.
Handler doesn't work too, because i can't give a handler's reference to GameView's constructor, that is performed after loading this xml file (a read about default constructor for xml files eg here How can I use GLSurfaceView in a LinearLayout together with other Views, such as TextView or Button?).
Do you have any idea what I should do now? Maybe my deduction is wrong, so please, tell me about this.
EDIT: I changed my xml file, instead of GameView I have now:
<LinearLayout
android:id="#+id/gameplayScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
Also I added an argument (third) into constructor's signature:
public GameView(Context context, AttributeSet as, Handler h) { ... }
and changed my onCreate in GameplayActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameplay);
LinearLayout ll = (LinearLayout)findViewById(R.id.gameplayScreen);
GV = new GameView(this, null, scoreHandler);
ll.addView(GV);
}
It works, now I can set TextView's text but after clicking on the back button another exception is thrown:
"Performing pause of activity that is not resumed: {org.gk.grApp/org.gk.grApp.MainMenuActivity}". I just started searching information about this.
First make a reference to the TextView on the activity level:
TextView txv;
In onCreate assign this reference:
txv = (TextView) findViewById(R.id.MyTextView);
Then make a method in your Activity after onCreate like this:
public void setTextView(final String txt){
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
txv.setText(txt);
}
});
}
Then you make a call from your custom view:
((MyActivity) getContext()).setTextView(str);