Custom Title bar in Android - android

I have create a custom title bar, It contains a TextView and label. i have added this to an activity as follows
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.homepage);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.windowtitle);
But now i want to display current time in the textview inside the titlebar.
How can this be done?

you can get text view like this.create a mytitle.xml layout with textview and then add this code it will work..
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText(" TITLE ");
}
}

Please try below code
LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.windowtitle, null, true);
TextView textView = (TextView) view.findViewById(R.id.txtdate);
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
txtdate.setText(c.getTime());

Related

How to make a clickable link in TextView.seterror()

Hi everyone.
I'd like to add a clickable url in the errormsg shown by using TextView.setError(msg) method.
please take a look on my code.
final EditText testview = findViewById(R.id.testview);
final TextView textView = findViewById(R.id.testtextview);
Button btntest = findViewById(R.id.testbutton);
btntest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
testview.setMovementMethod(new LinkMovementMethod());
testview.setError(Html.fromHtml("testurl click me "));
LayoutInflater inflater = LayoutInflater.from(testview.getContext());
TextView err = (TextView) inflater.inflate(
Resources.getSystem().getIdentifier("textview_hint","layout", "android"), null);
err.setMovementMethod(LinkMovementMethod.getInstance());
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml("testurl click me "));
textView.setError(Html.fromHtml("testurl click me "));
inflater = LayoutInflater.from(textView.getContext());
err = (TextView) inflater.inflate(
Resources.getSystem().getIdentifier("textview_hint","layout", "android"), null);
err.setMovementMethod(LinkMovementMethod.getInstance());
}
});
I have tried to setMovementMethod() on the specific TextView, which shows the errormsg, but the "click me" seems remain unclickable.
Thanks again.
If you can call setMovementMethod on error textview then this solution is perfect
final EditText testview = findViewById(R.id.editText);
final TextView textView = findViewById(R.id.testtextview);
Button btntest = findViewById(R.id.testbutton);
SpannableStringBuilder ssb = new SpannableStringBuilder(Html.fromHtml("testurl click me "));
URLSpan[] urlSpans = ssb.getSpans(0,ssb.length(),URLSpan.class);
int start = ssb.getSpanStart(urlSpans[0]);
int end = ssb.getSpanEnd(urlSpans[0]);
testview.setMovementMethod(LinkMovementMethod.getInstance());
ClickableSpan span = new ClickableSpan() {
#Override
public void onClick(#NonNull View view) {
Log.d("Cust","C");
}
};
ssb.setSpan(span,start,end,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//testview.setError(span.toString());
testview.setError(ssb);

AutoCompleteTextView.getText().toString() is null

I have a drop down AutoCompleteTextView in an action bar.
I am attempting to get text from the AutoCompleteTextView and store it into a string.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location2);//Location2 is my layout
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.location, null);//location is my custom action bar
actionBar.setCustomView(view);
Global global = new Global();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, global.getUniversities());
AutoCompleteTextView textView = (AutoCompleteTextView) view
.findViewById(R.id.searchLocation);
textView.setAdapter(adapter);
...
}
public void changeLocation(View view){
View v = View.inflate(this, R.layout.location, null);
AutoCompleteTextView searchLocation = (AutoCompleteTextView)
v.findViewById(R.id.searchLocation);
String searchLocationText = searchLocation.getText().toString();
Toast.makeText(this, searchLocationText,
Toast.LENGTH_LONG).show();
...
}
<AutoCompleteTextView
android:id="#+id/searchLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:drawableLeft="#drawable/search"
android:paddingTop="15dp"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:textColor="#FFFFFF" >
<requestFocus />
</AutoCompleteTextView>
The autocomplete feature works like a charm... But the toast shows that String searchLocationText is empty...
What is incorrect about the autocompletetextview in the actionbar?
How can I get the text into a string?
Edit:- More details
I am able to type in the AutoCompleteTextView, it autocompletes with a drop down list and everything is working fine. But I try to turn that information into a string and the string is empty...
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.AutoCompleteTextView.getText()' on a null object reference
Error refers to line:
String searchLocationText = searchLocation.getText().toString();
You need to get the text from the view you have attached to your layout. But you're inflating a new view here.
public void changeLocation(View view) {
View v = View.inflate(this, R.layout.location, null);
AutoCompleteTextView searchLocation = (AutoCompleteTextView)
v.findViewById(R.id.searchLocation);
String searchLocationText = searchLocation.getText().toString();
Toast.makeText(this, searchLocationText,
Toast.LENGTH_LONG).show();
[...]
}
Instead find the view on the activity like in onCreate()
public void changeLocation(View view) {
ActionBar actionBar = getSupportActionBar();
view = actionBar.getCustomView();
AutoCompleteTextView searchLocation = (AutoCompleteTextView) view
.findViewById(R.id.searchLocation);
String searchLocationText = searchLocation.getText().toString();
Toast.makeText(this, searchLocationText,
Toast.LENGTH_LONG).show();
[...]
}

How to add the Values in String Array which one enter through User?

I am tring to add all the values in String Array but it don't work:
public class Info extends Activity {
private static final String TAG = "Info";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Intent myIntent = getIntent();
Bundle b = myIntent.getExtras();
String choice = b.getString("choice");
String fname = b.getString("fname");
String lname = b.getString("lname");
String add = b.getString("add");
String coun = b.getString("coun");
String pro = b.getString("pro");
String pcode = b.getString("pcode");
Log.i(TAG,"selection: " + choice);
TextView textView = (TextView) findViewById(R.id.showmeText);
textView.append(": " + choice);
TextView textView1 = (TextView) findViewById(R.id.fname);
textView1.append(" " + fname);
TextView textView2 = (TextView) findViewById(R.id.lname);
textView2.append(" " + lname);
TextView textView3 = (TextView) findViewById(R.id.address1);
textView3.append(" " + add);
TextView textView4 = (TextView) findViewById(R.id.pro);
textView4.append(" " + pro);
TextView textView5 = (TextView) findViewById(R.id.country1);
textView5.append(" " + coun);
TextView textView6 = (TextView) findViewById(R.id.pcode);
textView6.append(" " + pcode);
}
I tried these all values in ListView but these not working because i was adding all the Textview in ListView.Can you please help me How to add these list in String array?
Crete a ListView in your UI Deisgn layout xml.
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/choice_list"
android:focusable="false"
android:scrollbarStyle="outsideOverlay">
Step1 : Create a String Array ex: String[] myStringArray.
Step 2: Push all the string values into Array.
Step 3 : in your activity get the reference for listview (choice_list)
Ex:
ListView choiceListView = (ListView)findviewById(R.layout.choice_list)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_dropdown_item_1line, myStringArray);
choiceListView .setAdapter(adapter);
That is all to see the out put as list view in normal activity. You don't need ListActivity , but can use this in normal activity too. The above is the solution for any activity which has a listview as a component in it.
If you like this please vote for me.
just try this
public class ArrayAdapterDemo extends ListActivity {
TextView selection;
"silly", "list" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
item.add(choice);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_expandable_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
String[] myStringArray = new String[7];
myStringArray[0] = choice;
myStringArray[1] = fname;
......
myStringArray[7] = pcode ;
Or you want to display all the text in a single textview. Your question is not clear.
I hope if its about pushing the values into the array then the above holds good.
pls paste the error here. I guess i might me because of either value missing or class cast exception.

ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent)

I'm getting this error ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2585 when i start an intent
public class SingleMenuItemActivity extends Activity {
TextView lblName;
TextView lblyest;
TextView lbltoday;
TextView lblHigh;
TextView lblLow;
TextView lblChange;
TextView lblPcchange;
String name,yesterday,today,high,low,change,pcchange;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
lblName = (TextView) findViewById(R.id.name);
lblyest = (TextView) findViewById(R.id.yesterday);
lbltoday = (TextView) findViewById(R.id.today);
lblHigh = (TextView) findViewById(R.id.high);
lblLow = (TextView) findViewById(R.id.low);
lblChange = (TextView) findViewById(R.id.change);
lblPcchange = (TextView) findViewById(R.id.pcchange);
Intent in = getIntent();
name = in.getStringExtra("name");;
yesterday = in.getStringExtra("yesterday");
today =in.getStringExtra("today");
high =in.getStringExtra("high");
low = in.getStringExtra("low");
change = in.getStringExtra("change");
pcchange =in.getStringExtra("pcchange");
//Error doesnt happen when the lines below are commented
lblName.setText((CharSequence)name.toString());
lblyest.setText(yesterday.toString());
lbltoday.setText(today.toString());
lblHigh.setText(high.toString());
lblLow.setText(low.toString());
lblChange.setText(change.toString());
lblPcchange.setText(pcchange.toString());
}
}
I know the error is triggered when i do the settext() function but i don't know how to fix it
You're setting variables, lblName and all those, to contain all of the child View's ... but you're doing it at the class level. Those Views are not created until the layout is inflated, in the line that reads
setContentView(R.layout.single_list_item);
, so all those variables are getting set to null.
Set the variables after your setContentView() call and see how it goes.
Try this code.
public class SingleMenuItemActivity extends Activity {
TextView lblName;
TextView lblyest;
TextView lbltoday;
TextView lblHigh;
TextView lblLow;
TextView lblChange;
TextView lblPcchange;
String name, yesterday, today, high, low, change, pcchange;
#Override
protected void onCreate (Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate( savedInstanceState );
setContentView( R.layout.single_list_item );
Intent in = getIntent();
name = in.getStringExtra( "name" );
;
yesterday = in.getStringExtra( "yesterday" );
today = in.getStringExtra( "today" );
high = in.getStringExtra( "high" );
low = in.getStringExtra( "low" );
change = in.getStringExtra( "change" );
pcchange = in.getStringExtra( "pcchange" );
lblName = (TextView) findViewById( R.id.name );
lblyest = (TextView) findViewById( R.id.yesterday );
lbltoday = (TextView) findViewById( R.id.today );
lblHigh = (TextView) findViewById( R.id.high );
lblLow = (TextView) findViewById( R.id.low );
lblChange = (TextView) findViewById( R.id.change );
lblPcchange = (TextView) findViewById( R.id.pcchange );
lblName.setText( (CharSequence) name.toString() );
/* lblyest.setText(yesterday.toString());
lbltoday.setText(today.toString());
lblHigh.setText(high.toString());
lblLow.setText(low.toString());
lblChange.setText(change.toString());
lblPcchange.setText(pcchange.toString()); */
}
}
Actually, You can not initialize views before setContentView() call. That's why, you are getting this issue.
move these lines after setContentView(R.layout.single_list_item);
TextView lblName = (TextView) findViewById(R.id.name);
TextView lblyest = (TextView) findViewById(R.id.yesterday);
TextView lbltoday = (TextView) findViewById(R.id.today);
TextView lblHigh = (TextView) findViewById(R.id.high);
TextView lblLow = (TextView) findViewById(R.id.low);
TextView lblChange = (TextView) findViewById(R.id.change);
TextView lblPcchange = (TextView) findViewById(R.id.pcchange);

setOnClickListener scope on dynamic added elements

I am adding programatically and dynamically some elements (buttons and text views) with android. I also need to set the setOnClickListener event for each of these buttons and from that event execute some action on the click of button:
do
{
EditText txt1 = new EditText(this);
EditText txt2 = new EditText(this);
Button showtxt = new Button(this);
linearLayout.addView(showtxt );
linearLayout.addView(txt1);
linearLayout.addView(txt2);
showtxt.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String aaa= txt1 .getText().toString();//HOW TO ACCESS txt1 and txt2 from here
String bbb= txt2 .getText().toString();
}
}
}
while(somecondition)
I am almost new to android. How can I access to txt1 and txt2 in the click callback function?
You need to make the define the variables where they will have class wide scope:
public class Example extends Activity {
EditText txt1;
EditText txt2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
txt1 = new EditText(this);
txt2 = new EditText(this);
...
Now your onClick function will be able to see txt1 and txt2.
Alternatively
Since you appear to be creating a lot of txt1 and txt2 in one LinearLayout, you can pass your Button a reference to its EditTexts:
do {
...
// EditText[] array = { txt1, txt2 };
// is the short version of
EditText[] array = new EditText[2];
array[0] = txt1;
array[1] = txt2;
showtxt.setTag(array);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText[] array = (EditText[]) v.getTag();
String aaa = array[0].getText().toString();
String bbb = array[1].getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});
} while(some condition)
This may not be not ideal, however without any further context I cannot guess your ultimate goal. Hope that helps!
Last Suggestion
If we call the Button and two EditTexts a row, you could store each row in a ViewGroup or View of its own. Say you wanted to have background colors for each row:
View row = new View(this); // or this could be another LinearLayout
row.setBackgroundColor(0x0000ff);
// Create and add the Button and EditTexts to row, as in row.addView(showtxt), etc
...
linearLayout.addView(row);
showtxt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View row = v.getParent()
String aaa = ((EditText) row.getChildAt(1)).getText().toString();
String bbb = ((EditText) row.getChildAt(2)).getText().toString();
Log.v("Example", aaa + " " + bbb);
}
});

Categories

Resources