Change TextView on content_main.xml in MainActivity.java - java

I'm using the Android Studio standard template "Navigation Drawer Activity" which contains the content_main.xml layout.
On this layout is my textView which I want to change the text from my MainActivity.java.
I tried following but nothing changes:
LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.content_main, null);
TextView txt = (TextView)view.findViewById(R.id.txt_head);
txt.setText("sdfsdf");
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/relativelayout_for_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.w4ter.ledcontroldesign.MainActivity"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical"
android:paddingTop="10dp">
<TextView
android:text="Presets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:id="#+id/txt_head" />
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#61000000" />
</LinearLayout>

there is no need of LayoutInflater. Simply use:
TextView textView = (TextView) findViewById(R.id.txt_head);
texView.setText("Hello");
Hope this helps.

You just need to access the TextView component the normal way. With that template the content_main.xml is included inside the main layout, so you sould do as follows in your main activity (maybe inside the onCreate method or wherever you need):
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = (TextView)view.findViewById(R.id.txt_head);
txt.setText("sdfsdf");
}
//...
}
Assuming that you have a app_bar_main.xml inside the main layout wich contains the content_main.xml. In the end everything is accessible from the main activity directly since the layouts are included in the main layout.
Hope it helps!

Related

Code referencing internal library not working

I am trying to make my first library to learn and see how things work. What is supposed to happen is that my main activity (in the sample or whatever app uses the local library) will show a loading animation from the library. But when I add in the code, it does not show in the sample. I'm not getting any errors or anything. What could be going on?
Sample Main Activity:
public class MainActivity extends AppCompatActivity {
private ProgressBar spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//This is what isnt working. The spinner
spinner = (ProgressBar)findViewById(com.marlonjones.ezloadanimlibrary.R.id.main_progress);
library XML:
<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">
<ProgressBar
android:id="#+id/main_progress"
android:layout_height="50dp"
android:layout_width="60dp"
android:layout_centerInParent="true"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/main_ind_progress"/>
</RelativeLayout>
main_ind_progress:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/loader_main"
android:pivotX="50%"
android:pivotY="50%"/>
The drawable main_ind_progress, and the drawable loader_main are both inside of the library.
All code: https://github.com/MJonesDev/EZLoadAnimLibrary
The ProgressBar view isn't added to your current Activity i.e. you have no view of the type ProgressBar that is why it isn't showing anything
You can assign an id to the root RelativeLayout in the content_main layout file of your current Activity then in the OnCreate() method
RelativeLayout rL = (RelativeLayout) findViewById(R.id.your_id);
// and then add your spinner to the relative layout
rL.addView(snipper);
or what you can do is make a ProgressBar in your current app's content_main.xml file
<ProgressBar
android:id="#+id/my_progress_bar"
android:layout_height="50dp"
android:layout_width="60dp"
android:layout_centerInParent="true"
android:indeterminate="true"
/>
and assign it the main_ind_progress to it from your library
ProgressBar spinner=(ProgressBar) findViewById(R.id.your_progress_bar_id);
spinner.setIntermediateDrawable(com.marlonjones.ezloadanimlibrary.R.drawable.main_ind_progress);

How to use a base layout and custom it in android

I'am trying to create a Linear Layout and an editText base on a layout.xml file here is the java code :
private EditText editText(int _intID) {
EditText editText = (EditText)findViewById(R.id.player_name);
editText.setId(_intID);
editText.setHint("Element "+_intID);
editTextList.add(editText);
return editText;
}
private LinearLayout linearlayout(int _intID)
{
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
LLMain.setId(_intID);
LLMain.addView(editText(_intID));
LLMain.setOrientation(LinearLayout.VERTICAL);
linearlayoutList.add(LLMain);
return LLMain;
}
Here is the xml layout I want to use as a base :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:id="#+id/linear_player_base">
<EditText
android:id="#+id/player_name"
android:background="#drawable/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionSend"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
/>
</LinearLayout>
I don't understand why when I do :
LinearLayout LLMain= (LinearLayout)findViewById(R.id.linear_player_base);
and then :
LLMain.setId(_intID);
I have this error : Attempt to invoke virtual method 'void android.widget.LinearLayout.setId(int)' on a null object reference
why LLMain is null ?
You must inflate a custom view with you layout and the find the linear layout in this view...
Do it like this:
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.playerLayout, null);
LinearLayout LLMain= (LinearLayout) v.findViewById(R.id.linear_player_base);
Have you loaded this xml file that you want to use as base ??
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutNameHere);

How do I hide or show layout elements when an Android Activity goes out of focus?

I am trying to hide certain elements in a layout when Android goes out of focus, such that when viewed in the "View recent applications" display, the layout elements will not be visible. Upon selecting the application and going into focus, these elements will be visible again.
My implementation below attempts to show the "content" layout when the application is in focus, and show the "overlay" layout when out of focus, via onWindowFocusChanged().
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the content page"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the overlay"/>
</RelativeLayout>
</FrameLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
FrameLayout layoutContainer;
RelativeLayout layoutContent;
RelativeLayout layoutOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutContainer = (FrameLayout) findViewById(R.id.container);
layoutContent = (RelativeLayout) findViewById(R.id.content);
layoutOverlay = (RelativeLayout) findViewById(R.id.overlay);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
layoutContent.setVisibility(View.VISIBLE);
layoutOverlay.setVisibility(View.GONE);
} else {
layoutOverlay.setVisibility(View.VISIBLE);
layoutContent.setVisibility(View.GONE);
}
}
}
enter code here
However, my implementation does not work. Anyone with any suggestion as to solve this? Thanks!
If you want to hide views in your layout in recent apps try like this
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);

Android Wear NullPointerException can't set text on TextView

I am trying to write an Android Wear application. For now I've got a TextView and would like to set the text programmatically.
private TextView mTextView;
private TextView text;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
text = (TextView)findViewById(R.id.text);
stub.setOnLayoutInflatedListener((stub -> {
mTextView = (TextView)stub.findViewById(R.id.text);
}};
text.setText("test");
}
When trying to run the application like that I get a nullpointerexception at
text.setText("test");
That led me to assume that the TextView "text" was not found. Normally on Android phone applications it is just fine to call the
text = (TextView)findViewById(R.id.text);
Directly after
setContentView();
But now there are things like the WatchViewStub and the setOnLayoutInflatedListener. Maybe there's something I've overseen?
However, how do I specify my TextView correctly on the Android Wear platform?
Edit:
activity_my.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
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:id="#+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="#layout/rect_activity_my"
app:roundLayout="#layout/round_activity_my"
tools:context=".MyActivity"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>
rect_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
tools:context=".MyActivity"
tools:deviceIds="wear_square">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
round_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MyActivity"
tools:deviceIds="wear_round">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
According to code from your Activity:
private TextView mTextView;
private TextView text;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
text = (TextView)findViewById(R.id.text);
stub.setOnLayoutInflatedListener((stub -> {
mTextView = (TextView)stub.findViewById(R.id.text);
}};
text.setText("test");
}
Why do you have two TextViews? You have only one either in your rect_activity: or round_activity: layouts.
You have to understand how WatchViewStub works. You cannot access views from your rect/round layouts right after setContentView because they are not inflated yet. Like you did - you can access your views only in OnLayoutInflatedListener listener callback.
You have tried to find your mTextView inside this callback - which is the way you should do it.
What is wrong?
You should remove following lines:
text = (TextView)findViewById(R.id.text);
and this one: (it also causes your NullPointerException)
text.setText("test");
and keep only the mTextView field.
What should you do?
Add following like right after mTextView = (TextView)stub.findViewById(R.id.text);:
mTextView.setText("test");
It must be incide the OnLayoutInflatedListener.
Your final code should look like:
private TextView mTextView;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener((stub -> {
mTextView = (TextView)stub.findViewById(R.id.text);
mTextView.setText("test");
}};
}
You do not have TextView in activity_my.xml
text = (TextView)findViewById(R.id.text);
if rect_activity.xml and round_activity.xml are for different activities then you need to create new activities and call setContentView for those xml
Only after that you can use the views in your code.
Instead of adding an OnLayoutInflatedListener you can extend InsetActivity and set your content view in InsetActivity.onReadyForContent():
import android.support.wearable.activity.InsetActivity;
public class MainActivity extends InsetActivity {
...
#Override
public void onReadyForContent() {
setContentView(R.layout.activity_main);
someTextField = (TextView) findViewById(R.id.some_field);

Change properties of TextView in ListView row xml

Hey, I want to change the TypeFace of one of the TextViews to an external font.. How can I do that?
Code:
public class English extends Activity {
<...>
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quran_english);
listview=(ListView) findViewById(R.id.QuranEnglishListView);
<..add into list..>
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.quran_english_row,
new String[] {"Arabic","English"},
new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});
listview.setAdapter(adapter);
xml for each row (quran_english_row.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">
<LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
xml for the layout (quran_english.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">
<ListView android:id="#+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
This crashes because of NullPointer:
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
So how can I reach the TextView that is in the row to change its TypeFace? Thanks
You can try to get the layout for each list item by
View rowView = adapter.getView(...);
Then get the TextView of the layout by
TextView txt = (TextView)rowView .findViewById(R.id.QuranEnglishTextViewEnglish);
txt.setTypeFace(...);
and set your TypeFaces. Haven't tested that yet, but i guess it could work.
But I recommend extending your own BaseAdapter. Then create another list-row class which provides the inflated layouts for your BaseAdapter. There you'll be able to set the typefaces easily for every TextView.
try this links it works http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html see steps and class MyTextView and MyButton shown above layout

Categories

Resources