I have several LinearLayouts contained inside their respective ScrollView which in turn are contained by a ViewFlipper. The odd stuff is that in some of the Layouts once they have the focus, it starts automatically in a place other than the top.
So what can be causing this ? In order to force them to start at the top, is there something like the tabindex property in html ?
Thanks
if you have problem called focus is gone automatically on other place instead of top position's field then you have to write this code simply at onStart() method of activity life cycle with respective field id in my case first field
is referenced by firstfield_et this is the variable of Activity.java file and it is also already referenced by etfirstfield from .xml file
the code is simply as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText firstfield_et= (EditText) findViewById(R.id.etfirstfield);
}
#Override
protected void onStart() {
super.onStart();
if(TextUtils.isEmpty(firstfield_et.getText().toString())){
firstfield_et.requestFocus();
}
}
Related
Quick notice: I am using SharedPreferences so that I can reload data when I re-open the app.
Problem
I have a LinearLayout in the main fragment of my application. Everything runs smoothly until I re-open the app and try to reinitialize the LinearLayout.
I am trying to initialize the LinearLayout with findViewById(). I have put the function in many different places. Currently I am trying to get it to work in onCreate and a function that is called from onCreate. Here is my code so far:
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linearLayout);
// this is where is load the SharedPreferences
// this is where I implement them back into the app ('reload' the app)
reload();
}
public void reload() {
// bunch of other irrelevant stuff
linearLayout = findViewById(R.id.linearLayout);
linearLayout.addView(/*other view*/); // this is where it complains
}
// the is for when the button is clicked
public void submitEntry(View view) {
// this is fine according to Logcat
linearLayout = findViewById(R.id.linearLayout);
}
}
I would expect after initializing it twice, or at least trying to, that is would've caught on but no. Logcat complains that linearLayout is a null object reference. I don't know what to do at this point but it's probably something simple that I've overlooked. Any help would be appreciated.
LinearLayout linearLayout= new LinearLayout(context);
Initialize like this it will help you!
I have a button with a method that is invoked upon clicking.
The method:
public void addToList(View view) {
System.out.println(1);
String str = "";
try{
str = edit.getText().toString();}
catch (Exception ex){
System.out.println( ex );
}
System.out.println(2);
new QueryInList( ).execute(helper, str);
System.out.println(3);
edit.setText(null);
System.out.println(4);
//adapter.notifyDataSetChanged();
}
Well, I always get the exception, it is a Nullpointerexception.
This quite baffles me, because edit IS initalized:
It is declared in the class:
private EditText edit;
and besides, it is initialized in onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
edit = (EditText)findViewById(R.id.textfield);
setContentView(R.layout.activity_view);
......}
So I wonder why I always get a Nullpointer?
Set the content view, before looking for the items. You dont have a view to find the items in until you set the content view.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
edit = (EditText)findViewById(R.id.textfield);
......}
Move edit = (EditText)findViewById(R.id.textfield); after your setContentView statement.
Here is a nice explanation from user #Squonk from another question:
setContentView(...) perfoms something called 'layout inflation'. What that means is it parses the XML in the relevant file (main.xml in your case) and creates instances of all the UI elements within it. It then attaches that view to the Activity. When you call findViewById(...) it doesn't reference your main.xml directly - instead it references the content view attached to the Activity, in other words the one inflated by setContentView(...)
I would really like to set the layout depending on which combination of two checkboxes are selected. Since there are four possible states, I have four layouts to display items underneath the checkboxes, if selected. I have made this work using four classes, but there must be a more efficient way to do this.
Basically, I would like to have drop-down EditTexts for user input displayed under the checkboxes only if they are selected. If the setContentView statements are replaced with the commented ones, I can cycle through any combination of checkboxes, but as the code is, only one layout change is able to be made and I don't understand why. Please help with any suggestions.
**I realize the CompoundButton object is unused here.
public class First extends Activity implements OnCheckedChangeListener{
CheckBox emailBox,smsBox;
#Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
emailBox=(CheckBox)findViewById(R.id.checkBox_1);
smsBox=(CheckBox)findViewById(R.id.checkBox_2);
emailBox.setOnCheckedChangeListener(this);
smsBox.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton compound,boolean isChecked){
if(!emailBox.isChecked()&&!smsBox.isChecked()){
setContentView(R.layout.activity_1);
// Toast.makeText(First.this,"None Checked",Toast.LENGTH_SHORT).show();
}
if(emailBox.isChecked()&&!smsBox.isChecked()){
setContentView(R.layout.activity_2);
// Toast.makeText(First.this,"Email Checked",Toast.LENGTH_SHORT).show();
}
if(smsBox.isChecked()&&!emailBox.isChecked()){
setContentView(R.layout.activity_3);
// Toast.makeText(First.this,"Sms Checked",Toast.LENGTH_SHORT).show();
}
if(emailBox.isChecked()&&smsBox.isChecked()){
setContentView(R.layout.activity_4);
// Toast.makeText(First.this,"Both Checked",Toast.LENGTH_SHORT).show();
}
}
}
More efficient way will be use fragments instead of changing layouts :)ORGroup Controls in layouts and set thier visiblity to View.GONE then set visiblility for appropriate group to View.Visible
To display a drop-down info box (like a text view) you can place e.g a lable which is empty(or rather you hide it) under these two check boxes and check for changes in checkboxes and then change the lable I mentioned earlier in runtime using java part of the code to display what u want. I hope I've got your point and this will help you.
I was able to solve this issue very simply by wrapping the relevant portions of the layout in a vertical LinearLayout and toggling between visibility='gone' and visibility='visible' as indicated in the code below; so I eventually thought I should come back here to share (if anyone has a simpler, more efficient method, perhaps let me know?)
public class MainActivity extends Activity implements OnCheckedChangeListener{
CheckBox emailBox,smsBox;
LinearLayout portion_1,portion_2,portion_3;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
portion_1=(LinearLayout)findViewById(R.id.hider_1);
portion_2=(LinearLayout)findViewById(R.id.hider_2);
portion_3=(LinearLayout)findViewById(R.id.hider_3);
emailBox=(CheckBox)findViewById(R.id.checkBox_1);
smsBox=(CheckBox)findViewById(R.id.checkBox_2);
emailBox.setOnCheckedChangeListener(this);
smsBox.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton compound,boolean isChecked){
if(!emailBox.isChecked()&&!smsBox.isChecked()){
portion_1.setVisibility(View.GONE);
portion_2.setVisibility(View.GONE);
portion_3.setVisibility(View.GONE);
emailBox.setChecked(false);
smsBox.setChecked(false);
}
if(emailBox.isChecked()&&!smsBox.isChecked()){
portion_1.setVisibility(View.VISIBLE);
portion_2.setVisibility(View.GONE);
portion_3.setVisibility(View.VISIBLE);
emailBox.setChecked(true);
smsBox.setChecked(false);
}
if(smsBox.isChecked()&&!emailBox.isChecked()){
portion_1.setVisibility(View.GONE);
portion_2.setVisibility(View.VISIBLE);
portion_3.setVisibility(View.VISIBLE);
emailBox.setChecked(false);
smsBox.setChecked(true);
}
if(emailBox.isChecked()&&smsBox.isChecked()){
portion_1.setVisibility(View.VISIBLE);
portion_2.setVisibility(View.VISIBLE);
portion_3.setVisibility(View.VISIBLE);
emailBox.setChecked(true);
smsBox.setChecked(true);
}
}
}
I know this is a very basic question, however as a newbie i cant get to work around it.
So, I want to have multiple activities to use same the xml layout(consist for example of 1 imagebutton, and multiple textviews with different IDs). Now, for every activity, I want them to view the same layout but override the views with data unique to every activity. What is the best way to do this? And also, the imagebutton should open different URLs in a video player(youtube links).
And can somebody tell me what is the most practical way to learn android programming?
UPDATE
This is my current code:
public class TemakiActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentviewer);
}
}
For example I have a textview with ID "descriptionviewer", and a button with ID "videolink", now, how do you code those in?
You can share the same layout file and the set the attributes for views in the onCreate(..) method of each activity.
If you want a different URL to open for each image button you could set it at runtime as follows
public void onCreate(Bundle b) {
Button button =(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//different action for each activity
}
});
}
Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.same_layout);
TextView urlDesc = (TextView)findViewById(R.id.descriptionviewer);
urlDesc.setText("url_1"); //now in other activities-- urlDesc.setText("url_2");
ImageButton aButton = (ImageButton)findViewById(R.id.videolink);
aButton.setOnClickListener(aButtonListener);
}
private OnClickListener aButtonListener = new OnClickListener() {
public void onClick(View v) {
// go open url_1 here. In other activities, open url_x, url_y, url_z
finish();
}
};
Same code just swapping the text you want to set for the TextView and url to open in OnClickListener(). No more to change.
Is it possible to add a button to the right corner of the app title?
e.g., adding a "refresh" button to the title of "Feed: my feeds"?
http://www.android.com/market/apps/feedr-lg-01.jpg
The simplest way to do that, IMHO, is to get rid of the standard title bar (android:theme="#android:style/Theme.NoTitleBar" in the <activity> element in the manifest) and put your own "title bar" at the top of the activity.
Note, though, that the "button in the title bar" style is more iPhone-ish. Android would typically have that in the option menu, so the UI is less cluttered (at the cost of two taps to do the refresh).
Why don't you try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitle= requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitle ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Set your layout for title here and mention your button in this layout);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("NEW TITLE");
myTitleText.setBackgroundColor(Color.BLUE);
}
}
yep this solveed an issue i had... trimmed version is below...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, Set your layout for title here and mention your button in this layout);
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
/* your code here */
}
}
I think a better approach would be to simply refresh the view if it is active by using a Handler. If your pulling content when the activity is resumed then any time you leave and come back to the view it will refresh. If you are expecting users to sit at the top level of the view and need to update the information then you can handle this with a delayed handler which will call your resume method and periodically refresh the view thus negating the need for a button.
Here is a link to the documentation for the handler class. I would start by looking into the basic use of handler. Then test the sendMessageDelayed method so that at the end of every call you restart the handler. Also be sure to only create a new handler if your activity is the top activity and don't bother refreshing the ui if it is not. Adding a simple isActive flag to on pause and on resume is a decent way to check for this.