Making a textview global - java

I am wondering if there is any way that you can make a textview visible over every screen so that I don't have to individually create a textview on every activity.
I want to do this so I can display my application version on every screen for debugging.
Thank you.

Yes! it is possible.
See you have to add this code.
<include layout="#layout/layout_version"/>
and
public class MainActivity extends BaseActivity {
}
And you will find version text.
Just add this few code.
(1) Create a layout that will represent version text.
layout_version.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" // or match_parent (according to your need)
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... other properties
/>
<!--... other component if required-->
</LinearLayout>
(2) Include this to your activity layout like activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
...>
<include layout="#layout/layout_version"/>
</LinearLayout>
(3) Create a BaseActivity.class or add my code if you have already.
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.innovanathinklabs.sample.BuildConfig;
import com.innovanathinklabs.sample.R;
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tvVersion = findViewById(R.id.tvVersion);
if (tvVersion != null) tvVersion.setText(BuildConfig.VERSION_NAME);
}
}
(4) Final step is to extend all your activity by BaseActivity.class.
public class MainActivity extends BaseActivity {
}
That's all!

Related

Why is the Toast not working? Is there anything wrong in this code?

I have just started with Android Development, i am facing an error with toast. I searched on Google as well, but couldn't fix my problem.
Here is the java Code:
package com.example.uibasicssection2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnHello:
Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHello = findViewById(R.id.btnHello);
btnHello.setOnClickListener(this);
}
}
XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say Hello"
android:layout_centerInParent="true"
android:id="#+id/btnHello"/>
</RelativeLayout>
When i run the above code I can see the button but when i click on the button there is no Toast Notification, is there anything wrong with this code?
//pass activity context
Toast.makeText(MainActivity.this,"Hello",Toast.LENGTH_SHORT).show();

How do i make use of two relative layouts on one single screen?

I cant operate with two relative layouts (one in another). my app doesn't starts. it keeps stopping. I am using a physical device (Samsung Galaxy On Max) for running my apps from android studio.
XML file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="161dp"
android:layout_marginTop="340dp"
android:layout_marginEnd="162dp"
android:onClick="Next"
android:text="Next"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/newLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<TextView
android:id="#+id/nextText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="132dp"
android:layout_marginTop="354dp"
android:layout_marginEnd="132dp"
android:background="#F30808"
android:text="There you Go!"
android:textColor="#0B0A0A"
android:textSize="24sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java file:
package com.example.twolayouts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView nextText;
Button Next;
RelativeLayout newLayout;
public void Next(View view)
{
Next.setVisibility(View.INVISIBLE);
newLayout.setVisibility(View.VISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextText=(TextView)findViewById(R.id.nextText);
Next=(Button)findViewById(R.id.Next);
newLayout=(RelativeLayout)findViewById(R.id.newLayout);
}
}
Why you are creating object of relative layout while your layout in xml is constraint layout.
Try this :
public class MainActivity extends AppCompatActivity {
TextView nextText;
Button Next;
ConstraintLayout newLayout;
public void Next(View view)
{
Next.setVisibility(View.INVISIBLE);
newLayout.setVisibility(View.VISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nextText=(TextView)findViewById(R.id.nextText);
Next=(Button)findViewById(R.id.Next);
newLayout=(ConstraintLayout)findViewById(R.id.newLayout);
}
}
You should try looking into your logcat windows it will help you out for what error you are facing.
Kindly check on which line of code the app is crashing. Post your error log here !
problem is you used constraint layout in your xml layout file and creating object of relative layout in java file
replace below line :
RelativeLayout newLayout;
with :
ConstraintLayout newLayout;

How to put GLSurfaceView inside a layout on android

I would like to create an app where the glsurfaceview is inside a relative layout together with a listview. While coding, my IDE shows no errors, but once I run the application it just crashes.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<ListView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:background="#6D6D6D"
android:id="#+id/listObjects"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="false"/>
<com.shirofuji.thrrededit.GL_handler
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:id="#+id/gl_layout"
android:layout_alignParentLeft="true"
android:layout_alignEnd="#id/listObjects"/>
</RelativeLayout>
MainActivity
package com.shirofuji.thrrededit;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.opengl.*;
public class MainActivity extends Activity
{
GL_handler gl_maineditor;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
//layout_3d.addView(gl_maineditor);
setContentView(R.layout.main);
}
#Override
public void onResume(){
super.onResume();
gl_maineditor.onResume();
}
#Override
public void onPause(){
super.onPause();
gl_maineditor.onPause();
}
}
GL_handler
package com.shirofuji.thrrededit;
import android.opengl.*;
import android.content.Context;
import android.view.View;
import android.util.*;
public class GL_handler extends View
{
GLSurfaceView mainsurface;
GL_renderer scene_renderer;
public GL_handler(Context ctx,AttributeSet attr){
super(ctx,attr);
mainsurface = new GLSurfaceView(ctx);
scene_renderer = new GL_renderer();
mainsurface.setRenderer(scene_renderer);
}
public void onResume(){
mainsurface.onResume();
}
public void onPause(){
mainsurface.onPause();
}
}
The order of calls in the onCreate() method of the activity is wrong:
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
setContentView(R.layout.main);
setContentView() needs to be called before you can get any view with findByViewId(). The sequence should be:
setContentView(R.layout.main);
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
With that out of the way, I think there's another aspect where you might be on the wrong track. Unless you have a master plan that is just not visible from the posted code. You're not placing a GLSurfaceView in your layout. The GL_handler you place in the layout derives from a plain View. It has a GLSurfaceView as a member variable, but that alone will not make the GLSurfaceView appear.
Unless you have a good reason, it should be much easier to derive your GL_handler class from GLSurfaceView. This will actually result in a GLSurfaceView that becomes part of your view hierarchy when the layout is inflated.

Can I use setContentView outside the oncreate method?

I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:
AlarmActivity.java:
package com.alarm.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button buton = new Button(this);
buton.setId(101);
buton.setText("New alarm");
buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(buton);
setContentView(layout1);
Button nou =(Button)findViewById(101);
nou.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
New_ala nou1=new New_ala();
nou1.nou_alarma();
}
});
}
}
New_ala.java:
package com.alarm.example;
public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);
}
}
TimePicker.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"
android:orientation="vertical" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/picker_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save">
</Button>
<Button
android:id="#+id/picker_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?
The code after setting the intent:
AlarmActivity:
package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, NewAlarmActivity.class));
}
}
NewAlarmActivity:
package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepicker);
}
}
You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.
The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker
The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.

Problem regarding extending ListActivity

In my project whenever I extend activity it works fine but as soon as I extend ListActivity it throws exception and shows file not found. Why is that? We already know that ListActivity itself extends the Activity class. The application must run fine.
Here's the java file:
package com.android.feedGrabber;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.sun.cnpi.rss.elements.Item;
import com.sun.cnpi.rss.elements.Rss;
import com.sun.cnpi.rss.parser.RssParser;
import com.sun.cnpi.rss.parser.RssParserFactory;
public class feedGrabber extends Activity {
public static Collection<Item> readRSSDocument(String url) throws Exception {
RssParser parser = RssParserFactory.createDefault();
URL feedUrl = new URL(url);
URLConnection urlc=feedUrl.openConnection();
urlc.setRequestProperty("User-Agent","");
urlc.connect();
Rss rss = parser.parse(urlc.getInputStream());
return rss.getChannel().getItems();
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int links=0;
try
{
for(Item item : feedGrabber.readRSSDocument("http://feeds.feedburner.com/Tutorialzine")){
if(links++>3)
break;
TextView t = (TextView)findViewById(R.id.title);
t.append("Title: ");
t.append(item.getTitle().toString());
t.append("\n\n");
//System.out.println("Title: " + item.getTitle());
//System.out.println("Link: " + item.getLink());
}
}catch(Exception e)
{
//
}
}
}
If I change "extends Activity" to "extends ListActivity" it gives the exception. I need to bring this change because I want to implement it on ListView instead of TextView.
Error it throws:
debug mode gets active and it highlights under Suspended(exception RunTimeException): ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663.
When you extend a ListActivity, it is expecting the Layout to have a ListView that has an id of "#android:id/list", if you do not have it, it will throw an exception
You layout should look something like this:
<?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">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/noresults"/>
</LinearLayout>
Without seeing your main.xml or the exception I can't be sure, but I'd suggest checking if your list has the #android:id/list identity as required.

Categories