1

I have a problem about how to get view From XML layout in Android? I am using spinner, it can save selected spinner value right now. I wanna get view of TextView with id titleTextView from content_doa.xml layout. I've tried to use LayoutInflater but it doesn't work, it doesn't give any effect yet. Can you help my problem?

Here you go:

FontSettings.java

public class FontSettings extends AppCompatActivity {

private Spinner spinner1, spinnerLatin;
private SharedPreferences mMyPrefs;
private SharedPreferences.Editor mMyEdit;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_font);

    // toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //this line shows back button

    //Display data size teks arab in dropdown list spinner
    final Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
    final LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final SharedPreferences sharedPref = getSharedPreferences("My_Prefs", 6);
    ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);
    spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);
    spinnerBackgroundChange.setSelection(sharedPref.getInt("VALUE", 6));

    //Save spinner value
    spinnerBackgroundChange.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            ((TextView) view).setTextColor(Color.parseColor("#226169"));

            switch(position) {
                case 0:
                    SharedPreferences.Editor editor0 = sharedPref.edit();
                    editor0.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor0.commit();

                    Activity activity = (Activity) parent.getContext();
                    TextView dgs = (TextView) activity.findViewById(R.id.tekzArab);
                    dgs.setTextSize(12);
                    break;

                case 1:
                    SharedPreferences.Editor editor1 = sharedPref.edit();
                    editor1.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor1.commit();

                    Activity activitys = (Activity) parent.getContext();
                    TextView dgf = (TextView) activitys.findViewById(R.id.tekzArab);
                    dgf.setTextSize(14);

                    View contentView = inflater.inflate(R.layout.content_doa, null,false);
                    TextView titleMessage = (TextView) contentView.findViewById(R.id.titleTextView);
                    titleMessage.setTextSize(14);
                    break;

                case 2:
                    SharedPreferences.Editor editor2 = sharedPref.edit();
                    editor2.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor2.commit();

                    Activity activityz = (Activity) parent.getContext();
                    TextView dgy = (TextView) activityz.findViewById(R.id.tekzArab);
                    dgy.setTextSize(16);
                    break;

                case 3:
                    SharedPreferences.Editor editor3 = sharedPref.edit();
                    editor3.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor3.commit();

                    Activity activitye = (Activity) parent.getContext();
                    TextView dgq = (TextView) activitye.findViewById(R.id.tekzArab);
                    dgq.setTextSize(18);
                    break;

                case 4:
                    SharedPreferences.Editor editor4 = sharedPref.edit();
                    editor4.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor4.commit();

                    Activity activityq = (Activity) parent.getContext();
                    TextView dgp = (TextView) activityq.findViewById(R.id.tekzArab);
                    dgp.setTextSize(20);
                    break;

                case 5:
                    SharedPreferences.Editor editor5 = sharedPref.edit();
                    editor5.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor5.commit();

                    Activity activityc = (Activity) parent.getContext();
                    TextView dgn = (TextView) activityc.findViewById(R.id.tekzArab);
                    dgn.setTextSize(22);
                    break;

                case 6:
                    SharedPreferences.Editor editor6 = sharedPref.edit();
                    editor6.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor6.commit();

                    Activity activitym = (Activity) parent.getContext();
                    TextView dgb = (TextView) activitym.findViewById(R.id.tekzArab);
                    dgb.setTextSize(24);
                    break;

                case 7:
                    SharedPreferences.Editor editor7 = sharedPref.edit();
                    editor7.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());
                    editor7.commit();

                    Activity activityo = (Activity) parent.getContext();
                    TextView dgl = (TextView) activityo.findViewById(R.id.tekzArab);
                    dgl.setTextSize(26);
                    break;

                default:
                    Activity activityu = (Activity) parent.getContext();
                    TextView dgx = (TextView) activityu.findViewById(R.id.tekzArab);
                    dgx.setTextSize(24);
                    break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


    //Display data size teks latin in dropdown list spinner
    Spinner spinnerLatin = (Spinner)findViewById(R.id.spinnerLatin);
    ArrayAdapter<CharSequence> spinnerArrayLatin = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayLatin.setDropDownViewResource(R.layout.textview_with_background);
    spinnerLatin.setAdapter(spinnerArrayLatin);
    // spinnerLatin default value
    spinnerLatin.setSelection(1);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
} }

settings_font.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#226169"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_collapseMode="pin"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- Font Arab -->
        <!-- Judul -->
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/fontArab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="15dp"
                android:paddingLeft="15dp"
                android:paddingTop="15dp"
                android:text="Font Arab"
                android:textColor="#226169"
                android:textSize="20sp" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tekzArab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:paddingBottom="20dp"
                android:paddingRight="10dp"
                android:paddingTop="20dp"
                android:text="أسْتَغْفِرُ اللهَ العَظِيمَ"
                android:textColor="#222222" />

        </RelativeLayout>


        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reldoa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:background="@android:color/white"
    android:orientation="vertical">

            <TextView
                android:id="@+id/sizedoa"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:text="Ukuran Font"
                android:textColor="#222222"
                android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reldoa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/sizelatin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingTop="5dp"
        android:text="Jenis Font"
        android:textColor="#222222"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:entries="@array/type_arrays"
        android:prompt="@string/type_font"/>

</RelativeLayout>

        <!-- Font latin -->

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/relLatin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="45dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/fontLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="15dp"
                android:paddingLeft="15dp"
                android:paddingTop="15dp"
                android:text="Font Latin"
                android:textColor="#226169"
                android:textSize="20sp" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tekzLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="20dp"
                android:paddingRight="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="20dp"
                android:text="Aku meminta ampunan kepada Allahَ"
                android:textColor="#222222" />

        </RelativeLayout>


        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/relLatin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/sizeLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:text="Ukuran Font"
                android:textColor="#222222"
                android:textSize="18sp" />

            <Spinner
                android:id="@+id/spinnerLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:entries="@array/country_arrays"
                android:prompt="@string/country_prompt" />

        </RelativeLayout>

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reldoa"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tipeLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:text="Jenis Font"
                android:textColor="#222222"
                android:textSize="18sp" />

            <Spinner
                android:id="@+id/spinnerTipe"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:entries="@array/type_arrays"
                android:prompt="@string/type_font"/>

        </RelativeLayout>

    </LinearLayout>
</ScrollView>

content_doa.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/coordinatorLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/colorButtonNormal">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical"
    android:padding="5dp"
    android:background="?attr/colorButtonNormal">

    <!-- TextView Nama Mahasiwa -->
    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="15sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="#222222"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/readmore"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="15sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:text="Baca selengkapnya"
        android:textColor="#000ecc"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/latinDoa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="8sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="@android:color/holo_green_dark"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/artiDoa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="8sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="#222222" />

    <TextView
        android:id="@+id/sumberDoa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="8sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textColor="#222222" />

    <TextView
        android:id="@+id/readless"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:lineSpacingExtra="15sp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:text="Baca lebih sedikit"
        android:textColor="#000ecc"
        android:textSize="14sp" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relmasjid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonbaca"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />

        <Button
            android:id="@+id/buttoncopy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Salin" />

        <Button
            android:id="@+id/buttonshare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:actionProviderClass="android.widget.ShareActionProvider"
            android:text="Bagikan" />

    </RelativeLayout>

</LinearLayout>

  • When you say your trying to get view what do you mean? Like as in you want to get a reference of the textview so you can set the text? Please clarify – Vahalaru Oct 23 '18 at 00:39
  • Yes, I wanna get a reference of the textview with id titleTextView located in content_doa.xml layout. so I can setTextSize at theTextView – Sa'ad Abdurrazzaq Oct 23 '18 at 00:52

2 Answers2

0

Ok, so I've got it now. You have an arraylist in your resources that your populating your spinner with. when the item is clicked the code will then get the position of that item in your array list and set it in shared preferences to then be accessed in ActivityContent.java.

In ActivityContent.java your value your getting from the shared preferences is the position of the item in arraylist.

First just to help you learn, I will explain what you can do with this. Then second, I will explain what you were intending to do.

First

You have the position of item in arraylist so what you can do with that is in your ActivityContent.java class, do this.

ActivityContent.java

String[] stringArray = getResources().getStringArray(R.array.country_arrays)
SharedPreferences sp = getSharedPreferences("My_Prefs", Activity.MODE_PRIVATE); 
int sizeItemSelected= sp.getInt("VALUE", 24);

//if array contains strings if not just make this variable an Int
String sizeToSetTextViewSizeTo = stringArray[sizeItemSelected];
int tvSize = Integer.parseInt(sizeToSetTextViewSizeTo);


TextView titleTV = (TextView) findViewById(R.id.titleTextView);
titleTV.setTextSize(tvSize);

Second

You just pass the selected item (the size you want to set the textview to in activitycontent) in place of passing the position.

What I think you were intending the code to do in the first place

case 1:
SharedPreferences.Editor editor1 = sharedPref.edit();
editor1.putInt("VALUE", spinnerBackgroundChange.getSelectedItem());                   
editor1.commit();
break;

Instead of this

case 1:
SharedPreferences.Editor editor1 = sharedPref.edit();
editor1.putInt("VALUE", spinnerBackgroundChange.getSelectedItemPosition());                   
editor1.commit();
break;

The only thing is I have never see it done like this withing a case: inside of its own

OnItemSelectedListender()

as you have it. But I don't see why that would effect First or seccond options of code.

Vahalaru
  • 380
  • 4
  • 15
  • But unfortunately I have another problem,, When your first code placed in MainActivity with architecture like this (https://www.androidhive.info/2017/11/android-recyclerview-with-search-filter-functionality/), it's not working! Help me please... – Sa'ad Abdurrazzaq Oct 25 '18 at 05:29
-1

UPDATED with link to solved question Sorry for no code. I can add a few lines when I get home but the best option is number 3 and that is something you need to research and play with.

You can not directly interact with views from another activity. That activitys layout isn't inflated so you will just end up with null pointer exceptions. You must do this indirectly. There's 3 ways to do that right off the top of my head.

  1. SharedPreferences Set a value in shared preferences from your spinner like you are. But in your activitycontent.java you have it get shared preferences and look to see if a value resides in a certain key value pair. If so then in activitycontent.java have it set text size to that value. If it is null or empty then it will be the value you have set the the contentdoa.xml value to it. This would be easiest since you would wouldn't have top write code for a database and you wouldn't have to completely re-write your entire app

Activitycontent.java

Add this to the onCreate

SharedPreferences sp = getSharedPreferences("My_Prefs", Activity.MODE_PRIVATE); 
 int titleTextSize = sp.getInt("VALUE", 24);
TextView titleTV = (TextView) findViewById(R.id.titleTextView);
titleTV.setTextSize(titleTextSize);

Setting TextViewSize programatticly

Look at that post and read through the answers and comments. Also another thing you could try is adding an I'd to the linear layout, then referencing it first then something like.

linearLayout.findViewById..... you know how the rest goes
  1. Create a SQLite database and store your values in that. For all your modifications and on anything that can be changed in each activity you have that views activity check if value is set if so set views textSize to that value. This I would recommend more since you have so many things you can change.
  2. What I would recommend even more is looking into and working using this for your apps settings. Default SharedPrefences for Apps Settings
Vahalaru
  • 380
  • 4
  • 15
  • Yes, I have another activity, assume the name of this activity is, ActivityContent.java, and this activity wired together with content_doa.xml. So, how I can set the size font size at the content_doa.xml via spinner click? I don't understand your theory,, can you write me a code? – Sa'ad Abdurrazzaq Oct 23 '18 at 04:01
  • Oh I see what you mean. You Intend to change that activitys textview text size from the settings. I'll update my answer, I'll try and put some code but I'm on my phone so I can update with more code in about an hour. – Vahalaru Oct 23 '18 at 06:12
  • I've tried your code to Activitycontent.java but doesn't work, it show blank. – Sa'ad Abdurrazzaq Oct 24 '18 at 12:13
  • Hi @Vahalaru, it show blank. I have no idea, seems it can't get text size, it just get the position of selected spinner array. Help me please, I've been stuck here many days! – Sa'ad Abdurrazzaq Oct 24 '18 at 13:44
  • In the shareedpreferences instead of using "VALUE" such as "TVSIZE" Try putting the number you want the size to be in that. And in content activity calling getInt "TVSIZE" – Vahalaru Oct 24 '18 at 13:48
  • Yes, I did. Still not working. I change VALUE to VALUE1, and then I put 1 and calling getInt "VALUE1". editor1.putInt("VALUE1", spinnerBackgroundChange.getSelectedItemPosition()); int titleTextSize = sharedPref.getInt("VALUE1", 1); – Sa'ad Abdurrazzaq Oct 24 '18 at 14:03
  • That's the problem right there. Your passing the reference of the items position instead of the value of the int at that position. Ummm put a . After getitemposition() see if any variables show and the one you want might be there. If not you will probably have to modify your adapter and make it so you can get the value of the int at items position. – Vahalaru Oct 24 '18 at 14:18
  • Without being able to see your voice it's hard to say exactly. But we're on the right track. Logically you can now say your getting the position. So were close. The next step would be well then how to get the value of a variable or element of a list at position. – Vahalaru Oct 24 '18 at 14:22
  • Yes, thats the problem, how to get the value of a variable or element of a list at position? I've searched in google many times, and i got nothing. – Sa'ad Abdurrazzaq Oct 24 '18 at 14:30