Set TextView Text from View class Not from Activity - android

This is my Activity class . Here I have a Textview.
I want to Set the TextView from View Class.
public class TestApp extends Activity
{
TextView NameTxtView;
CustomView view;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.Main);
view = (CustomView)findViewById(R.id.customview);
NameTxtView = (TextView) findViewById(R.id.nameTxtxVw);
}
}
This is my View Class. Here i want to set the TextView Text. I can't set this Text on Activity. Because I am getting value on View class.
public class CustomView extends View
{
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
public void onDraw(Canvas canvas)
{
NameTxtView.settext("Test");
}
}
Any Idea how to do that?
thanks

Do this way
TestApp.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.demo.CustomView.CustmViewListener;
public class TestApp extends Activity
{
TextView NameTxtView;
CustomView view;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.Main);
view = (CustomView)findViewById(R.id.customview);
NameTxtView = (TextView) findViewById(R.id.nameTxtxVw);
view.setCustmViewListener(new CustmViewListener() {
#Override
public void onUpdateValue(String updatedValue) {
NameTxtView.setText(updatedValue);
}
});
}
}
CustomView.java
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class CustomView extends View {
CustmViewListener custmViewListener;
public CustmViewListener getCustmViewListener() {
return custmViewListener;
}
public void setCustmViewListener(CustmViewListener custmViewListener) {
this.custmViewListener = custmViewListener;
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onDraw(Canvas canvas) {
if (getCustmViewListener() != null) {
getCustmViewListener().onUpdateValue("passYourValueHere");
}
}
public interface CustmViewListener {
void onUpdateValue(String updatedValue);
}
}

Try like this :
In CustomView Constructor call LayoutInflaterService and pass Id for your Layout & TextView :
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
final RelativeLayout yourLayout = (RelativeLayout) layoutInflater.inflate(R.layout.dialog_menu, null);
TextView myText = (TextView) yourLayout.findViewById(R.id.nameTxtxVw);
myText.setText("");

Your problem is that the activity class has a context and your second one does not. To solve this create a global variable: static Activity activity and initialize it in the constructor.
Then call: new CustomClass(ActivityClass.this).
Next whenever you want to do something related to an activity simply put activity. in front of stuff

Related

Android Fragment onAttach Error

i have problem on my on Attach its doing red line on him and writing me :
"overrides deprecated method in 'android.support.v4.app.Fragment' "
Please help me understand what i am doing wrong ?
ty you for all the helpers !!
package com.example.omermalka.memecreator;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by omermalka on 14/11/2015.
*/
public class TopSectionFragment extends Fragment {
private static EditText TopText;
private static EditText BottomText;
TopSectionListener acitivtyCommander;
public interface TopSectionListener{
public void createMime(String top , String Bottom);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
acitivtyCommander = (TopSectionListener) activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString());
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment, container, false);
TopText = (EditText) view.findViewById(R.id.TopTextInput);
BottomText = (EditText) view.findViewById(R.id.BottomTextInput);
final Button button = (Button) view.findViewById(R.id.BottomTextInput);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonClicked(v);
}
}
);
return view;
}
public void buttonClicked(View v) {
acitivtyCommander.createMime(TopText.getText().toString(),BottomText.getText().toString());
}
}
You need to change
public void onAttach(Activity activity)
to
public void onAttach(Context context)
Final code:
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
acitivtyCommander = (TopSectionListener) context;
} catch (ClassCastException e){
throw new ClassCastException(context.toString());
}
}

Android generate new objects from onClick

I try to develop a simple Android App with one Button which generates new TextViews on each click.
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(this);
}
});
}
}
My code is wrong because of:
TextView mTV1 = new TextView(this);
I could find some similar examples, which generate objects programmatically in onCreate(). But I want to generate and modify new objects in onClick().
Would anybody please help?
Change
TextView mTV1 = new TextView(this);
to
TextView mTV1 = new TextView(CreateTV.this);
Views can only be instantiated with a context as parameter
As you can see in the documentation a TextView needs the context to be created. TextView(Context context)
Since you are trying to create a TextView inside a ClickListener you can not use this as a reference to a Context-extending object.
As McAdam331 pointed out, use new TextView(getActivity), this works because Activity extends Context.
In addition to change TextView mTV1 = new TextView(this); to TextView mTV1 = new TextView(CreateTV.this);, you must add the TextView within a view like the following:
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(CreateTV.this);
addContentView(mTV1);
}
});
}
}
I would prefer adding a Context, setting it to final and then call the Textview using the Context.
Example:
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
final Context mContext = this;
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(mContext);
addContentView(mTV1);
}
});
}
}
If you want to use the Context outside the onCreate method (and within Listeners) you can define a Context.
private Context context;
public void onCreate(....) {
this.context = this;
}
private void aMethod() {
context....
}
Theres another way doing such cool stuff. Create a Class and extends it by Application.
public class MainApplication extends Application {
public static Context getContext() { return this; }
}
Then add the MainApplication to your Manifest.
<application
android:name=".MainApplication"
>
and access it from everywhere with MainApplication.getContext();

I would like to know how to draw using canvas over my XML layout

I've created a custom XML layout and I’m trying to draw the map background and the pawn player( the bitmap) over it.
Instead, its painting the pawn player over a white background without the map background that I put as the background on the XML file.
MyViev Class:
package com.example.alpha;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Bitmap playerW;
float changingY;
float changingX;
public MyView(Context context) {
super(context);
playerW = BitmapFactory
.decodeResource(getResources(), R.drawable.black);
changingY=0;
// TODO Auto-generated constructor stub
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(playerW,4+changingX, (canvas.getHeight())-288-changingY, null);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}
my MainActivity Class:
package com.example.alpha;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourView = new MyView(this);
setContentView(ourView);
}
}
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<view class="com.example.alpha.MyView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mapeasy"
/>
It's because you're not actually setting it to use your XML layout. Instead, you've set the content View to be a new instance of MyView, which doesn't have the XML background property set.
Therefor you have 2 options:
Option 1: Call ourView.setBackgroundDrawable(R.drawable.mapeasy); after you created a new instance of MyView.
or
Option 2: Set the content View to be your actual layout file and then find your MyView by using findViewById(int).
Eg.
public class MainActivity extends ActionBarActivity {
MyView ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_file);
ourView = (MyView)findViewById(R.id.myView);
}
}

TextView not effected onProgressChanged of seekBar

I have this extension of SeekBar:
package com.simplemathgame;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SeekBarPlus extends SeekBar implements OnSeekBarChangeListener{
private TextView numberOfDrills;
public SeekBarPlus(Context context) {
super(context);
}
public SeekBarPlus(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SeekBarPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
Log.w("SeekBarChanged", "change to" + progress);
numberOfDrills.setText(String.valueOf(progress));
}
public void setTextView(TextView textView){
numberOfDrills = textView;
Log.w("SeekBar", "text to bar");
}
}
And here is the main activity code:
package com.simplemathgame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.simplemathgame.SeekBarPlus;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBarPlus addSeekBar = (SeekBarPlus) findViewById(R.id.add_seek_bar);
SeekBarPlus subSeekBar = (SeekBarPlus) findViewById(R.id.sub_seek_bar);
SeekBarPlus mulSeekBar = (SeekBarPlus) findViewById(R.id.mul_seek_bar);
SeekBarPlus divSeekBar = (SeekBarPlus) findViewById(R.id.div_seek_bar);
TextView numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
TextView numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
TextView numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
TextView numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);
addSeekBar.setTextView(numberOfAddDrills);
subSeekBar.setTextView(numberOfSubDrills);
mulSeekBar.setTextView(numberOfMulDrills);
divSeekBar.setTextView(numberOfDivDrills);
}
}
After I move the progress bar nothing happens, I have all the needed elements (TextViews).
I would like:
I would like the SeekBarPlus Automatically listen to it's changes and react as I have coded in the onProgressChanged method,in other words I would like that onProgressChanged would be triggered without any code in the main activity.
Screenshot
After reading the documentation:
Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to
be notified of the user's actions.
You need to have a listener on your Seekbar in order to update.
Update
This is possible, but you have to create a single view to house everything and then attach it it that way. Once you have created this, then add the custom view to your layout file. Then add callbacks as necessary from your custom view. Of course this implies that you added your listeners IN your custom view class.
Example
You would have something like this in your class, make sure to set the orientation of the views:
package com.blah.my.package
class MyCustomClass extends LinearLayout{
CustomSeekbar v1 ...
CustomSeekbar v2 ...
CustomSeekbar v3 ...
CustomSeekbar v4 ...
CustomSeekbar v5 ...
CustomSeekbar v6 ...
...
Constructors and methods n' stuff...
}
Once you have this, then in your layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.blah.my.package.MyCustomClass ... />
...
<LinearLayout>
To learn more:
http://developer.android.com/training/custom-views/index.html
The easiest way is to implement OnSeekBarChangeListener directly on your extended class:
public class SeekBarPlus extends SeekBar implements OnSeekBarChangeListener{
public SeekBarPlus(Context context) {
super(context);
setOnSeekBarChangeListener(this);
}
public SeekBarPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setOnSeekBarChangeListener(this);
}
public SeekBarPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOnSeekBarChangeListener(this);
}
....
}
Otherwise none of the on* methods will get called. Note: this really should be a composite widget and you shouldn't need to be passing in the TextView.
You also need to do what Sergio suggested and not use the int value directly: numberOfDrills.setText("" + progress);
That's because you're sending an int to setText(), so it doesn't work because it expects a String. Change the line to:
numberOfDrills.setText("" + progress);
Or:
numberOfDrills.setText(String.valueOf(progress));
You may want to change this 4 lines too in your MainActivity, you are setting 4 TextViews to the same seekbar:
/*addSeekBar.setTextView(numberOfAddDrills);
addSeekBar.setTextView(numberOfSubDrills);
addSeekBar.setTextView(numberOfMulDrills);
addSeekBar.setTextView(numberOfDivDrills);*/
addSeekBar.setTextView(numberOfAddDrills);
subSeekBar.setTextView(numberOfSubDrills);
mulSeekBar.setTextView(numberOfMulDrills);
divSeekBar.setTextView(numberOfDivDrills);

I can't setText() in another class in android

I have a TextView with the id android:id="#+id/yazi", and I have a button that has build in android:OnClick="gonderB"
and I can complie this code:
package com.seri.bir;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
Bilmez b;
TextView t;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = new Bilmez();
t = (TextView) findViewById(R.id.yazi);
}
public void gonderB (View v,TextView t,Bilmez b){
String s = " ..."+this;
b.yaziYaz(v,s,t);
}
}
class Bilmez {
public void yaziYaz(View v,String s,TextView t){
t.setText(s);
}
}
However I have an error.
Can I setText in another class?
You can overwrite onClick of the activity. Avoid the using of the android:OnClick="gonderB" line in the xml file. I think it is better to implement the onClickListener and attach it to View Objects within your code.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Bilmez b;
TextView t;
Button bt;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = new Bilmez();
t = (TextView) findViewById(R.id.yazi);
Button bt = (Button) findViewById(R.id.btn);
bt.setOnClickListener(this);
}
#Override
public void onClick(View clickedView) {
switch (clickedView.getId()) {
case R.id.btn:
String s = "...." + this;
b.changeText(t,s);
break;
}} //end of main class }
In the changeText method you change the text of the TextView. This method can if be placed in another class if you like that.
class Bilmez {
public void changeText(TextView t, String s){
t.setText(s);
}
}
Perhaps what you are experiencing is a need to run the function on the UI thread?
public void yaziYaz(View v,final String s,final TextView t) {
runOnUiThread(new Runnable() {
public void run() {
t.setText(s);
}
});
}
i think you should do that:
public void gonderB (new View v,TextView t,Bilmez b){
String s = " ..."+this;
b.yaziYaz(v,s,t);
}

Categories

Resources