I am currently trying to get a string out of an EditText field.
Here is my Code:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
public class FirstRunDialog extends DialogFragment {
public static final String DEBUG_TAG = FirstRunDialog.class.getSimpleName();
private static final String PREFS_NAME = "MyPrefsFile";
private LayoutInflater inflater;
private View dialogView;
private EditText input;
private DialogInterface.OnClickListener listener;
private SharedPreferences settings;
private SharedPreferences.Editor editor;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof DialogInterface.OnClickListener) {
listener = (DialogInterface.OnClickListener) activity;
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.app_name);
builder.setView(R.layout.first_run_dialog);
builder.setCancelable(false);
builder.setPositiveButton("Key-Testen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
dialogView = getActivity().getLayoutInflater().inflate(R.layout.first_run_dialog, null);
input = (EditText) dialogView.findViewById(R.id.editText);
editor = settings.edit();
editor.putString("apiKey", input.getText().toString());
editor.commit();
Log.d(DEBUG_TAG, "apiKey Input: " + input.getText().toString());
Log.d(DEBUG_TAG, "apiKey: " + settings.getString("apiKey", ""));
dialog.dismiss();
}
});
builder.setNegativeButton("Key-Erstellen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Open Browser, then go to: https://account.arena.net/applications
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://account.arena.net/applications"));
startActivity(browserIntent);
}
});
return builder.create();
}
}
This code is not working! According to the answers how to retrieve text from an EditText I did nothing wrong. The EditText is located in an AlertFragment.
The current output for the Log.d(...) is (and the same goes for the second):
apiKey Input:
Thx in advance.
Do not inflate a new view when the button is pressed.
Try using getDialog():
builder.setPositiveButton("Key-Testen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
input = (EditText) getDialog().findViewById(R.id.editText);
editor = settings.edit();
editor.putString("apiKey", input.getText().toString());
editor.commit();
Log.d(DEBUG_TAG, "apiKey Input: " + input.getText().toString());
Log.d(DEBUG_TAG, "apiKey: " + settings.getString("apiKey", ""));
dialog.dismiss();
}
});
try this
take out this line from the onclick
input = (EditText) dialogView.findViewById(R.id.editText);
and inflate the view
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
builder .setView(dialogView);
EditText input = (EditText) dialogView.findViewById(R.id.editText);
then input.gettext().tostring in the onclick, sorry for my english
and I hope to help
Related
How to display the name after the user has inputted the data and how to make it a button that i can add something there making it something like a profile i want you add a alarm inside it but first i got to make this database.
i dont really know how to how to fix it i tried this code but it doesnt work or it always resulted to a force close when i try to add a
name = edtName.getText().toString();
AccountInfo a = new AccountInfo(name);
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
private EditText edtName;
private Button btnAdd;
Spinner spin1;
Spinner spin2;
Spinner spin3;
#SuppressLint("WrongConstant")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tablayout);
appBarLayout = findViewById(R.id.bar);
viewPager = findViewById(R.id.viewpager);
edtName = findViewById(R.id.edtName);
spin1 = findViewById(R.id.spinnerFrequency);
spin2 = findViewById(R.id.SpinnerTime);
spin3 = findViewById(R.id.SpinnerQty);
btnAdd = findViewById(R.id.btnAdd);
String name = edtName.getText().toString();
Adapter adapter = new Adapter(getSupportFragmentManager());
/*screen*/
adapter.AddFragment(new Drugfragment(), "Drug");
adapter.AddFragment(new Appointmentfragment(), "Appointment");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
/* DBHelper mDBHlpr = new DBHelper(this);
AccountInfo a = new AccountInfo(name);
mDBHlpr.insertRecord(a);
String account_just_inserted = mDBHlpr.getAccountName();
Log.d(
"ACCOUNT INSERTED",
"The Name of the account just inserted is :-" + account_just_inserted
);*/
final Account c = new Account();
if(c==null) {
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("LOG IN");
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.account,null));
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.accountname,null));
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.exit(0);
}
});
builder.show();
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
MAINACTIVITY
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
private EditText edtName;
private Button btnAdd;
Spinner spin1;
Spinner spin2;
Spinner spin3;
#SuppressLint("WrongConstant")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tablayout);
appBarLayout = findViewById(R.id.bar);
viewPager = findViewById(R.id.viewpager);
edtName = findViewById(R.id.edtName);
spin1 = findViewById(R.id.spinnerFrequency);
spin2 = findViewById(R.id.SpinnerTime);
spin3 = findViewById(R.id.SpinnerQty);
btnAdd = findViewById(R.id.btnAdd);
Adapter adapter = new Adapter(getSupportFragmentManager());
/*screen*/
adapter.AddFragment(new Drugfragment(), "Drug");
adapter.AddFragment(new Appointmentfragment(), "Appointment");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
/* DBHelper mDBHlpr = new DBHelper(this);
AccountInfo a = new AccountInfo(name);
mDBHlpr.insertRecord(a);
String account_just_inserted = mDBHlpr.getAccountName();
Log.d(
"ACCOUNT INSERTED",
"The Name of the account just inserted is :-" + account_just_inserted
);*/
final Account c = new Account();
if(c==null) {
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("LOG IN");
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.account,null));
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.accountname,null));
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.exit(0);
}
});
builder.show();
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
You can use textwatcher for this , it will change your name String every time when text is changing in EditText field.
et1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
name = edtName.getText().toString();
//it will change your name String every time when text is changing in EditText fiels
}
});
Hope this is what you want.
Its rather simple. On vb.net is just this... c# is only slightly different.
Dim b As New Button
b.text = "New Button"
b.Location = New Point(0, 0)
AddHandler b.Click, AddressOf handle_click
Private Sub handle_click(sender As Object, e As EventArgs)
'code here to react to the buttons click.
End Sub
Here's a link with lots of info .. Hope it helps
Controls in Java
package com.example.riplee07.trydialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> listss = new ArrayList<String>();
ArrayAdapter <String>adapter;
String add1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText tt = (EditText)findViewById(R.id.editText);
final ListView lv = (ListView)findViewById(R.id.listView1);
Button br = (Button)findViewById(R.id.button);
add1 = tt.getText().toString();
adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,listss);
lv.setAdapter(adapter);
listss.add(add1);
adapter.notifyDataSetChanged();
br.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View ss = LayoutInflater.from(MainActivity.this).inflate(R.layout.textt,null);
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(MainActivity.this);
builderDialog.setView(ss)
.setPositiveButton("Print", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listss.add(add1);
adapter.notifyDataSetChanged();
}
}).setNegativeButton("Cancel", null)
.setCancelable(false);
AlertDialog alert = builderDialog.create();
alert.show();
}
});
}
}
Try this code below (for getting edittext data):
br.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View ss = LayoutInflater.from(MainActivity.this).inflate(R.layout.textt,null);
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(MainActivity.this);
builderDialog.setView(ss)
.setPositiveButton("Print", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText et = (EditText) ss.findViewById(E.id.editText);
listss.add(et.getText().toString());
adapter.notifyDataSetChanged();
}
}).setNegativeButton("Cancel", null)
.setCancelable(false);
AlertDialog alert = builderDialog.create();
alert.show();
}
});
Here is the method in which i call on Button button.setOnClickListener
public void saveNote(View view){
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.add_new_note_layout);
mTitle = (EditText)dialog.findViewById(R.id.titleID);
mMessage = (EditText)dialog.findViewById(R.id.messageID);
save = (Button)dialog.findViewById(R.id.saveBtn);
setFilePathll();
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int titleLength = mTitle.getText().length();
if(titleLength != 0){
String title = mTitle.getText().toString();
String msg = mMessage.getText().toString();
helper.addNote(title, mFilePath, msg);
dialog.dismiss();
}
else
{
Toast.makeText(MainActivity.this,"Empty Message is not valid",Toast.LENGTH_LONG).show();
}
}
});
dialog.show();
}
Here helper.addNote(title, mFilePath, msg); is the method which i declared in the database helper class to insert data.
you Should use RecyclerView for what you want instead of using 'ListView'
I want to read from a preferences file in a DialogFragment. If I do this:
prefs = getSharedPreferences("numberPicker.preferences", 0);
then I get a compile time error, because getSharedReference is a ContextWrapper method but DialogFragment isn't a ContextWrapper (I use android.support.v4.app.DialogFragment for backwards compatibility by the way).
If alternatively, as a 'workaround', I use a SharedPreferences object prefs created in a class InitSpel (which is a FragmentActivity and therefore IS a ContextWrapper) then I get no error (nor at compile time nor at runtime) however the values are not stored (next time I start the app the values baan1 and baan2 are still 0).
How to solve?
package mypackage;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
public class VraagBanenDialogFragment extends DialogFragment {
private View v;
private EditText editText1;
private EditText editText2;
//private ArrayList<Baan> baanNummers;
private int[] oldBanen;
private int[] currentBanen;
private SharedPreferences prefs;
/* public VraagBanenDialogFragment(ArrayList<Baan> baanNummers) {
this.baanNummers = baanNummers;
}
*/
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Restore preferences
//prefs = InitSpel.prefs;
prefs = getSharedPreferences("numberPicker.preferences", 0);
int baan1 = prefs.getInt( "BAAN_01", 0 );
int baan2 = prefs.getInt( "BAAN_02", 0 );
//oldBanen = new int[InitSpel.aantalParallel];
oldBanen = new int[2];
oldBanen[0] = baan1;
oldBanen[1] = baan2;
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
v = inflater.inflate(R.layout.vraag_banen, null);
// velden vullen met opgeslagen waarden
editText1 = (EditText) v.findViewById(R.id.editText1);
editText2 = (EditText) v.findViewById(R.id.editText2);
editText1.setText(String.valueOf(baan1));
editText2.setText(String.valueOf(baan2));
//editText1.setText(String.valueOf(baanNummers.get(0).getBaanNummer()));
//editText2.setText(String.valueOf(baanNummers.get(1).getBaanNummer()));
builder.setView(v)
// Add action buttons
.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
int baan1 = Integer.valueOf(editText1.getText().toString());
int baan2 = Integer.valueOf(editText2.getText().toString());
InitSpel.setBaanNummer(0, baan1);
InitSpel.setBaanNummer(1, baan2);
// en banen nog bij de preferences op schijf opslaan...
// We need an Editor object (prefs.edit()) to make preference changes.
// All objects are from android.context.Context
prefs.edit().putInt("BAAN_01", baan1);
prefs.edit().putInt("BAAN_02", baan2);
// Commit the edits!
prefs.edit().commit();
}
})
.setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO cancel;
}
});
return builder.create();
}
}
Use getActivity() since Activity extends Context, and Context has a getSharedPreferences() method.
prefs = getActivity().getSharedPreferences("numberPicker.preferences", 0);
I'd also recommend keeping a reference to the Editor object to ensure proper saving.
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("BAAN_01", baan1);
editor.putInt("BAAN_02", baan2);
// Commit the edits!
editor.commit();
or chain:
prefs.edit()
.putInt("BAAN_01", baan1)
.putInt("BAAN_02", baan2)
.commit();
The question is as you can see that I cant set the value of edittext dynamically before showDialog().
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int DIALOG_TEXT_ENTRY = 7;
private int user_id;
private int dialogChoice;
private String mobileNum;
private EditText input2 ;
private EditText input1 ;
public TextView textView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog_text_entry);
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mobileNum =tm.getLine1Number();
// input1.setText(mobileNum);
textView.setText("hello");
showDialog(DIALOG_TEXT_ENTRY);
}
#Override
protected Dialog onCreateDialog(int i) {
switch (i) {
case DIALOG_TEXT_ENTRY:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
input2 = (EditText) textEntryView.findViewById(R.id.number_edit);
input1 = (EditText) textEntryView.findViewById(R.id.username_edit);
textView = (TextView) textEntryView.findViewById(R.id.username_view);
return new AlertDialog.Builder(MainActivity.this)
// .setIconAttribute(android.R.attr.accountType)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent i = new Intent(MainActivity.this, IccActivity.class);
startActivity(i);
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
}
return null;
}
}
textView.setText("hello"); this line kills the app.
thanks in advance.
you should put this line textView.setText("hello"); into onCreateDialog() method as you are setting the value before it gets initialized.
You haven't initialized textView. You need this in your onCreate() before you do the textView.setText()
textView = (TextView) textEntryView.findViewById(R.id.username_view);
I guess you are getting NullPointerException? You should first ser your 'textView' field.
Find the TexView text that is in the dialog and set the text of the TextView
Try this link
The application seems to crash when I try and get the text from the EditText:
package com.example.helloandroid;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
public class MatrixMultiply extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.new_button:
openNewGameDialog();
break;
case R.id.exit_button:
finish();
break;
// More buttons go here (if any) ...
}
}
private static final String TAG = "Matrix";
private void openNewGameDialog() {
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.text, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Matrices");
alert.setMessage("Please enter the size of the matrix");
// Set an EditText view to get user input
alert.setView(textEntryView);
AlertDialog matrixSize = alert.create();
final EditText height1 = (EditText) matrixSize.findViewById(R.id.h1);
final EditText width1 = (EditText) MatrixMultiply.this.findViewById(R.id.w1);
final EditText height2 = (EditText) MatrixMultiply.this.findViewById(R.id.h2);
final EditText width2 = (EditText) MatrixMultiply.this.findViewById(R.id.w2);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String h1 = height1.getText().toString();
String w1 = width1.getText().toString();
String h2 = height2.getText().toString();
String w2 = width2.getText().toString();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
private void startGame(int i) { Log.d(TAG, "clicked on " + i); // Start game here...
}
}
it will be crashing due to a thing that you will calling this method from a thread other then UI Main Worker thread.
For debugging purpose try calling this method in the very beginining of app in the main class by main thread.
If it appears then make your threading issue clear in the app.
Use Handler class object to reslove such issues. I will put some more light if you need
The problem is that height1 is not found within the dialog. Really, you shouldn't seearch one within the dialog, because it belongs not to the dialog, but to the activity. Try this:
final EditText height1 = (EditText) YourActivityClassName.this.findViewById(R.id.h1);
It will search for the R.id.h1 within your activity.