Access button from Layout within GlSurfaceView - android

I am working on Android and have being working on OpenGL ES. I have a xml layout as follows (I have took some things out to show only therelative content:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/myText"/>
<MyGLSurfaceView
android:id="#+id/mySurface"/>
Then in my GlSurfaceView I am trying to access the button that is in the same layout. However this is were I am having a problem.
I have tried the following:
View v = (View) getParent();
myTextViewString = (TextView) v.findViewById(R.id.myText);
this
myTextViewString = (TextView) findViewById(R.id.myText);
and this
myTextViewString = (TextView) ((RelativeLayout) getParent()).findViewById(R.id.myText);
I cannot seem to figure out how to access this button outside of the GLSurfaceView but is in the same activity inside my GLSurfaceView.java.
I do know that it has something to do with not being able to get the parent( I am assuming because it does not extend Activity). I have looked around and I cannot find a way to achieve this.

One clean and straightforward way to do this is to pass the button view to the GLSurfaceView. Aside from avoiding navigation through the view hierarchy, this also makes your view code more generic, since it will not have to know the id of a specific button.
In the onCreate() method of the activity, after calling setContentView(), you could have this:
MyGLSurfaceView glView = (MyGLSurfaceView) findViewById(R.id.mySurface);
TextView textView = (TextView) findViewById(R.id.myText);
glView.setTextView(textView);
In MyGLSurfaceView:
private TextView mTextView;
void setTextView(TextView textView) {
mTextView = textView;
}
Then, in other methods of MyGLSurfaceView, you can use mTextView anytime you need access to the button.

Related

new object issue

I'm new to Android Studio.
I made a TextView and a Button in Android Studio.
when I click button it supposed to trigger this method:
public void click (View view)
{
TextView tex = (TextView) findViewById(R.id.text_view);
tex.setText("Hello");
}
The code works in this way .
but when I made the method like this:
public void click (View view)
{
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
}
the code doesn't do what it supposed to do. I mean nothing happen to the TextView text.
Can anyone explain to me what's the difference? and why this happens or what's wrong about the second case?
TextView tex = new TextView(this);
That code creates new textview instance but this textview has no connection with your view.
But TextView tex = (TextView) findViewById(R.id.text_view); finds the first descendant view with the given ID and assigns it to your local variable so you have connection.
More info https://developer.android.com/reference/android/view/View#findViewById(int)
public void click (View view)
{
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
}
In this section, tex is a textView object but it does not have any field on the view.
TextView tex= findViewById(R.id.textView)
this line assigns a view to tex object.
The reason is quite obvious.
it seems to me that you are fairly new to programming and doesn't understand java very well, and doesn't have any programming experience of some sort. You see, Semi colons are used to terminate a line of code. So, thats one of the things you missed. Second, this:
TextView tex = (TextView) findViewById(R.id.text_view);
tex.setText("Hello");
gets a reference from a view on the first line and sets the text on the second line. While this:
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
Creates a new view on the first line. Gets a reference of the view on the second whilst not using its reference. And sets the text on the third line to the new view that was created on the first line.
Suggestion
OOP is not a really hard subject, but, if you really want to code, better know a little more about simple programming before diving in to OOP.
https://developer.android.com/reference/android/view/View.html#findViewById(int)
findviewbyid returns a View.
TextView tex = new TextView(this);
findViewById(R.id.text_view);
when you do this, you created a new TextView but you never actualy did anything with the View returned by findViewById since you ignored it.
There are two good ways to this as listed below
setOnClickListner
Using android view binding library (Maybe in combination with ViewModel and difficult, research nice and slow ! :p)
using android:onClick="methodName()" attribute, (I personally don't prefer this, AFAIK doesn't work in fragment)
Here is the Easiest one!
Put this code into onCreate after setContentView is called
Button btn = findViewById(R.id.your_btn_id);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle triggner here i.e set hello to your textView
}
});
Happy coding!
You need add tex into your layout to see it:
((ViewGroup) findViewById(R.id.some_container_id)).addView(tex)
When you call
findViewById(R.id.text_view);
and ignore returning value, this function have no effect

Change textsize of textview with button

I want to implement a feature that allows user to change the textSize of a textView in another view inside the app,
So I have a button with its "onClick" property set to:
Class mainActivity
public void increaseFont(View view)
{
MainViewPager.changeTextViewTextSize(mTextSize);
}
Class MainViewPager
static public void changeTextViewTextSize(int aTextSize)
{
View detailView = (View) LayoutInflater.from(mContext).inflate(R.layout.details, null);
TextView description = (TextView) detailView.findViewById(R.id.story_description);
description.setTextSize(aTextSize);
}
QUESTIONS is the textSize can't be changed when clicking the button. So how to?
The text size can changed at run time of course. You issue is related to the method changeTextViewTextSize. Using the inflater you are creating a new instance of R.layout.details, and through it, you are looking for the TextView you want to change the text size. But that layout is not at screen. It is not what you are seeing.

Android TextView setText

Okay, so I have this code:
<?xml version="1.0" encoding="utf-8?">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/Text"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:text="Text"/>
</LinearLayout>
(Main Activity) below:
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");
This doesn't replace the text, and I do have the class, and main methods. onCreate etc.
It seems that the XML file is the only one it will take a value from, if there's no android:text it shows nothing.
I'm basically trying to just change the text via the .setText function, what am I doing wrong?
Thanks
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement");
}
}
Layouts for files: /storage/emulated/0/AppProjects/New/res/layout/main.xml
/storage/emulated/0/AppProjects/New/gen/com/mycompany/New/R.java
/storage/emulated/0/AppProjects/New/src/com/mycompany/new/MainActivity.java
Move your findViewById after setContentView(), and change your variable names to follow Java convention:
public class MainActivity extends Activity {
#Override
public void onCreate(savedInstanceState){
super.onCreate(Bundle savedInstanceState);
setContentView(R.layout.main);
//this line should be called after you inflate your view with setContentView
//so move it here, instead in MainActivity body
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement");
}
}
Also, there's a typo in your xml layout. Change this line:
<?xml version="1.0" encoding="utf-8?">
to
<?xml version="1.0" encoding="utf-8"?>
You cannot find your views before setContentView() method. Move your lines after setContentView():
TextView Text = (TextView) findViewById(R.id.Text);
Text.setText("Text Replacement);
Also, please follow java naming conventions, as obect names never start with Caps but class names do..
Move the below to onCreate
TextView Text = (TextView) findViewById(R.id.Text);
Like
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.Text);
tv.setText("Text Replacement);
findViewById looks for a view with the id in the current inflated layout. So first you need to set the layout to the activity and then initialize views.
Also consider renaming Text to something more appropriate and meaningfull.
Edit:
Make sure you have the textview with id Text in main.xml. Build and clean your project.
You should innialize your view and set proper properties in andriod after setContentView method is been called.
So this below code should come after setcontent view
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement);
Looks like you have two "" before the "Text"
Try deleting one...
Should look like this:
android:text="Text"/>
First, consider using "wrap_content" or "match_parent" attributes instead of "200dp" for example.
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Text"/>
Then, please rename your "Text" variable into "text". Java or Android may be thinking that Text is not your variable but a class:
TextView text = (TextView) findViewById(R.id.Text);
text.setText("Text Replacement!");
Ur variable is declared as TextView Text
Instead u should use text because in Java Anything starting with capital letter is treated as a class ,it is a bad programming practice ! The variables should be in lowercase(hungerian notation) !

Android Programmatically Stroke

I want to draw a black stroke on my text in Android.
I have seen this example:
How do you draw text with a border on a MapView in Android?
Where the solution overrides onDraw() to create the stroke.
The problem is, I'm still relatively starting out in Android, and I have no idea how to transition to using that solution.
In my onCreate I set the text typeface (it's custom):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeatures();
// Set content view and component listeners
setContentView(R.layout.meme_maker);
setListeners();
context = this;
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
TextView mmt = (TextView) findViewById(R.id.meme_maker_title);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView tbc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setTypeface(tf);
tbc.setTypeface(tf);
mmt.setTypeface(tf);
}
And I have an onClickListener where I change the text content of the TextView, based on the user writing the text he/she wants in a TextEntry and clicking a button afterwards.
ImageView ii = (ImageView) findViewById(R.id.insert_image);
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText tt = (EditText) findViewById(R.id.top_text_text);
EditText bt = (EditText) findViewById(R.id.bottom_text_text);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView btc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setText(tt.getText().toString().toUpperCase());
btc.setText(bt.getText().toString().toUpperCase());
}
});
It's pretty straightforward so far. My question is: how to insert the stroke of the text? Where? Do I need to create a Canvas and Paint objects?
The simplest way to get a shadow for text rendered in a TextView is to set up a style as described in this answer. That requires very little work and sounds like it will work fine in your situation.
Using the technique you link to involves extending an existing View class, overriding onDraw(), and using Canvas.drawText() on the canvas passed to onDraw() to render the text yourself. That can be exactly what you need in some situations, but sounds like overkill for your current situation. If you want to look into it further, the Android dev guide on the subject is a good read.

Get reference to Views in my Android Activity

I have a LinearLayout comprising of a few Buttons and I add this to my activity in the onCreate(..) method with setContentView(R.layout.myscreen). No surprises so far.
How do I get a reference to an iterator to these buttons? I'd like to add listeners to them but I'd rather not directly reference the Button's using their android:id.
Similar questions have been asked here and here but they don't quite answer my question.
Try something like this provide an id root_layout in xml to LinearLayout
LinearLayout mLayout = (LinearLayout) findViewById(R.id.root_layout);
for(int i = 0; i < mLayout.getChildCount(); i++)
{
Button mButton = (Button) mLayout.getChildAt(i);
mButton.setOnClickListener(this);
}
Where mLayout is object of you Linear Layout and Your activity must implements OnClickListener and here goes general listener
#Override
public void onClick(View v)
{
Button mButton = (Button)v;
String buttonText = mButton.getText().toString();
}
NOTE: For this to work properly you Linear Layout must only contains button no other views
You should take a look at my answer here.
In short. I'd assign the buttons a listener by setting the onClick attribute in the XML layout on each Button.
Inside of your Activity you'll need a public method like the one below which basically is what you want to do in your listener.
public void myFancyMethod(View v) {
// do something interesting here
}
If you want to go for accessing other elements you may try following syntax:
<ElementClass> <referencevariable> = (<ElementClass>) findViewById(R.id.<id_of_the_element>);
For Example:
TextView textView= (TextView) findViewById(R.id.t1); //I used t1 to refer my textview in the Layout.
This might work.
Then you can use these views with their inbuilt methods to perform as many as work you want.

Categories

Resources